AutoGetGridAdv.cs 6.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
using FlyADBase;
using GalaSoft.MvvmLight.Command;
using Newtonsoft.Json;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace Flyad7_WPF
{
    [JsonObject(MemberSerialization.OptIn)]
    public class AutoGetGridAdv:INotifyPropertyChanged
    {
        FlyAD7 flyad;

        public bool IsError { get; set; }

        public string ErrMsg { get; set; }

        public string ProgressMsg { get; set; }
        
        public bool IsRunning { get; private set; }

        [JsonProperty]
        public int FB_Pos1 { get; set; } = 1000;

        [JsonProperty]
        public int FB_Pos2 { get; set; } = 3000;

        [JsonProperty]
        public int StartV { get; set; } = 100;

        [JsonProperty]
        public int StepV { get; set; } = 100;

        [JsonProperty]
        public int EndV { get; set; } = 2000;

        public RelayCommand StartCmd { get; private set; }
        public RelayCommand StopCmd { get; private set; }
潘栩锋's avatar
潘栩锋 committed
47
        public RelayCommand AnalyzeCmd { get; private set; }
潘栩锋's avatar
潘栩锋 committed
48
        List<TimeGridAdv2EventArgs> timeGridAdv2Datas = new List<TimeGridAdv2EventArgs>();
潘栩锋's avatar
潘栩锋 committed
49
        public AutoGetGridAdv(FlyAD7 flyad)
50
        {
潘栩锋's avatar
潘栩锋 committed
51
            this.flyad = flyad;
52 53
            StartCmd = new RelayCommand(Start);
            StopCmd = new RelayCommand(Stop);
潘栩锋's avatar
潘栩锋 committed
54
            AnalyzeCmd = new RelayCommand(Analyze);
55
            Load();
潘栩锋's avatar
潘栩锋 committed
56

57
            this.PropertyChanged += AutoSave;
潘栩锋's avatar
潘栩锋 committed
58
            
59 60 61
        }


潘栩锋's avatar
潘栩锋 committed
62 63


64
        CancellationTokenSource cancellationTokenSource;
潘栩锋's avatar
潘栩锋 committed
65
        async void Start() 
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        {
            
            if (IsRunning)
                return;

            

            cancellationTokenSource = new CancellationTokenSource();
            IsError = false;
            IsRunning = true;
            ProgressMsg = "准备动作,到达 开始位置";
            await Task.Delay(1);
            flyad.SetVelocity((UInt32)(StartV + EndV) / 2);
            flyad.Runto(FB_Pos1);
            if (!await Wait()) goto _end;

            ProgressMsg = "正式开始";
            await Task.Delay(1);
            //正式开始
潘栩锋's avatar
潘栩锋 committed
85 86
            timeGridAdv2Datas.Clear();
            flyad.TimeGridAdv2Event += Flyad_TimeGridAdv2Event;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
            int v = StartV;
            int cnt = (EndV - StartV) / StepV;
            for(int j=0;j<cnt;j++)
            {
                
                flyad.SetVelocity((UInt32)v);
                for (int i = 0; i < 2; i++)
                {
                    ProgressMsg = $"{j*2+i}/{cnt*2} 以速度 {v}pps 来回扫描";
                    await Task.Delay(1);
                    flyad.Runto(FB_Pos2);
                    if (!await Wait()) goto _end;
                    flyad.Runto(FB_Pos1);
                    if (!await Wait()) goto _end;
                }
                v += StepV;
            }
        _end:
            await Task.Delay(1000);//等1秒,等最后一个TimeGridAdvEvent收到
潘栩锋's avatar
潘栩锋 committed
106
            flyad.TimeGridAdv2Event -= Flyad_TimeGridAdv2Event;
107 108 109 110
            
            //保存数据
            string strDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            string path = System.IO.Path.Combine(strDesktopPath, $"autoGridAdv_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.json");
潘栩锋's avatar
潘栩锋 committed
111
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(timeGridAdv2Datas);
112 113 114
            File.WriteAllText(path, json);

            ProgressMsg = $"成功保存到 {path}";
潘栩锋's avatar
潘栩锋 committed
115
            timeGridAdv2Datas.Clear();
116 117 118
            IsRunning = false;
        }

潘栩锋's avatar
潘栩锋 committed
119
        private void Flyad_TimeGridAdv2Event(object sender, TimeGridAdv2EventArgs e)
120
        {
潘栩锋's avatar
潘栩锋 committed
121
            timeGridAdv2Datas.Add(e);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        }
        async Task<bool> Wait() 
        {
            while (true)
            {
                try
                {
                    await Task.Delay(2000, cancellationTokenSource.Token);
                }
                catch 
                {
                    //被叫醒了
                    ErrMsg = "强制退出";
                    IsError = true;
                    await Task.Delay(1);
                    return false;
                }
                if (!flyad.IsConnected)
                {
                    ErrMsg = "连接断开";
                    IsError = true;
                    await Task.Delay(1);
                    return false;
                }
                if (flyad.DriveStatus == DRIVE_MAN_STATUS.RUNNING)
                {
                    continue;
                }
                else if (flyad.DriveStatus == DRIVE_MAN_STATUS.STOP)
                {
                    return true;
                }
                else 
                {
                    ErrMsg = $"异常停止 flyad.DriveStatus={flyad.DriveStatus}";
                    IsError = true;
                    return false;
                }
            }
        }
潘栩锋's avatar
潘栩锋 committed
162
        void Stop() 
163 164 165 166 167 168 169 170 171 172
        {
            if (!IsRunning)
                return;
            if (cancellationTokenSource == null)
                return;
            if (cancellationTokenSource.IsCancellationRequested)
                return;

            cancellationTokenSource.Cancel();
        }
潘栩锋's avatar
潘栩锋 committed
173 174 175 176 177 178 179

        private void Analyze()
        {
            WdGridAdvAnalyze wd = new WdGridAdvAnalyze();
            wd.Init();
            wd.ShowDialog();
        }
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
        public event PropertyChangedEventHandler PropertyChanged;

        private bool isIgnoreSave;
        private async void AutoSave(object sender, PropertyChangedEventArgs e)
        {
            if (isIgnoreSave)
                return;

            if (this.GetType()
                .GetProperty(e.PropertyName)
                .GetCustomAttributes(typeof(JsonPropertyAttribute), false)
                .Count() > 0)
            {
                isIgnoreSave = true;
                await Task.Delay(5000);//Task.Yield();
                Save();
                isIgnoreSave = false;
            }
        }

        void Save() 
        {
            try
            {
潘栩锋's avatar
潘栩锋 committed
204
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(this, Formatting.Indented);
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
                File.WriteAllText("autoGridAdv.json", json);
            }
            catch (Exception e ) 
            {
            
            }
        }
        void Load() 
        {
            try
            {
                string json = File.ReadAllText("autoGridAdv.json");
                isIgnoreSave = true;
                Newtonsoft.Json.JsonConvert.PopulateObject(json, this);
                isIgnoreSave = false;
            }
            catch (Exception e)
            {

            }
        }
    }
}