-
Notifications
You must be signed in to change notification settings - Fork 0
/
soarSUA.c
5718 lines (5254 loc) · 206 KB
/
soarSUA.c
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
#include <PalmOS.h>
#include "soaring.h"
#include "soarSUA.h"
#include "soarForm.h"
#include "soarDB.h"
#include "soarUtil.h"
#include "soarUMap.h"
#include "soarIO.h"
#include "Mathlib.h"
#include "soarMath.h"
#include "soarMem.h"
#include "soarWind.h"
#include "soarComp.h"
//#define CHKBALL // check bounding ball in SUA warnings
//#define DRAW_BOUNDS // draw the bounding box and ball for each SUA element
//#define DRAW_ARC // draw ARC as using sector routine
SUAIndex *suaidx;
SUAData *suadata;
SUAAlertData *suaalert;
SUAAlertRet *suaalertret;
SUAIndex *selectedSUA;
Int16 selectedSUAListIdx=0;
Int16 currentSUAPage=0;
Int16 numSUARecs=0;
Int16 suasortType = SUASortByDistA;
Int16 QNHnumdigits = 0;
double temppalt, tempgalt;
Boolean SUAselectall = false;
Boolean lookingahead = false;
Int16 *warnlist=NULL;
Boolean dispsuareadonly = false;
Char WarnString[40];
Int32 SUAallclasses;
Int32 SUAdrawclasses;
Int32 SUAwarnclasses;
UInt32 warning_time = 0xffffffff;
Boolean allowgenalerttap = false;
double OpenAirfirstlat = INVALID_LAT_LON;
double OpenAirfirstlon = INVALID_LAT_LON;
double OpenAirlastlat = INVALID_LAT_LON;
double OpenAirlastlon = INVALID_LAT_LON;
double SUAdatalastlat = INVALID_LAT_LON;
double SUAdatalastlon = INVALID_LAT_LON;
extern Boolean inflight;
extern Int16 curformpriority;
extern Boolean recv_palt;
extern Boolean terrainvalid;
extern Boolean menuopen;
extern Boolean allowExit;
extern IndexedColorType indexSUA, indexSUAwarn, indexBlack, indexRed;
extern double xdist, ydistul, ydistll;
extern Boolean allowgenalerttap;
extern double mapmaxlat, mapminlat;
extern double mapmaxlon, mapminlon;
extern Int32 rxrecs;
extern char rxtype[15];
extern UInt32 origform;
extern UInt8 mapmode;
extern Boolean draw_log;
extern UInt32 cursecs;
extern Char buf[PARSELEN];
void check_sua_elements()
{
Boolean SUAWarned = false;
UInt16 x;
static UInt16 SUAWarn_count=0;
double projlat, projlon, altadj;
DateTimeType timenow;
MemHandle suaidx_hand=NULL;
MemHandle output_hand=NULL;
MemPtr output_ptr=NULL;
SUAIndex *tempsuaidx;
UInt16 checkrecs;
// only check when GPS active and warnings are on
if ((data.config.CheckSUAWarn) && (data.input.SUAnumrecs > 0) && (inflight) && !draw_log) {
// HostTraceOutputTL(appErrorClass, "SUA Check %s",DblToStr(SUAWarn_count,0));
// check for SUA warnings : 64 at one time
if ((data.config.SUAlookahead > 0) && (data.input.SUAwarnrecs > 1)) {
checkrecs = data.input.SUAwarnrecs / 2; // checking a look ahead position as well doubles the number of checks
} else {
checkrecs = data.input.SUAwarnrecs;
}
SUAWarned = false;
for (x = 0; x < checkrecs; x++) {
SUAWarn_count++;
if (SUAWarn_count >= data.input.SUAnumrecs) {
SUAWarn_count = 0;
// HostTraceOutputTL(appErrorClass, "Done all SUA %s",data.logger.gpsutc);
// PlaySound(784, 125, sndMaxAmp); // for debugging / testing only
}
// check current position
lookingahead = false;
SUAWarned = WarnSUA(SUAWarn_count, data.input.gpslatdbl, data.input.gpslngdbl,
data.input.inusealt, data.logger.pressalt, data.input.inusealt-data.input.terelev,
data.config.vert_dist, data.config.horiz_dist, data.config.SUArewarn);
if ((data.config.SUAlookahead > 0) && (mapmode == CRUISE)) {
// project glider position in cruise mode only, check if inside airspace (not near)
RangeBearingToLatLonLinearInterp(data.input.gpslatdbl, data.input.gpslngdbl,
data.input.ground_speed.value * (data.config.SUAlookahead / 3600.0), data.input.true_track.value,
&projlat, &projlon);
altadj = 0.0; // consider some altitude adjustment based on glide??
lookingahead = true;
SUAWarned = WarnSUA(SUAWarn_count, projlat, projlon,
data.input.inusealt-altadj, data.logger.pressalt-altadj, data.input.inusealt-data.input.terelev-altadj,
data.config.vert_dist, 0.0, 0.0);
}
}
//used to automatically dismiss SUA warnings
if ((suaalert->alerttype == SUAWARNING_ALERT) && (data.application.form_id == form_genalert) && (data.config.autodismisstime > 0)) {
// automatically close alert after set delay
if (cursecs > warning_time + data.config.autodismisstime) {
// automatically dismiss the warning and close window
// HostTraceOutputTL(appErrorClass, "SUA autodismiss");
warning_time = 0xffffffff;
HandleWaitDialogWin(0);
suaalertret->valid = true;
suaalertret->btnselected = 6;
suaalertret->alerttype = SUAWARNING_ALERT;
suaalertret->alertidxret = suaalert->alertidx;
suaalertret->priority = suaalert->priority;
}
}
// check for return from SUA alert
if ((suaalert->alerttype == SUAWARNING_ALERT) && suaalertret->valid) {
// HostTraceOutputTL(appErrorClass, "Dismiss %hd", suaalertret->alertidxret);
// one of the buttons pressed, need to update the suaidx record
// open SUA index database
suaidx_hand = MemHandleNew(sizeof(SUAIndex));
tempsuaidx = MemHandleLock(suaidx_hand);
OpenDBQueryRecord(suaidx_db, suaalertret->alertidxret, &output_hand, &output_ptr);
MemMove(tempsuaidx, output_ptr, sizeof(SUAIndex));
MemHandleUnlock(output_hand);
// update last warned time
tempsuaidx->Dismissed = true;
// HostTraceOutputTL(appErrorClass, "SUA Selected %hd",suaalertret->btnselected);
switch (suaalertret->btnselected) {
case 3: // 5 mins
// HostTraceOutputTL(appErrorClass, "SUA 5 mins");
tempsuaidx->LastWarned = cursecs + 300;
if (tempsuaidx->WarnType == SUAWARN_APPROACHING) {
tempsuaidx->ApproachLastWarned = tempsuaidx->LastWarned;
}
tempsuaidx->WarnType = SUAWARN_SNOOZE;
break;
case 4: // 1 hour
// HostTraceOutputTL(appErrorClass, "SUA 1 hour");
tempsuaidx->LastWarned = cursecs + 3600;
if (tempsuaidx->WarnType == SUAWARN_APPROACHING) {
tempsuaidx->ApproachLastWarned = tempsuaidx->LastWarned;
}
tempsuaidx->WarnType = SUAWARN_SNOOZE;
break;
case 5: // Today (24 hrs or until SoarPilot starts again)
// HostTraceOutputTL(appErrorClass, "SUA midnight");
TimSecondsToDateTime(cursecs, &timenow);
timenow.hour = 23;
timenow.minute = 59;
timenow.second = 59;
tempsuaidx->LastWarned = TimDateTimeToSeconds(&timenow);
//tempsuaidx->LastWarned = cursecs + (UInt32)3600*24;
if (tempsuaidx->WarnType == SUAWARN_APPROACHING) {
tempsuaidx->ApproachLastWarned = tempsuaidx->LastWarned;
}
tempsuaidx->WarnType = SUAWARN_NONE;
break;
case 7:
// HostTraceOutputTL(appErrorClass, "SUA immediate and popup SUA display");
tempsuaidx->LastWarned = cursecs;
if (tempsuaidx->WarnType == SUAWARN_APPROACHING) {
tempsuaidx->ApproachLastWarned = cursecs + (1 - data.config.SUArewarn) * data.config.SUAlookahead;
}
selectedSUAListIdx = suaalertret->alertidxret;
MemMove(selectedSUA, tempsuaidx, sizeof(SUAIndex));
dispsuareadonly = true;
FrmGotoForm(form_disp_sua);
break;
default: // immediately active again (new warning when more serious)
// HostTraceOutputTL(appErrorClass, "SUA immediate");
tempsuaidx->LastWarned = cursecs;
if (tempsuaidx->WarnType == SUAWARN_APPROACHING) {
tempsuaidx->ApproachLastWarned = cursecs + (1 - data.config.SUArewarn) * data.config.SUAlookahead;
}
break;
}
// Update suaidx record
OpenDBUpdateRecord(suaidx_db, sizeof(SUAIndex), tempsuaidx, suaalertret->alertidxret);
// free memory
MemHandleUnlock(suaidx_hand);
MemHandleFree(suaidx_hand);
// clear alert response
suaalertret->alerttype = NULL_ALERT;
suaalertret->alertidxret = -1;
suaalertret->valid = false;
suaalertret->btnselected = -1;
}
}
return;
}
// Check a single SUA element for airspace warning
// Inputs:
// SUAelement // which SUA element to check
// curlat, curlon // position to check for airspace warnings
// curalt, curFL, curAGL // current altitudes in MSL, FL and AGL
// vert_dist // vertical warning distance in ft
// horiz_dist // horizontal warning distance in nm
// SUArewarn // % of vertical or horizontal distance to display an urgent warning
Boolean WarnSUA(UInt16 SUAelement, double curlat, double curlon, double curalt,
double curFL, double curAGL, double vert_dist, double horiz_dist, double SUArewarn)
{
Boolean SUAwarned;
MemHandle suaidx_hand=NULL;
MemHandle output_hand=NULL;
MemPtr output_ptr=NULL;
double neardist;
// double coslat;
UInt16 totalidxrecs=0;
Int8 relalt=0;
double width=0;
Boolean chkbase, chktops;
Boolean rewarnalt, rewarnbase, rewarntops;
double chkdist;
double topsalt, basealt;
// HostTraceOutputTL(appErrorClass, "Warn SUAelement=|%s|", DblToStr(SUAelement,0));
// HostTraceOutputTL(appErrorClass, " Altitude =|%s|", DblToStr(curalt,0));
// HostTraceOutputTL(appErrorClass, " Cur Lat =|%s|", DblToStr(curlat,3));
// HostTraceOutputTL(appErrorClass, " Cur Lon =|%s|", DblToStr(curlon,3));
// HostTraceOutputTL(appErrorClass, " Vert Dist =|%s|", DblToStr(vert_dist,1));
// HostTraceOutputTL(appErrorClass, " Horiz Dist =|%s|", DblToStr(horiz_dist,1));
// HostTraceOutputTL(appErrorClass, "----------------");
SUAwarned = false;
// catch if pressure altitude source defined but no data received
if (!recv_palt && (data.config.pressaltsrc != GPSALT)) {
curFL = curalt + data.input.QNHaltcorrect;
}
// check SUAelement number is valid
totalidxrecs = OpenDBCountRecords(suaidx_db);
// HostTraceOutputTL(appErrorClass, " totalidxrecs =|%s|", DblToStr(totalidxrecs,0));
if ((totalidxrecs > 0) && (SUAelement < totalidxrecs)) {
// open SUA index database
suaidx_hand = MemHandleNew(sizeof(SUAIndex));
suaidx = MemHandleLock(suaidx_hand);
OpenDBQueryRecord(suaidx_db, SUAelement, &output_hand, &output_ptr);
MemMove(suaidx, output_ptr, sizeof(SUAIndex));
MemHandleUnlock(output_hand);
// HostTraceOutputTL(appErrorClass, "----------------");
// HostTraceOutputTL(appErrorClass, " Title =|%s|", suaidx->title);
// HostTraceOutputTL(appErrorClass, " Element =|%hd|", SUAelement);
// HostTraceOutputTL(appErrorClass, " WarnType =|%hd|", suaidx->WarnType);
// HostTraceOutputTL(appErrorClass, " Type =|%s|", DblToStr(suaidx->plottype,0));
// HostTraceOutputTL(appErrorClass, " Tops =|%s|", DblToStr(suaidx->tops,0));
// HostTraceOutputTL(appErrorClass, " Base =|%s|", DblToStr(suaidx->base,0));
// HostTraceOutputTL(appErrorClass, " Max Lat =|%s|", DblToStr(suaidx->SUAmaxlat,3));
// HostTraceOutputTL(appErrorClass, " Min Lat =|%s|", DblToStr(suaidx->SUAminlat,3));
// HostTraceOutputTL(appErrorClass, " Max Lon =|%s|", DblToStr(suaidx->SUAmaxlon,3));
// HostTraceOutputTL(appErrorClass, " Min Lon =|%s|", DblToStr(suaidx->SUAminlon,3));
// HostTraceOutputTL(appErrorClass, " Dismissed =|%s|", DblToStr(suaidx->Dismissed,0));
// HostTraceOutputTL(appErrorClass, "----------------");
// check warnings are active for this type and not deactivated for this element
if ( (data.config.suawarntypes & suaidx->type)
&& (SUAwarnclasses & suaidx->class)
&& (suaidx->WarnActive)) {
// HostTraceOutputTL(appErrorClass, "Active OK");
// check altitude types for base and tops
rewarnbase = false;
chkbase = false;
// HostTraceOutputTL(appErrorClass, "Base=|%s|", DblToStr(suaidx->basetype,0));
switch (suaidx->basetype) {
case SUA_ALT:
basealt = curalt;
break;
case SUA_FL:
basealt = curFL;
break;
case SUA_AGL:
case SUA_SFC:
basealt = curAGL;
break;
default:
basealt = curalt;
break;
}
if (basealt >= suaidx->base - vert_dist) {
chkbase = true;
if (basealt >= suaidx->base - (vert_dist * SUArewarn)) {
rewarnbase = true;
}
}
// HostTraceOutputTL(appErrorClass, "Tops=|%s|", DblToStr(suaidx->topstype,0));
rewarntops = false;
chktops = false;
switch (suaidx->topstype) {
case SUA_ALT:
topsalt = curalt;
break;
case SUA_FL:
topsalt = curFL;
break;
case SUA_AGL:
case SUA_SFC:
topsalt = curAGL;
break;
default:
topsalt = curalt;
break;
}
if (topsalt <= suaidx->tops + vert_dist) {
chktops = true;
if (topsalt <= suaidx->tops + (vert_dist * SUArewarn)) {
rewarntops = true;
}
}
// HostTraceOutputTL(appErrorClass, "Tops=|%s|", DblToStr(chktops,0));
// HostTraceOutputTL(appErrorClass, "Base=|%s|", DblToStr(chkbase,0));
// HostTraceOutputTL(appErrorClass, "rewarnalt=|%s|", DblToStr(rewarnalt,0));
// HostTraceOutputTL(appErrorClass, "rewarn=|%s|", DblToStr(vert_dist * SUArewarn,0));
rewarnalt = (rewarntops && rewarnbase);
// check altitude is within tops and base +/- vert_dist
if ((chkbase) && (chktops)) {
// HostTraceOutputTL(appErrorClass, "Alt OK");
// define relalt as above, below or in the airspace by altitude
relalt = 0;
if (basealt < suaidx->base) {
relalt = -1; // below airspace
// HostTraceOutputTL(appErrorClass, "Below");
} else if (topsalt > suaidx->tops) {
relalt = 1; // above airspace
// HostTraceOutputTL(appErrorClass, "Above");
}
if (suaidx->plottype == SUAAWY) {
// add half airway width to horiz_dist for checking bounding box
width = suaidx->width / 2.0;
} else {
width = 0.0;
}
// check position bounding box + horiz_dist
if ((curlat <= suaidx->SUAmaxlat + (horiz_dist + width) / 60.0) &&
(curlat >= suaidx->SUAminlat - (horiz_dist + width) / 60.0) &&
(curlon <= suaidx->SUAmaxlon + (horiz_dist + width) / data.input.coslat / 60.0) &&
(curlon >= suaidx->SUAminlon - (horiz_dist + width) / data.input.coslat / 60.0)) {
// HostTraceOutputTL(appErrorClass, "Box OK");
#ifdef CHKBALL
// check bounding ball + horiz_dist
neardist = LatLonToRange2(curlat, curlon, suaidx->reflat, suaidx->reflon);
if (neardist < (suaidx->maxdist + horiz_dist + width) * (suaidx->maxdist + horiz_dist + width)) {
// HostTraceOutputTL(appErrorClass, "Ball OK");
#endif
// Inside altitude limits and both bounding box and ball so need to do detailed check
// check INSIDE or NEAR for each type of element
// HostTraceOutputTL(appErrorClass, "Type: %s",DblToStr(suaidx->plottype,0));
switch (suaidx->plottype) {
case SUATFR:
case SUACIRCLE:
#ifndef CHKBALL
neardist = LatLonToRange2(curlat, curlon, suaidx->reflat, suaidx->reflon);
#endif
// check if INSIDE circle
if (neardist <= (suaidx->maxdist * suaidx->maxdist)) {
// check for exiting airspace warnings
if (suaidx->warnonexit && (relalt == 0) && (horiz_dist > 0)) {
// check vertical for high or low
rewarnbase = false;
chkbase = false;
if ((basealt <= suaidx->base + vert_dist) && (basealt >= suaidx->base)) {
chkbase = true;
if (basealt <= suaidx->base + (vert_dist * SUArewarn)) {
rewarnbase = true;
}
}
rewarntops = false;
chktops = false;
if ((topsalt >= suaidx->tops - vert_dist) && (topsalt <= suaidx->tops)) {
chktops = true;
if (topsalt >= suaidx->tops - (vert_dist * SUArewarn)) {
rewarntops = true;
}
}
rewarnalt = (rewarntops || rewarnbase);
relalt = 0;
if (basealt <= suaidx->base + vert_dist) {
relalt = -1;
} else if (topsalt >= suaidx->tops - vert_dist) {
relalt = 1;
}
// check horizontal for edge
chkdist = suaidx->maxdist - horiz_dist;
if ((neardist >= chkdist * chkdist) || chkbase || chktops) {
// near edge, top or bottom
SUAwarned = true;
// check if urgent edge warning
chkdist = suaidx->maxdist - horiz_dist * SUArewarn;
if (neardist >= chkdist * chkdist) {
WarnSet(SUAelement, relalt, true, true, true);
} else {
chkdist = suaidx->maxdist - horiz_dist;
WarnSet(SUAelement, relalt, (neardist >= chkdist * chkdist), rewarnalt, true);
}
} else {
// if not set to SUAURGENT_IN warning
SUAwarned = true;
WarnSet(SUAelement, relalt, true, true, false);
}
} else {
// normal INSIDE warning
SUAwarned = true;
WarnSet(SUAelement, relalt, true, rewarnalt, false);
}
} else if (horiz_dist > 0) {
// check if NEAR circle within horiz_dist
chkdist = suaidx->maxdist + horiz_dist;
if (neardist <= chkdist * chkdist) {
SUAwarned = true;
// check if urgent warning
chkdist = suaidx->maxdist + horiz_dist * SUArewarn;
if (neardist <= chkdist * chkdist) {
WarnSet(SUAelement, relalt, false, rewarnalt, false);
} else {
WarnSet(SUAelement, relalt, false, false, false);
}
} else { // if not clear the warning
WarnClear();
}
} else { // if not clear the warning
WarnClear();
}
break;
case SUAAWY:
// check using checkdist function with width to get inside or distance
chkdist = suaidx->width / 2.0; // note: for speed, exit checkdist function if less than this distance
neardist = checkdist(curlat, curlon, suaidx->startidx, suaidx->stopidx, chkdist);
if (neardist <= chkdist * chkdist) {
// check for exiting airspace warnings
if (suaidx->warnonexit && (relalt == 0) && (horiz_dist > 0)) {
rewarnbase = false;
chkbase = false;
if ((basealt <= suaidx->base + vert_dist) && (basealt >= suaidx->base)) {
chkbase = true;
if (basealt <= suaidx->base + (vert_dist * SUArewarn)) {
rewarnbase = true;
}
}
rewarntops = false;
chktops = false;
if ((topsalt >= suaidx->tops - vert_dist) && (topsalt <= suaidx->tops)) {
chktops = true;
if (topsalt >= suaidx->tops - (vert_dist * SUArewarn)) {
rewarntops = true;
}
}
rewarnalt = (rewarntops || rewarnbase);
relalt = 0;
if (basealt <= suaidx->base + vert_dist) {
relalt = -1;
} else if (topsalt >= suaidx->tops - vert_dist) {
relalt = 1;
}
// check horizontal for edge
chkdist = suaidx->width / 2.0 - horiz_dist;
if ((neardist >= chkdist * chkdist) || chkbase || chktops) {
// near edge, top or bottom
SUAwarned = true;
// check if urgent edge warning
chkdist = suaidx->width / 2.0 - horiz_dist * SUArewarn;
if (neardist >= chkdist * chkdist) {
WarnSet(SUAelement, relalt, true, true, true);
} else {
chkdist = suaidx->width / 2.0 - horiz_dist;
WarnSet(SUAelement, relalt, (neardist >= chkdist * chkdist), rewarnalt, true);
}
} else {
// if not set to SUAURGENT_IN warning
SUAwarned = true;
WarnSet(SUAelement, relalt, true, true, false);
}
} else {
// normal INSIDE warning
SUAwarned = true;
WarnSet(SUAelement, relalt, true, rewarnalt, false);
}
} else if (horiz_dist > 0) {
// check if near to airway
chkdist = suaidx->width / 2.0 + horiz_dist;
if (neardist <= chkdist * chkdist) {
// near to airway
SUAwarned = true;
// check if urgent warning
chkdist = suaidx->width / 2.0 + horiz_dist * SUArewarn;
if (neardist <= chkdist * chkdist) {
WarnSet(SUAelement, relalt, false, rewarnalt, false);
} else {
WarnSet(SUAelement, relalt, false, false, false);
}
} else { // if not clear warning
WarnClear();
}
} else { // if not clear the warning
WarnClear();
}
break;
case SUAARC: // should never have SUAARC as first type but included here just in case
case SUAPOINT:
// HostTraceOutputTL(appErrorClass, "Check POLYGON");
// check if INSIDE polygon
// HostTraceOutputTL(appErrorClass, "Check In/Out");
if (checkinout(curlat, curlon, suaidx->startidx, suaidx->stopidx)) {
// check for exiting airspace warnings
if (suaidx->warnonexit && (relalt == 0) && (horiz_dist > 0)) {
rewarnbase = false;
chkbase = false;
if ((basealt <= suaidx->base + vert_dist) && (basealt >= suaidx->base)) {
chkbase = true;
if (basealt <= suaidx->base + (vert_dist * SUArewarn)) {
rewarnbase = true;
}
}
rewarntops = false;
chktops = false;
if ((topsalt >= suaidx->tops - vert_dist) && (topsalt <= suaidx->tops)) {
chktops = true;
if (topsalt >= suaidx->tops - (vert_dist * SUArewarn)) {
rewarntops = true;
}
}
rewarnalt = (rewarntops || rewarnbase);
relalt = 0;
if (basealt <= suaidx->base + vert_dist) {
relalt = -1;
} else if (topsalt >= suaidx->tops - vert_dist) {
relalt = 1;
}
// check horizontal for edge
neardist = checkdist(curlat, curlon, suaidx->startidx, suaidx->stopidx, horiz_dist * SUArewarn);
chkdist = horiz_dist;
if ((neardist <= chkdist * chkdist) || chkbase || chktops) {
// near edge, top or bottom
SUAwarned = true;
// check if urgent edge warning
chkdist = horiz_dist * SUArewarn;
if (neardist <= chkdist * chkdist) {
WarnSet(SUAelement, relalt, true, true, true);
} else {
chkdist = horiz_dist;
WarnSet(SUAelement, relalt, (neardist <= chkdist * chkdist), rewarnalt, true);
}
} else {
// if not set to SUAURGENT_IN warning
SUAwarned = true;
WarnSet(SUAelement, relalt, true, true, false);
}
} else {
// normal INSIDE warning
SUAwarned = true;
WarnSet(SUAelement, relalt, true, rewarnalt, false);
}
} else if (horiz_dist > 0) { // check if NEAR polygon within horiz_dist
// note: for speed, exit checkdist function if less than horiz_dist * SUArewarn
// HostTraceOutputTL(appErrorClass, "Check Distance");
neardist = checkdist(curlat, curlon, suaidx->startidx, suaidx->stopidx, horiz_dist * SUArewarn);
// check if near within horiz_dist
chkdist = horiz_dist;
if (neardist <= chkdist * chkdist) {
SUAwarned = true;
// check if urgent warning
chkdist = horiz_dist * SUArewarn;
if (neardist <= chkdist * chkdist) {
WarnSet(SUAelement, relalt, false, rewarnalt, false);
} else {
WarnSet(SUAelement, relalt, false, false, false);
}
} else { // if not clear the warning
WarnClear();
}
} else { // if not clear the warning
WarnClear();
}
break;
default:
break;
} // from switch statement
#ifdef CHKBALL
} else { // from bounding ball check
WarnClear();
}
#endif
} else { // from bounding box check
WarnClear();
}
} else { // from altitude checks
WarnClear();
}
} else { // from active and ignore checks
WarnClear();
}
// Update suaidx record
OpenDBUpdateRecord(suaidx_db, sizeof(SUAIndex), suaidx, SUAelement);
} // from totalidxrecs checks
// free memory
MemHandleUnlock(suaidx_hand);
MemHandleFree(suaidx_hand);
return(SUAwarned);
}
void WarnSet(UInt16 SUAelement, Int16 relalt, Boolean inside, Boolean rewarn, Boolean warnonexit)
// Warn when near airspace
{
Int16 NewWarnType;
Char TypeString[11];
Char ClassString[2];
Char TTstr[15];
Char BTstr[15];
// find which warning to display
if (lookingahead) {
NewWarnType = SUAWARN_APPROACHING;
rewarn = false;
} else if (warnonexit) {
// exiting airspace warnings
if (relalt == -1) {
// low warnings
if (suaidx->base == 0) return; // no need for low warnings when the base is at ground level!
if (rewarn) {
if (inside) {
NewWarnType = SUAURGENT_LOW_EDGE;
} else {
NewWarnType = SUAURGENT_LOW;
}
} else {
if (inside) {
NewWarnType = SUAWARN_LOW_EDGE;
} else {
NewWarnType = SUAWARN_LOW;
}
}
} else if (relalt == 1) {
// high warnings
if (rewarn) {
if (inside) {
NewWarnType = SUAURGENT_HIGH_EDGE;
} else {
NewWarnType = SUAURGENT_HIGH;
}
} else {
if (inside) {
NewWarnType = SUAWARN_HIGH_EDGE;
} else {
NewWarnType = SUAWARN_HIGH;
}
}
} else {
// edge warnings
if (rewarn) {
NewWarnType = SUAURGENT_EDGE;
} else {
NewWarnType = SUAWARN_EDGE;
}
}
} else if (rewarn) {
// URGENT warnings
if (inside) {
// Horizontally inside airspace
if (relalt == -1) { // check if below
NewWarnType = SUAURGENT_BELOW;
} else if (relalt == 1) { // check if above
NewWarnType = SUAURGENT_ABOVE;
} else { // must be inside
NewWarnType = SUAURGENT_IN;
}
} else {
// near the airspace horizontally
if (relalt == -1) { // check if below
NewWarnType = SUAURGENT_NEAR_BELOW;
} else if (relalt == 1) { // check if above
NewWarnType = SUAURGENT_NEAR_ABOVE;
} else { // must be near with vertical limits
NewWarnType = SUAURGENT_NEAR;
}
}
} else {
// normal warnings
if (inside) {
// Horizontally inside airspace
if (relalt == -1) { // check if below
NewWarnType = SUAWARN_BELOW;
} else if (relalt == 1) { // check if above
NewWarnType = SUAWARN_ABOVE;
} else { // must be inside
NewWarnType = SUAWARN_IN;
}
} else {
// near the airspace horizontally
if (relalt == -1) { // check if below
NewWarnType = SUAWARN_NEAR_BELOW;
} else if (relalt == 1) { // check if above
NewWarnType = SUAWARN_NEAR_ABOVE;
} else { // must be near with vertical limits
NewWarnType = SUAWARN_NEAR;
}
}
}
// display if the warning is not dismissed or a different type of warning
if ((!suaidx->Dismissed) || (suaidx->WarnType != NewWarnType)) {
// HostTraceOutputTL(appErrorClass, "Checking Warning.....");
// HostTraceOutputTL(appErrorClass, "Suaidx Type=|%s|",DblToStr(suaidx->WarnType,0));
// HostTraceOutputTL(appErrorClass, "New Warn Type=|%s|",DblToStr(NewWarnType,0));
// HostTraceOutputTL(appErrorClass, "Relalt=|%s|", DblToStr(relalt,0));
// HostTraceOutputTL(appErrorClass, "Inside=|%s|", DblToStr(inside,0));
// HostTraceOutputTL(appErrorClass, "Rewarn=|%s|", DblToStr(rewarn,0));
// HostTraceOutputTL(appErrorClass, "WarnonExit=|%s|", DblToStr(warnonexit ,0));
// check for lower priority alerts and time since last warning not expired
// for example when flying out of / away from the SUA item, or open alert form on screen
if (((NewWarnType > suaidx->WarnType) || (suaidx->warnonexit && ((NewWarnType < SUAURGENT_IN) && (suaidx->WarnType >= SUAURGENT_IN)))) && (cursecs > suaidx->LastWarned)) {
// play warning sound
if (rewarn) {
PlayNoGPSSound();
} else {
PlayWarnSound();
}
// higher or equal priority warning for same SUA item
// HostTraceOutputTL(appErrorClass, "Higher Priority Alert");
if (suaidx->warnonexit && (NewWarnType >= SUAURGENT_IN) && (suaidx->WarnType < SUAURGENT_IN)) {
// entering airspace
StrCopy(WarnString, " ! ENTERING Airspace !");
} else if (suaidx->warnonexit && (NewWarnType < SUAURGENT_IN) && (suaidx->WarnType >= SUAURGENT_IN)) {
// leaving airspace
StrCopy(WarnString, " ! LEAVING Airspace !");
} else {
// Decide appropriate warning text
switch (NewWarnType) {
// approaching warnings
case SUAWARN_APPROACHING:
StrCopy(WarnString, " Approaching Airspace ");
break;
// normal warnings
case SUAWARN_BELOW:
StrCopy(WarnString, " Below Airspace ");
break;
case SUAWARN_ABOVE:
StrCopy(WarnString, " Above Airspace ");
break;
case SUAWARN_IN:
StrCopy(WarnString, " INSIDE Airspace ");
break;
case SUAWARN_NEAR_BELOW:
StrCopy(WarnString, " Below Near Airspace ");
break;
case SUAWARN_NEAR_ABOVE:
StrCopy(WarnString, " Above Near Airspace ");
break;
case SUAWARN_NEAR:
StrCopy(WarnString, " Near Airspace ");
break;
// URGENT warnings
case SUAURGENT_BELOW:
StrCopy(WarnString, " ! Below Airspace !");
break;
case SUAURGENT_ABOVE:
StrCopy(WarnString, " ! Above Airspace !");
break;
case SUAURGENT_IN:
StrCopy(WarnString, " ! INSIDE Airspace !");
break;
case SUAURGENT_NEAR_BELOW:
StrCopy(WarnString, " ! Below Near Airspace !");
break;
case SUAURGENT_NEAR_ABOVE:
StrCopy(WarnString, " ! Above Near Airspace !");
break;
case SUAURGENT_NEAR:
StrCopy(WarnString, " ! Near Airspace !");
break;
// exiting airspace warnings
case SUAWARN_EDGE:
StrCopy(WarnString, " Near Airspace Edge ");
break;
case SUAWARN_LOW:
StrCopy(WarnString, " Near Airspace Bottom ");
break;
case SUAWARN_LOW_EDGE:
StrCopy(WarnString, " Near Airspace Bottom Edge ");
break;
case SUAWARN_HIGH:
StrCopy(WarnString, " Near Airspace Top ");
break;
case SUAWARN_HIGH_EDGE:
StrCopy(WarnString, " Near Airspace Top Edge ");
break;
case SUAURGENT_EDGE:
StrCopy(WarnString, " ! Near Airspace Edge !");
break;
case SUAURGENT_LOW:
StrCopy(WarnString, " ! Near Airspace Bottom !");
break;
case SUAURGENT_LOW_EDGE:
StrCopy(WarnString, "!Near Airspace Bottom Edge! ");
break;
case SUAURGENT_HIGH:
StrCopy(WarnString, " ! Near Airspace Top !");
break;
case SUAURGENT_HIGH_EDGE:
StrCopy(WarnString, " ! Near Airspace Top Edge !");
break;
default:
StrCopy(WarnString, " ! UNKNOWN Warning !");
break;
}
}
// HostTraceOutputTL(appErrorClass, "Warning=|%s|",WarnString);
// update latest warn type
suaidx->WarnType = NewWarnType;
// Convert the current type to a string
// 11 is the maximum length of the resulting string including the null terminator
// false means to use the long form of the string for output
GetSUATypeStr(TypeString, suaidx->type, 11, false);
GetSUAClassStr(ClassString, suaidx->class);
// decode tops and base types to text
GetSUAALTTypeStr(TTstr, suaidx->topstype, suaidx->tops, suaidx->topsunits, 15);
GetSUAALTTypeStr(BTstr, suaidx->basetype, suaidx->base, suaidx->baseunits, 15);
// HostTraceOutputTL(appErrorClass, "Type Tops: %s",TTstr);
// HostTraceOutputTL(appErrorClass, "Type Tops: %s",DblToStr(suaidx->topstype,0));
// HostTraceOutputTL(appErrorClass, "Type Base: %s",BTstr);
// HostTraceOutputTL(appErrorClass, "Type Base: %s",DblToStr(suaidx->basetype,0));
// Alert Pilot
// HostTraceOutputTL(appErrorClass, "Title: %s",suaidx->title);
// HostTraceOutputTL(appErrorClass, "Type: %s",TypeString);
// HostTraceOutputTL(appErrorClass, "Tops: %s",DblToStr(suaidx->tops,0));
// HostTraceOutputTL(appErrorClass, "Base: %s",DblToStr(suaidx->base,0));
// popup warning only if greater priority than any current alert on screen
if (NewWarnType >= curformpriority) {
// Build warning message
// log time warned
warning_time = cursecs;
// use warning type as priority for genalert form
suaalert->alerttype = SUAWARNING_ALERT;
suaalert->priority = suaidx->WarnType;
// Buttons
suaalert->numbtns = 3;
// StrCopy(suaalert->btn0text, "DISMISS");
StrCopy(suaalert->btn0text, "5 Mins");
StrCopy(suaalert->btn1text, "1 Hour");
StrCopy(suaalert->btn2text, "Today");
// Text
//StrCopy(suaalert->displaytext, trim(WarnString, ' ', true));
StrCopy(suaalert->displaytext, WarnString);
StrCat(suaalert->displaytext, "\n");
StrCat(suaalert->displaytext, "\n");
StrCat(suaalert->displaytext, suaidx->title);
StrCat(suaalert->displaytext, "\nClass: ");
if (StrCompare(ClassString, " ") != 0) {
StrCat(suaalert->displaytext, ClassString);
StrCat(suaalert->displaytext, " / ");
}
StrCat(suaalert->displaytext, TypeString);
StrCat(suaalert->displaytext, "\nTops: ");
StrCat(suaalert->displaytext, TTstr);
StrCat(suaalert->displaytext, "\nBase: ");
StrCat(suaalert->displaytext, BTstr);
StrCat(suaalert->displaytext, "\nRadio: ");
StrCat(suaalert->displaytext, suaidx->radiofreq);
// store SUA element idx with warning
suaalert->alertidx = SUAelement;
// I put this here for now. It's also inside of the
// window event frmOpen are for the alert window.
// This is to allow you to tell when there is a response
// that needs to be processed.
// Would need to handle any return/response from the
// Alert window prior to this.
suaalertret->valid = false;
suaalertret->btnselected = -1;
// decide if warning is urgent or not.
allowgenalerttap = true; // allow tapping anywhere in the alert window to dimiss
if (rewarn) {
// HostTraceOutputTL(appErrorClass, "URGENT: %hd", suaalert->alertidx);
// HostTraceOutputTL(appErrorClass, "URGENT: %hd", suaidx->WarnType);
// PlayNoGPSSound();
StrCopy(suaalert->title, "URGENT Airspace Alert");
// StrCopy(suaalert->title, trim(WarnString, ' ', true));
HandleWaitDialogWin(1);
} else {
// HostTraceOutputTL(appErrorClass, "Warning: %hd", suaalert->alertidx);
// HostTraceOutputTL(appErrorClass, "Warning: %hd", suaidx->WarnType);
// PlayWarnSound();
StrCopy(suaalert->title, "Airspace Warning");
// StrCopy(suaalert->title, trim(WarnString, ' ', true));
HandleWaitDialogWin(1);
}
}
// stops repeat warnings on the same SUA item
suaidx->Dismissed = true;
} else { // low priority, don't display a warning
// HostTraceOutputTL(appErrorClass, "Lower Priority Alert");
// still update latest warn type if not an approach warning
if ((NewWarnType != SUAWARN_APPROACHING) && (cursecs > suaidx->LastWarned)) {
suaidx->WarnType = NewWarnType;
suaidx->Dismissed = true;
}
}
}
return;
}
void WarnClear()
// clear the warning from an suaidx