-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSourcesForm.pas
More file actions
997 lines (835 loc) · 35.3 KB
/
Copy pathSourcesForm.pas
File metadata and controls
997 lines (835 loc) · 35.3 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
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
unit SourcesForm;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Normal, Vcl.StdCtrls, Vcl.ExtCtrls, BaseTypes,
AdvUtil, Vcl.Grids, AdvObj, BaseGrid, AdvGrid, DBAdvGrid, AdvSmoothStatusIndicator, AdvGDIP,
Data.FMTBcd, Data.DB, FireDAC.Stan.Intf, FireDAC.Stan.Option, Math,
FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf,
FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt, FireDAC.Comp.DataSet,
FireDAC.Comp.Client, AdvSmoothButton, AdvPanel, Miscellaneous, Vcl.Menus,
Source, SourceForm, SocketSource, UDPSource, APRSSource,
GatewaySource, SerialSource, MQTTSource, WSMQTTSource,
TCPSettings, UDPSettings, APRSSettings, MQTTSettings, WSMQTTSettings, UploadStatus,
Sondehub, MQTTUplink, SSDV, SondehubSource;
type
THABSource = record
InUse: Boolean;
SourceID: Integer;
Group: String;
SourceEnabled: Boolean;
Started: Boolean;
Connected: Boolean;
Code: String;
Description: String;
SourceType: TSourceType;
SourceForm: TfrmSource;
Source: TSource;
Upload: Boolean;
Status: String;
LastPacketAt: TDateTime;
FreqError: Array[0..1] of Double;
MultipleChannels: Boolean;
LatestPosition: THABPosition;
HaveReceivedSSDV: Boolean;
SSDVCount: Integer;
end;
type
TUploader = record
Enabled: Boolean;
Attempted: Boolean;
Success: Boolean;
LastSuccess: TDateTime;
Indicator: TAdvSmoothStatusIndicator;
StatusForm: TfrmUploadStatus;
end;
type
TfrmSources = class(TfrmNormal)
scrollMain: TScrollBox;
btnAddSource: TAdvSmoothButton;
tmrSSDV: TTimer;
pnlUploads: TPanel;
Panel3: TPanel;
Label1: TLabel;
indSondehub: TAdvSmoothStatusIndicator;
indMQTT: TAdvSmoothStatusIndicator;
indSSDV: TAdvSmoothStatusIndicator;
indHABLINK: TAdvSmoothStatusIndicator;
procedure btnAddSourceClick(Sender: TObject);
procedure tmrSSDVTimer(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure pnlUploadsResize(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure UploaderClick(Sender: TObject);
private
{ Private declarations }
HABSources: Array[1..32] of THABSource;
Uploaders: Array[1..4] of TUploader;
SondehubUploader: TSondehubThread;
SSDVUploader: TSSDVThread;
MQTTUploader, HabLinkUploader: TMQTTThread;
function MaximumPayloadDistance: Double;
procedure ShowSourceStatus(SourceIndex: Integer);
procedure LoadSource(SourceIndex: Integer);
procedure ReloadSourceSettings(SourceIndex: Integer);
procedure LoadSourceSettings(SourceIndex: Integer);
procedure DataSourceClick(Sender: TObject);
function ShowSettingsForm(SourceIndex: Integer): Boolean;
function FindFreeSource: Integer;
procedure SondehubStatusCallback(SourceID: Integer; Active, OK: Boolean; Status: String);
procedure MQTTStatusCallback(SourceID: Integer; Active, OK: Boolean; Status: String);
procedure HabLinkStatusCallback(SourceID: Integer; Active, OK: Boolean; Status: String);
procedure SSDVStatusCallback(SourceID: Integer; Active, OK: Boolean; Status: String);
function NewPosition(SourceIndex: Integer; Position: THABPosition): Boolean;
procedure AddStatusToLog(SourceIndex: Integer);
function APRSFilter: String;
procedure CloseThread(Thread: TThread);
procedure WaitForThread(Thread: TThread);
procedure ShowUplinkStatus(UplinkIndex: Integer);
procedure ShowAllUplinkStatus;
public
{ Public declarations }
procedure LoadSources;
procedure HABCallback(SourceIndex: Integer; Connected: Boolean; Line: String; Position: THABPosition);
function StorePosition(SourceIndex: Integer; Connected: Boolean; Line: String; Position: THABPosition): Boolean;
procedure AddNewSource;
procedure ModifySource(SourceIndex: Integer);
procedure DeleteSource(SourceIndex: Integer);
function SourceIsEnabled(SourceIndex: Integer): Boolean;
procedure EnableSource(SourceIndex: Integer; EnableSource: Boolean);
procedure SendUplink(SourceIndex: Integer; When: TUplinkWhen; WhenValue, Channel: Integer; Prefix, Msg, Password: String);
procedure UpdateSourceFilters;
procedure ApplySettings;
end;
const
SONDEHUB_UPLOADER = 1;
MQTT_UPLOADER = 2;
SSDV_UPLOADER = 3;
HABLINK_UPLOADER = 4;
var
frmSources: TfrmSources;
implementation
{$R *.dfm}
uses Data, Main, Map, SettingsForm, NewSource, Payloads, Misc,
GatewaySettings, LoRaSerialSettings, HabitatSettings, LogtailSettings,
LoRaSerialSource, LoRaGatewaySource;
procedure TfrmSources.LoadSources;
var
SourceIndex: Integer;
Dummy: String;
begin
InitialiseSettings;
// Sondehub uploader
SondehubUploader := TSondehubThread.Create(SondehubStatusCallback);
SondehubUploader.SetListener(HAB_BASE, HAB_BASE_VERSION, DataModule1.tblSettings.FieldByName('Callsign').AsString);
SondehubUploader.SetListenerPosition(DataModule1.tblSettings.FieldByName('Latitude').AsFloat,
DataModule1.tblSettings.FieldByName('Longitude').AsFloat,
DataModule1.tblSettings.FieldByName('Altitude').AsFloat);
Uploaders[SONDEHUB_UPLOADER].StatusForm.AddStatusToLog('Set listener as ' + HAB_BASE + ' / ' + DataModule1.tblSettings.FieldByName('Callsign').AsString);
Uploaders[SONDEHUB_UPLOADER].StatusForm.AddStatusToLog('Set listener position as ' + DataModule1.tblSettings.FieldByName('Latitude').AsString + ',' +
DataModule1.tblSettings.FieldByName('Longitude').AsString + ',' +
DataModule1.tblSettings.FieldByName('Altitude').AsString);
// MQTT Uploader
MQTTUploader := TMQTTThread.Create(MQTTStatusCallback);
// HabLink uploader
HabLinkUploader := TMQTTThread.Create(HabLinkStatusCallback);
// SSDV Uploader
SSDVUploader := TSSDVThread.Create(SSDVStatusCallback);
ApplySettings;
with DataModule1.tblSources do begin
First;
SourceIndex := 0;
while not EOF do begin
Inc(SourceIndex);
LoadSource(SourceIndex);
Next;
end;
end;
end;
function GetSettingName(var Settings: String): String;
begin
Result := GetString(Settings, '=');
end;
function GetSettingValue(var Settings: String): String;
begin
Result := GetString(Settings, ';');
end;
procedure TfrmSources.LoadSource(SourceIndex: Integer);
begin
with HABSources[SourceIndex], DataModule1.tblSources do begin
InUse := True;
SourceID := FieldByName('ID').AsInteger;
Group := SourceID.ToString;
SourceType := TSourceType(FieldByName('Type').AsInteger);
if SourceType = stGateway then begin
SourceForm := TfrmLoRaGatewaySource.Create(Self);
Source := TGatewaySource.Create(SourceIndex, Group, HABCallback);
MultipleChannels := True;
end else if SourceType = stSerial then begin
SourceForm := TfrmLoRaSerialSource.Create(Self);
Source := TSerialSource.Create(SourceIndex, Group, HABCallback);
end else if SourceType = stTCP then begin
SourceForm := TfrmSource.Create(Self);
Source := TSocketSource.Create(SourceIndex, Group, HABCallback);
end else if SourceType = stUDP then begin
SourceForm := TfrmSource.Create(Self);
Source := TUDPSource.Create(SourceIndex, Group, HABCallback);
end else if SourceType = stAPRS then begin
SourceForm := TfrmSource.Create(Self);
Source := TAPRSSource.Create(SourceIndex, Group, HABCallback);
Source.SetFilter(APRSFilter);
end else if SourceType = stMQTT then begin
SourceForm := TfrmSource.Create(Self);
Source := TMQTTSource.Create(SourceIndex, Group, HABCallback);
end else if SourceType = stWSMQTT then begin
SourceForm := TfrmSource.Create(Self);
Source := TWSMQTTSource.Create(SourceIndex, Group, HABCallback);
end else if SourceType = stSondehub then begin
SourceForm := TfrmSource.Create(Self);
Source := TSondehubSource.Create(SourceIndex, Group, HABCallback);
TSondehubSource(Source).Latitude := DataModule1.tblSettings.FieldByName('Latitude').AsFloat;
TSondehubSource(Source).Longitude := DataModule1.tblSettings.FieldByName('Longitude').AsFloat;
TSondehubSource(Source).Radius := MaximumPayloadDistance;
end else begin
SourceForm := nil;
end;
if SourceForm <> nil then begin
btnAddSource.Visible := False;
SourceForm.SourceIndex := SourceIndex;
SourceForm.Group := Group;
SourceForm.Code := FieldByName('Code').AsString;
LoadSourceSettings(SourceIndex);
end;
end;
end;
procedure TfrmSources.LoadSourceSettings(SourceIndex: Integer);
var
Settings, Setting, Value: String;
begin
with HABSources[SourceIndex], DataModule1.tblSources do begin
Code := FieldByName('Code').AsString;
Description := FieldByName('Name').AsString;
Settings := FieldByName('Settings').AsString;
Upload := GetBooleanSetting('Upload', Settings);
SourceEnabled := FieldByName('Enabled').AsBoolean;
// Add to source settings
SetSettingBoolean(Group, 'Enabled', FieldByName('Enabled').AsBoolean);
SetSettingString(Group, 'Host', FieldByName('Host').AsString);
SetSettingString(Group, 'Port', FieldByName('Port').AsString);
SetSettingString(Group, 'Topic', FieldByName('Topic').AsString);
SetSettingString(Group, 'UserName', FieldByName('UserName').AsString);
SetSettingString(Group, 'Password', FieldByName('Password').AsString);
while Settings <> '' do begin
Setting := GetSettingName(Settings);
Value := GetSettingValue(Settings);
if (Setting <> '') and (Value <> '') then begin
SetSettingString(Group, Setting, Value);
end;
if SourceForm <> nil then begin
SourceForm.ShowSetting(Setting, Value);
end;
end;
if SourceForm <> nil then begin
SourceForm.Enabled := FieldByName('Enabled').AsBoolean;
end;
// Indicator.Caption := Code;
if SourceForm <> nil then begin
SourceForm.lblTitle.Caption := Code + ': ' + Description;
SourceForm.pnlMain.Parent := scrollMain;
end;
ShowSourceStatus(SourceIndex);
end;
end;
procedure TfrmSources.ReloadSourceSettings(SourceIndex: Integer);
begin
with HABSources[SourceIndex] do begin
with DataModule1.tblSources do begin
if FindKey([SourceID]) then begin
LoadSourceSettings(SourceIndex);
end;
end;
end;
end;
function TfrmSources.NewPosition(SourceIndex: Integer; Position: THABPosition): Boolean;
var
Callsign: String;
PositionIsNew: Boolean;
begin
// HABSources[SourceIndex].LatestPosition := Position;
// Got a packet
HABSources[SourceIndex].LastPacketAt := Now;
// From a remote source?
Position.ReceivedRemotely := HABSources[SourceIndex].SourceType in [stLogtail, stHabitat, stAPRS, stMQTT, stWSMQTT, stSondehub];
// Add date if necessary
if Trunc(Position.TimeStamp) < (Trunc(Now)-1) then begin
if (Frac(Now) < 60/86400) and (Frac(Position.TimeStamp) > (1-120/86400)) then begin
// It's now the start of the day, and payload seems to be from yesterday, so use yesterday's date
Position.TimeStamp := Position.TimeStamp + Trunc(Now) - 1;
end else begin
Position.TimeStamp := Position.TimeStamp + Trunc(Now);
end;
end;
if (not DataModule1.tblSettings.FieldByName('Latitude').IsNull) and (not DataModule1.tblSettings.FieldByName('Longitude').IsNull) then begin
// Calculate distance
Position.Distance := CalculateDistance(Position.Latitude, Position.Longitude,
DataModule1.tblSettings.FieldByName('Latitude').Asfloat,
DataModule1.tblSettings.FieldByName('Longitude').AsFloat) / 1000.0;
// Calculate direction
Position.Direction := CalculateDirection(Position.Latitude, Position.Longitude,
DataModule1.tblSettings.FieldByName('Latitude').Asfloat,
DataModule1.tblSettings.FieldByName('Longitude').AsFloat);
// Calculate elevation
Position.Elevation := CalculateElevation(DataModule1.tblSettings.FieldByName('Latitude').Asfloat,
DataModule1.tblSettings.FieldByName('Longitude').AsFloat,
DataModule1.tblSettings.FieldByName('Altitude').AsFloat,
Position.Latitude, Position.Longitude, Position.Altitude);
end;
// Add to our payload list
PositionIsNew := frmPayloads.NewPosition(Position, HABSources[SourceIndex].Code, HABSources[SourceIndex].Description);
// Upload telemtry, if enabled for this source
if HABSources[SourceIndex].Upload then begin
// Now check/upload to each target in turn
if DataModule1.tblSettings.FieldByName('UplinkSondehub').AsBoolean then begin
SondehubUploader.SaveTelemetryToSondehub(SourceIndex, Position);
end;
if DataModule1.tblSettings.FieldByName('UplinkMQTT').AsBoolean then begin
MQTTUploader.SendTelemetry(Position.PayloadID, DataModule1.tblSettings.FieldByName('Callsign').AsString, Position.Line);
end;
if DataModule1.tblSettings.FieldByName('UplinkHABLINK').AsBoolean then begin
HablinkUploader.SendTelemetry(Position.PayloadID, DataModule1.tblSettings.FieldByName('Callsign').AsString, Position.Line);
end;
end;
ShowSourceStatus(SourceIndex);
// Add to source history
if PositionIsNew or not Position.ReceivedRemotely then begin
HABSources[SourceIndex].SourceForm.AddPosition(Position);
end;
Result := PositionIsNew;
end;
procedure TfrmSources.pnlUploadsResize(Sender: TObject);
var
i, ButtonWidth: Integer;
begin
ButtonWidth := pnlUploads.ClientWidth div (High(Uploaders) + 1 - Low(Uploaders)) - indSSDV.Margins.Left - indSSDV.Margins.Right;
for i := Low(Uploaders) to High(Uploaders) do begin
Uploaders[i].Indicator.Width := ButtonWidth;
end;
end;
procedure TfrmSources.ShowSourceStatus(SourceIndex: Integer);
begin
with HABSources[SourceIndex] do begin
// Colours
if SourceEnabled then begin
if Connected then begin
if (Now - LastPacketAt) < 60/86400 then begin
SourceForm.pnlTitle.Color := clLime;
SourceForm.pnlTitle.Font.Color := clBlack;
end else begin
SourceForm.pnlTitle.Color := clGreen;
SourceForm.pnlTitle.Font.Color := clWhite;
end;
end else if Started then begin
SourceForm.pnlTitle.Color := clRed;
SourceForm.pnlTitle.Font.Color := clWhite;
end else begin
SourceForm.pnlTitle.Color := clSilver;
SourceForm.pnlTitle.Font.Color := clBlack;
end;
end else begin
SourceForm.pnlTitle.Color := clGray;
SourceForm.pnlTitle.Font.Color := clWhite;
end;
end;
end;
procedure TfrmSources.AddStatusToLog(SourceIndex: Integer);
begin
if HABSources[SourceIndex].SourceForm <> nil then begin
HABSources[SourceIndex].SourceForm.AddStatusToLog(HABSources[SourceIndex].Status);
end;
end;
procedure TfrmSources.btnAddSourceClick(Sender: TObject);
begin
AddNewSource;
end;
function TfrmSources.FindFreeSource: Integer;
var
i: Integer;
begin
for i := Low(HABSources) to High(HABSources) do begin
if not HABSources[i].InUse then begin
Result := i;
Exit;
end;
end;
Result := 0;
end;
procedure TfrmSources.CloseThread(Thread: TThread);
begin
if Thread <> nil then begin
Thread.Terminate;
end;
end;
procedure TfrmSources.WaitForThread(Thread: TThread);
begin
if Thread <> nil then begin
Thread.WaitFor;
end;
end;
procedure TfrmSources.FormCreate(Sender: TObject);
var
i: Integer;
begin
inherited;
Uploaders[SONDEHUB_UPLOADER].Indicator := indSondehub;
Uploaders[MQTT_UPLOADER].Indicator := indMQTT;
Uploaders[SSDV_UPLOADER].Indicator := indSSDV;
Uploaders[HABLINK_UPLOADER].Indicator := indHablink;
for i := Low(Uploaders) to High(Uploaders) do begin
Uploaders[i].Indicator.Tag := i;
Uploaders[i].StatusForm := TfrmUploadStatus.Create(Self);
Uploaders[i].StatusForm.Section := Uploaders[i].Indicator.Caption;
Uploaders[i].StatusForm.Caption := Uploaders[i].StatusForm.Caption + ' - ' + Uploaders[i].Indicator.Caption;
end;
end;
procedure TfrmSources.FormDestroy(Sender: TObject);
var
Index: Integer;
begin
// Close and wait for threads
for Index := Low(HABSources) to High(HABSources) do begin
CloseThread(HABSources[Index].Source);
end;
CloseThread(SondehubUploader);
CloseThread(SSDVUploader);
CloseThread(HabLinkUploader);
CloseThread(MQTTUploader);
WaitForThread(SondehubUploader);
WaitForThread(SSDVUploader);
WaitForThread(HabLinkUploader);
WaitForThread(MQTTUploader);
for Index := Low(HABSources) to High(HABSources) do begin
WaitForThread(HABSources[Index].Source);
end;
for Index := Low(HABSources) to High(HABSources) do begin
if HABSources[Index].Source <> nil then begin
HABSources[Index].Source.Free;
end;
end;
SondehubUploader.Free;
MQTTUploader.Free;
HabLinkUploader.Free;
SSDVUploader.Free;
CloseSettings;
end;
function FixSourceIndex(Index: Integer): Integer;
begin
if Index >= 4 then begin
Result := Index + 2;
end else begin
Result := Index + 1;
end;
end;
procedure TfrmSources.AddNewSource;
var
SourceIndex, SourceTypeIndex: Integer;
frmNewSource: TfrmNewSource;
begin
SourceIndex := FindFreeSource;
if SourceIndex > 0 then begin
// We can add a new source
frmNewSource := TfrmNewSource.Create(Self);
if frmNewSource.ShowModal = mrOK then begin
// Type of source
SourceTypeIndex := FixSourceIndex(frmNewSource.ComboBox1.ItemIndex);
HABSources[SourceIndex].SourceType := TSourceType(SourceTypeIndex);
// Add to table
with DataModule1.tblSources do begin
Append;
FieldByName('Enabled').AsBoolean := True;
FieldByName('Type').AsInteger := SourceTypeIndex;
Post;
// Last;
HABSources[SourceIndex].SourceID := FieldByName('ID').AsInteger;
// Fill in settings
if ShowSettingsForm(SourceIndex) then begin
// Load source
LoadSource(SourceIndex);
end else begin
// Delete from table
Delete;
end;
SaveToFile(ExtractFilePath(Application.ExeName) + 'sources.json');
end;
end;
frmNewSource.Free;
end;
end;
procedure TfrmSources.HABCallback(SourceIndex: Integer; Connected: Boolean; Line: String; Position: THABPosition);
begin
StorePosition(SourceIndex, Connected, Line, Position);
end;
function TfrmSources.StorePosition(SourceIndex: Integer; Connected: Boolean; Line: String; Position: THABPosition): Boolean;
var
Status, Callsign: String;
MyBookmark: TBookmark;
begin
Result := False;
// Mark connected
if Connected <> HABSources[SourceIndex].Connected then begin
HABSources[SourceIndex].Connected := Connected;
ShowSourceStatus(SourceIndex);
end else if not HABSources[SourceIndex].Started then begin
HABSources[SourceIndex].Started := True;
ShowSourceStatus(SourceIndex);
end;
// New Position
if Position.InUse then begin
if Position.PayloadID <> '' then begin
Result := NewPosition(SourceIndex, Position);
end;
if HABSources[SourceIndex].MultipleChannels then begin
Status := IntToStr(Position.Channel) + ': ' + Position.Line;
end else begin
Status := Position.Line;
end;
HABSources[SourceIndex].LatestPosition := Position;
if HABSources[SourceIndex].SourceForm <> nil then begin
HABSources[SourceIndex].SourceForm.DoAFC(Position.Channel, HABSources[SourceIndex].FreqError[Position.Channel]);
end;
end else if Position.CallingModeMessage then begin
Status := 'Calling Mode: ' + Position.PayloadID + ',' + FormatFloat('0.000', Position.CallingModeFrequency) + 'MHz';
end else if Line <> '' then begin
Status := Line;
end;
// Update status
if (Status <> '') and (Status <> HABSources[SourceIndex].Status) then begin
HABSources[SourceIndex].Status := Status;
AddStatusToLog(SourceIndex);
with DataModule1.tblSources do begin
MyBookmark := GetBookmark;
if FindKey([HABSources[SourceIndex].SourceID]) then begin
if Status <> FieldByName('Status').AsString then begin
Edit;
FieldByName('Status').AsString := Status;
Post;
end;
GotoBookmark(MyBookmark);
end;
end;
end;
// SSDV Packet
if Position.IsSSDV then begin
if HABSources[SourceIndex].Upload then begin
if DataModule1.tblSettings.FieldByName('UplinkSSDV').AsBoolean then begin
Callsign := DataModule1.tblSettings.FieldByName('Callsign').AsString;
if Callsign <> '' then begin
SSDVUploader.SaveSSDVToHabitat(Position.Line, Callsign);
end;
end;
end;
if not HABSources[SourceIndex].HaveReceivedSSDV then begin
if HABSources[SourceIndex].SourceForm <> nil then begin
HABSources[SourceIndex].SourceForm.AddStatusToLog('Receiving SSDV');
end;
HABSources[SourceIndex].HaveReceivedSSDV := True;
end;
Inc(HABSources[SourceIndex].SSDVCount);
end;
if Position.HasCurrentRSSI then begin
HABSources[SourceIndex].SourceForm.ShowCurrentRSSI(Position.Channel, Position.CurrentRSSI);
if HABSources[SourceIndex].LatestPosition.InUse then begin
// We have a position so we know what payload we last received
frmPayloads.ShowCurrentRSSI(HABSources[SourceIndex].LatestPosition.PayloadID, Position.CurrentRSSI);
end;
end;
if Position.HasSNR then begin
HABSources[SourceIndex].SourceForm.ShowSNR(Position.Channel, Position.SNR);
if HABSources[SourceIndex].LatestPosition.InUse then begin
// We have a position so we know what payload we last received
frmPayloads.ShowSNR(HABSources[SourceIndex].LatestPosition.PayloadID, Position.SNR);
end;
end;
if Position.HasPacketRSSI then begin
HABSources[SourceIndex].SourceForm.ShowPacketRSSI(Position.Channel, Position.PacketRSSI);
if HABSources[SourceIndex].LatestPosition.InUse then begin
// We have a position so we know what payload we last received
frmPayloads.ShowPacketRSSI(HABSources[SourceIndex].LatestPosition.PayloadID, Position.PacketRSSI);
end;
end;
if Position.HasFrequency then begin
HABSources[SourceIndex].FreqError[Position.Channel] := Position.FrequencyError / 1000.0;
HABSources[SourceIndex].SourceForm.ShowFrequencyError(Position.Channel, Position.FrequencyError);
if HABSources[SourceIndex].LatestPosition.InUse then begin
// We have a position so we know what payload we last received
frmPayloads.ShowFrequencyError(HABSources[SourceIndex].LatestPosition.PayloadID, Position.FrequencyError);
end;
end;
if Position.Device <> '' then begin
HABSources[SourceIndex].SourceForm.AddStatusToLog('Device = ' + Position.Device);
end;
if Position.Version <> '' then begin
HABSources[SourceIndex].SourceForm.AddStatusToLog('Version = ' + Position.Version);
HABSources[SourceIndex].SourceForm.SetDeviceVersion(MyStrToFloat(Position.Version));
end;
if Position.CallingModeMessage then begin
HABSources[SourceIndex].SourceForm.SetFromCallingMode(Position.CallingModeFrequency, Position.CallingModeImplicit, Position.CallingModeError, Position.CallingModeBandwidth, Position.CallingModeSpreading, Position.CallingModeOptimize);
end;
end;
procedure TfrmSources.tmrSSDVTimer(Sender: TObject);
var
i: Integer;
begin
for i := Low(HABSources) to High(HABSources) do begin
if HABSources[i].InUse and
HABSources[i].HaveReceivedSSDV and
(HABSources[i].SSDVCount > 0) then begin
if HABSources[i].SourceForm <> nil then begin
HABSources[i].SourceForm.AddStatusToLog('Receiving ' + HABSources[i].SSDVCount.ToString + ' SSDV packets/min');
end;
HABSources[i].SSDVCount := 0;
end;
end;
end;
procedure TfrmSources.DataSourceClick(Sender: TObject);
var
SourceIndex: Integer;
begin
// Which source?
SourceIndex := TComponent(Sender).Tag;
if ShowSettingsForm(SourceIndex) then begin
ReloadSourceSettings(SourceIndex);
end;
end;
procedure TfrmSources.DeleteSource(SourceIndex: Integer);
begin
with HABSources[SourceIndex] do begin
if MessageDlg('Are you sure you want to permanently remove ' + Code + ': ' + Description + ' ?',
mtWarning, mbOKCancel, 0) = mrOK then begin
// Delete from table
with DataModule1.tblSources do begin
if FindKey([SourceID]) then begin
Delete;
end;
end;
// Delete from list
if SourceForm <> nil then begin
SourceForm.Free;
SourceForm := nil;
end;
// Indicator.Free;
InUse := False;
SourceID := 0;
DataModule1.tblSources.SaveToFile(ExtractFilePath(Application.ExeName) + 'sources.json');
end;
end;
end;
function TfrmSources.ShowSettingsForm(SourceIndex: Integer): Boolean;
var
SettingsForm: TfrmSettings;
begin
Result := False;
case HABSources[SourceIndex].SourceType of
// stLogtail: SettingsForm := TfrmLogtailSettings.Create(Self);
stGateway: SettingsForm := TfrmGatewaySettings.Create(Self);
stSerial: SettingsForm := TfrmLoRaSerialSettings.Create(Self);
stTCP: SettingsForm := TfrmTCPSettings.Create(Self);
stUDP: SettingsForm := TfrmUDPSettings.Create(Self);
stHabitat: SettingsForm := TfrmHabitatSettings.Create(Self);
stAPRS: SettingsForm := TfrmAPRSSettings.Create(Self);
stMQTT: SettingsForm := TfrmMQTTSettings.Create(Self);
stWSMQTT: SettingsForm := TfrmWSMQTTSettings.Create(Self);
stSondehub: SettingsForm := TfrmLogtailSettings.Create(Self);
else SettingsForm := nil;
end;
if SettingsForm <> nil then begin
Result := SettingsForm.LoadForm(HABSources[SourceIndex].SourceID);
if HABSources[SourceIndex].SourceForm <> nil then begin
HABSources[SourceIndex].SourceForm.Enabled := SettingsForm.chKEnabled.Checked;
end;
SettingsForm.Free;
end;
end;
procedure TfrmSources.UploaderClick(Sender: TObject);
var
Index: Integer;
begin
Index := TAdvSmoothStatusIndicator(Sender).Tag;
if Uploaders[Index].StatusForm <> nil then begin
Uploaders[Index].StatusForm.Show;
end;
end;
procedure TfrmSources.MQTTStatusCallback(SourceID: Integer; Active, OK: Boolean; Status: String);
begin
Uploaders[MQTT_UPLOADER].Attempted := Active;
Uploaders[MQTT_UPLOADER].Success := OK;
Uploaders[MQTT_UPLOADER].StatusForm.AddStatusToLog(Status);
Uploaders[MQTT_UPLOADER].Indicator.Hint := Status;
if OK then Uploaders[MQTT_UPLOADER].LastSuccess := Now;
ShowUplinkStatus(MQTT_UPLOADER);
end;
procedure TfrmSources.HabLinkStatusCallback(SourceID: Integer; Active, OK: Boolean; Status: String);
begin
Uploaders[HABLINK_UPLOADER].Attempted := Active;
Uploaders[HABLINK_UPLOADER].Success := OK;
Uploaders[HABLINK_UPLOADER].StatusForm.AddStatusToLog(Status);
Uploaders[HABLINK_UPLOADER].Indicator.Hint := Status;
if OK then Uploaders[HABLINK_UPLOADER].LastSuccess := Now;
ShowUplinkStatus(HABLINK_UPLOADER);
end;
procedure TfrmSources.SSDVStatusCallback(SourceID: Integer; Active, OK: Boolean; Status: String);
begin
Uploaders[SSDV_UPLOADER].Attempted := Active;
Uploaders[SSDV_UPLOADER].Success := OK;
Uploaders[SSDV_UPLOADER].StatusForm.AddStatusToLog(Status);
Uploaders[SSDV_UPLOADER].Indicator.Hint := Status;
if OK then Uploaders[SSDV_UPLOADER].LastSuccess := Now;
ShowUplinkStatus(SSDV_UPLOADER);
end;
procedure TfrmSources.SondehubStatusCallback(SourceID: Integer; Active, OK: Boolean; Status: String);
begin
Uploaders[SONDEHUB_UPLOADER].Attempted := Active;
Uploaders[SONDEHUB_UPLOADER].Success := OK;
Uploaders[SONDEHUB_UPLOADER].StatusForm.AddStatusToLog(Status);
Uploaders[SONDEHUB_UPLOADER].Indicator.Hint := Status;
if OK then Uploaders[SONDEHUB_UPLOADER].LastSuccess := Now;
ShowUplinkStatus(SONDEHUB_UPLOADER);
end;
procedure TfrmSources.ModifySource(SourceIndex: Integer);
begin
if ShowSettingsForm(SourceIndex) then begin
ReloadSourceSettings(SourceIndex);
end;
end;
function TfrmSources.SourceIsEnabled(SourceIndex: Integer): Boolean;
begin
Result := HABSources[SourceIndex].SourceEnabled;
end;
procedure TfrmSources.EnableSource(SourceIndex: Integer; EnableSource: Boolean);
begin
// Tell source form
SetSettingBoolean(HABSources[SourceIndex].Group, 'Enabled', EnableSource);
if HABSources[SourceIndex].SourceForm <> nil then begin
HABSources[SourceIndex].SourceForm.Enabled := EnableSource;
end;
// Tell source thread
HABSources[SourceIndex].SourceEnabled := EnableSource;
SetGroupChangedFlag(HABSources[SourceIndex].Group, True);
// Show status
ShowSourceStatus(SourceIndex);
// Save in table / file
with DataModule1.tblSources do begin
if FindKey([HABSources[SourceIndex].SourceID]) then begin
Edit;
FieldByName('Enabled').AsBoolean := EnableSource;
Post;
SaveToFile(ExtractFilePath(Application.ExeName) + 'sources.json');
end;
end;
end;
procedure TfrmSources.SendUplink(SourceIndex: Integer; When: TUplinkWhen; WhenValue, Channel: Integer; Prefix, Msg, Password: String);
begin
HABSources[SourceIndex].Source.SendUplink(When, WhenValue, Channel, Prefix, Msg, Password);
end;
procedure TfrmSources.UpdateSourceFilters;
var
i: Integer;
begin
for i := Low(HABSources) to High(HABSources) do begin
if HABSources[i].InUse then begin
if HABSources[i].SourceType = stAPRS then begin
// Update APRS source filter
HABSources[i].Source.SetFilter(APRSFilter);
end;
end;
end;
end;
function TfrmSources.MaximumPayloadDistance: Double;
var
Distance: Double;
begin
Distance := 0;
with DataModule1.tblWhiteList do begin
First;
while not EOF do begin
if FieldByName('Distance').AsFloat > Distance then begin
Distance := FieldByName('Distance').AsFloat;
end;
Next;
end;
end;
Result := Distance;
end;
function TfrmSources.APRSFilter: String;
var
Latitude, Longitude, Distance: Double;
begin
// r/51.9/-2.5/50000
Latitude := DataModule1.tblSettings.FieldByName('Latitude').AsFloat;
Longitude := DataModule1.tblSettings.FieldByName('Longitude').AsFloat;
Distance := MaximumPayloadDistance;
Result := ' r/' +
MyFormatFloat('0.00000', Latitude) + '/' +
MyFormatFloat('0.00000', Longitude) + '/' +
MyFormatFloat('0', Distance);
end;
procedure TfrmSources.ShowUplinkStatus(UplinkIndex: Integer);
begin
with Uploaders[UplinkIndex] do begin
// Colours
if Enabled then begin
if Attempted then begin
if Success then begin
if Now < (LastSuccess + 60 / 86400) then begin
Indicator.Appearance.Fill.Color := clLime;
Indicator.Appearance.Font.Color := clBlack;
end else begin
Indicator.Appearance.Fill.Color := clGreen;
Indicator.Appearance.Font.Color := clWhite;
end;
end else begin
Indicator.Appearance.Fill.Color := clRed;
Indicator.Appearance.Font.Color := clWhite;
end;
end else begin
if Success then begin
Indicator.Appearance.Fill.Color := clGray;
Indicator.Appearance.Font.Color := clWhite;
end else begin
Indicator.Appearance.Fill.Color := clWebDarkOrange;
Indicator.Appearance.Font.Color := clBlack;
end;
end;
end else begin
Indicator.Appearance.Fill.Color := clSilver;
Indicator.Appearance.Font.Color := clGray;
end;
end;
end;
procedure TfrmSources.ShowAllUplinkStatus;
var
i: Integer;
begin
for i := Low(Uploaders) to High(Uploaders) do begin
ShowUplinkStatus(i);
end;
end;
procedure TfrmSources.ApplySettings;
var
Topic: String;
begin
Uploaders[SONDEHUB_UPLOADER].Enabled := DataModule1.tblSettings.FieldByName('UplinkSondehub').AsBoolean;
Uploaders[MQTT_UPLOADER].Enabled := DataModule1.tblSettings.FieldByName('UplinkMQTT').AsBoolean;
Uploaders[HABLINK_UPLOADER].Enabled := DataModule1.tblSettings.FieldByName('UplinkHABLINK').AsBoolean;
Uploaders[SSDV_UPLOADER].Enabled := DataModule1.tblSettings.FieldByName('UplinkSSDV').AsBoolean;
try
Topic := StringReplace(DataModule1.tblSettings.FieldByName('MQTTTopic').AsString, '$LISTENER$', DataModule1.tblSettings.FieldByName('Callsign').AsString, [rfReplaceAll, rfIgnoreCase]);
MQTTUploader.SetMQTTDetails(DataModule1.tblSettings.FieldByName('MQTTHost').AsString,
DataModule1.tblSettings.FieldByName('MQTTUserName').AsString,
DataModule1.tblSettings.FieldByName('MQTTPassword').AsString,
Topic, '');
HablinkUploader.SetMQTTDetails('hab.link', 'daffyduck', 'HAB2022', 'incoming/payloads/$PAYLOAD$/$LISTENER$/sentence', '');
finally
ShowAllUplinkStatus;
end;
end;
end.