forked from acasadoalonso/SWiface
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparserfuncs.py
More file actions
executable file
·806 lines (725 loc) · 29.6 KB
/
parserfuncs.py
File metadata and controls
executable file
·806 lines (725 loc) · 29.6 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
#!/usr/bin/python3
#
# Parser functions for the OGN APRS applications
#
import urllib.request
import urllib.error
import urllib.parse
import json
import os
import sys
import atexit
import socket
import airportsdata
from urllib.error import HTTPError
from suntime import Sun, SunTimeException
from ogn.parser import parse
from datetime import datetime, timezone
from dtfuncs import naive_utcnow, naive_utcfromtimestamp
# --------------------------------------------------------------------------
aprssources = { # sources based on the APRS TOCALL
"APRS" : "OGN", # old tocall
"OGNSDR": "OGN", # station Sofware defined radio
"OGFLR" : "OGN", # Flarm
"OGNFLR": "OGN", # Flarm
"OGFLR6": "OGN", # Flarm
"OGFLR7": "OGN", # Flarm
"OGNTRK": "OGN", # OGN Tracker
"OGNDSX": "OGN", # old DSX
"OGNDVS": "WTX", # Weather stations
"APYSNR": "WTX", # Weather stations
"OGNTTN": "TTN", # the things LoRaWan network
"OGTTN2": "TTN", # the things LoRaWan network V2 - deprecated
"OGTTN3": "TTN", # the things LoRaWan network V3 - cumunity edition
"OGNHEL": "HELI", # helium LoRaWan
"OGOBS" : "OBS", # OBS LoRaWan
"OGADSB": "ADSB", # ADSB
"ONADSB": "ADSB", # ADSB
"OGADSL": "ADSL", # ADS-L
"OGNFNT": "FANE", # FANET
"OGFNT" : "FANE", # FANET
"OGNPAW": "PAW", # PilotAware
"OGPAW" : "PAW", # PilotAware
"OGSPOT": "SPOT", # SPOT
"OGINRE": "INREACH", # Garmin InReach
"OGFLYM": "FLYM", # FlyMaster
"OGSPID": "SPID", # spider
"OGSKYL": "SKYL", # Sky lines (XCsoar)
"OGLT24": "LT24", # LiveTrack 24
"OGCAPT": "CAPT", # capture
"OGNAVI": "NAVI", # NAVITER
"OGNSKY": "SKYS", # Sky Safe
"OGNAVZ": "AVIA", # AVIAZE
"OGNMTK": "MTRK", # Microtrack
"OGNXCG": "XCG", # Cross Country Guide
"OGNMAV": "NMAV", # MAV link
"OGNEMO": "NEMO", # Canadian NEMO
"OGNFNO": "NEURONE", # Neurone
"OGNSXR": "OGNB", # OGNbase
"OGAIRM": "AIRM", # Airmate
"OGNMYC": "MYC", # My cloud base
"FXCAPP": "FXC", # FXC
"OGMSHT": "MSHT", # Metashtic
"OGNPUR": "PURT", # Pure track
"OGNDLY": "DLYM", # Delayed fixes (IGC mandated)
"OGNVOL": "VOLA", # Volandoo
"OGAPIK": "APIK" # ?
}
# --------------------------------------------------------------------------
aprssymtypes=[
"/z", # 0 = ?
"/'", # 1 = (moto-)glider (most frequent)
"/'", # 2 = tow plane (often)
"/X", # 3 = helicopter (often)
"/g", # 4 = parachute (rare but seen - often mixed with drop plane)
"\\^", # 5 = drop plane (seen)
"/_", # 6 = MicroLight (FANET) (rare but seen)
"/g", # 7 = para-glider (rare but seen)
"\\^", # 8 = powered aircraft (often)
"/^", # 9 = jet aircraft (rare but seen)
"/z", # A = UFO (people set for fun)
"/O", # B = balloon (seen once)
"/O", # C = airship (seen once)
"/'", # D = UAV (drones, can become very common)
"/z", # E = ground support (ground vehicles at airfields)
"\\n", # F = static object (ground relay ?)
"/n" # F = Node
]
# --------------------------------------------------------------------------
aprstypes=[
"Unknown", # 0 = ?
"Glider", # 1 = (moto-)glider (most frequent)
"Plane", # 2 = tow plane (often)
"Helicopter", # 3 = helicopter (often)
"ParaGlider", # 7 = para-glider (rare but seen)
"DropPlane", # 5 = drop plane (seen)
"MicroLight", # 6 = MicroLight / hang-glider (rare but seen)
"Parachute", # 4 = parachute (rare but seen - often mixed with drop plane)
"PowerAircraft", # 8 = powered aircraft (often)
"Jet", # 9 = jet aircraft (rare but seen)
"UFO", # A = UFO (people set for fun)
"Balloon", # B = balloon (seen once)
"Airship", # C = airship (seen once)
"Drone", # D = UAV (drones, can become very common)
"GroundVehicle", # E = ground support (ground vehicles at airfields)
"GroundStation", # F = static object (ground relay ?)
"StaticObject" # F = static object (ground relay ?)
]
APRSerrs=[] # list of unknown APRS types
# --------------------------------------------------------------------------
def isfloat(s):
return (s.replace('.','',1).isdigit())
unkacft={} # list of unknow aircraft types
def get_aircraft_type(sym1, sym2): # return the aircraft type based on the symbol table
sym=sym1 +sym2
idx=0
while idx < len(aprssymtypes):
if sym == aprssymtypes[idx]:
return (aprstypes[idx])
idx += 1
# deal with the NEMO for the time being
if sym1 == 'I' and sym2 == '&':
return ("Station")
if sym not in unkacft: # print the error once
unkacft[sym]=1
print (">>> Unknown or Wrong Acft Type", sym1, sym2, "<<<", file=sys.stderr)
return ("UNKNOWN")
def isFloat(string):
try:
float(string)
return True
except ValueError:
return False
#
# low level parser functions
#
def get_longitude(packet):
if 'longitude' in packet:
longitude = packet['longitude']
else:
longitude = -1
return longitude
def get_latitude(packet):
if 'latitude' in packet:
latitude = packet['latitude']
else:
latitude = -1
return latitude
def get_altitude(packet):
if 'altitude' in packet and packet['altitude'] is not None:
altitude = packet['altitude']
else:
altitude = -1
return altitude
def getinfoairport(icao):
airports = airportsdata.load() # key is the ICAO identifier (the default)
if icao in airports:
return(airports[icao])
else:
return (None)
# def get_daodatum(packet):
# try:
# daodatum = packet[0].dao_datum_byte
# except ValueError:
# daodatum = ' '
# return daodatum
# def get_resolution(packet):
# try:
# resolution = packet[0].pos_resolution[0]
# except ValueError:
# resolution = -1
# return resolution
def get_speed(packet):
if 'ground_speed' in packet:
speed = packet['ground_speed']
else:
speed = -1
return speed
def get_course(packet):
if 'track' in packet:
course = packet['track']
else:
course = -1
return course
def get_relay(packet):
if 'relay' in packet:
relay = packet['relay']
if relay is None:
relay="NORELAY"
else:
relay = "NORELAY"
return relay
def get_path(packet):
if 'beacon_type' in packet:
path = packet['beacon_type']
else:
path = "NOPATH"
return path
def get_beacontype(packet):
if 'beacon_type' in packet:
path = packet['beacon_type']
else:
path = "NOPATH"
return path
def get_aprstype(packet):
if 'aprs_type' in packet:
aprstype = packet['aprs_type']
else:
aprstype = ''
return aprstype
def get_dst_callsign(packet):
if 'dstcall' in packet:
dst_callsign = packet['dstcall']
else:
dst_callsign = ''
return dst_callsign
def get_destination(packet):
if 'receiver_name' in packet:
destination = packet['receiver_name']
else:
destination = ''
return destination
def get_header(packet):
if 'aprs_type' in packet:
header = packet['aprs_type']
else:
header = ''
return header
def get_otime(packet):
if 'timestamp' in packet:
otime = packet['timestamp']
else:
if hasattr(datetime, 'naive_utcfromtimestamp'):
otime = datetime.naive_utcfromtimestamp(0)
else:
otime = datetime.utcfromtimestamp(0)
return otime
def get_station(packet):
if 'receiver_name' in packet:
station = packet['receiver_name']
station = station.upper() # translate to uppercase
else:
station=''
return (station)
unksrc={} # list of unknow sources
def get_source(dstcallsign):
src = str(dstcallsign)
if src in aprssources:
return (aprssources[src])
if src not in unksrc:
print(">>> Unknown SOURCE:", src, naive_utcnow(),"<<<", file=sys.stderr)
unksrc[src]=1
return ("UNKW")
# ########################################################################
def gdatal(data, typer): # get data on the left
p = data.find(typer) # scan for the type requested
if p == -1:
return (" ")
pb = p
while (data[pb] != ' ' and data[pb] != '/' and pb >= 0):
pb -= 1
ret = data[pb +1:p] # return the data requested
return(ret)
# #######################################################################
def gdatall(data, typer): # get data on the left
p = data.find(typer) # scan for the type requested
if p == -1:
return (" ")
pb = p
while (data[pb] != ' ' and pb >= 0):
pb -= 1
ret = data[pb +1:p] # return the data requested
return(ret)
# #######################################################################
def gdatar(data, typer): # get data on the right
p = data.find(typer) # scan for the type requested
if p == -1:
return (" ")
p = p +len(typer)
pb = p
maxd = len(data) -1
while (pb < maxd):
if data[pb] == ' ' or data[pb] == '\n' or data[pb] == '\r':
pb += 1
break
pb += 1
ret = data[p:pb] # return the data requested
return(ret)
# #######################################################################
#
# geo specifics validations
#
# #######################################################################
def dao(dd): # return the 3 digit of the decimal minutes
dd1 = abs(float(dd))
cdeg = int(dd1)
mmss = dd1 - float(cdeg)
minsec = round(mmss *60.0,4)
decmin= "%06.3f" % (minsec)
return decmin[5] # just return the last digit
def deg2dmslat(dd): # convert degrees float in degrees and decimal minutes (to two decimal places)
dd1 = abs(float(dd))
cdeg = int(dd1)
mmss = dd1 - float(cdeg)
minsec = round(mmss *60.0,4)
decmin= "%2.2d%06.3f" % (cdeg, minsec)
return decmin[0:7]
def deg2dmslon(dd): # convert degrees float in degrees and decimal minutes (to two decimal places)
dd1 = abs(float(dd))
cdeg = int(dd1)
mmss = dd1 - float(cdeg)
minsec = round(mmss *60.0,4)
decmin= "%3.3d%06.3f" % (cdeg, minsec)
return decmin[0:8]
def decdeg2dms(dd): # convert degress float into DDMMSS
is_positive = dd >= 0
dd = abs(dd)
minutes, seconds = divmod(dd *3600, 60)
degrees, minutes = divmod(minutes, 60)
degrees = degrees if is_positive else -degrees
return (degrees, minutes, seconds)
# ########################################################################
#
# High level APRS parser function
#
# ########################################################################
def parseraprs(packet_str, msg):
rc=packet_str.find(":/______")
if rc != -1:
return -2
# args: packet_str the packet stream with the data, msg the dict where to return the parsed data
try:
packet = parse(packet_str)
except:
return -1
# print (">>>Packet:", packet, file=sys.stderr)
# ignore if do data or just the keep alive message
if len(packet_str) > 0 and packet_str[0] != "#":
date = naive_utcnow() # get the date
if 'name' in packet:
callsign = packet['name'] # get the call sign FLARM ID or station name
gid = callsign # id
else:
return -1
longitude = get_longitude(packet)
latitude = get_latitude(packet)
altitude = get_altitude(packet)
# resolution = get_resolution(packet)
# daodatum = get_daodatum(packet)
speed = get_speed(packet) # ground_speed
course = get_course(packet) # track
path = get_path(packet) # aprs_receiver, tracker, aprs_aircraft
relay = get_relay(packet) # relay TCPIP, OGN123456*, RELAY* , OGNDLY*
aprstype = get_aprstype(packet) # aprs type: status or position
dst_callsign = get_dst_callsign(packet) # APRS, OGNTRK,
source = get_source(dst_callsign) # convert to SOURCE
destination = get_destination(packet) # receiver name
# header = get_header(packet) # aprs type
otime = get_otime(packet) # msg time
data = packet_str
ix = packet_str.find('>')
cc = packet_str[0:ix]
ix = packet_str.find(':') # look for the message type
# check if it is position report or status report
msgtype = packet_str[ix +1:ix +2]
if msgtype != '>' and msgtype != '/' and aprstype != 'position_weather': # only status or location messages
if gid not in APRSerrs:
print("MMM Check APRStype >>>", aprstype, "ID:", gid, "Msg:", data, file=sys.stderr)
APRSerrs.append(gid) # print the error only oncea
msg['id'] = gid # return the parsed data into the dict
return (-1)
# ===================================================================================================== #
# if TCPIP records The the WX
if dst_callsign == 'OGNDVS': # if it is a wether station ??
windspeed = gdatall(data, 'kt ')
temp = gdatal (data, 'F ')
humidity = gdatal (data, '% ')
rain = gdatal (data, 'mm/h')
msg['id'] = gid # return the parsed data into the dict
msg['path'] = path
msg['relay'] = relay
msg['station'] = gid
msg['aprstype'] = aprstype
msg['otime'] = otime
msg['windspeed']= windspeed
msg['temp'] = temp
msg['humidity'] = humidity
msg['rain'] = rain
msg['source'] = 'WTX'
return (msg)
if aprstype == 'position_weather': # if it is a wether station ??
msg['id'] = gid # return the parsed data into the dict
msg['path'] = path
msg['relay'] = relay
msg['station'] = gid
msg['aprstype'] = aprstype
msg['otime'] = otime
#print ("WTXin:", packet)
if "comment" in packet:
wtx = packet['comment'].rstrip() # status informationa
elif "user_comment" in packet:
wtx = packet['user_comment'].rstrip() # status informationa
else:
return (msg)
if len(wtx) == 0 or 'wind_speed' in packet: # if info is already parsered
if 'wind_speed' in packet:
msg['wind_speed'] = packet['wind_speed']
if 'wind_direction' in packet:
msg['wind_direction'] = packet['wind_direction']
if 'wind_speed_peek' in packet:
msg['wind_speed_peak'] = packet['wind_speed_peak']
if 'temperature' in packet:
msg['temperature'] = packet['temperature']
if 'humidity' in packet:
msg['humidity'] = packet['humidity']
if 'rainfall_1h' in packet:
msg['rainfall_1h'] = packet['rainfall_1h']
if 'rainfall_24h' in packet:
msg['rainfall_24h'] = packet['rainfall_24h']
if 'barometric_pressure' in packet:
msg['barometric_pressure'] = packet['barometric_pressure']
msg['source'] = 'WTX'
return (msg)
# parse the APRS weather messagea
temp = 0
gust = 0
humidity = 0
rain = 0
rain24 = 0
baro = 0
print("WTX", packet)
if len(wtx) < 27:
return (msg)
if wtx[3]=='g' and len(wtx) >6 and wtx[4:6] != '...' and wtx[4:6] != ' ':
gust=int(wtx[4:6]) # gust
if wtx[7]=='t' and len(wtx) >10 and wtx[8:10] != '...' and wtx[8:10] != ' ':
temp=int(wtx[8:10]) # temperature
if wtx[11]=='r' and len(wtx) >14 and wtx[12:14] != '...' and wtx[12:14] != ' ':
rain=int(wtx[12:14]) # rainfall
if wtx[15]=='p' and len(wtx) >18 and wtx[16:18] != '...' and wtx[16:18] != ' ':
rain24=int(wtx[16:18]) # rainfall
if wtx[19]=='b' and len(wtx) >24 and wtx[20:24] != '.....' and wtx[20:24] != ' ':
baro=int(wtx[20:24]) # barometric presure
if wtx[25]=='h' and len(wtx) >27 and wtx[26:27] != '..' and wtx[26:27] != ' ':
humidity=int(wtx[26:27]) # humidity
msg['wind_speed'] = packet['wind_speed']
msg['wind_direction']= packet['wind_direction']
msg['temperature'] = temp
msg['humidity'] = humidity
msg['rainfall_1h'] = rain
msg['rainfall_24h'] = rain24
msg['barometric_pressure'] = baro
msg['wind_speed_peak'] = gust
msg['source'] = 'WTX'
#print ("WTXout:", msg)
return (msg)
# ===================================================================================================== #
# if TCPIP records The station records
if (path == 'aprs_receiver' or path == 'receiver') and (msgtype == '>' or msgtype == '/'): # handle the TCPIP
if cc.isupper():
gid = callsign
else:
gid = cc
station = gid
# scan for the body of the APRS message
p = data.find(' v0.') # the comment side
status = " "
if aprstype == 'status':
if "comment" in packet:
status = packet['comment'].rstrip() # status informationa
elif "user_comment" in packet:
status = packet['user_comment'].rstrip() # status informationa
else:
status = " "
if 'cpu_temp' in packet:
temp=packet['cpu_temp']
else:
tempC = gdatal(data, "C ") # temperature
if tempC == ' ':
temp = -99.9 # -99 means no temp declared
else:
if isFloat(tempC): # check for numeric, just in case
temp = float(tempC) # temperature
else:
temp = -99.9
if 'version' in packet:
version=packet['version'] # firmware version
else:
version=''
if 'cpu_load' in packet:
cpu=packet['cpu_load']
else:
cpus = gdatar(data, "CPU:") # CPU usage
cpu = 0.0
if (cpus != "" and cpus != " " and cpus[0] != "-" and cpus[0:3] != "0.-"):
cpu = float(cpus) # CPU usage
if 'rec_input_noise' in packet:
rf=packet['rec_input_noise']
else:
rft = gdatar(data, "RF:").rstrip() # look for the RF data
if len(rft) > 64:
rft = rft[0:64] # sanity check
rf = gdatal(rft, "dB").rstrip() # RF noise
irf = rft.find('dB')
rft = rf[0:irf] # delete the extra inforomation
if 'platform' in packet:
msg['platform'] = packet['platform']
else:
msg['platform'] = ''
if 'free_ram' in packet:
msg['free_ram'] = packet['free_ram']
else:
msg['free_ram'] = ''
if 'total_ram' in packet:
msg['total_ram'] = packet['total_ram']
else:
msg['total_ram'] = ''
if 'ntp_error' in packet:
msg['ntp_error'] = packet['ntp_error']
else:
msg['ntp_error'] = ''
msg['id'] = gid # return the parsed data into the dict
msg['path'] = path
msg['relay'] = relay
msg['station'] = station
msg['aprstype'] = aprstype
msg['otime'] = otime
msg['latitude'] = latitude
msg['longitude'] = longitude
msg['altitude'] = altitude
msg['version'] = version
msg['cpu'] = cpu
msg['temp'] = temp
msg['rf'] = rf
msg['status'] = status
msg['source'] = source
return (msg)
# ===================================================================================================== #
# if std records aprs_aircraft or tracker
station = destination
if packet['aprs_type'] != 'status': # if not status report
if otime != 0: # if time is provided ???
# the aprs msgs has the time in this case
hora = otime.strftime("%H%M%S")
else:
# scan for the body of the APRS message
p1 = data.find(':/') +2
if data[p1 +6] == 'h': # case of HHMMSS
hora = data[p1:p1 +6] # get the GPS time in UTC
elif data[p1 +6] == 'z': # case of DDHHMM
# get the GPS time in UTC, ignore date
hora = data[p1 +2:p1 +6] +'00'
else:
# the aprs msgs has not time in this case
hora = date.strftime("%H%M%S")
else: # the case of aprs status report
if otime == 0: # if not time from the packet
# the aprs msgs has not time in this case
hora = date.strftime("%H%M%S")
else:
# the aprs msgs has the time in this case
hora = otime.strftime("%H%M%S")
# scan for the altitude on the body of the message
p2 = data.find('/A=') +3
if len(data) > (p2 + 7) and data[p2 +7] == '!': # get the unique id
extpos = data[p2 +7:p2 +12] # get extended position indicator
else:
extpos = ' '
p3 = data.find(' id') # scan for uniqueid info
if p3 != -1:
uniqueid = "id" +gdatar(data, "id") # get the unique id
else:
uniqueid = ' ' # no unique ID
if len(uniqueid) > 15:
uniqueid = ' ' # no unique ID
roclimb = gdatal(data, "fpm ") # get the rate of climb
if roclimb == ' ' or not isfloat(roclimb): # if no rot provided
roclimb = 0
rot = gdatal(data, "rot") # get the rate of turn
if rot == ' ' or not isfloat(rot): # if no rot provided
rot = 0
sensitivity = gdatal(data, "dB ") # get the sensitivity
if sensitivity == ' ' or not isfloat(sensitivity): # if no sensitivity provided
sensitivity = 0
p6 = data.find('gps') # scan for gps info
if p6 != -1:
gps = gdatar(data, "gps") # get the gpsdata
else:
p6 = data.find(' GPS') # scan for gps info
if p6 != -1:
gps = "GPS" # generic GPS mark
else:
gps = "NO" # no GPS data
if len(gps) > 6:
gps=gps[0:6] # max 6 chars
if len(station) > 9:
station=station[0:9] # max 9 chars
if len(source) > 8:
source=source[0:8] # max 8 chars
dte = date.strftime("%y%m%d") # the aprs msgs has not date
msg['path'] = path # return the data parsed in the dict
msg['relay'] = relay # return the data parsed in the dict
msg['idflarm'] = gid
msg['id'] = gid
msg['date'] = dte
msg['time'] = hora
msg['station'] = station
msg['latitude'] = latitude
msg['longitude'] = longitude
msg['altitude'] = altitude
# msg['resolution'] = resolution
# msg['daodatum'] = daodatum
msg['speed'] = speed
msg['course'] = course
msg['roclimb'] = roclimb
msg['rot'] = rot
msg['sensitivity'] = sensitivity
msg['gps'] = gps
msg['uniqueid'] = uniqueid
msg['extpos'] = extpos
msg['aprstype'] = aprstype
msg['otime'] = otime
msg['source'] = source
if aprstype == 'status':
# scan for the body of the APRS message
p = data.find(':>')
status = data[p +2:p +254].rstrip() # status information
msg['status'] = status
else:
msg['status'] = "NOSTATUS"
if station == "DLY2APRS":
ix = data.find('>')
if data[ix +1:ix +7] == "OGNTRK":
idx=data[ix +8:].find(',')
nsta=data[ix +8:ix +idx +8]
# print("SSS:", nsta, ix, idx, data, file=sys.stderr)
msg['station']=nsta
msg['source']="DLYM"
msg['relay']="OGNDLY*"
if 'symboltable' in packet and 'symbolcode' in packet:
msg['acfttype']=get_aircraft_type(packet['symboltable'], packet['symbolcode'])
if source == "ADSB":
fn =gdatar(data, " fn")
reg =gdatar(data, " reg")
model=gdatar(data, " model")
if fn != ' ':
msg['fn']=fn
if reg != ' ':
msg['reg']=reg
if model != ' ':
msg['model']=model
return(msg)
else:
return -1 # if length ZERO or just the keep alive
#
# #######################################################################
#
def SRSSgetapidata(url): # get the data from the API server
req = urllib.request.Request(url) # buil the request
req.add_header("Content-Type", "application/json")
req.add_header("Content-type", "application/x-www-form-urlencoded")
try:
r = urllib.request.urlopen(req) # open the url resource
js=r.read().decode('UTF-8')
j_obj = json.loads(js) # convert to JSON
return j_obj # return the JSON object
except HTTPError as err:
print ("Error gathering SRSS from api.sunrise-sunset.org", err)
return ({})
#
# routines to get the sunrise or sunset for an specific location
#
def SRSSgetjsondata(lat, lon, obj='sunset', prt=False):
ts = 0 # init the return time since epoch
# let try first to use the web api
url = "http://api.sunrise-sunset.org/json?lat=" +lat +"&lng=" +lon +"&formatted=0"
jsondata = SRSSgetapidata(url) # get the data from the web
# print jsondata
if prt: # if print requested
print(json.dumps(jsondata, indent=4))
if 'status' in jsondata and jsondata['status'] == "OK": # only if results are OK
results = jsondata["results"] # get the reults part
timeref = results[obj] # get the object that we need
print(jsondata['status'], obj, timeref)
# convert to time format
ttt = datetime.strptime(timeref, "%Y-%m-%dT%H:%M:%S+00:00")
# number of second until beginning of the day
td = ttt -datetime(1970, 1, 1)
ts = int(td.total_seconds()) # Unix time - seconds from the epoch
else:
if obj=='sunset': # lets try using the suntime module
sun=Sun(float(lat),float(lon))
ttt=sun.get_sunset_time()
print("Sunset from suntime:", ttt)
naive = ttt.replace(tzinfo=None)
td = naive -datetime(1970, 1, 1)
ts = int(td.total_seconds()) # Unix time - seconds from the epoch
if obj=='sunrise':
sun=Sun(float(lat),float(lon))
ttt=sun.get_sunrise()
print("Sunrise from suntime:", ttt)
naive = ttt.replace(tzinfo=None)
td = naive -datetime(1970, 1, 1)
ts = int(td.total_seconds()) # Unix time - seconds from the epoch
return (ts) # return it
#
# ########################################################################
#
def alive(app, keepalive=0, first='no', register=False):
alivename = app +".alive"
hostname = socket.gethostname()
if (first == 'yes'):
# create a file just to mark that we are alive
alivefile = open(alivename, 'w')
if register:
atexit.register(lambda: os.remove(alivename))
else:
# append a file just to mark that we are alive
alivefile = open(alivename, 'a')
local_time = datetime.now()
alivetime = local_time.strftime("%y-%m-%d %H:%M:%S")
alivefile.write(alivetime +":" +hostname +" "+str(keepalive)+" \n") # write the time as control
alivefile.close() # close the alive file
return()
# ########################################################################
# import datetime