-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMySimulatorModel.cs
More file actions
676 lines (654 loc) · 25.1 KB
/
Copy pathMySimulatorModel.cs
File metadata and controls
676 lines (654 loc) · 25.1 KB
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
using FlightSimulatorApp.Utilities;
using Microsoft.Maps.MapControl.WPF;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Text;
using System.Threading;
namespace FlightSimulatorApp.Model
{
public class MySimulatorModel : ISimulatorModel
{
private readonly ITelnet telnetClient;
private volatile bool stop;
public event PropertyChangedEventHandler PropertyChanged;
private volatile bool errorsInIpAndPort = false;
private readonly Queue<string> commandsQueue = new Queue<string>();
//Connection Buttons property as a data member
private string dm_currStatus = "Disconnected";
private string dm_error;
[Obsolete]
private string dm_IPaddress = ConfigurationSettings.AppSettings.Get("IPdefault");
[Obsolete]
private string dm_port = ConfigurationSettings.AppSettings.Get("Portdefault");
//Dashboard Properties as data members
private string dm_heading = "ERR";
private string dm_verticalSpeed = "ERR";
private string dm_groundSpeed = "ERR";
private string dm_airSpeed = "ERR";
private string dm_altitude = "ERR";
private string dm_internalRoll = "ERR";
private string dm_internalPitch = "ERR";
private string dm_altimeter = "ERR";
//Joystick's + Sliders' properties as data members
private double dm_rudder = 0;
private double dm_elevator = 0;
private double dm_aileron = 0;
private double dm_throttle = 0;
//Map's properties as data members
private double dm_latitude = 32.002644;
private double dm_longitude = 34.888781;
private Location dm_location = new Location(32.002644, 34.888781);
private readonly Mutex mutex = new Mutex();
public MySimulatorModel(ITelnet tc)
{
this.telnetClient = tc;
this.stop = false;
}
//Connection Buttons property
public string CurrStatus
{
get { return dm_currStatus; }
set
{
dm_currStatus = value;
NotifyPropertyChanged("CurrStatus");
}
}
//Dashboard Properties
public string Heading
{
get { return dm_heading; }
set {
if (Double.TryParse(value, out double temp))
{
//Success in converting to double - No error occurred
temp *= 1000;
int valAsInt = (int)temp;
temp = (double)valAsInt;
temp /= 1000;
dm_heading = temp.ToString();
}
else
{
dm_heading = "ERR";
}
NotifyPropertyChanged("Heading");
}
}
public string VerticalSpeed {
get { return dm_verticalSpeed; }
set
{
if (Double.TryParse(value, out double temp))
{
//Success in converting to double - No error occurred
temp *= 1000;
int valAsInt = (int)temp;
temp = (double)valAsInt;
temp /= 1000;
dm_verticalSpeed = temp.ToString();
}
else
{
dm_verticalSpeed = "ERR";
}
NotifyPropertyChanged("VerticalSpeed");
}
}
public string GroundSpeed {
get { return dm_groundSpeed; }
set
{
if (Double.TryParse(value, out double temp))
{
//Success in converting to double - No error occurred
temp *= 1000;
int valAsInt = (int)temp;
temp = (double)valAsInt;
temp /= 1000;
dm_groundSpeed = temp.ToString();
}
else
{
dm_groundSpeed = "ERR";
}
NotifyPropertyChanged("GroundSpeed");
}
}
public string AirSpeed {
get { return dm_airSpeed; }
set
{
if (Double.TryParse(value, out double temp))
{
//Success in converting to double - No error occurred
temp *= 1000;
int valAsInt = (int)temp;
temp = (double)valAsInt;
temp /= 1000;
dm_airSpeed = temp.ToString();
}
else
{
dm_airSpeed = "ERR";
}
NotifyPropertyChanged("AirSpeed");
}
}
public string Altitude {
get { return dm_altitude; }
set
{
if (Double.TryParse(value, out double temp))
{
//Success in converting to double - No error occurred
temp *= 1000;
int valAsInt = (int)temp;
temp = (double)valAsInt;
temp /= 1000;
dm_altitude = temp.ToString();
}
else
{
dm_altitude = "ERR";
}
NotifyPropertyChanged("Altitude");
}
}
public string InternalRoll {
get { return dm_internalRoll; }
set
{
if (Double.TryParse(value, out double temp))
{
//Success in converting to double - No error occurred
temp *= 1000;
int valAsInt = (int)temp;
temp = (double)valAsInt;
temp /= 1000;
dm_internalRoll = temp.ToString();
}
else
{
dm_internalRoll = "ERR";
}
NotifyPropertyChanged("InternalRoll");
}
}
public string InternalPitch {
get { return dm_internalPitch; }
set
{
if (Double.TryParse(value, out double temp))
{
//Success in converting to double - No error occurred
temp *= 1000;
int valAsInt = (int)temp;
temp = (double)valAsInt;
temp /= 1000;
dm_internalPitch = temp.ToString();
}
else
{
dm_internalPitch = "ERR";
}
NotifyPropertyChanged("InternalPitch");
}
}
public string Altimeter {
get { return dm_altimeter; }
set
{
if (Double.TryParse(value, out double temp))
{
//Success in converting to double - No error occurred
temp *= 1000;
int valAsInt = (int)temp;
temp = (double)valAsInt;
temp /= 1000;
dm_altimeter = temp.ToString();
}
else
{
dm_altimeter = "ERR";
}
NotifyPropertyChanged("Altimeter");
}
}
//Joystick's + Sliders' properties
public double Rudder {
get { return dm_rudder; }
set
{
dm_rudder = value;
NotifyPropertyChanged("Rudder");
}
}
public double Elevator {
get { return dm_elevator; }
set
{
dm_elevator = value;
NotifyPropertyChanged("Elevator");
}
}
public double Aileron {
get { return dm_aileron; }
set
{
dm_aileron = value;
NotifyPropertyChanged("Aileron");
}
}
public double Throttle {
get { return dm_throttle; }
set
{
dm_throttle = value;
NotifyPropertyChanged("Throttle");
}
}
//Map's properties - Determine the plane's location over the Bing map
public double Longitude
{ get { return dm_longitude; }
set
{
if ((value >= -180) && (value <= 180))
{
dm_longitude = value;
NotifyPropertyChanged("Longitude");
} else
{
dm_error = "ERR";
}
}
}
public double Latitude
{
get { return dm_latitude; }
set
{
if ((value <= 90) && (value >= -90))
{
dm_latitude = value;
NotifyPropertyChanged("Latitude");
} else
{
dm_error = "ERR";
}
}
}
public Location Location {
get { return dm_location; }
set
{
dm_location = value;
NotifyPropertyChanged("Location");
}
}
public void Connect(string ip, string port)
{
try
{
//Checking if the given ip and port are valid!
if (Int32.TryParse(port, out int portAsInt))
{
if ((ip.Contains(".")) && (System.Net.IPAddress.TryParse(ip, out System.Net.IPAddress address)))
{
try
{
telnetClient.Connect(ip, portAsInt);
stop = false;
CurrStatus = "Connected";
errorsInIpAndPort = false;
Err = "";
} catch (Exception)
{
CurrStatus = "Connection Has Failed";
}
} else
{
CurrStatus = "Invalid port or IP address - Please try again";
errorsInIpAndPort = true;
}
} else
{
CurrStatus = "Invalid port or IP address - Please try again";
errorsInIpAndPort = true;
}
}
catch (TimeoutException)
{
//Timeout occurred
CurrStatus = "Timeout Occurred";
}
catch (Exception)
{
CurrStatus = "Connection Has Failed";
}
}
public void Disconnect()
{
try
{
if (!errorsInIpAndPort)
{
stop = true;
telnetClient.Disconnect();
CurrStatus = "Disconnected";
Err = "";
}
} catch (Exception)
{
stop = true;
CurrStatus = "Disconnected";
Err = "";
}
}
public void Start()
{
if (!errorsInIpAndPort)
{
new Thread(delegate ()
{
while (!stop)
{
mutex.WaitOne();
try
{
string tempStr;
if (telnetClient.IsSocketAvailableWriting())
{
telnetClient.Write("get /instrumentation/heading-indicator/indicated-heading-deg");
}
else
{
Err = "Timeout - Heading";
}
if (telnetClient.IsSocketAvailableReading())
{
tempStr = telnetClient.Read();
Heading = tempStr;
if (Err.Equals("Timeout - Heading"))
{
Err = "";
}
}
else
{
Err = "Timeout - Heading";
}
if (telnetClient.IsSocketAvailableWriting())
{
telnetClient.Write("get /instrumentation/gps/indicated-vertical-speed");
}
else
{
Err = "Timeout - VerticalSpeed";
}
if (telnetClient.IsSocketAvailableReading())
{
tempStr = telnetClient.Read();
VerticalSpeed = tempStr;
if (Err.Equals("Timeout - VerticalSpeed"))
{
Err = "";
}
}
else
{
Err = "Timeout - VerticalSpeed";
}
if (telnetClient.IsSocketAvailableWriting())
{
telnetClient.Write("get /instrumentation/gps/indicated-ground-speed-kt");
}
else
{
Err = "Timeout - GroundSpeed";
}
if (telnetClient.IsSocketAvailableReading())
{
tempStr = telnetClient.Read();
GroundSpeed = tempStr;
if (Err.Equals("Timeout - GroundSpeed"))
{
Err = "";
}
}
else
{
Err = "Timeout - GroundSpeed";
}
if (telnetClient.IsSocketAvailableWriting())
{
telnetClient.Write("get /instrumentation/airspeed-indicator/indicated-speed-kt");
}
else
{
Err = "Timeout - AirSpeed";
}
if (telnetClient.IsSocketAvailableReading())
{
tempStr = telnetClient.Read();
AirSpeed = tempStr;
if (Err.Equals("Timeout - AirSpeed"))
{
Err = "";
}
}
else
{
Err = "Timeout - AirSpeed";
}
if (telnetClient.IsSocketAvailableWriting())
{
telnetClient.Write("get /instrumentation/gps/indicated-altitude-ft");
}
else
{
Err = "Timeout - Altitude";
}
if (telnetClient.IsSocketAvailableReading())
{
tempStr = telnetClient.Read();
Altitude = tempStr;
if (Err.Equals("Timeout - Altitude"))
{
Err = "";
}
}
else
{
Err = "Timeout - Altitude";
}
if (telnetClient.IsSocketAvailableWriting())
{
telnetClient.Write("get /instrumentation/attitude-indicator/internal-roll-deg");
}
else
{
Err = "Timeout - InternalRoll";
}
if (telnetClient.IsSocketAvailableReading())
{
tempStr = telnetClient.Read();
InternalRoll = tempStr;
if (Err.Equals("Timeout - InternalRoll"))
{
Err = "";
}
}
else
{
Err = "Timeout - InternalRoll";
}
if (telnetClient.IsSocketAvailableWriting())
{
telnetClient.Write("get /instrumentation/attitude-indicator/internal-pitch-deg");
}
else
{
Err = "Timeout - InternalPitch";
}
if (telnetClient.IsSocketAvailableReading())
{
tempStr = telnetClient.Read();
InternalPitch = tempStr;
if (Err.Equals("Timeout - InternalPitch"))
{
Err = "";
}
}
else
{
Err = "Timeout - InternalPitch";
}
if (telnetClient.IsSocketAvailableWriting())
{
telnetClient.Write("get /instrumentation/altimeter/indicated-altitude-ft");
}
else
{
Err = "Timeout - Altimeter";
}
if (telnetClient.IsSocketAvailableReading())
{
tempStr = telnetClient.Read();
Altimeter = tempStr;
if (Err.Equals("Timeout - Altimeter"))
{
Err = "";
}
}
else
{
Err = "Timeout - Altimeter";
}
if (telnetClient.IsSocketAvailableWriting())
{
telnetClient.Write("get /position/longitude-deg");
}
else
{
Err = "Timeout - Longitude";
}
if (telnetClient.IsSocketAvailableReading())
{
tempStr = telnetClient.Read();
if (!(tempStr.Equals("ERR")))
{
Longitude = Double.Parse(tempStr);
if (Err.Equals("Timeout - Longitude"))
{
Err = "";
}
}
}
else
{
Err = "Timeout - Longitude";
}
if (telnetClient.IsSocketAvailableWriting())
{
telnetClient.Write("get /position/latitude-deg");
}
else
{
Err = "Timeout - Latitude";
}
if (telnetClient.IsSocketAvailableReading())
{
tempStr = telnetClient.Read();
if (!(tempStr.Equals("ERR")))
{
Latitude = Double.Parse(tempStr);
if (Err.Equals("Timeout - Latitude"))
{
Err = "";
}
}
}
else
{
Err = "Timeout - Latitude";
}
Location = new Location(Latitude, Longitude);
mutex.ReleaseMutex();
Thread.Sleep(250); //SLEEP FOR 250 MS - that determines we will ask for details 4 times in a sec.
}
catch (Exception)
{
mutex.ReleaseMutex();
Err = "ERR";
Thread.Sleep(250);
Err = "";
}
}
}).Start();
new Thread(delegate ()
{
while (!stop)
{
try
{
if (!errorsInIpAndPort)
{
if (telnetClient.IsSocketAvailableWriting())
{
// send only if the queue not empty
if (commandsQueue.Count != 0)
{
mutex.WaitOne();
telnetClient.Write(commandsQueue.Dequeue());
mutex.ReleaseMutex();
}
}
else
{
Err = "Timeout in set";
}
}
Thread.Sleep(250);
Err = "";
}
catch (Exception)
{
mutex.ReleaseMutex();
Err = "Timeout in set";
Thread.Sleep(250);
Err = "";
}
}
}).Start();
}
}
public void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void SendCommandToSimulator(string command)
{
commandsQueue.Enqueue(command);
}
public string Err
{
get
{
return dm_error;
}
set
{
dm_error = value;
NotifyPropertyChanged("Err");
}
}
[Obsolete]
public string IPaddress { get { return dm_IPaddress; } set { dm_IPaddress = value; } }
[Obsolete]
public string Port { get { return dm_port; } set { dm_port = value; } }
}
}