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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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; }
public RelayCommand AnalyzeCmd { get; private set; }
List<TimeGridAdv2EventArgs> timeGridAdv2Datas = new List<TimeGridAdv2EventArgs>();
public AutoGetGridAdv(FlyAD7 flyad)
{
this.flyad = flyad;
StartCmd = new RelayCommand(Start);
StopCmd = new RelayCommand(Stop);
AnalyzeCmd = new RelayCommand(Analyze);
Load();
this.PropertyChanged += AutoSave;
}
CancellationTokenSource cancellationTokenSource;
async void Start()
{
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);
//正式开始
timeGridAdv2Datas.Clear();
flyad.TimeGridAdv2Event += Flyad_TimeGridAdv2Event;
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收到
flyad.TimeGridAdv2Event -= Flyad_TimeGridAdv2Event;
//保存数据
string strDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string path = System.IO.Path.Combine(strDesktopPath, $"autoGridAdv_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.json");
string json = Newtonsoft.Json.JsonConvert.SerializeObject(timeGridAdv2Datas);
File.WriteAllText(path, json);
ProgressMsg = $"成功保存到 {path}";
timeGridAdv2Datas.Clear();
IsRunning = false;
}
private void Flyad_TimeGridAdv2Event(object sender, TimeGridAdv2EventArgs e)
{
timeGridAdv2Datas.Add(e);
}
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;
}
}
}
void Stop()
{
if (!IsRunning)
return;
if (cancellationTokenSource == null)
return;
if (cancellationTokenSource.IsCancellationRequested)
return;
cancellationTokenSource.Cancel();
}
private void Analyze()
{
WdGridAdvAnalyze wd = new WdGridAdvAnalyze();
wd.Init();
wd.ShowDialog();
}
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
{
string json = Newtonsoft.Json.JsonConvert.SerializeObject(this, Formatting.Indented);
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)
{
}
}
}
}