-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocMorse.cs
1088 lines (899 loc) · 39.8 KB
/
DocMorse.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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Collections.Concurrent;
//using System.Data.SqlClient;
//Microsoft.Data.SqlClient the new api for ms db's
//https://mariadb.com/kb/en/mariadb-and-net/ // Question and answer.
//https://docs.microsoft.com/en-us/azure/mysql/connect-csharp // good tutorial for starting up.
//https://mysqlconnector.net/ mit project for ado.net connectivity.
using Play.Interfaces.Embedding;
using Play.Rectangles;
using Play.Edit;
using Play.Integration;
using Play.Parse.Impl;
using Play.Forms;
//using MySql.Data.MySqlClient;
namespace Play.MorsePractice {
public class CallsDoc : Editor {
public CallsDoc( IPgBaseSite oSite ) : base( oSite ) { }
public override WorkerStatus PlayStatus {
get { return( WorkerStatus.BUSY ); }
}
}
/// <summary>
/// This class is not dedicated to only the morse code operations.
/// Factored out all the logging helpers from this class.
/// </summary>
/// <seealso cref="DocNotes"/>
public class MorseDoc:
IPgParent,
IPgLoad<TextReader>,
IPgSave<TextWriter>,
IDisposable
{
readonly IPgScheduler _oScheduler;
protected class MorseDocSlot :
IPgBaseSite,
IPgFileSite
{
protected readonly MorseDoc _oHost;
protected readonly string _strName;
public MorseDocSlot( MorseDoc oHost, string strName ) {
_oHost = oHost ?? throw new ArgumentNullException();
_strName = strName ?? throw new ArgumentNullException();
}
public IPgParent Host => _oHost;
public void LogError(string strMessage, string strDetails, bool fShow=true) {
_oHost.LogError( strMessage, strDetails, fShow );
}
public void Notify( ShellNotify eEvent ) {
}
FILESTATS IPgFileSite.FileStatus => _oHost._oSiteFile.FileStatus;
Encoding IPgFileSite.FileEncoding => _oHost._oSiteFile.FileEncoding;
string IPgFileSite.FilePath => _oHost._oSiteFile.FilePath;
string IPgFileSite.FileBase => _oHost._oSiteFile.FileBase + " / " + _strName;
}
readonly string _strMorseTable = @"Play.MorsePractice.Content.international-morse-code.txt";
readonly IPgBaseSite _oSiteBase;
readonly IPgFileSite _oSiteFile;
// Stuff for the morse code pracice view.
public Editor Source { get; } // practice code tones generated from this editor.
public Editor Notes { get; } // practice screen notes. and primary view.
public Editor Stats { get; } // put stats or more audio reference here.
public Editor Morse { get; } // Our reference to the morse values. Let's make this audio!!
protected static readonly HttpClient _oHttpClient = new HttpClient();
/// <summary>
/// Document object for a little Morse Practice document.
/// </summary>
public MorseDoc( IPgBaseSite oSiteBase ) {
_oSiteBase = oSiteBase ?? throw new ArgumentNullException();
_oSiteFile = oSiteBase as IPgFileSite ?? throw new ArgumentException( "Host needs the IPgFileSite interface" );
_oScheduler = Services as IPgScheduler ?? throw new ArgumentException("Host requries IPgScheduler");
Source = new Editor ( new MorseDocSlot( this, "Source" ) ); // Morse code source for practice.
Notes = new Editor ( new MorseDocSlot( this, "Notes" ) ); // Notes for listening to morse, or log files.
Stats = new Editor ( new MorseDocSlot( this, "Stats" ) );
Morse = new Editor ( new MorseDocSlot( this, "Ref" ) ); // Refrence table of morse code letters.
new ParseHandlerText ( Notes, "text" );
}
private bool _fDisposed = false;
public void Dispose() {
if( !_fDisposed ) {
Source.Dispose();
Notes .Dispose();
Stats .Dispose();
Morse .Dispose();
_fDisposed = true;
}
}
protected void LogError( string strMessage, string strDetails, bool fShow = false ) {
_oSiteBase.LogError( strMessage, strDetails, fShow );
}
public bool IsDirty => Notes.IsDirty; // || Sources.IsDirty;
public IPgParent Parentage => _oSiteBase.Host;
public IPgParent Services => Parentage.Services;
/// <summary>
/// Load Morse code reference text.
/// </summary>
/// <remarks>Not likely to fail once get working. So no return value.</remarks>
protected void LoadMorse() {
Assembly oAssembly = Assembly.GetExecutingAssembly();
try {
using( Stream oStream = oAssembly.GetManifestResourceStream( _strMorseTable )) {
using( TextReader oText = new StreamReader( oStream, true ) ) {
Morse.Load( oText );
}
}
} catch( Exception oEx ) {
Type[] rgErrors = { typeof( NullReferenceException ),
typeof( ArgumentNullException ),
typeof( ArgumentException ),
typeof( FileLoadException ),
typeof( BadImageFormatException ),
typeof( NotImplementedException ) };
if( rgErrors.IsUnhandled( oEx ) )
throw;
}
}
/// <summary>
/// Both InitNew and Load call this base initialization function.
/// </summary>
protected bool Initialize() {
if( !Source.InitNew() )
return false;
if( !Notes.InitNew() )
return false;
if( !Stats.InitNew() )
return false;
LoadMorse();
// LoadStats(); We'll load the stats table here, it'll double as an audio reference.
return true;
}
public bool InitNew() {
if( !Initialize() )
return false;
return true;
}
public bool Load(TextReader oStream) {
if( !Initialize() )
return false;
if( !Notes.Load( oStream ) )
return false;
return true;
}
public bool Save(TextWriter oStream) {
Notes.Save( oStream );
return true;
}
}
/// <summary>
/// this will be our new call logger and ic-705 communicator.
/// </summary>
public class DocNotes:
IPgParent,
IPgLoad<TextReader>,
IPgSave<TextWriter>,
IPgCiVEvents,
IDisposable
{
readonly IPgScheduler _oScheduler;
readonly IPgRoundRobinWork _oTaskQrz;
readonly IPgRoundRobinWork _oTaskCiv;
readonly IPgRoundRobinWork _oTaskTimer;
protected class MorseDocSlot :
IPgBaseSite,
IPgFileSite
{
protected readonly DocNotes _oHost;
protected readonly string _strName;
public MorseDocSlot( DocNotes oHost, string strName ) {
_oHost = oHost ?? throw new ArgumentNullException();
_strName = strName ?? throw new ArgumentNullException();
}
public IPgParent Host => _oHost;
public void LogError(string strMessage, string strDetails, bool fShow=true) {
_oHost.LogError( strMessage, strDetails, fShow );
}
public void Notify( ShellNotify eEvent ) {
}
FILESTATS IPgFileSite.FileStatus => _oHost._oSiteFile.FileStatus;
Encoding IPgFileSite.FileEncoding => _oHost._oSiteFile.FileEncoding;
string IPgFileSite.FilePath => _oHost._oSiteFile.FilePath;
string IPgFileSite.FileBase => _oHost._oSiteFile.FileBase + " / " + _strName;
}
readonly IPgBaseSite _oSiteBase;
readonly IPgFileSite _oSiteFile;
// Stuff for the morse code pracice view.
public Editor Notes { get; } // practice screen notes. and primary view.
public CallsDoc Calls { get; } // List of callsigns in left hand column of notes file.
public DocProperties Properties { get; }
public Editor CallSign { get; }
public Editor CallSignBio { get; } // Text generated by walking the html stream.
public Editor CallSignAddress { get; } // This is the mailing address displayed.
public Editor CallSignBioHtml { get; } // base 64 converted HTML streaml;
public Editor CallSignPageHtml{ get; } // This is the main page returned by qrz.
SerialPort _oCiV; // Good cantidate for "init once"
readonly ConcurrentQueue<byte[]> _oMsgQueue = new ConcurrentQueue<byte[]>();
readonly Line _oDataGram = new TextLine( 0, string.Empty );
readonly Grammer<char> _oCiVGrammar;
readonly DatagramParser _oParse;
public bool FlagScanCalls { get; set; } = true; // Set up call back to buffer to parse calls.
public bool FlagSComsOn { get; set; } = false; // Turn on the com ports.
Dictionary <int, RepeaterDir> _rgRepeaters = new Dictionary<int, RepeaterDir>();
protected static readonly HttpClient _oHttpClient = new HttpClient();
protected static Type[] CiVErrorList {
get {
Type[] rgErrors = { typeof( UnauthorizedAccessException ),
typeof( IOException ),
typeof( ArgumentOutOfRangeException ),
typeof( ArgumentException ),
typeof( InvalidOperationException ),
typeof( NullReferenceException ),
typeof( FileNotFoundException ) };
return rgErrors;
}
}
/// <summary>
/// Document object for a little Morse Practice document.
/// </summary>
public DocNotes( IPgBaseSite oSiteBase ) {
_oSiteBase = oSiteBase ?? throw new ArgumentNullException();
_oSiteFile = oSiteBase as IPgFileSite ?? throw new ArgumentException( "Host needs the IPgFileSite interface" );
_oScheduler = Services as IPgScheduler ?? throw new ArgumentException("Host requries IPgScheduler");
_oTaskQrz = _oScheduler.CreateWorkPlace() ?? throw new ApplicationException("No worksite for file downloader.");
_oTaskCiv = _oScheduler.CreateWorkPlace() ?? throw new ApplicationException("No worksite for Civ." );
_oTaskTimer = _oScheduler.CreateWorkPlace() ?? throw new ApplicationException("No worksite for Keydown timer." );
try {
_oCiVGrammar = (Grammer<char>)((IPgGrammers)Services).GetGrammer( "civ" );
_oParse = new DatagramParser( new CharStream( _oDataGram ), _oCiVGrammar );
_oParse.ListerAdd( this );
} catch ( Exception oEx ) {
Type[] rgErrors = { typeof( NullReferenceException ),
typeof( InvalidCastException ),
typeof( ArgumentNullException ),
typeof( FileNotFoundException ),
typeof( GrammerNotFoundException ) };
if( !rgErrors.Contains( oEx.GetType() ) )
throw;
LogError( "Morse", "Couldn't get grammar for Ci-V parser. But able to run elsewise." );
}
Notes = new Editor ( new MorseDocSlot( this, "Notes" ) ); // Notes for listening to morse, or log files.
Calls = new CallsDoc ( new MorseDocSlot( this, "Calls" ) ); // document for outline, compiled list of stations
Properties = new DocProperties( new MorseDocSlot( this, "Properties" ) );
CallSign = new Editor ( new MorseDocSlot( this, "CallSign" ) );
CallSignPageHtml = new Editor ( new MorseDocSlot( this, "PageSrc" ) );
CallSignBioHtml = new Editor ( new MorseDocSlot( this, "PageBioSrc" ) );
CallSignBio = new Editor ( new MorseDocSlot( this, "PageBio" ) );
CallSignAddress = new Editor ( new MorseDocSlot( this, "CallAddr" ) );
new ParseBioHTMLSkimmer( this );
new ParseQrzHTMLSkimmer( this );
new ParseHandlerText ( Notes, "text" );
new ParseHandlerText ( Calls, "text" );
new ParseHandlerText ( CallSign, "text" );
new ParseHandlerText ( CallSignBio, "text" );
}
private bool _fDisposed = false;
public void Dispose() {
if( !_fDisposed ) {
_oCiV ?.Close();
_oParse?.Dispose();
Notes .Dispose();
Calls .Dispose();
CallSign .Dispose();
CallSignPageHtml.Dispose();
CallSignBio .Dispose();
CallSignBioHtml .Dispose();
CallSignAddress .Dispose();
_fDisposed = true;
}
}
protected void LogError( string strMessage, string strDetails, bool fShow = false ) {
_oSiteBase.LogError( strMessage, strDetails, fShow );
}
public bool IsDirty => Notes.IsDirty; // || Sources.IsDirty;
public IPgParent Parentage => _oSiteBase.Host;
public IPgParent Services => Parentage.Services;
//public ICollection<ICollection<Line>> Rows { get; } = new List<ICollection<Line>>();
public void CiVError( string strError ) {
LogError( "Ci-V", strError );
}
public struct RepeaterDir {
public RepeaterDir( int iFreq, int iOffs, int iTime, string strInfo = "", string strDesc = "" ) {
_iFrequency = iFreq;
_iOffset = iOffs;
Timeout = iTime;
Info = new TextLine( 0, strInfo );
Description = new TextLine( 0, strDesc );
}
private readonly int _iFrequency;
private readonly int _iOffset;
public int Timeout { get; }
public Line Info { get; }
public Line Description { get; }
public int Output => _iFrequency;
public int Input => _iFrequency + _iOffset;
}
/// <summary>
/// It is interesting that we only get notice of transmission by the change
/// of frequency from listening to TX freq. Only for (duplex) repeaters which is
/// the only time we probably care about timing out the repeater. Convenient!
/// </summary>
/// <param name="iFrequency"></param>
public void CiVFrequencyChange( int iFrequency ) {
if( _rgRepeaters.TryGetValue( iFrequency, out RepeaterDir oRepeater ) ) {
Properties.UpdateValue( 0, "Timer triggered..." );
_oTaskTimer.Queue( ListenForTimout( oRepeater.Timeout ), 0 );
} else {
Properties.UpdateValue( 0, "Stopped." );
_oTaskTimer.Stop(); // stopped talking most likely.
}
}
public void CiVModeChange( string strMode, string strFilter ) {
}
/// <summary>
/// Take the time and subtract 10 seconds because not so precise!! ^_^;;
/// </summary>
/// <param name="iTime">Time in seconds.</param>
/// <returns></returns>
IEnumerator<int> ListenForTimout( int iTime ) {
DateTime oStart = DateTime.Now.AddSeconds( iTime - 10 );
TimeSpan oSpan;
do {
oSpan = oStart.Subtract( DateTime.Now );
if( oSpan.TotalSeconds < 0 )
break;
Properties.UpdateValue( 0, "Ticking..." + Math.Round( oSpan.TotalSeconds ).ToString( "N0" ) );
yield return ( oSpan.TotalSeconds > 10 ) ? 10000 : 1000;
} while( true );
Properties.UpdateValue( 0, "TIMEOUT!" );
}
/// <summary>
/// This is a task we use to monitor the serial port message queue.
/// While I could make a parser that parses over 'byte' for ease of debugging
/// I convert the byte stream to a human readable 'char' stream. Also I can stick
/// the stream into my editor and see colorization, so it's a win / win.
/// </summary>
IEnumerator<int> ListenToCom() {
while( true ) {
if( _oMsgQueue.TryDequeue( out byte[] rgMsg ) ) {
SerialToDatagram( rgMsg );
}
yield return 1000; // Return after 1000 ms.
}
}
/// <summary>
/// this procedure displays the data as one datagram per line. I've
/// written the ci-v grammar to parse this stream. You can use one
/// of the other SerialTo... functions to debug things.
/// </summary>
/// <seealso cref="SerialToList"/>
/// <seealso cref="SerialToOffset"/>
protected void SerialToDatagram( byte[] rgMsg ) {
_oDataGram.Empty();
foreach( byte bByte in rgMsg ) {
string strByte = bByte.ToString( "X2" );
_oDataGram.TryAppend( strByte + " " );
}
_oParse?.Parse();
}
/// <summary>
/// this procedure displays the data as one datagram per line in the
/// Notes editor. Not currently in use.
/// </summary>
protected void SerialToList( byte[] rgMsg ) {
StringBuilder rgBuilder = new StringBuilder();
foreach( byte bByte in rgMsg ) {
string strByte = bByte.ToString( "X2" );
rgBuilder.Append( strByte );
if( bByte == 0xfd ) {
Notes.LineAppend( rgBuilder.ToString() );
rgBuilder.Clear();
} else {
rgBuilder.Append( " " );
}
}
Notes.LineAppend( rgBuilder.ToString() );
rgBuilder.Clear();
}
/// <summary>
/// This procedure identifies each byte by it's offset and shows each
/// on a single line in the Notes editor. Not currently in use.
/// </summary>
protected void SerialToOffset( byte[] rgMsg ) {
int iIndex = 0;
StringBuilder rgBuilder = new StringBuilder();
foreach( byte bByte in rgMsg ) {
rgBuilder.Append( "offset(" );
rgBuilder.Append( iIndex.ToString( "D2" ) );
rgBuilder.Append( ") :" );
rgBuilder.Append( bByte.ToString( "X2" ));
Notes.LineAppend( rgBuilder.ToString() );
rgBuilder.Clear();
if( bByte == 0xfd ) {
iIndex = 0;
Notes.LineAppend( string.Empty );
} else {
iIndex++;
}
}
}
/// <summary>
/// set up the COM port i/o.
/// </summary>
/// <remarks>
/// Turns out we can always set ourselves up to listen to a COM port, BUT
/// we might not be able to open it, or we don't want to open it right away.
/// </remarks>
protected void InitSerial() {
// This is crude since we're polling even if no messages.
// we'll fix this up later.
_oTaskCiv.Queue( ListenToCom(), 100 );
try {
_oCiV = new SerialPort("COM4");
_oCiV.BaudRate = 9600;
_oCiV.Parity = Parity.None;
_oCiV.StopBits = StopBits.One;
_oCiV.DataBits = 8;
_oCiV.Handshake = Handshake.None;
_oCiV.DataReceived += CiV_DataReceived;
} catch( IOException ) {
LogError( "Morse COM access", "Unable to open COM port. Timer disabled" );
Properties.UpdateValue( 1, "Failed Alloc" );
return;
}
try {
if( FlagSComsOn ) {
_oCiV.Open();
Properties.UpdateValue( 1, "On" );
} else {
Properties.UpdateValue( 1, "Off" );
}
} catch( Exception oEx ) {
if( CiVErrorList.IsUnhandled( oEx ) )
throw;
_oCiV.Dispose();
LogError( "Morse COM access", "Unable to open COM port. Timer disabled" );
Properties.UpdateValue( 1, "Failed Open" );
}
}
protected void InitRepeaters() {
List< RepeaterDir > rgTemp = new List<RepeaterDir>();
rgTemp.Add( new RepeaterDir( 146960000, -600000, 180 ));
rgTemp.Add( new RepeaterDir( 53170000, -1700000, 120 ));
rgTemp.Add( new RepeaterDir( 146820000, -600000, 120 ));
rgTemp.Add( new RepeaterDir( 52870000, -1700000, 180 ));
foreach( RepeaterDir oItem in rgTemp ) {
_rgRepeaters.Add( oItem.Input, oItem );
}
}
/// <summary>
/// Properties are initialized first... BUT the values and where they
/// come from might not be initialized yet. So those will have to come
/// come later.
/// </summary>
protected void InitProperties() {
Properties.AddLabel( "Timer" );
Properties.AddLabel( "Timer Enable" );
}
/// <summary>
/// This event comes in asynchronously on it's own thread. Queue up the message
/// so that the foreground tread can pick it up.
/// </summary>
/// <remarks>Would be nice if we could post a message to the forground so that
/// it spins up a task that can terminate once all the data has been pulled.</remarks>
private void CiV_DataReceived( object sender, SerialDataReceivedEventArgs e ) {
int iBytesWaiting = _oCiV.BytesToRead;
if( iBytesWaiting > 0 ) {
byte[] rgMsg = new byte[iBytesWaiting];
_oCiV?.Read( rgMsg, 0, iBytesWaiting );
_oMsgQueue.Enqueue( rgMsg );
}
}
/// <summary>
/// Both InitNew and Load call this base initialization function.
/// </summary>
protected bool Initialize() {
if( !Notes.InitNew() )
return false;
if( !Calls.InitNew() )
return false;
if( !Properties.InitNew() )
return false;
// Note: Change this to trigger after a notes parse.
//_oTaskSched.Queue( EnumCallsScanTask(), 3000 );
if( FlagScanCalls ) {
Notes.BufferEvent += Notes_BufferEvent;
}
if( !CallSign.InitNew())
return false;
if( !CallSignPageHtml.InitNew() )
return false;
if( !CallSignBioHtml.InitNew())
return false;
if( !CallSignBio.InitNew() )
return false;
if( !CallSignAddress.InitNew() )
return false;
InitProperties();
InitSerial ();
InitRepeaters ();
//for( int i=0; i<3; ++i ) {
// List<Line> rgRow = new List<Line>(7);
// for( int j=0; j<7; ++j ) {
// rgRow.Add( new TextLine( j, j.ToString() ) );
// }
// Rows.Add( rgRow );
//}
return true;
}
private void Notes_BufferEvent( BUFFEREVENTS eEvent ) {
if( eEvent == BUFFEREVENTS.FORMATTED ) {
ScanCallsigns();
}
}
public bool InitNew() {
if( !Initialize() )
return false;
return true;
}
public bool Load(TextReader oStream) {
if( !Initialize() )
return false;
if( !Notes.Load( oStream ) )
return false;
return true;
}
public bool Save(TextWriter oStream) {
Notes.Save( oStream );
return true;
}
/// <summary>
/// Scan the entire file for callsigns and pop them into the "Calls" editor.
/// </summary>
public void ScanCallsigns() {
Calls.Clear();
List< string > rgCallSigns = new List<string>();
try {
foreach( Line oLine in Notes ) {
// Note: When Formatting goes type IPgWordRange I can remove the filter.
foreach( IColorRange oColor in oLine.Formatting ) {
if( oColor is IPgWordRange oWord &&
string.Compare( oWord.StateName, "callsign" ) == 0 && oWord.Offset == 0 )
{
rgCallSigns.Add( oLine.SubString( oWord.Offset, oWord.Length ) );
}
}
}
IEnumerable<IGrouping<string, string>> dupes = rgCallSigns.GroupBy(x => x.ToLower() ).OrderBy( y => y.Key.ToLower() );
foreach( IGrouping<string, string> foo in dupes ) {
Calls.LineAppend( foo.Key + " : " + foo.Count().ToString() );
}
Calls.LineInsert( "Operator Count : " + dupes.Count().ToString() );
} catch( Exception oEx ) {
Type[] rgErrors = { typeof( NullReferenceException ),
typeof( ArgumentNullException ),
typeof( InvalidCastException ) };
if( rgErrors.IsUnhandled( oEx ) )
throw;
LogError( "Serious Error", "ScanCallsigns suffered an error." );
}
}
public IEnumerator<int> EnumCallsScanTask() {
int iTimeInMs = 1000 * 60;
while( true ) {
ScanCallsigns();
yield return iTimeInMs;
}
}
public static void test() {
//var builder = new MySqlConnectionStringBuilder
//{
// Server = "YOUR-SERVER.mysql.database.azure.com",
// Database = "YOUR-DATABASE",
// UserID = "USER@YOUR-SERVER",
// Password = "PASSWORD",
// SslMode = MySqlSslMode.Required,
//};
//using (var conn = new MySqlConnection(builder.ConnectionString))
//{
// Console.WriteLine("Opening connection");
// await conn.OpenAsync();
// using (var command = conn.CreateCommand())
// {
// command.CommandText = "SELECT * FROM inventory;";
// using (var reader = await command.ExecuteReaderAsync())
// {
// while (await reader.ReadAsync())
// {
// Console.WriteLine(string.Format(
// "Reading from table=({0}, {1}, {2})",
// reader.GetInt32(0),
// reader.GetString(1),
// reader.GetInt32(2)));
// }
// }
// }
// Console.WriteLine("Closing connection");
//}
//Console.WriteLine("Press RETURN to exit");
//Console.ReadLine();
}
static readonly HttpClient client = new HttpClient();
/// <summary>
/// This is all obsolete and should use the async functions.
/// </summary>
public IEnumerator<int> EnumWatchTask(Task<HttpResponseMessage> oTask)
{
Task<Stream> oTask3 = null;
while (oTask != null) {
switch (oTask.Status) {
case TaskStatus.RanToCompletion:
oTask3 = oTask.Result.Content.ReadAsStreamAsync();
oTask.Dispose();
oTask = null;
break;
case TaskStatus.Faulted:
case TaskStatus.Canceled:
CallSignBioHtml.Clear();
CallSignBioHtml.LineAppend("Task Faulted: " + oTask.Exception.ToString());
oTask.Dispose();
yield break;
default:
yield return 250;
break;
}
}
Stream oStream = null;
while (oTask3 != null) {
switch (oTask3.Status) {
case TaskStatus.RanToCompletion:
oStream = oTask3.Result;
oTask3.Dispose();
oTask3 = null;
break;
case TaskStatus.Faulted:
case TaskStatus.Canceled:
oTask3.Dispose();
yield break;
default:
yield return 250;
break;
}
}
bool fLoaded = false;
try {
using (StreamReader oReadQrz = new StreamReader(oStream, Encoding.UTF8, true, 1024, true )) {
// Just look for the first base64.decode and that should be it.
// Kind of gross to be burning strings. But it'll do in a pinch.
// Note: Looks like we assume the base64 blob is all on one line!
while (oReadQrz.Peek() >= 0) {
string strLine = oReadQrz.ReadLine();
string strSrch = "Base64.decode(\"";
int iStart = strLine.LastIndexOf(strSrch);
int iEnd = strLine.LastIndexOf("\"");
if (iStart > 0) {
byte[] rgBiography = null;
iStart += strSrch.Length;
if (iEnd > iStart + 20) { // Just an arbitrary extra.
try {
rgBiography = Convert.FromBase64String(strLine.Substring(iStart, iEnd - iStart));
MemoryStream stmBio = new MemoryStream(rgBiography);
using (StreamReader oFoo = new StreamReader(stmBio)) {
fLoaded = CallSignBioHtml.Load(oFoo);
}
} catch (Exception oEx) {
Type[] rgErrors = { typeof( ArgumentNullException ),
typeof( FormatException ) };
if( rgErrors.IsUnhandled( oEx ))
throw;
// FromBase64String actually throws more exception types than documented.
}
}
break;
}
}
}
// On this second pass we load the stream into the page html editor. We could
// probably do this first, THEN pull the base64 blob if we deal with the parse better.
// The first scan is mostly hacky. ^_^;;
oStream.Seek( 0, SeekOrigin.Begin );
using (StreamReader oReadQrz = new StreamReader(oStream)) {
CallSignPageHtml.Load( oReadQrz );
}
} catch( Exception oEx ) {
Type[] rgErrors = { typeof( ArgumentException ),
typeof( NullReferenceException ),
typeof( ArgumentNullException ),
typeof( ObjectDisposedException ) };
if( rgErrors.IsUnhandled( oEx ))
throw;
oStream.Close();
}
if (!fLoaded) {
PageReset();
CallSignBio.Load("No Biography");
}
}
protected void PageReset() {
CallSignPageHtml.Clear();
CallSignBioHtml .Clear();
CallSignBio .Clear();
CallSignAddress .Clear();
}
/// <summary>
/// New code to read the qrz station page. Not finished yet.
/// </summary>
public bool StationRead( Stream oStream ) {
try {
Editor oTempEdit = new Editor( null );
using( TextReader oReader = new StreamReader( oStream )) {
if( !oTempEdit.Load( oReader ) ) {
_oSiteBase.LogError( "Editor Load", "Could not read stream.", true );
return false;
}
};
} catch( Exception oEx ) {
Type[] rgErrors = { typeof( NullReferenceException),
typeof( ArgumentException ),
typeof( ArgumentNullException ) };
if( rgErrors.IsUnhandled( oEx ))
throw;
}
return true;
}
public void StationLoad( string strCallsign ) {
CallSign.Clear();
CallSign.Load( strCallsign );
StationLoad();
}
public void StationLoad() {
string strURL = "https://www.qrz.com/lookup";
string strMediaType = "application/x-www-form-urlencoded";
ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
StringBuilder oQuery = new StringBuilder();
oQuery.Append("tquery=");
oQuery.Append(CallSign[0]);
oQuery.Append("&mode=callsign");
if (_oTaskQrz.Status == WorkerStatus.FREE) {
try {
StringContent oContent = new StringContent(oQuery.ToString(), System.Text.Encoding.UTF8, strMediaType);
Task<HttpResponseMessage> oTask = client.PostAsync(strURL, oContent);
_oTaskQrz.Queue(EnumWatchTask(oTask), 100);
} catch (HttpRequestException) {
PageReset();
CallSignBio.LineAppend("Unable to process request");
return;
}
}
}
/// <summary>
/// A way to sink SQL events. Unfinished.
/// </summary>
/// <param name="oEvent"></param>
public void TableListenerAdd(IPgTableEvents oEvent) {
}
public void TableListenerRemove(IPgTableEvents oEvent) {
}
public bool Execute( Guid guidCmnd ) {
if( guidCmnd == GlobalCommands.Play ) {
try {
if( !_oCiV.IsOpen ) {
_oCiV.Open();
Properties.UpdateValue( 1, "On" );
} else {
Properties.UpdateValue( 1, "Already Opened" );
}
} catch( Exception oEx ) {
if( CiVErrorList.IsUnhandled( oEx ) )
throw;
Properties.UpdateValue( 1, "Open Error" );
}
return true; // Handled, even if ended in error.
}
if( guidCmnd == GlobalCommands.Stop ) {
try {
if( _oCiV.IsOpen ) {
_oCiV.Close();
Properties.UpdateValue( 1, "Off" );
} else {
Properties.UpdateValue( 1, "Already Closed" );
}
} catch( Exception oEx ) {
if( CiVErrorList.IsUnhandled( oEx ) )
throw;
Properties.UpdateValue( 1, "Close Error" );
}
return true; // Handled, even if ended in error.
}
return false;
}
}
public class DocProperties : IPgParent, IPgLoad {
protected readonly IPgBaseSite _oSiteBase;
public IPgParent Parentage => _oSiteBase.Host;
public IPgParent Services => Parentage.Services;
public void LogError( string strMessage ) { _oSiteBase.LogError( "Property Page Client", strMessage ); }
public Editor Property_Labels { get; }
public Editor Property_Values { get; }
protected class DocSlot :
IPgBaseSite
{
protected readonly DocProperties _oHost;
public DocSlot( DocProperties oHost ) {
_oHost = oHost ?? throw new ArgumentNullException( "Host" );
}
public IPgParent Host => _oHost;
public void LogError(string strMessage, string strDetails, bool fShow=true) {
_oHost.LogError( strDetails );
}
public void Notify( ShellNotify eEvent ) {
// Might want this value when we close to save the current playing list!!
}
}
public DocProperties( IPgBaseSite oSiteBase ) {
_oSiteBase = oSiteBase ?? throw new ArgumentNullException( "Site must not be null." );
Property_Labels = new Editor( new DocSlot( this ) );
Property_Values = new Editor( new DocSlot( this ) );
}
public bool InitNew() {
if( !Property_Labels.InitNew() )
return false;
if( !Property_Values.InitNew() )
return false;
return true;