AutoForwBackw.cs 1.9 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using FlyADBase;
using Misc;
8
using System.Windows.Threading;
潘栩锋's avatar
潘栩锋 committed
9 10 11 12 13 14 15 16 17

namespace Flyad7_WPF
{
    /// <summary>
    /// 来回走
    /// </summary>
    public class AutoForwBackw : INotifyPropertyChanged
    {

18
        DispatcherTimer timer;
潘栩锋's avatar
潘栩锋 committed
19 20
        FlyAD7 flyad;

21 22 23 24 25

        public bool FB_Enable { get; set; }
        public int FB_Pos1 { get; set; } = 1000;

        public int FB_Pos2 { get; set; } = 3000;
潘栩锋's avatar
潘栩锋 committed
26 27 28 29 30 31 32 33 34 35 36 37

        public AutoForwBackw(FlyAD7 flyad)
        {
            this.flyad = flyad;
            FB_init();
        }
        void timer_Tick(object sender, EventArgs e)
        {
            if (!flyad.IsConnected)
            {
                FB_Enable = false;
            }
潘栩锋's avatar
潘栩锋 committed
38 39 40 41
            if (!flyad.IsFinish)
                return;
            if (flyad.IsTimeToPushTimeGridAdv)//推送完成
                return;
潘栩锋's avatar
潘栩锋 committed
42

潘栩锋's avatar
潘栩锋 committed
43 44

            if (Math.Abs(FB_Pos1 - flyad.Position) > Math.Abs(FB_Pos2 - flyad.Position))
潘栩锋's avatar
潘栩锋 committed
45
            {
潘栩锋's avatar
潘栩锋 committed
46 47 48 49 50
                flyad.Runto(FB_Pos1);
            }
            else
            {
                flyad.Runto(FB_Pos2);
潘栩锋's avatar
潘栩锋 committed
51 52 53 54
            }
        }
        void FB_init()
        {
55 56
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
潘栩锋's avatar
潘栩锋 committed
57 58
            timer.Tick += new EventHandler(timer_Tick);
            FB_Enable = false;
潘栩锋's avatar
潘栩锋 committed
59
            this.PropertyChanged += AutoForwBackw_PropertyChanged;
潘栩锋's avatar
潘栩锋 committed
60 61
        }

潘栩锋's avatar
潘栩锋 committed
62
        private void AutoForwBackw_PropertyChanged(object sender, PropertyChangedEventArgs e)
潘栩锋's avatar
潘栩锋 committed
63
        {
潘栩锋's avatar
潘栩锋 committed
64
            if (e.PropertyName == nameof(FB_Enable))
潘栩锋's avatar
潘栩锋 committed
65 66 67 68 69 70 71 72 73 74 75
            {
                if (FB_Enable)
                {
                    timer.Start();
                }
                else
                {
                    timer.Stop();
                }
            }
        }
潘栩锋's avatar
潘栩锋 committed
76

潘栩锋's avatar
潘栩锋 committed
77 78 79
        public event PropertyChangedEventHandler PropertyChanged;
    }
}