-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
816 lines (702 loc) · 36.6 KB
/
MainWindow.xaml.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
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace shvAlert
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public static class ArrayExtensionMethods
{
public static ArraySegment<T> GetSegment<T>(this T[] arr, int offset, int? count = null)
{
if (count == null) { count = arr.Length - offset; }
return new ArraySegment<T>(arr, offset, count.Value);
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Read_Call_Series();
Read_Def_REGEXP();
}
List<CallSignAlloc> callsignseries = new List<CallSignAlloc>();
List<CallSignRegExp> callsignregexp = new List<CallSignRegExp>();
List<UDPModel.StatusModel> itemsStatusModel = new List<UDPModel.StatusModel>();
List<UDPModel.DecodeModel> itemsDecodeModel = new List<UDPModel.DecodeModel>();
private System.Net.Sockets.UdpClient udpClient = null;
private DispatcherTimer _timer;
//List<UDPModel.HeartbeatModel> itemsHeartbeatModel = new List<UDPModel.HeartbeatModel>();
private void ButtonAlertConnect2Mshv_Click(object sender, RoutedEventArgs e)
{
//ListenBroadcastMessage();
//RecieveUDPMessage();
//DeserializeUDPMessage();
//Task t = await OpenUDP(2237);
DataGridAlertResult.ItemsSource = itemsDecodeModel;
//ListViewAlertDebug.ItemsSource = itemsHeartbeatModel;
SetupTimer();
//Debug.WriteLine("ButtonAlertConnect2Mshv_Click SetupTimer");
ListViewAlertDebug.Items.Add("ButtonAlertConnect2Mshv_Click SetupTimer");
}
private void SetupTimer()
{
_timer = new DispatcherTimer();
_timer.Interval = new TimeSpan(0, 0, 15);
_timer.Tick += new EventHandler(MyTimerMethod);
_timer.Start();
Debug.WriteLine("SetupTimer _timer.Start()");
// 画面が閉じられるときに、タイマを停止
this.Closing += new CancelEventHandler(StopTimer);
}
private void MyTimerMethod(object sender, EventArgs e)
{
Debug.WriteLine("MyTimerMethod Start");
if (udpClient != null)
{
return;
}
//((Button)sender).IsEnabled = false;
System.Net.IPEndPoint localEP = new System.Net.IPEndPoint(System.Net.IPAddress.Any, int.Parse("2237"));
udpClient = new System.Net.Sockets.UdpClient(localEP);
DataGridAlertResult.Items.Refresh();
//非同期的なデータ受信を開始
udpClient.BeginReceive(ReceiveUDPCallback, udpClient);
}
private void StopTimer(object sender, CancelEventArgs e)// タイマを停止
{
_timer.Stop();
Debug.WriteLine("StopTimer _timer.Stop()");
}
private void ButtonStopAlert_Click(object sender, RoutedEventArgs e)
{
_timer.Stop();
Debug.WriteLine("ButtonStopAlert_Click _timer.Stop()");
}
public void Read_Call_Series()
{
string file_call_series = @"CallSignAllocation.txt";
if (System.IO.File.Exists(file_call_series))
{
using (StreamReader sr = new StreamReader(file_call_series))
{
Debug.WriteLine(@"file_call_series = " + file_call_series);
string line;
int no_data = 0;
while ((line = sr.ReadLine()) != null)
{
//Debug.WriteLine(line);
string[] fields = line.Split('\t');// 0=AAA-ABZ 1=Name
if (fields[0].Contains("-") && fields.Count() == 2)
{
//--------------------------------------------------------------Remove * and Split AAA-ABZ
string[] prefix = fields[0].Split('-');// 0=AAA 1=ABZ
if (prefix[0].Length == 3) { }
else
{
string str = prefix[0].Replace('*', ' ');
str = str.Trim();
prefix[0] = str;
}
//--------------------------------------------------------------Convert to ASCII Code. A=65 Z=90 0=48 9=57 Decimal
int[] PrefixStart = new int[3];
int[] PrefixEnd = new int[3];
for (int i = 0; i < 3; i++)
{
char[] cs = new char[] { 'A', 'B', 'C' };
char[] ce = new char[] { 'A', 'B', 'C' };
cs = prefix[0].ToCharArray();
ce = prefix[1].ToCharArray();
PrefixStart[i] = (int)cs[i];
PrefixEnd[i] = (int)ce[i];
}
//--------------------------------------------------------------Create callsignseries Table
for (int p1 = PrefixStart[0]; p1 <= PrefixEnd[0]; p1++)//First Letter SSN-STZ Sudan (Republic of the)
{
for (int p2 = PrefixStart[1]; p2 <= PrefixEnd[1]; p2++)//Second Letter
{
if (PrefixStart[2] == 65 && PrefixEnd[2] == 90)//Third Letter A-Z FULL
{
CallSignAlloc logItem = new CallSignAlloc()
{
strPrefix = ((char)p1).ToString() + ((char)p2).ToString(),
strPrefix3 = @"*",
strName = fields[1]
};
callsignseries.Add(logItem);
no_data++;
Debug.WriteLine(@"No.( {0} ) Start( {1} ) - End( {2} ) Name( {3} )", no_data, logItem.strPrefix, logItem.strPrefix3, logItem.strName);
}
else //A-Z Part
{
for (int p3 = PrefixStart[2]; p3 <= PrefixEnd[2]; p3++)
{
CallSignAlloc logItem = new CallSignAlloc()
{
strPrefix = ((char)p1).ToString() + ((char)p2).ToString(),
strPrefix3 = ((char)p3).ToString(),
strName = fields[1]
};
callsignseries.Add(logItem);
no_data++;
Debug.WriteLine(@"No.( {0} ) Start( {1} ) - End( {2} ) Name( {3} )", no_data, logItem.strPrefix, logItem.strPrefix3, logItem.strName);
}
PrefixStart[2] = 65;//A=65 Z=90
}
}
}
}
else
{
Debug.WriteLine(@"field[0] is NOT contained - string , OR TAB NOT found:" + line);
}
}
ListViewAlertDebug.Items.Add(file_call_series + " File read normaly. Number of data: " + no_data);
}
}
else
{
Debug.WriteLine(@"File not found:" + file_call_series);
}
}
public void Read_Def_REGEXP()
{
string file_call_regexp = @"CallSignRegularExpression.txt";
if (System.IO.File.Exists(file_call_regexp))
{
using (StreamReader sr = new StreamReader(file_call_regexp))
{
Debug.WriteLine(@"file_call_regexp = " + file_call_regexp);
string line;
int no_data = 0;
while ((line = sr.ReadLine()) != null)
{
Match matche = Regex.Match(line, @"\^.*\$");
if (0 < matche.Value.Length)
{
string[] fields = line.Split('$');
Debug.WriteLine("REGEXP: " + matche.Value + " ---> " + fields[1]);
CallSignRegExp regItem = new CallSignRegExp()
{
strRegExp = matche.Value,
strCountry = fields[1]
};
callsignregexp.Add(regItem);
no_data++;
Debug.WriteLine(@"No.( {0} ) RegExp( {1} ) - Country( {2} ) ", no_data, regItem.strRegExp, regItem.strCountry);
}
else
{
Debug.WriteLine("Comment: " + line);
}
}
ListViewAlertDebug.Items.Add(file_call_regexp + " File read normaly. Number of data: " + no_data);
}
}
else
{
Debug.WriteLine(@"File not found:" + file_call_regexp);
}
}
private void DispatcherMSG(string strMSG)
{
ListViewAlertDebug.Dispatcher.Invoke(() =>
{
ListViewAlertDebug.Items.Add(strMSG);
ListViewAlertDebug.Items.Refresh();
if (ListViewAlertDebug.Items.Count > 0)
{
var border = VisualTreeHelper.GetChild(ListViewAlertDebug, 0) as Decorator;
if (border != null)
{
var scroll = border.Child as ScrollViewer;
if (scroll != null) scroll.ScrollToEnd();
}
}
});
}
private void DispatcherLABEL(Label labelName, string strMSG)
{
labelName.Dispatcher.Invoke(() =>
{
labelName.Content = strMSG;
});
}
private string gMyGridLoc;
private void ReceiveUDPCallback(IAsyncResult ar)
{
System.Net.Sockets.UdpClient udp = (System.Net.Sockets.UdpClient)ar.AsyncState;
//非同期受信を終了する
System.Net.IPEndPoint remoteEP = null;
byte[] rcvBytes;
try
{
rcvBytes = udp.EndReceive(ar, ref remoteEP);
}
catch (System.Net.Sockets.SocketException ex)
{
Debug.WriteLine("受信エラー({0}/{1})", ex.Message, ex.ErrorCode);
return;
}
catch (ObjectDisposedException ee)
{
Debug.WriteLine("Socketは閉じられています。");
Debug.WriteLine("Caught: {0}", ee.Message);
return;
}
string rcvMsg = System.Text.Encoding.UTF8.GetString(rcvBytes);
string displayMsg = string.Format("[{0} ({1})] > {2}", remoteEP.Address, remoteEP.Port, rcvMsg);
string rcvHEX = BitConverter.ToString(rcvBytes);
//Debug.WriteLine(rcvHEX);
//Debug.WriteLine(displayMsg);
//-----------------------------------------------
UDPMessageUtils nmu = new UDPMessageUtils();
nmu.gIndex = 0;
//UDPModel.HeartbeatModel hm;
//UDPModel.StatusModel sm;
UDPModel.DecodeModel dm;
uint iMagic = nmu.Unpack4uint(rcvBytes, "magic");
uint iSchema = nmu.Unpack4uint(rcvBytes, "schema");
uint iMessageType = nmu.Unpack4uint(rcvBytes, "messageType");
Debug.WriteLine("iMessageType = {0}", iMessageType);
switch (iMessageType)
{
case 0://Heartbeat Out/In 0 quint32
string id0 = nmu.Unpackstring(rcvBytes, "id"); //Id (unique key) utf8
uint maxsch = nmu.Unpack4uint(rcvBytes, "maxscheme"); //Maximum schema number quint32
string version = nmu.Unpackstring(rcvBytes, "version"); //version utf8
string revision = "-";
if (id0 != @"JTDX")
{
revision = nmu.Unpackstring(rcvBytes, "revision"); //revision utf8
}
string hm = string.Format("id {0}, maxscheme {1}, version {2}, revision{3}", id0, maxsch, version, revision);
DispatcherLABEL(LabelID, id0);
DispatcherLABEL(LabelMaximumSchemaNum, maxsch.ToString());
DispatcherLABEL(LabelVersion, version);
DispatcherLABEL(LabelRevision, revision);
//DispatcherMSG(hm);
break;
case 1: //----------------------------------------------------------------------Status Out 1 quint32
string id1 = nmu.Unpackstring(rcvBytes, "id1"); //Id (unique key) utf8
if (id1.Contains("MSHV"))
{
UInt64 DialFrequency = nmu.Unpack8uint(rcvBytes, "DialFrequency"); //Dial Frequency (Hz) quint64
string Mode = nmu.Unpackstring(rcvBytes, "Mode"); //Mode utf8
string DXcall = nmu.Unpackstring(rcvBytes, "DXcall"); //DX call utf8
string Report = nmu.Unpackstring(rcvBytes, "Report"); //Report utf8
string TxMode = nmu.Unpackstring(rcvBytes, "TxMode"); //Tx Mode utf8
bool TxEnbledBool = nmu.Unpackbool(rcvBytes, "TxEnbledBool"); //Tx Enabled bool
bool TransmittingBool = nmu.Unpackbool(rcvBytes, "TransmittingBool"); //Transmitting bool
bool DecodingBool = nmu.Unpackbool(rcvBytes, "DecodingBool"); //Decoding bool
int RxDF = nmu.Unpack4int(rcvBytes, "RxDF"); //Rx DF qint32
int TxDF = nmu.Unpack4int(rcvBytes, "TxDF"); //Tx DF qint32
string DEcall = nmu.Unpackstring(rcvBytes, "DEcall"); //DE call utf8
string DEgrid = nmu.Unpackstring(rcvBytes, "DEgrid"); //DE grid utf8
string DXgrid = nmu.Unpackstring(rcvBytes, "DXgrid"); //DX grid utf8
bool TxWatchingBool = nmu.Unpackbool(rcvBytes, "TxWatchingBool"); //Tx Watchdog bool
string SubMode = nmu.Unpackstring(rcvBytes, "SubMode"); //Sub-mode utf8
bool FastModeBool = nmu.Unpackbool(rcvBytes, "FastModeBool"); //Fast mode bool
int SpecialOpMode = nmu.Unpack1int(rcvBytes, "SpecialOpMode"); //Special operation mode quint8
string sm = string.Format(
"{0}, {1}, {2}, {3}, {4}, {5}," +
"TxEnbledBool {6}, TransmittingBool {7}, DecodingBool {8}, RxDF {9}, TxDF {10}," +
" {11}, {12}, {13}, TxWatchingBool {14}, SubMode {15}," +
"FastModeBool {16}, SpecialOpMode {17}",
id1, DialFrequency, Mode, DXcall, Report, TxMode,
TxEnbledBool, TransmittingBool, DecodingBool, RxDF, TxDF,
DEcall, DEgrid, DXgrid, TxWatchingBool, SubMode,
FastModeBool, SpecialOpMode
);
gMyGridLoc = DEgrid;
DispatcherMSG(sm);
DispatcherLABEL(LabelDialFrequency, (DialFrequency / 1000).ToString("#,0"));
DispatcherLABEL(LabelDXcall, DXcall);
DispatcherLABEL(LabelDEcall, DEcall);
DispatcherLABEL(LabelDXgrid, DXgrid);
DispatcherLABEL(LabelDEgrid, DEgrid);
DispatcherLABEL(LabelMode, Mode);
DispatcherLABEL(LabelTxMode, TxMode);
DispatcherLABEL(LabelReport, Report);
}
else if (id1.Contains("WSJT") || id1.Contains("JTDX"))
{
UInt64 DialFrequency = nmu.Unpack8uint(rcvBytes, "DialFrequency"); //Dial Frequency (Hz) quint64
string Mode = nmu.Unpackstring(rcvBytes, "Mode"); //Mode utf8
string DXcall = nmu.Unpackstring(rcvBytes, "DXcall"); //DX call utf8
string Report = nmu.Unpackstring(rcvBytes, "Report"); //Report utf8
string TxMode = nmu.Unpackstring(rcvBytes, "TxMode"); //Tx Mode utf8
bool TxEnbledBool = nmu.Unpackbool(rcvBytes, "TxEnbledBool"); //Tx Enabled bool
bool TransmittingBool = nmu.Unpackbool(rcvBytes, "TransmittingBool"); //Transmitting bool
bool DecodingBool = nmu.Unpackbool(rcvBytes, "DecodingBool"); //Decoding bool
uint RxDF = nmu.Unpack4uint(rcvBytes, "RxDF"); //Rx DF qint32 >>> quint32
uint TxDF = nmu.Unpack4uint(rcvBytes, "TxDF"); //Tx DF qint32 >>> quint32
string DEcall = nmu.Unpackstring(rcvBytes, "DEcall"); //DE call utf8
string DEgrid = nmu.Unpackstring(rcvBytes, "DEgrid"); //DE grid utf8
string DXgrid = nmu.Unpackstring(rcvBytes, "DXgrid"); //DX grid utf8
bool TxWatchingBool = nmu.Unpackbool(rcvBytes, "TxWatchingBool"); //Tx Watchdog bool
string SubMode = nmu.Unpackstring(rcvBytes, "SubMode"); //Sub-mode utf8
bool FastModeBool = nmu.Unpackbool(rcvBytes, "FastModeBool"); //Fast mode bool
string sm = "";
if (id1.Contains("WSJT"))
{
int SpecialOpMode = nmu.Unpack1int(rcvBytes, "SpecialOpMode"); //Special operation mode quint8
int FrequencyTolerance = nmu.Unpack4int(rcvBytes, "FrequencyTolerance"); //Frequency Tolerance quint32
int TRPeriod = nmu.Unpack4int(rcvBytes, "TRPeriod"); //T / R Period quint32
string ConfigurationName = nmu.Unpackstring(rcvBytes, "ConfigurationName"); //Configuration Name utf8
sm = string.Format(
"{0}, DialFrequency {1}, Mode {2}, DXcall {3}, Report {4}, TxMode {5}," +
"TxEnbledBool {6}, TransmittingBool {7}, DecodingBool {8}, RxDF {9}, TxDF {10}," +
"DEcall {11}, DEgrid {12}, DXgrid {13}, TxWatchingBool {14}, SubMode {15}," +
"FastModeBool {16}, SpecialOpMode {17}, FrequencyTolerance {18}, TRPeriod {19}, ConfigurationName {20} ",
id1, DialFrequency, Mode, DXcall, Report, TxMode, TxEnbledBool, TransmittingBool, DecodingBool, RxDF, TxDF,
DEcall, DEgrid, DXgrid, TxWatchingBool, SubMode, FastModeBool, SpecialOpMode, FrequencyTolerance, TRPeriod, ConfigurationName
);
}
else
{
bool TxFirstBool = nmu.Unpackbool(rcvBytes, "TxFirstBool"); //TX first bool
sm = string.Format(
"{0}, DialFrequency {1}, Mode {2}, DXcall {3}, Report {4}, TxMode {5}," +
"TxEnbledBool {6}, TransmittingBool {7}, DecodingBool {8}, RxDF {9}, TxDF {10}," +
"DEcall {11}, DEgrid {12}, DXgrid {13}, TxWatchingBool {14}, SubMode {15}," +
"FastModeBool {16}, TxFirstBool {17}",
id1, DialFrequency, Mode, DXcall, Report, TxMode, TxEnbledBool, TransmittingBool, DecodingBool, RxDF, TxDF,
DEcall, DEgrid, DXgrid, TxWatchingBool, SubMode, FastModeBool, TxFirstBool
);
}
gMyGridLoc = DEgrid;
DispatcherMSG(sm);
DispatcherLABEL(LabelDialFrequency, (DialFrequency / 1000).ToString("#,0"));
DispatcherLABEL(LabelDXcall, DXcall);
DispatcherLABEL(LabelDEcall, DEcall);
DispatcherLABEL(LabelDXgrid, DXgrid);
DispatcherLABEL(LabelDEgrid, DEgrid);
DispatcherLABEL(LabelMode, Mode);
DispatcherLABEL(LabelTxMode, TxMode);
DispatcherLABEL(LabelReport, Report);
}
else
{
DispatcherMSG(string.Format("*** Illegal id1 {0} Found. It is not MSHV,WSJT-X,JTDX.", id1));
}
break;
case 2: //----------------------------------------------------------------------Decode
string id2 = nmu.Unpackstring(rcvBytes, "id");
bool boNew = nmu.Unpackbool(rcvBytes, "isNew");
DateTime datetm = nmu.UnpackDateTime(rcvBytes, "tm");
int SNR = nmu.Unpack4int(rcvBytes, "snr");
float DT = nmu.Unpack8float(rcvBytes, "dt");
uint DF = nmu.Unpack4uint(rcvBytes, "df");
string MODE = nmu.Unpackstring(rcvBytes, "mode");
string Message = nmu.Unpackstring(rcvBytes, "message");
string[] strCountry = GetCountry(Message); // Get Call Sign Allocation Info. strCountry[2]=Distance Km
dm = new UDPModel.DecodeModel()
{
//heartbeat_client_id = iMagic,
//heartbeat_maximum_schema_number = iSchema,
decode_client_id = id2,
decode_new = boNew,
decode_time = datetm,
decode_snr = SNR,
decode_delta_time = Math.Round(DT, 1),
decode_delta_frequency = DF,
decode_mode = MODE,
decode_message = Message,
alloc_left = strCountry[0], // Left Call
alloc_right = strCountry[1], // Right Call
distance_from_here = strCountry[2],
distance_between_them = @""
};
DataGridAlertResult.Dispatcher.Invoke(() =>
{
itemsDecodeModel.Add(dm);
Debug.WriteLine(itemsDecodeModel.Count);
DataGridAlertResult.ItemsSource = itemsDecodeModel;
DataGridAlertResult.Items.Refresh();
//DataGridAlertResult.UpdateLayout();
if (DataGridAlertResult.Items.Count > 0)
{
var border = VisualTreeHelper.GetChild(DataGridAlertResult, 0) as Decorator;
if (border != null)
{
var scroll = border.Child as ScrollViewer;
if (scroll != null) scroll.ScrollToEnd();
}
}
//string[] strCountry = GetCountry(Message);
});
break;
default:
Debug.WriteLine("*** iMessageType {0} Found", iMessageType);
break;
}
//再びデータ受信を開始する
udp.BeginReceive(ReceiveUDPCallback, udp);
//Debug.WriteLine("BeginReceive Again");
}
private void DataGridAlertResult_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
Style h_Right = new Style();
h_Right.Setters.Add(new Setter(HorizontalAlignmentProperty, HorizontalAlignment.Right));
e.Column.CellStyle = h_Right;
string headername = e.Column.Header.ToString();
switch (headername)
{
case "decode_client_id":
e.Column.Header = "Client";
break;
case "decode_new":
e.Column.Header = "New";
break;
case "decode_time":
e.Column.Header = "UTC";
break;
case "decode_snr":
e.Column.Header = "dB";
break;
case "decode_delta_time":
e.Column.Header = "DT";
break;
case "decode_delta_frequency":
e.Column.Header = "DF";
break;
case "decode_mode":
e.Column.Header = "Mode";
break;
case "decode_message":
e.Column.Header = "Message";
break;
case "alloc_left":
e.Column.Header = "Call 1";
break;
case "alloc_right":
e.Column.Header = "Call 2";
break;
case "distance_from_here":
e.Column.Header = "From Here Km";
break;
case "distance_between_them":
e.Column.Header = "Between Km";
break;
default:
e.Column.Header = "Default";
break;
}
}
//public string[] GetCountry2(string decodedMSG)
//{
// string[] retCountry = new string[2];
// return retCountry;
//}
public string[] GetCountry(string decodedMSG)
{
// CQ JA1AAA PM96
// JA1AAA JA9SSS PM96
// JA1AAA JA9SSS -10
// JA1AAA JA9SSS R-10
// JA1AAA JA9SSS 73
// JA1AAA JA9SSS RR73
// JA1AAA JA9SSS RRR
//-----------------------------------------------------
string[] retCountry = new string[3]; // LeftCall RightCall RightCallDistance Km
MatchCollection results = Regex.Matches(decodedMSG, "[^ ]+");
foreach (Match m in results)
{
Debug.WriteLine(m.Value);
}
string[] fields = new string[3];
int fieldsLength = results.Count();
switch (fieldsLength)
{
case 2:// JA1AAA JA9SSS
DispatcherMSG(decodedMSG + " --- Length 2");
//Array.Resize(ref fields, fields.Length + 1);
//fields[fields.Length - 1] = @"NULL";
retCountry[2] = @"Null GL";
fields[0] = results[0].Value;
fields[1] = results[1].Value;
fields[2] = @"NULL";
break;
case 3:// JA1AAA JA9SSS PM96
Debug.WriteLine("===> " + results[2].Value); // PM96, 73, RR73, RRR
fields[0] = results[0].Value;
fields[1] = results[1].Value;
fields[2] = results[2].Value;
break;
case 4:
// CQ EU JA1AAA PM96
// <ZW86LABRE> WF3W R FN20 ---need bug fix
//
DispatcherMSG(decodedMSG + " --- Length 4");
fields[0] = results[0].Value;
fields[1] = results[2].Value;
fields[2] = results[3].Value;
//var list = new List<string>();
//list.AddRange(fields);
//list.RemoveAt(1);// del EU
//fields = list.ToArray();
break;
default:
DispatcherMSG(decodedMSG + " --- Illegal Length = " + fieldsLength);
retCountry[0] = @"ILLEGAL";
retCountry[1] = @"FIELDS";
retCountry[2] = @"LENGTH";
return retCountry;
}
for (int i = 0; i < 2; i++)
{
string calli = CheckIllegalCall(fields[i]);
string pre2 = calli.Substring(0, 2);
if (pre2 == @"CQ")
{
retCountry[i] = pre2;
}
else
{
Debug.Write(pre2 + " in " + calli + " " + i + ", ");
if (callsignseries.Exists(x => x.strPrefix.Contains(pre2)))
{
retCountry[i] = callsignseries.Find(x => x.strPrefix == pre2).strName;
Debug.WriteLine(retCountry[i] + " exists in callsignseries, " + decodedMSG);
if (retCountry[i].Contains("REGEXP"))
{
string Province = GetCountryREGEXP(calli);
retCountry[i] = Province;
Debug.WriteLine("REGEXP: " + Province + " " + fields[i]);
}
}
else
{
retCountry[i] = pre2 + " n/a";
string s = decodedMSG + ", Prefix " + pre2 + " NOT FOUND in callsignseries";
Debug.WriteLine(s);
DispatcherMSG(s);
}
}
//Debug.WriteLine(retCountry[i]);
}
//Debug.WriteLine("===> " + fields[2]);
Match match2 = Regex.Match(fields[2], @"[A-R]{2}[0-9]{2}");
if (0 < match2.Value.Length)
{
if (fields[2] != @"RR73")
{
int dist = GetDistanceBetween(fields[2], gMyGridLoc); // Get Distance Between
retCountry[2] = dist.ToString();
}
else
{
retCountry[2] = @"";
}
}
return retCountry;
}
public string CheckIllegalCall(string callSign)// <RA0AA> JA1AAA/M
{
int orgLength = callSign.Length;
string orgCall = callSign;
if (callSign.Contains(@"<")) callSign = callSign.Replace(@"<", @"");
if (callSign.Contains(@">")) callSign = callSign.Replace(@">", @"");
if (callSign.Contains(@"/")) callSign = callSign.Substring(0, callSign.LastIndexOf(@"/"));
if (orgLength != callSign.Length)
{
DispatcherMSG(@"CheckIllegalCall Modified: " + orgCall + " to " + callSign);
}
return callSign;
}
public string GetCountryREGEXP(string callSign)// RA0AA
{
foreach (var item in callsignregexp)
{
Match matche = Regex.Match(callSign, item.strRegExp);
if (0 < matche.Value.Length)
{
return item.strCountry;
}
}
DispatcherMSG(@"GetCountryREGEXP: Province NOT found " + callSign);
return "Province NOT found " + callSign;
}
public int GetDistanceBetween(string DxGrid, string DeGrid)
{
//int[] retCountry = new int[2];
//DxGrid = @"NO65";
//DeGrid = @"PM96";
char[] cdx = DxGrid.ToCharArray();
char[] cde = DeGrid.ToCharArray();
double lonDX = ((int)cdx[0] - 65) * 20.0 + Convert.ToInt32(cdx[2].ToString()) * 2.0 + 1.0 - 180.0;
double latDX = ((int)cdx[1] - 65) * 10.0 + Convert.ToInt32(cdx[3].ToString()) + 0.5 - 90.0;
double lonDE = ((int)cde[0] - 65) * 20.0 + Convert.ToInt32(cde[2].ToString()) * 2.0 + 1.0 - 180.0;
double latDE = ((int)cde[1] - 65) * 10.0 + Convert.ToInt32(cde[3].ToString()) + 0.5 - 90.0;
//int longitude =(CODE(MID(A1,1,1))-65)*20 + VALUE(MID(A1,3,1))*2 + (CODE(MID(A1,5,1))-97)/12 + 1/24 - 180
//int latitude =(CODE(MID(A1, 2, 1)) - 65) * 10 + VALUE(MID(A1, 4, 1)) + (CODE(MID(A1, 6, 1)) - 97) / 24 + 1 / 48 - 90;
//--------------------------------------------------------------Convert to ASCII Code. A=65 Z=90 0=48 9=57 Decimal
/*
Assuming for the sake of precision that the particular point in the grid square
that you want the exact latitude and longitude for is the midpoint of the 6-character subsquare,
this can be done readily with Excel formulas.
If the 6-character grid square data is in cell A1, in a format similar to AA00aa (i.e. upper-case, then digits, then lower-case),
the formula for the latitude (based directly on the Python code posted previously) is:
=(CODE(MID(A1,2,1))-65)*10 + VALUE(MID(A1,4,1)) + (CODE(MID(A1,6,1))-97)/24 + 1/48 - 90
and the formula for the longitude is
=(CODE(MID(A1,1,1))-65)*20 + VALUE(MID(A1,3,1))*2 + (CODE(MID(A1,5,1))-97)/12 + 1/24 - 180
If you want the latitude and longitude of the southwest corner of the subsquare, just leave out the + 1/48 and + 1/24 terms.
Add error-checking, upper- and lower-case conversion, conversion of four-character squares to six-character by adding 'mm',
and other embellishments as you see fit.
*/
return CalcDistance(latDX, latDE, lonDX, lonDE);
}
public int CalcDistance(double lat1, double lat2, double lon1, double lon2)
{
double R = 6371000;
double phi1 = ToRadian(lat1);
double phi2 = ToRadian(lat2);
double deltaPhi = ToRadian(lat2 - lat1);
double deltaLambda = ToRadian(lon2 - lon1);
double a = Math.Sin(deltaPhi / 2) * Math.Sin(deltaPhi / 2) +
Math.Cos(phi1) * Math.Cos(phi2) *
Math.Sin(deltaLambda / 2) * Math.Sin(deltaLambda / 2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
double d = R * c / 1000.0; //Km
/*
var R = 6371e3; // metres
var φ1 = lat1.toRadians();
var φ2 = lat2.toRadians();
var Δφ = (lat2-lat1).toRadians();
var Δλ = (lon2-lon1).toRadians();
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
*/
return (int)d; //Km
}
public double ToRadian(double angle)
{
return (double)(angle * Math.PI / 180);
}
}
}