-
Notifications
You must be signed in to change notification settings - Fork 53
/
navparse.cc
3180 lines (2714 loc) · 118 KB
/
navparse.cc
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 <stdio.h>
#include <string>
#include <iostream>
#include <arpa/inet.h>
#include "fmt/format.h"
#include "fmt/printf.h"
#include <fstream>
#include <map>
#include <bitset>
#include <vector>
#include <thread>
#include <signal.h>
#include <mutex>
#include "ext/powerblog/h2o-pp.hh"
#include "minicurl.hh"
#include <time.h>
#include "ubx.hh"
#include "bits.hh"
#include "minivec.hh"
#include "navmon.pb.h"
#include "ephemeris.hh"
#include "gps.hh"
#include "glonass.hh"
#include "beidou.hh"
#include "galileo.hh"
#include "tle.hh"
#include <optional>
#include "navmon.hh"
#include <Tle.h>
#include "navparse.hh"
#include <fenv.h>
#include "influxpush.hh"
#include "sbas.hh"
#include <sys/resource.h>
#include "CLI/CLI.hpp"
#include "gpscnav.hh"
#include "rtcm.hh"
#include "version.hh"
//#include "nequick.hh"
static char program[]="navparse";
using namespace std;
extern const char* g_gitHash;
struct ObserverFacts
{
Point pos;
double groundSpeed{-1};
double accuracy{-1.0};
string serialno;
string hwversion;
string swversion;
string mods;
string vendor;
double clockOffsetNS{-1};
double clockOffsetDriftNS{-1};
double clockAccuracyNS{-1};
double freqAccuracyPS{-1};
time_t uptime;
string githash;
string owner;
string remark;
bool impinav{false};
time_t impinavTime{0};
time_t lastSeen{0};
};
std::map<int, ObserverFacts> g_srcfacts;
struct SBASAndReceiverStatus
{
SBASState status;
struct PerRecv
{
time_t last_seen{0};
};
map<int, PerRecv> perrecv;
};
typedef map<int, SBASAndReceiverStatus> sbas_t;
sbas_t g_sbas;
GetterSetter<sbas_t> g_sbaskeeper;
map<int, BeidouAlmanacEntry> g_beidoualma;
map<int, pair<GlonassMessage, GlonassMessage>> g_glonassalma;
map<int, GalileoMessage::Almanac> g_galileoalma;
map<int, GPSAlmanac> g_gpsalma;
GetterSetter<map<int, BeidouAlmanacEntry>> g_beidoualmakeeper;
GetterSetter<map<int, pair<GlonassMessage, GlonassMessage>>> g_glonassalmakeeper;
GetterSetter<map<int, GalileoMessage::Almanac>> g_galileoalmakeeper;
GetterSetter<map<int, GPSAlmanac>> g_gpsalmakeeper;
TLERepo g_tles;
struct GNSSReceiver
{
Point position;
};
double g_GSTUTCOffset, g_GSTGPSOffset, g_GPSUTCOffset, g_BeiDouUTCOffset, g_GlonassUTCOffset, g_GlonassGPSOffset;
/*
The situation. We have a single ephemeris function that is able to operate on
Galileo, GPS1 and Beidou structs. It does so using functions like getT0e().
This ephemeris function also does speed and doppler.
We have structs for all four GNSS.
All GNSS have ephemerides that are spread out over multiple messages.
Because of that, we have to do some kind of atomic update.
We also have an SVStat that has some knowledge about IODs.
It would be great is we could ask the svstat about "the latest ephemeris"
and get a useful answer.
More concretely, we could ask the svstat about the latest *position* or *speed*.
It would be easier to do that. There is no unified "ephemeris object" between all the GNSS.
We do need a unified "do we have a complete ephemeris method".
For Galileo this is messages 1, 2, 3 and 4
For GPS it is 2 and 3
For BeiDou it is message 2 and message 3 in succession (?)
sow is in each message, forms a link
For GLONASS I don't really know
We also care about ephemeris discontinuities.
Idea is that SVStat has four structs around, one for each GNSS, each containing "the lastest complete ephemeris".
ephglomsg etc
It also has infrastructure for storing the ephemerides as they are being assembled.
Open question:
*/
// XXX conversion glonass??
int SVStat::wn() const
{
if(gnss == 0)
return gpsmsg.wn;
else if(gnss == 2)
return galmsg.wn;
else if(gnss == 3)
return beidoumsg.wn;
else if(gnss == 6) {
uint32_t glotime = glonassMessage.getGloTime(); // this starts GLONASS time at 31st of december 1995, 00:00 UTC
return glotime / (7*86400);
}
return 0;
}
int SVStat::tow() const
{
if(gnss == 0)
return gpsmsg.tow;
else if(gnss == 2)
return galmsg.tow;
else if(gnss == 3)
return beidoumsg.sow;
else if(gnss == 6) {
uint32_t glotime = glonassMessage.getGloTime(); // this starts GLONASS time at 31st of december 1995, 00:00 UTC
return glotime % (7*86400);
}
return 0;
}
const GPSLikeEphemeris& SVStat::liveIOD() const
{
if(gnss == 0)
return ephgpsmsg;
else if(gnss == 2)
return ephgalmsg;
else if(gnss == 3)
return ephBeidoumsg;
throw std::runtime_error("Asked for GPS like ephemeris for gnss " + to_string(gnss));
}
const GPSLikeEphemeris& SVStat::prevIOD() const
{
if(gnss == 0)
return oldephgpsmsg;
else if(gnss == 2)
return oldephgalmsg;
else if(gnss == 3)
return oldephBeidoumsg;
throw std::runtime_error("Asked for old-GPS like ephemeris for gnss " + to_string(gnss));
}
bool SVStat::completeIOD() const
{
if(gnss == 6)
return false;
// yeah now what
return true;
}
double SVStat::getCoordinates(double tow, Point* p, bool quiet) const
{
if(gnss == 6)
return ::getCoordinates(tow, ephglomsg, p);
// getCoordinates needs to be overloaded for GLONASS
return ::getCoordinates(tow, liveIOD(), p, quiet);
}
double SVStat::getOldEphCoordinates(double tow, Point* p, bool quiet) const
{
if(gnss == 6)
return ::getCoordinates(tow, oldephglomsg, p);
// getCoordinates needs to be overloaded for GLONASS
return ::getCoordinates(tow, prevIOD(), p, quiet);
}
void SVStat::getSpeed(double tow, Vector* v) const
{
return ::getSpeed(tow, liveIOD(), v);
}
DopplerData SVStat::doDoppler(double tow, const Point& us, double freq) const
{
if(gnss == 6)
return ::doDoppler(tow, us, ephglomsg, freq);
else
return ::doDoppler(tow, us, liveIOD(), freq);
}
double satUTCTime(const SatID& id);
void SVStat::reportNewEphemeris(const SatID& id, InfluxPusher& idb)
{
int ephage;
if(gnss==6)
ephage = ephAge(ephglomsg.getT0e(), oldephglomsg.getT0e());
else
ephage = ephAge(liveIOD().getT0e(), prevIOD().getT0e());
Point p, oldp;
getCoordinates(tow(), &p);
getOldEphCoordinates(tow(), &oldp);
double hours = ephage / 3600;
double disco = Vector(p, oldp).length();
// cout<<id.first<<","<<id.second<<" discontinuity after "<< hours<<" hours: "<< disco <<endl;
if(hours < 4) {
latestDisco= disco;
latestDiscoAge= ephage;
}
else
latestDisco= -1;
if(gnss==0) { // GPS
const auto& eg = ephgpsmsg;
idb.addValue(id, "ephemeris-actual", {
{"iod", eg.getIOD()},
{"t0e", eg.t0e},
{"sqrta", eg.sqrtA},
{"e", eg.e},
{"cuc", eg.cuc},
{"cus", eg.cus},
{"crc", eg.crc},
{"crs", eg.crs},
{"m0", eg.m0},
{"deltan", eg.deltan},
{"i0", eg.i0},
{"cic", eg.cic},
{"cis", eg.cis},
{"omegadot", eg.omegadot},
{"omega0", eg.omega0},
{"idot", eg.idot},
{"af0", eg.af0},
{"af1", eg.af1},
{"af2", eg.af2},
{"t0c", eg.t0c},
{"omega", eg.omega}}, satUTCTime(id));
}
if(gnss==2) {
const auto& eg = ephgalmsg;
idb.addValue(id, "ephemeris-actual", {
{"iod", eg.getIOD()},
{"t0e", eg.t0e},
{"sqrta", eg.sqrtA},
{"e", eg.e},
{"cuc", eg.cuc},
{"cus", eg.cus},
{"crc", eg.crc},
{"crs", eg.crs},
{"m0", eg.m0},
{"deltan", eg.deltan},
{"i0", eg.i0},
{"cic", eg.cic},
{"cis", eg.cis},
{"omegadot", eg.omegadot},
{"omega0", eg.omega0},
{"idot", eg.idot},
{"af0", eg.af0},
{"af1", eg.af1},
{"af2", eg.af2},
{"t0c", eg.t0c},
{"omega", eg.omega}}, satUTCTime(id));
}
if(hours < 24) {
idb.addValue(id, "eph-disco",
{{"meters", disco},
{"seconds", hours*3600.0},
{"oldx", oldp.x},
{"oldy", oldp.y},
{"oldz", oldp.z},
{"x", p.x},
{"y", p.y},
{"z", p.z},
{"iod", gnss == 6 ? -1.0 : 1.0*liveIOD().getIOD()},
{"oldiod", gnss== 6 ? -1.0 : 1.0*prevIOD().getIOD()}}, satUTCTime(id));
}
}
svstats_t g_svstats;
GetterSetter<svstats_t> g_statskeeper;
int latestWN(int gnssid, const svstats_t& stats)
{
map<int, SatID> ages;
for(const auto& s: stats)
if(s.first.gnss == (unsigned int)gnssid)
ages[7*s.second.wn()*86400 + s.second.tow()]= s.first;
if(ages.empty())
throw runtime_error("Asked for latest WN for "+to_string(gnssid)+": we don't know it yet ("+to_string(stats.size())+")");
return stats.find(ages.rbegin()->second)->second.wn();
}
int latestTow(int gnssid, const svstats_t& stats)
{
map<int, SatID> ages;
for(const auto& s: stats)
if(s.first.gnss == (unsigned int) gnssid)
ages[7*s.second.wn()*86400 + s.second.tow()]= s.first;
if(ages.empty())
throw runtime_error("Asked for latest TOW for "+to_string(gnssid)+": we don't know it yet ("+to_string(stats.size())+")");
return stats.find(ages.rbegin()->second)->second.tow();
}
// nanoseconds posix time from that gnss WN and TOW
int64_t nanoTime(int gnssid, int wn, double tow)
{
int offset;
if(gnssid == 0) // GPS
offset = 315964800;
if(gnssid == 2) // Galileo, 22-08-1999
offset = 935280000;
if(gnssid == 3) {// Beidou, 01-01-2006 - I think leap seconds count differently in Beidou!! XXX
offset = 1136073600;
return 1000000000ULL*(offset + wn * 7*86400 + tow - g_dtLSBeidou);
}
if(gnssid == 6) { // GLONASS
offset = 820368000;
return 1000000000ULL*(offset + wn * 7*86400 + tow); // no leap seconds in glonass
}
return 1000000000ULL*(offset + wn * 7*86400 + tow - g_dtLS);
}
// same in seconds
double satUTCTime(const SatID& id)
{
return nanoTime(id.gnss, g_svstats[id].wn(), g_svstats[id].tow())/1000000000.0;
}
/* The GST start epoch is defined as 13 seconds before midnight between 21st August and
22nd August 1999, i.e. GST was equal to 13 seconds at 22nd August 1999 00:00:00 UTC. */
std::string humanTime(int gnssid, int wn, int tow)
{
time_t t = nanoTime(gnssid, wn, tow)/1000000000;
struct tm tm;
gmtime_r(&t, &tm);
char buffer[80];
strftime(buffer, sizeof(buffer), "%a, %d %b %Y %T %z", &tm);
return buffer;
}
std::optional<double> getHzCorrection(time_t now, int src, unsigned int gnssid, unsigned int sigid, const svstats_t& svstats)
{
std::optional<double> allHzCorr;
double alltot=0;
int allcount=0;
// cout<<"getHzCorrection called for src "<<src<<" gnss "<<gnssid <<" sigid "<< sigid <<endl;
for(const auto& s: svstats) {
if(s.first.gnss != gnssid)
continue;
if(s.first.sigid != sigid)
continue;
if(auto iter = s.second.perrecv.find(src); iter != s.second.perrecv.end() && now - iter->second.deltaHzTime < 60) {
// cout<<" Found entry for SV "<<s.first.gnss<<","<<s.first.sv<<","<<s.first.sigid<<" from src "<<iter->first<<", deltaHz: "<<iter->second.deltaHz<< " age " << now - iter->second.deltaHzTime<<" db "<<iter->second.db<<endl;
alltot+=iter->second.deltaHz;
allcount++;
}
}
if(allcount > 3) {
allHzCorr = alltot/allcount;
// cout<<"Returning "<<*allHzCorr<<endl;
}
else
; // cout<<"Not enough data"<<endl;
return allHzCorr;
}
std::string humanBhs(int bhs)
{
static vector<string> options{"ok", "OUT", "will be out of service", "test"};
if(bhs >= (int)options.size()) {
cerr<<"Asked for humanBHS "<<bhs<<endl;
return "??";
}
return options.at(bhs);
}
void addHeaders(h2o_req_t* req)
{
h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CACHE_CONTROL,
NULL, H2O_STRLIT("max-age=3"));
// Access-Control-Allow-Origin
h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_ACCESS_CONTROL_ALLOW_ORIGIN,
NULL, H2O_STRLIT("*"));
}
time_t getSatelliteUTC(svstats_t& svstats)
{
time_t ret;
try {
ret=utcFromGST(latestWN(2, svstats), latestTow(2, svstats));
}
catch(...)
{
ret=utcFromGPS(latestWN(0, svstats), latestTow(0, svstats));
// if this throws, we are done
}
return ret;
}
static double get_cpu_seconds_total()
{
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
return usage.ru_utime.tv_sec + usage.ru_utime.tv_usec/1000000.0 + usage.ru_stime.tv_sec + usage.ru_stime.tv_usec/1000000.0;
}
void storeSelfStats(InfluxPusher& idb, time_t t)
{
map<int, int> receivers;
time_t now;
try {
now=getSatelliteUTC(g_svstats);
}
catch(...) {
return;
}
map<SatID, int> svcount, sigcount;
map<int, int> siggnsscount, svgnsscount;
for(const auto& sv : g_svstats) {
bool fresh=false;
for(const auto& pr : sv.second.perrecv) {
int age = now - pr.second.t;
if(age < 30) {
fresh= true;
receivers[pr.first]++;
}
}
if(fresh) {
sigcount[sv.first]++;
siggnsscount[sv.first.gnss]++;
SatID id = sv.first;
id.sigid=0;
svcount[id]++;
}
}
for(const auto& sv : svcount)
svgnsscount[sv.first.gnss]++;
vector<pair<string,InfluxPusher::var_t>> tags;
idb.addValue(tags, "self", {
{"measurements", idb.d_nummsmts},
{"dedup-msmts", idb.d_numdedupmsmts},
{"values", idb.d_numvalues},
{"total-live-svs", (int64_t)svcount.size()},
{"total-live-signals", (int64_t)sigcount.size()},
{"gps-svs", svgnsscount[0]},
{"galileo-svs", svgnsscount[2]},
{"beidou-svs",svgnsscount[3]},
{"glonass-svs",svgnsscount[6]},
{"gps-sigs", siggnsscount[0]},
{"galileo-sigs", siggnsscount[2]},
{"beidou-sigs", siggnsscount[3]},
{"glonass-sigs", siggnsscount[6]},
{"total-cpu-msec", 1000.0*get_cpu_seconds_total()},
{"total-live-receivers", (int64_t)receivers.size()}
}, t);
for(const auto& p : idb.d_msmtmap) {
idb.addValue({{"metric", p.first}}, "msmts", {{"value", p.second}}, t);
}
}
// GALILEO ONLY FOR NOW
void storeCoverageStats(InfluxPusher& idb, time_t t)
try {
int tow;
tow=latestTow(2, g_svstats);
vector<Point> sats;
for(const auto &g : g_galileoalma) {
Point sat;
getCoordinates(tow, g.second, &sat);
if(g.first < 0)
continue;
SatID id{2,(uint32_t)g.first,1};
const auto& svstat = g_svstats[id];
if(svstat.completeIOD() && svstat.galmsg.sisa == 255) {
continue;
}
if(svstat.galmsg.e1bhs || svstat.galmsg.e1bdvs) {
continue;
}
sats.push_back(sat);
}
vector<SatID> aux{{2, 14, 1}, {2,18,1}};
for(const auto& id : aux) {
if(!g_svstats.count(id))
continue;
const auto& svstat = g_svstats[id];
if(svstat.completeIOD() && svstat.galmsg.sisa == 255) {
continue;
}
if(svstat.galmsg.e1bhs || svstat.galmsg.e1bdvs) {
continue;
}
Point sat;
getCoordinates(tow, svstat.galmsg, &sat);
sats.push_back(sat);
}
// cout<<endl;
auto cov = emitCoverage(sats);
int cells=0;
int pdopexceeds5=0, pdopexceeds10=0, pdopexceeds20=0;
int covlow5=0, covlow10=0, covlow20=0;
for(const auto& latvect : cov) {
for(const auto& longpair : latvect.second) {
cells++;
if(get<4>(longpair) >= 6.0 || get<4>(longpair) < 0.0 )
pdopexceeds5++;
if(get<5>(longpair) >= 6.0 || get<5>(longpair) < 0.0 )
pdopexceeds10++;
if(get<6>(longpair) >= 6.0 || get<6>(longpair) < 0.0 )
pdopexceeds20++;
if(get<1>(longpair) < 4)
covlow5++;
if(get<2>(longpair) < 4)
covlow10++;
if(get<3>(longpair) < 4)
covlow20++;
// else
//cout<<get<6>(longpair) << endl;
}
}
/*
fmt::printf("At %s, %.2f%% (%d) of %d cells exceeded PDOP 6 for 5 degrees horizon (%d sats)\n", humanTime(t), 100.0*pdopexceeds5/cells, pdopexceeds5, cells, sats.size());
fmt::printf("At %s, %.2f%% (%d) of %d cells exceeded PDOP 6 for 10 degrees horizon (%d sats)\n", humanTime(t), 100.0*pdopexceeds10/cells, pdopexceeds10, cells, sats.size());
fmt::printf("At %s, %.2f%% (%d) of %d cells exceeded PDOP 6 for 20 degrees horizon (%d sats)\n", humanTime(t), 100.0*pdopexceeds20/cells, pdopexceeds20, cells, sats.size());
fmt::printf("At %s, %.2f%% (%d) of %d cells have less than 4 sats in view for 5 degrees horizon (%d sats)\n", humanTime(t), 100.0*covlow5/cells, covlow5, cells, sats.size());
fmt::printf("At %s, %.2f%% (%d) of %d cells have less than 4 sats in view for 10 degrees horizon (%d sats)\n", humanTime(t), 100.0*covlow10/cells, covlow10, cells, sats.size());
fmt::printf("At %s, %.2f%% (%d) of %d cells have less than 4 sats in view for 20 degrees horizon (%d sats)\n", humanTime(t), 100.0*covlow20/cells, covlow20, cells, sats.size());
*/
idb.addValue({{"sigid", 1},{"gnss", 2}}, "quality", {
{"pdop5perc", 100.0*pdopexceeds5/cells},
{"pdop10perc", 100.0*pdopexceeds10/cells},
{"pdop20perc", 100.0*pdopexceeds20/cells},
{"covlow5", 100.0*covlow5/cells},
{"covlow10", 100.0*covlow10/cells},
{"covlow20", 100.0*covlow20/cells},
{"sats", (int)sats.size()}
}, t);
}
catch(std::exception&e) {
cout<<"Error with coverage: "<<e.what()<<endl;
}
int main(int argc, char** argv)
try
{
bool doVERSION{false};
CLI::App app(program);
string localAddress("127.0.0.1:29599");
string htmlDir("./html");
string influxDBName("null");
bool doGalileoReportSpeedup{false};
bool doLogRFData{false};
app.add_flag("--version", doVERSION, "show program version and copyright");
app.add_flag("--log-rf-data", doLogRFData, "store per station RF/correlator data");
app.add_flag("--gal-report-speedup", doGalileoReportSpeedup, "skip debugging data, glonass, beidou, SBAS");
app.add_option("--bind,-b", localAddress, "Address to bind to");
app.add_option("--html", htmlDir, "Where to source the HTML & JavaScript");
app.add_option("--influxdb", influxDBName, "Name of influxdb database");
try {
app.parse(argc, argv);
} catch(const CLI::Error &e) {
return app.exit(e);
}
if(doVERSION) {
showVersion(program, g_gitHash);
exit(0);
}
// feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW );
// g_tles.parseFile("active.txt");
g_tles.parseFile("galileo.txt");
g_tles.parseFile("glo-ops.txt");
g_tles.parseFile("gps-ops.txt");
g_tles.parseFile("beidou.txt");
signal(SIGPIPE, SIG_IGN);
InfluxPusher idb(influxDBName);
MiniCurl::init();
H2OWebserver h2s("galmon");
h2s.addHandler("/global.json", [](auto handler, auto req) {
addHeaders(req);
nlohmann::json ret = nlohmann::json::object();
auto svstats = g_statskeeper.get();
ret["leap-seconds"] = g_dtLS;
try {
// if we haven't seen galileo, we have no idea
ret["last-seen"]=utcFromGST(latestWN(2, svstats), latestTow(2, svstats));
}
catch(...)
{
try {
ret["last-seen"]=utcFromGPS(latestWN(0, svstats), latestTow(0, svstats));
}
catch(...)
{}
}
ret["gst-utc-offset-ns"] = g_GSTUTCOffset;
ret["gst-gps-offset-ns"] = g_GSTGPSOffset;
ret["gps-utc-offset-ns"] = g_GPSUTCOffset;
ret["beidou-utc-offset-ns"] = g_BeiDouUTCOffset;
ret["glonass-utc-offset-ns"] = g_GlonassUTCOffset;
ret["glonass-gps-offset-ns"] = g_GlonassGPSOffset;
map<int, int> siggnsscount, svgnsscount;
map<SatID, int> svcount, sigcount;
map<int, int> receivers;
time_t now=time(0);
for(const auto& sv : svstats) {
bool fresh=false;
for(const auto& pr : sv.second.perrecv) {
int age = now - pr.second.t;
if(age < 30) {
fresh= true;
receivers[pr.first]++;
}
}
if(fresh) {
sigcount[sv.first]++;
siggnsscount[sv.first.gnss]++;
SatID id = sv.first;
id.sigid=0;
svcount[id]++;
}
}
for(const auto& sv : svcount)
svgnsscount[sv.first.gnss]++;
ret["total-live-receivers"] = receivers.size();
ret["total-live-svs"] = svcount.size();
ret["total-live-signals"] = sigcount.size();
ret["gps-svs"] = svgnsscount[0];
ret["galileo-svs"] = svgnsscount[2];
ret["beidou-svs"] = svgnsscount[3];
ret["glonass-svs"] = svgnsscount[6];
ret["gps-sigs"] = siggnsscount[0];
ret["galileo-sigs"] = siggnsscount[2];
ret["beidou-sigs"] = siggnsscount[3];
ret["glonass-sigs"] = siggnsscount[6];
return ret;
});
h2s.addHandler("/almanac.json", [](auto handler, auto req) {
addHeaders(req);
auto beidoualma = g_beidoualmakeeper.get();
auto svstats = g_statskeeper.get();
nlohmann::json ret = nlohmann::json::object();
for(const auto& ae : beidoualma) {
nlohmann::json item = nlohmann::json::object();
item["gnssid"]=3;
if(ae.second.alma.getT0e() > 7*86400)
continue;
Point sat;
getCoordinates(latestTow(3, svstats), ae.second.alma, &sat);
item["eph-ecefX"]= sat.x/1000;
item["eph-ecefY"]= sat.y/1000;
item["eph-ecefZ"]= sat.z/1000;
auto longlat = getLongLat(sat.x, sat.y, sat.z);
item["eph-longitude"] = 180*longlat.first/M_PI;
item["eph-latitude"]= 180*longlat.second/M_PI;
item["t0e"] = ae.second.alma.getT0e();
item["t"]= ephAge(ae.second.alma.getT0e(), latestTow(3, svstats))/86400.0;
item["inclination"] = 180 * ae.second.alma.getI0() /M_PI;
item["observed"]=false;
if(auto iter = svstats.find({3, (uint32_t)ae.first, 0}); iter != svstats.end()) {
if(time(0) - nanoTime(3, iter->second.wn(), iter->second.tow())/1000000000 < 300)
item["observed"] = true;
}
if(ephAge(ae.second.alma.getT0e(), latestTow(3, svstats)) < 0) {
auto match = g_tles.getBestMatch(nanoTime(3, latestWN(3, svstats), latestTow(3, svstats))/1000000000.0,
sat.x, sat.y, sat.z);
if(match.distance < 200000) {
item["best-tle"] = match.name;
item["best-tle-norad"] = match.norad;
item["best-tle-int-desig"] = match.internat;
item["best-tle-dist"] = match.distance/1000.0;
item["tle-ecefX"] = match.ecefX/1000;
item["tle-ecefY"] = match.ecefY/1000;
item["tle-ecefZ"] = match.ecefZ/1000;
item["tle-eciX"] = match.eciX/1000;
item["tle-eciY"] = match.eciY/1000;
item["tle-eciZ"] = match.eciZ/1000;
item["tle-latitude"] = 180*match.latitude/M_PI;
item["tle-longitude"] = 180*match.longitude/M_PI;
item["tle-altitude"] = match.altitude;
}
}
auto name = fmt::sprintf("C%02d", ae.first);
item["name"]=name;
ret[name] = item;
}
auto glonassalma = g_glonassalmakeeper.get();
for(const auto& ae : glonassalma) {
nlohmann::json item = nlohmann::json::object();
// ae.second.first -> even ae.second.sceond -> odd
item["gnssid"]=6;
item["e"] = ae.second.first.getE();
item["inclination"] = 180 * ae.second.first.getI0() /M_PI;
item["health"] = ae.second.first.CnA;
item["tlambdana"] = ae.second.second.gettLambdaNa();
item["lambdana"] = ae.second.second.getLambdaNaDeg();
item["hna"] = ae.second.second.hna;
item["observed"] = false;
for(uint32_t sigid : {0,1,2}) { // XXX SIGIDS
if(auto iter = svstats.find({6, (uint32_t)ae.first, sigid}); iter != svstats.end()) {
if(time(0) - nanoTime(6, iter->second.wn(), iter->second.tow())/1000000000 < 300) {
item["observed"] = true;
auto longlat = getLongLat(iter->second.glonassMessage.x, iter->second.glonassMessage.y, iter->second.glonassMessage.z);
item["eph-longitude"] = 180*longlat.first/M_PI;
item["eph-latitude"]= 180*longlat.second/M_PI;
break;
}
}
}
auto name = fmt::sprintf("R%02d", ae.first);
item["name"]=name;
ret[name] = item;
}
auto galileoalma = g_galileoalmakeeper.get();
for(const auto& ae : galileoalma) {
nlohmann::json item = nlohmann::json::object();
item["gnssid"]=2;
item["e"] = ae.second.getE();
item["e1bhs"] = ae.second.e1bhs;
item["e5bhs"] = ae.second.e5bhs;
item["t0e"] = ae.second.getT0e();
item["t"]= ephAge(ae.second.getT0e(), latestTow(2, svstats))/86400.0;
item["eph-age"] = ephAge(latestTow(2, svstats), ae.second.getT0e());
item["i0"] = 180.0 * ae.second.getI0()/ M_PI;
item["inclination"] = 180 * ae.second.getI0() /M_PI;
item["omega"] = ae.second.getOmega();
item["sqrtA"]= ae.second.getSqrtA();
item["M0"] = ae.second.getM0();
item["delta-n"] = ae.second.getDeltan();
item["omega-dot"] = ae.second.getOmegadot();
item["omega0"] = ae.second.getOmega0();
item["idot"] = ae.second.getIdot();
item["t0e"] = ae.second.getT0e();
Point sat;
double E=getCoordinates(latestTow(2, svstats), ae.second, &sat);
item["E"]=E;
item["eph-ecefX"]= sat.x/1000;
item["eph-ecefY"]= sat.y/1000;
item["eph-ecefZ"]= sat.z/1000;
auto longlat = getLongLat(sat.x, sat.y, sat.z);
item["eph-longitude"] = 180*longlat.first/M_PI;
item["eph-latitude"]= 180*longlat.second/M_PI;
item["observed"] = false;
for(uint32_t sigid : {0,1,5}) {
if(auto iter = svstats.find({2, (uint32_t)ae.first, sigid}); iter != svstats.end()) {
if(iter->second.completeIOD()) {
item["sisa"] = iter->second.galmsg.sisa;
}
// if we hit an 'observed', stop trying sigids
if(time(0) - nanoTime(2, iter->second.wn(), iter->second.tow())/1000000000 < 300) {
item["observed"] = true;
break;
}
}
}
auto match = g_tles.getBestMatch(nanoTime(2, latestWN(2, svstats), latestTow(2, svstats))/1000000000.0,
sat.x, sat.y, sat.z);
if(match.distance < 200000) {
item["best-tle"] = match.name;
item["best-tle-norad"] = match.norad;
item["best-tle-int-desig"] = match.internat;
item["best-tle-dist"] = match.distance/1000.0;
item["tle-ecefX"] = match.ecefX/1000;
item["tle-ecefY"] = match.ecefY/1000;
item["tle-ecefZ"] = match.ecefZ/1000;
item["tle-eciX"] = match.eciX/1000;
item["tle-eciY"] = match.eciY/1000;
item["tle-eciZ"] = match.eciZ/1000;
item["tle-latitude"] = 180*match.latitude/M_PI;
item["tle-longitude"] = 180*match.longitude/M_PI;
item["tle-altitude"] = match.altitude;
}
auto name = fmt::sprintf("E%02d", ae.first);
item["name"]= name;
ret[name] = item;
}
auto gpsalma = g_gpsalmakeeper.get();
for(const auto& ae : gpsalma) {
nlohmann::json item = nlohmann::json::object();
item["gnssid"]=0;
item["e"] = ae.second.getE();
item["health"] = ae.second.health;
item["t0e"] = ae.second.getT0e();
item["t"]= ephAge(ae.second.getT0e(), latestTow(0, svstats))/86400.0;
item["eph-age"] = ephAge(latestTow(0, svstats), ae.second.getT0e());
item["i0"] = 180.0 * ae.second.getI0()/ M_PI;
item["inclination"] = 180 * ae.second.getI0() /M_PI;
Point sat;
getCoordinates(latestTow(0, svstats), ae.second, &sat);
item["eph-ecefX"]= sat.x/1000;
item["eph-ecefY"]= sat.y/1000;
item["eph-ecefZ"]= sat.z/1000;
auto longlat = getLongLat(sat.x, sat.y, sat.z);
item["eph-longitude"] = 180*longlat.first/M_PI;
item["eph-latitude"]= 180*longlat.second/M_PI;
item["observed"] = false;
for(uint32_t sigid : {0,1,4}) {
if(auto iter = svstats.find({0, (uint32_t)ae.first, sigid}); iter != svstats.end()) {
if(time(0) - nanoTime(0, iter->second.wn(), iter->second.tow())/1000000000 < 300)
item["observed"] = true;
}
}
auto match = g_tles.getBestMatch(nanoTime(0, latestWN(0, svstats), latestTow(0, svstats))/1000000000.0,
sat.x, sat.y, sat.z);
if(match.distance < 200000) {
item["best-tle"] = match.name;
item["best-tle-norad"] = match.norad;
item["best-tle-int-desig"] = match.internat;
item["best-tle-dist"] = match.distance/1000.0;
item["tle-ecefX"] = match.ecefX/1000;
item["tle-ecefY"] = match.ecefY/1000;
item["tle-ecefZ"] = match.ecefZ/1000;
item["tle-eciX"] = match.eciX/1000;
item["tle-eciY"] = match.eciY/1000;
item["tle-eciZ"] = match.eciZ/1000;
item["tle-latitude"] = 180*match.latitude/M_PI;
item["tle-longitude"] = 180*match.longitude/M_PI;
item["tle-altitude"] = match.altitude;
}
auto name = fmt::sprintf("G%02d", ae.first);
item["name"]=name;
ret[name] = item;
}
return ret;
});
h2s.addHandler("/observers.json", [](auto handler, auto req) {
addHeaders(req);
nlohmann::json ret = nlohmann::json::array();
for(const auto& src : g_srcfacts) {
nlohmann::json obj;
obj["id"] = src.first;
auto latlonh = ecefToWGS84(src.second.pos.x, src.second.pos.y, src.second.pos.z);
get<0>(latlonh) *= 180.0/M_PI;
get<1>(latlonh) *= 180.0/M_PI;
get<0>(latlonh) = ((int)(10*get<0>(latlonh)))/10.0;
get<1>(latlonh) = ((int)(10*get<1>(latlonh)))/10.0;
get<2>(latlonh) = ((int)(10*get<2>(latlonh)))/10.0;
obj["latitude"] = get<0>(latlonh);
obj["longitude"] = get<1>(latlonh);
obj["last-seen"] = src.second.lastSeen;
obj["ground-speed"] = src.second.groundSpeed;
obj["swversion"] = src.second.swversion;
obj["hwversion"] = src.second.hwversion;
obj["mods"] = src.second.mods;