forked from badvectors/ATISPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.cs
414 lines (321 loc) · 12.5 KB
/
Plugin.cs
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Media;
using System.Net.Http;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using vatsys;
using vatsys.Plugin;
using Timer = System.Timers.Timer;
namespace ATISPlugin
{
[Export(typeof(IPlugin))]
public class Plugin : IPlugin
{
public string Name => "ATIS Editor";
public static string DisplayName => "ATIS Editor";
private static readonly string MetarUri = "https://metar.vatsim.net/metar.php?id=";
public static readonly Version Version = new Version(2, 3);
private static readonly string VersionUrl = "https://raw.githubusercontent.com/badvectors/ATISPlugin/master/Version.json";
private static readonly string ZuluUrl = "https://raw.githubusercontent.com/badvectors/ATISPlugin/master/Zulu.json";
private static readonly HttpClient Client = new HttpClient();
private static CustomToolStripMenuItem ATISMenu;
public static ATISControl ATIS1;
public static ATISControl ATIS2;
public static ATISControl ATIS3;
public static ATISControl ATIS4;
private static EditorWindow Editor;
public static string DatasetPath { get; set; }
public static ATIS ATISData { get; set; }
public static Sectors Sectors { get; set; }
public static Airspace Airspace { get; set; }
public static List<ZuluInfo> ZuluInfo { get; set; } = new List<ZuluInfo>();
public static SoundPlayer SoundPlayer { get; set; } = new SoundPlayer();
private static Timer METARTimer { get; set; } = new Timer();
private static Timer BroadcastTimer { get; set; } = new Timer();
public static List<ATISAudio> ToBroadcast { get; set; } = new List<ATISAudio>();
public static List<VSCSFrequency> Frequencies { get; set; } = new List<VSCSFrequency>();
public Plugin()
{
vatsys.ATIS.Disable();
try
{
Network.Connected += OnUpdate;
Network.Disconnected += Network_Disconnected;
Audio.VSCSFrequenciesChanged += Audio_VSCSFrequenciesChanged;
Audio.FrequencyErrorStateChanged += Audio_VSCSFrequenciesChanged;
Network.PrimaryFrequencyChanged += Audio_VSCSFrequenciesChanged;
ATISMenu = new CustomToolStripMenuItem(CustomToolStripMenuItemWindowType.Main, CustomToolStripMenuItemCategory.Windows, new ToolStripMenuItem(DisplayName));
ATISMenu.Item.Click += ATISMenu_Click;
MMI.AddCustomMenuItem(ATISMenu);
GetSettings();
if (DatasetPath == null)
{
Errors.Add(new Exception("Could not load vatSys settings."), DisplayName);
return;
}
GetData();
if (Airspace == null)
{
Errors.Add(new Exception("Could not load Airspace data."), DisplayName);
return;
}
if (Sectors == null)
{
Errors.Add(new Exception("Could not load Sectors data."), DisplayName);
return;
}
if (ATISData == null)
{
Errors.Add(new Exception("Could not load ATIS data."), DisplayName);
return;
}
var directory = Path.Combine(DatasetPath, "Temp");
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
var toDelete = Directory
.EnumerateFiles(directory, "*.*", SearchOption.TopDirectoryOnly)
.Where(s => Path.GetExtension(s).TrimStart('.').ToLowerInvariant() == "wav");
foreach (var file in toDelete)
{
if (file.Contains("AIS.wav")) continue;
File.Delete(file);
}
ATIS1 = new ATISControl(0);
ATIS2 = new ATISControl(1);
ATIS3 = new ATISControl(2);
ATIS4 = new ATISControl(3);
ATIS1.StatusChanged += OnUpdate;
ATIS2.StatusChanged += OnUpdate;
ATIS3.StatusChanged += OnUpdate;
ATIS4.StatusChanged += OnUpdate;
METARTimer.Elapsed += new ElapsedEventHandler(METARTimer_Elapsed);
METARTimer.Interval = TimeSpan.FromMinutes(5).TotalMilliseconds;
METARTimer.AutoReset = false;
METARTimer.Start();
BroadcastTimer.Elapsed += new ElapsedEventHandler(BroadcastTimer_Elasped);
BroadcastTimer.Interval = TimeSpan.FromSeconds(5).TotalMilliseconds;
BroadcastTimer.AutoReset = false;
BroadcastTimer.Start();
}
catch (Exception ex)
{
Errors.Add(new Exception(ex.Message), DisplayName);
if (ex.InnerException != null) Errors.Add(new Exception(ex.InnerException.Message), DisplayName);
}
_ = GetZuluInfo();
_ = CheckVersion();
}
private static async Task CheckVersion()
{
try
{
var response = await Client.GetStringAsync(VersionUrl);
var version = JsonConvert.DeserializeObject<Version>(response);
if (version.Major == Version.Major && version.Minor == Version.Minor) return;
Errors.Add(new Exception("A new version of the plugin is available."), DisplayName);
}
catch { }
}
private static async Task GetZuluInfo()
{
try
{
var response = await Client.GetStringAsync(ZuluUrl);
var zuluInfo = JsonConvert.DeserializeObject<ZuluInfo[]>(response);
foreach (var info in zuluInfo)
{
ZuluInfo.Add(info);
}
}
catch (Exception ex)
{
Errors.Add(new Exception(ex.Message), DisplayName);
}
}
private async void BroadcastTimer_Elasped(object sender, ElapsedEventArgs e)
{
var toBroadcast = ToBroadcast.ToList();
foreach (var atb in toBroadcast)
{
var atis = ToBroadcast.FirstOrDefault(x => x.Id == atb.Id);
if (atis != null) ToBroadcast.Remove(atis);
if (!Network.GetATISConnected(atb.ATISIndex)) continue;
try
{
await AFV.AddOrUpdateATISBot(atb.Audio, atb.ATISIndex, atb.Callsign, atb.Frequency, atb.VisPoint, atb.Duration);
}
catch (Exception ex)
{
Errors.Add(new Exception($"Could not start voice ATIS: {ex.Message}"), Plugin.DisplayName);
}
}
BroadcastTimer.Start();
}
private async void METARTimer_Elapsed(object sender, ElapsedEventArgs e)
{
var changes = false;
try
{
var atis1Updated = await ATIS1.UpdateMetar();
if (atis1Updated)
{
changes = true;
OnMETARUpdate(1);
}
}
catch { }
try
{
var atis2Updated = await ATIS2.UpdateMetar();
if (atis2Updated)
{
changes = true;
OnMETARUpdate(2);
}
}
catch { }
try
{
var atis3Updated = await ATIS3.UpdateMetar();
if (atis3Updated)
{
changes = true;
OnMETARUpdate(3);
}
}
catch { }
try
{
var atis4Updated = await ATIS4.UpdateMetar();
if (atis4Updated)
{
changes = true;
OnMETARUpdate(4);
}
}
catch { }
if (changes) PlayUpdateSound();
METARTimer.Start();
}
private void Audio_VSCSFrequenciesChanged(object sender, EventArgs e)
{
Frequencies.Clear();
foreach (var frequency in Audio.VSCSFrequencies)
{
if (!frequency.Transmit) continue;
Frequencies.Add(frequency);
}
}
private async void Network_Disconnected(object sender, EventArgs e)
{
if (Editor != null && !Editor.IsDisposed)
{
Editor.Dispose();
}
ToBroadcast.Clear();
await ATIS1?.Delete(false);
await ATIS2?.Delete(false);
await ATIS3?.Delete(false);
await ATIS4?.Delete(false);
}
private void OnUpdate(object sender, EventArgs e)
{
Editor?.RefreshEvent.Invoke(this, null);
}
private void PlayUpdateSound()
{
var sound = Path.Combine(Helpers.GetProgramFolder(), "wav", "AIS.wav");
SoundPlayer.SoundLocation = sound;
SoundPlayer.Play();
}
private void OnMETARUpdate(int number)
{
ShowEditorWindow();
Editor.Change(number);
Editor?.RefreshEvent.Invoke(this, null);
}
private void GetSettings()
{
var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
if (!configuration.HasFile) return;
if (!File.Exists(configuration.FilePath)) return;
var config = File.ReadAllText(configuration.FilePath);
XmlDocument doc = new XmlDocument();
doc.LoadXml(config);
XmlElement root = doc.DocumentElement;
var userSettings = root.SelectSingleNode("userSettings");
var settings = userSettings.SelectSingleNode("vatsys.Properties.Settings");
foreach (XmlNode node in settings.ChildNodes)
{
if (node.Attributes.GetNamedItem("name").Value == "DatasetPath")
{
DatasetPath = node.InnerText;
break;
}
}
}
private void GetData()
{
ATISData = (ATIS)LoadXML(DatasetPath + "\\ATIS.xml", typeof(ATIS));
Sectors = (Sectors)LoadXML(DatasetPath + "\\Sectors.xml", typeof(Sectors));
Airspace = (Airspace)LoadXML(DatasetPath + "\\Airspace.xml", typeof(Airspace));
}
public static async Task<string> GetMetar(string icao)
{
var response = await Client.GetAsync($"{MetarUri}{icao}");
if (!response.IsSuccessStatusCode) return null;
var content = await response.Content.ReadAsStringAsync();
return content;
}
public object LoadXML(string filePath, Type type)
{
if (!File.Exists(filePath)) return null;
var fileContents = File.ReadAllText(filePath);
var serializer = new XmlSerializer(type);
using (StringReader reader = new StringReader(fileContents))
{
return serializer.Deserialize(reader);
}
}
private void ATISMenu_Click(object sender, EventArgs e)
{
ShowEditorWindow();
}
private static void ShowEditorWindow()
{
if (ATISData == null || Sectors == null || Airspace == null)
{
Errors.Add(new Exception("Plugin was not started due to missing data."), DisplayName);
return;
}
MMI.InvokeOnGUI((MethodInvoker)delegate ()
{
if (Editor == null || Editor.IsDisposed)
{
Editor = new EditorWindow();
}
else if (Editor.Visible) return;
Editor.Show();
});
}
public void OnFDRUpdate(FDP2.FDR updated)
{
return;
}
public void OnRadarTrackUpdate(RDP.RadarTrack updated)
{
return;
}
}
}