-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPyPacketRadio.py
More file actions
1384 lines (1261 loc) · 61 KB
/
PyPacketRadio.py
File metadata and controls
1384 lines (1261 loc) · 61 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
998
999
1000
#!/usr/bin/env python3
from __future__ import print_function
try:
import __builtin__ as builtins # type: ignore # Python 2
except ImportError:
import builtins # Python 3
import os
import time
from datetime import date, datetime
import sys
import asyncio
import gc
import re
import math
# psutil and websockets needs pip install (and maybe codecs as well?)
import psutil
import websockets
import codecs
import functools
import json
from http import HTTPStatus
from html import escape
import random
import yaml
from unidecode import unidecode
# Next two are for the radio side of things
import serial
import configparser
import pickle
def num2byte(number):
return bytearray.fromhex("{0:#0{1}x}".format(number,4)[2:])
config = configparser.ConfigParser()
config.read('./config.ini')
#------------------------------------------------------------- Log Files Loads ---------------------------------------------------------------------------
Chan_Hist = {}
Moni_Hist = {}
MHeard = {}
LoraDB = {}
MHeardPath = 'MHeard.pkl'
LoraDBPath = 'LoraDB.pkl'
MoniLogPath = 'MoniLog.pk1'
ChanLogPath = 'ChanLog.pk1'
MyCall = config.get('radio', 'mycall')
MyLora = ''
SendLengt = 127
MyPath = os.getcwd() + os.path.sep + 'txtfiles' + os.path.sep
monitorbuffer = []
channelbuffers = []
today_date = date.today()
time_now = datetime.now()
channels = config.get('tncinit', '3')
callsign = ""
polling = 1
channel_to_read_byte = b'x00'
isLora = False
OnLora = False
mylorachan = {}
pdelay = 0.0016
if os.path.exists(LoraDBPath):
with open(LoraDBPath, 'rb') as f:
LoraDB = pickle.load(f)
if os.path.exists(MHeardPath):
with open(MHeardPath, 'rb') as f:
MHeard = pickle.load(f)
if os.path.exists(MoniLogPath):
with open(MoniLogPath, 'rb') as f:
monitorbuffer = pickle.load(f)
if os.path.exists(ChanLogPath):
with open(ChanLogPath, 'rb') as f:
channelbuffers = pickle.load(f)
#------------------------------------------------------------- Websocket & HTTP ---------------------------------------------------------------------------
MYPORT = 8765
MIME_TYPES = {"html": "text/html", "js": "text/javascript", "css": "text/css", "json": "text/json"}
USERS = set()
async def process_request(sever_root, path, request_headers):
if "Upgrade" in request_headers:
return # Probably a WebSocket connection
if path == '/':
path = '/index.html'
full_path = os.path.realpath(os.path.join(sever_root, path[1:]))
response_headers = [('Server', 'asyncio websocket server'), ('Connection', 'close')]
if path != '/server.json':
if os.path.commonpath((sever_root, full_path)) != sever_root or not os.path.exists(full_path) or not os.path.isfile(full_path):
print("[Network]\33[1;31m HTTP GET {} 404 File not found".format(path) + "\33[0m")
return HTTPStatus.NOT_FOUND, [], b'404 NOT FOUND'
extension = full_path.split(".")[-1]
mime_type = MIME_TYPES.get(extension, "application/octet-stream")
response_headers.append(('Content-Type', mime_type))
if path != '/server.json':
body = open(full_path, 'rb').read()
else:
body = str('{"port": ' + str(MYPORT) + '}').encode()
response_headers.append(('Content-Length', str(len(body))))
return HTTPStatus.OK, response_headers, body
async def register(websocket):
global USERS
global tlast
print("[Network]\33[32m New WebSocket connection from", str(websocket.remote_address)[1:-1].replace('\'','').replace(', ',':') + "\33[0m")
USERS.add(websocket)
async def unregister(websocket):
global USERS
print("[Network]\33[32m WebSocket connection closed for", str(websocket.remote_address)[1:-1].replace('\'','').replace(', ',':') + "\33[0m")
USERS.remove(websocket)
async def mysocket(websocket, path):
global ACTIVECHAN
global ACTCHANNELS
global OnLora
await register(websocket)
try:
async for message in websocket:
if message == '^SendBulk^':
ser.write(b'\x00\x01\x00\x4D')
tmp = ser.readline()[2:-1].decode()
await sendmsg(0,'cmd1','M:' + tmp)
await asyncio.sleep(0.0016)
ser.write(b'\x00\x01\x00\x49')
tmp = ser.readline()[2:-1].decode()
await sendmsg(0,'cmd1','I:' + tmp)
await asyncio.sleep(0.0016)
if monitorbuffer:
for lines in monitorbuffer:
await websocket.send(json.dumps(lines[0]).replace("\\n", "n"))
await asyncio.sleep(0.1)
if channelbuffers:
for lines in channelbuffers:
await websocket.send(json.dumps(lines[0]).replace("\\n", "n"))
await asyncio.sleep(0.1)
await websocket.send('{"cmd":"bulkdone"}')
await websocket.send('{"cmd":"homepoint","station":"' + MyCall + '","lat":"' + str(config.get('radio', 'latitude')) + '","lon":"' + str(config.get('radio', 'longitude')) + '"}')
await sendmsg(0,'cmd3',json.dumps(ACTCHANNELS).replace('\"','\\\"'))
elif message[:1] == '@':
if message[1:].isnumeric():
tmp2 = int(message[1:])
if tmp2 == 9:
# LoraNet
OnLora = True
print('Websocket on Lora Net')
elif tmp2 == 10:
# APRS
OnLora = False
print('Websocket on APRS Net')
else:
OnLora = False
ACTIVECHAN = num2byte(tmp2)
ser.write(ACTIVECHAN + b'\x01\x00\x49')
tmp = ser.readline()[2:-1].decode()
await sendmsg(tmp2,'cmd1','I:' + tmp)
# macybe send connected to name ?
ser.write(ACTIVECHAN + b'\x01\x00\x43')
# await sendmsg(int(message[1:]),'cmd3','C:' + tmp)
ACTCHANNELS[tmp2][1] = ser.readline()[2:-1].decode()
await sendmsg(tmp2,'cmd3',json.dumps(ACTCHANNELS).replace('\"','\\\"'))
elif message[:1] == '^' and OnLora == False:
# We want to send a command!
ser.write(ACTIVECHAN + b'\x01' + num2byte(len(message[1:]) -1) + bytearray(message[1:], 'utf-8'))
data = ser.readline()
data_decode = codecs.decode(data, 'cp850')
data_decode = re.sub(r'[\x00-\x09\xfe-\xff]', '', data_decode) # lets get rid of non printable shiz
print('[ DEBUG ]\33[0;33m Cmd Send Responce : "' + data_decode + '"')
elif message[:1] == '^' and OnLora:
dest = '!' + message[2:]
await sendmsg(9,'echo','Requesting Position data from ' + LoraDB[message[2:]][2] + ', Please wait....')
try:
meshtastic_client.sendPosition(destinationId=dest, wantResponse=True, channelIndex=0)
except:
await sendmsg(9,'echo', 'Position request Failed!')
else:
await sendmsg(9,'echo', 'Position request success.')
# meshtastic_client.sendTelemetry(destinationId=dest, wantResponse=True, channelIndex=channelIndex)
else:
print("[Network] " + message + "\33[0m")
# need know what channel we actually sennding on . . .
if OnLora:
meshtastic_client.sendText(message)
await sendmsg(9,'echo',message)
else:
ch = int(ACTIVECHAN.hex(), 16)
await sendmsg(ch,'echo',message)
sendqueue.append([ch,message])
except Exception as e:
print("[Network] \33[1;31m" + repr(e))
finally:
await unregister(websocket)
async def sendmsg(chan, cmd, message):
global USERS
timenow = int(time.time())
if USERS:
try:
for user in USERS:
await user.send('{"time":' + str(timenow) + ',"chan":' + str(chan) + ',"cmd":"' + cmd + '","data":"' + message + '"}') # might need a replace \r for \n as well; needs see...
except Exception as e:
print("[Network] \33[1;31m" + repr(e))
if cmd != 'cmd1' and cmd != 'cmd2' and cmd != 'cmd3' and cmd != 'cmd100' and cmd != 'loraHeard':
tmp = {}
tmp['time'] = timenow
tmp['chan'] = chan
tmp['cmd'] = cmd
tmp['data'] = message
if chan == 0:
monitorbuffer.append([tmp])
if len(monitorbuffer) > 300:
monitorbuffer.pop(0)
else:
if "deviceMetrics" not in message:
channelbuffers.append([tmp])
if len(channelbuffers) > 500:
channelbuffers.pop(0)
#------------------------------------------------------------- Console Chat ---------------------------------------------------------------------------
async def main():
if config.get('meshtastic', 'plugin_enable') == 'True':
pub.subscribe(on_meshtastic_message, "meshtastic.receive", loop=asyncio.get_event_loop())
pub.subscribe(on_meshtastic_connection, "meshtastic.connection.established")
pub.subscribe(on_lost_meshtastic_connection,"meshtastic.connection.lost")
# Need add active channel here
while True:
text = await ainput("")
message = text[:-1]
await sendmsg(0,'echo',message)
_print('\033[1A' + '\033[K', end='')
print("[Console] " + message)
if message[:1] == '^' and OnLora == False:
ser.write(ACTIVECHAN + b'\x01' + num2byte(len(message[1:]) -1) + bytearray(message[1:], 'utf-8'))
data = ser.readline()
data_decode = codecs.decode(data, 'cp850')
data_decode = re.sub(r'[\x00-\x09\xfe-\xff]', '', data_decode) # lets get rid of non printable shiz
print('[ DEBUG ]\33[0;33m Cmd Send Responce : "' + data_decode + '"')
else:
sendqueue.append([0,message])
async def ainput(string: str) -> str:
await asyncio.get_event_loop().run_in_executor(
None, lambda s=string: sys.stdout.write(s+' '))
return await asyncio.get_event_loop().run_in_executor(
None, sys.stdin.readline)
#----------------------------------------------------------- Meshtastic Lora Con ------------------------------------------------------------------------
import meshtastic.tcp_interface
import meshtastic.serial_interface
# import base64
from pubsub import pub
meshtastic_client = None
lora_lastmsg = ''
def value_to_graph(value, min_value=-19, max_value=1, graph_length=8):
value = max(min_value, min(max_value, value))
position = int((value - min_value) / (max_value - min_value) * (graph_length - 1))
graph = ['_'] * graph_length
graph[position] = '|'
return ''.join(graph)
def connect_meshtastic(force_connect=False):
global meshtastic_client, MyLora
if meshtastic_client and not force_connect:
return meshtastic_client
meshtastic_client = None
# Initialize Meshtastic interface
retry_limit = 3
attempts = 1
successful = False
target_host = config.get('meshtastic', 'host')
comport = config.get('meshtastic', 'serial_port')
cnto = target_host
if config.get('meshtastic', 'interface') != 'tcp':
cnto = comport
print("[LoraNet] Connecting to meshtastic on " + cnto + "...")
while not successful and attempts <= retry_limit:
try:
if config.get('meshtastic', 'interface') == 'tcp':
meshtastic_client = meshtastic.tcp_interface.TCPInterface(hostname=target_host)
else:
meshtastic_client = meshtastic.serial_interface.SerialInterface(comport)
successful = True
except Exception as e:
attempts += 1
if attempts <= retry_limit:
print("[LoraNet] Attempt #{attempts-1} failed. Retrying in {attempts} secs... {e}")
time.sleep(attempts)
else:
print("[LoraNet] Could not connect: {e}")
return None
nodeInfo = meshtastic_client.getMyNodeInfo()
print("[LoraNet] Connected to " + nodeInfo['user']['id'] + " > " + nodeInfo['user']['shortName'] + " / " + nodeInfo['user']['longName'] + " using a " + nodeInfo['user']['hwModel'])
tmp = {}
tmp['time'] = int(time.time())
tmp['chan'] = 9
tmp['cmd'] = 'cmd4'
tmp['data'] = "Connected to " + nodeInfo['user']['id'] + " > " + nodeInfo['user']['shortName'] + " / " + nodeInfo['user']['longName'] + " using a " + nodeInfo['user']['hwModel']
channelbuffers.append([tmp])
MyLora = (nodeInfo['user']['id'])[1:]
logLora((nodeInfo['user']['id'])[1:], ['NODEINFO_APP', nodeInfo['user']['shortName'], nodeInfo['user']['longName'], nodeInfo['user']["macaddr"],nodeInfo['user']['hwModel']])
# Lets get the Local Node's channels
nodeInfo = meshtastic_client.getNode('^local')
channels = nodeInfo.channels
if channels:
for channel in channels:
# psk_base64 = base64.b64encode(channel.settings.psk).decode('utf-8')
# print(f"Index: {channel.index}, Role: {channel.role}, PSK (Base64): {psk_base64}, Name: {channel.settings.name}")
if channel.settings.name == '':
mylorachan[channel.index] = str(channel.index)
else:
mylorachan[channel.index] = unidecode(channel.settings.name)
updatesnodes()
return meshtastic_client
def on_lost_meshtastic_connection(interface):
print("[LoraNet] Lost connection. Reconnecting...")
connect_meshtastic(force_connect=True)
def on_meshtastic_connection(interface, topic=pub.AUTO_TOPIC):
# called when we (re)connect to the radio
# defaults to broadcast, specify a destination ID if you wish
# interface.sendText("hello mesh")
print("[LoraNet] Connection Made...")
def logLora(nodeID, info):
tnow = int(time.time())
if nodeID in LoraDB:
LoraDB[nodeID][0] = tnow # time last seen
else:
LoraDB[nodeID] = [tnow, '', '', 81.0, 186.0, 0, '', '', tnow, '', '', '',-1]
sendqueue.append([0,'[LoraNET] New lora station registered with station id !' + nodeID])
if info[0] == 'NODEINFO_APP':
tmp = str(info[1].encode('ascii', 'xmlcharrefreplace'), 'ascii').replace("\n", "") # short name
if tmp != '':
LoraDB[nodeID][1] = tmp
else:
LoraDB[nodeID][1] = nodeID[-4:]
tmp = str(info[2].encode('ascii', 'xmlcharrefreplace'), 'ascii').replace("\n", "") # long name
if tmp != '':
LoraDB[nodeID][2] = tmp
else:
LoraDB[nodeID][2] = '!' + nodeID
LoraDB[nodeID][6] = info[3] # mac adress
LoraDB[nodeID][7] = info[4] # hardware
elif info[0] == 'POSITION_APP':
LoraDB[nodeID][3] = info[1] # latitude
LoraDB[nodeID][4] = info[2] # longitude
LoraDB[nodeID][5] = info[3] # altitude
LoraDB[nodeID][9] = LatLon2qth(info[1],info[2])
def on_meshtastic_message(packet, interface, loop=None):
# loop=None
try:
# print(yaml.dump(packet))
global lora_lastmsg
donoting = True
if "decoded" in packet:
data = packet["decoded"]
text_from = ''
if "fromId" in packet and packet["fromId"] is not None:
text_from = packet["fromId"][1:]
text_mqtt = ''
text_msgs = ''
fromraw = text_from
tnow = int(time.time())
if text_from in LoraDB:
LoraDB[text_from][0] = tnow
if LoraDB[text_from][1] != '':
text_from = LoraDB[text_from][1] + " (" + LoraDB[text_from][2] + ")"
else:
LoraDB[text_from] = [tnow, '', '', 81.0, 186.0, 0, '', '', tnow, '', '', '', -1]
if "viaMqtt" in packet:
LoraDB[fromraw][10] = ' via mqtt'
text_mqtt = ' via mqtt'
else:
LoraDB[fromraw][10] = ''
# if MyLora != fromraw: print(yaml.dump(packet))
# Lets Work the Msgs
if data["portnum"] == "TELEMETRY_APP":
if "deviceMetrics" in data["telemetry"]:
text = data["telemetry"]["deviceMetrics"]
if "voltage" in text and "batteryLevel" in text:
# sendqueue.append([9,'deviceMetrics:{\\"id\\":\\"' + packet["fromId"][1:] + '\\",\\"v\\":' + str(round(text["voltage"],2)) + ',\\"b\\":' + str(text["batteryLevel"]) + '}'])
text_raws = 'Node Telemetry ' + str(round(text["voltage"],2)) + 'v ' + str(text["batteryLevel"]) + '%'
if text_mqtt == '' and MyLora != fromraw:
donoting = False
elif data["portnum"] == "POSITION_APP":
text = data["position"]
if "altitude" in text:
text_msgs = "Node Position "
if "latitude" in text:
text_msgs += "latitude " + str(round(text["latitude"],4)) + " "
if "longitude" in text:
text_msgs += "longitude " + str(round(text["longitude"],4)) + " "
qth = LatLon2qth(round(text["latitude"],6), round(text["longitude"],6))
# text_msgs += "(" + qth + ") "
if "altitude" in text:
text_msgs += "altitude " + str(text["altitude"]) + " meter"
# if not is_hour_between(1, 10) and "viaMqtt" not in packet:
# sendqueue.append([0,'[LoraNET] Position beacon from ' + text_from + ' QTH ' + qth[:-2]])
text_raws = text_msgs
# sendqueue.append([9,text_from + text_mqtt + ' ' + text_msgs])
logLora(packet["fromId"][1:], ['POSITION_APP', text["latitude"], text["longitude"], text["altitude"]])
# ["latitudeI"] ["longitude"] ["altitude"] ["time"] ["precisionBits"]
if text_mqtt == '' and MyLora != fromraw:
donoting = False
elif data["portnum"] == "NODEINFO_APP":
text = data["user"]
if "shortName" in text:
lora_sn = str(text["shortName"].encode('ascii', 'xmlcharrefreplace'), 'ascii')
lora_ln = str(text["longName"].encode('ascii', 'xmlcharrefreplace'), 'ascii')
lora_mc = text["macaddr"]
lora_mo = text["hwModel"]
logLora(packet["fromId"][1:], ['NODEINFO_APP', lora_sn, lora_ln, lora_mc, lora_mo])
text_raws = "Node Info using hardware " + lora_mo
text_from = LoraDB[packet["fromId"][1:]][1] + " (" + LoraDB[packet["fromId"][1:]][2] + ")"
if text_mqtt == '':
donoting = False
sendqueue.append([9,text_from + text_mqtt + ' ' + text_raws])
elif data["portnum"] == "TEXT_MESSAGE_APP" and "text" in data:
text_msgs = str(data["text"].encode('ascii', 'xmlcharrefreplace'), 'ascii').rstrip()
text_raws = data["text"]
text_chns = 'Private'
if "toId" in packet:
if packet["toId"] == '^all':
text_chns = '0'
if "channel" in packet:
text_chns = str(mylorachan[packet["channel"]].encode('ascii', 'xmlcharrefreplace'), 'ascii')
if text_chns != 'Private':
sendqueue.append([0,'[LoraNET] [Ch ' + text_chns + '] ' + text_from + text_mqtt + ': ' + text_msgs])
donoting = False
sendqueue.append([9,text_from + text_mqtt + ' on Channel ' + text_chns + ' ' + text_msgs])
elif data["portnum"] == "NEIGHBORINFO_APP":
text_raws = 'Node Neighborinfo'
if "neighborinfo" in data and "neighbors" in data["neighborinfo"]:
text = data["neighborinfo"]["neighbors"]
text_raws += ' ['
for neighbor in text:
nodeid = hex(neighbor["nodeId"])[2:]
if nodeid in LoraDB and LoraDB[nodeid][1] != '':
nodeid = LoraDB[nodeid][1]
else:
nodeid = '!' + nodeid
text_raws += nodeid
if "snr" in neighbor:
text_raws += ' (' + str(neighbor["snr"]) + 'dB), '
text_raws = text_raws[:-2] + ']'
if text_mqtt == '':
# and MyLora != fromraw:
donoting = False
else:
text_raws = 'Node ' + (data["portnum"].split('_APP', 1)[0]).title()
if text_mqtt == '' and MyLora != fromraw:
donoting = False
# Cleanup and get ready to print
if data["portnum"] != "POSITION_APP" and data["portnum"] != "TEXT_MESSAGE_APP":
logLora(packet["fromId"][1:],['UPDATETIME'])
if "snr" in packet and packet['snr'] is not None:
LoraDB[fromraw][11] = str(packet['snr']) + 'dB'
if "rxSnr" in packet and packet['rxSnr'] is not None:
LoraDB[fromraw][11] = str(packet['rxSnr']) + 'dB'
if donoting == False and text_raws != '': # and lora_lastmsg != text_raws:
lora_lastmsg = text_raws
print("[LoraNet]\33[0;37m " + text_from + LoraDB[fromraw][10] + "\33[0m")
if "viaMqtt" in packet:
_print('\33[0;32m' + (' ' * 21) + text_raws + '\33[0m')
else:
text_from = ''
if LoraDB[fromraw][12] > 0:
text_from = ' (Hops ' + str(LoraDB[fromraw][12]) + ')'
if "hopLimit" in packet:
text_from = text_from[:-1] + '/' + str(packet["hopLimit"]) + ')'
if LoraDB[fromraw][11] != '' and MyLora != fromraw:
v = float(LoraDB[fromraw][11].replace('dB', ''))
text_from += f" ({round(v,1)}dB {value_to_graph(v)} )"
# ' (' + LoraDB[fromraw][11] + ')'
_print('\33[0;33m' + (' ' * 21) + text_raws + text_from + '\33[0m')
except Exception as e:
print("[LoraNet] \33[1;31m" + repr(e))
def updatesnodes():
info = ''
itmp = 0
for nodes, info in meshtastic_client.nodes.items():
if "user" in info:
tmp = info['user']
if "id" in tmp:
# Only push to DB if we actually get a node ID
nodeID = str(tmp['id'])[1:]
nodeLast = 0
itmp = itmp + 1
if "lastHeard" in info and info["lastHeard"] is not None: nodeLast = info['lastHeard']
if nodeID in LoraDB:
LoraDB[nodeID][0] = nodeLast
else:
LoraDB[nodeID] = [nodeLast, '', '', 81.0, 186.0, 0, '', '', nodeLast, '', '', '',-1]
if "shortName" in tmp: LoraDB[nodeID][1] = str(tmp['shortName'].encode('ascii', 'xmlcharrefreplace'), 'ascii').replace("\n", "")
if "longName" in tmp: LoraDB[nodeID][2] = str(tmp['longName'].encode('ascii', 'xmlcharrefreplace'), 'ascii').replace("\n", "")
if "macaddr" in tmp: LoraDB[nodeID][6] = str(tmp['macaddr'])
if "hwModel" in tmp: LoraDB[nodeID][7] = str(tmp['hwModel'])
if "hopsAway" in info: LoraDB[nodeID][12] = info['hopsAway']
if "position" in info:
tmp2 = info['position']
if "latitude" in tmp2 and "longitude" in tmp2:
LoraDB[nodeID][3] = tmp2['latitude']
LoraDB[nodeID][4] = tmp2['longitude']
LoraDB[nodeID][9] = LatLon2qth(tmp2['latitude'],tmp2['longitude'])
if "altitude" in tmp:
LoraDB[nodeID][5] = tmp['altitude']
if "viaMqtt" in info: LoraDB[nodeID][10] = ' via mqtt'
if "snr" in info and info['snr'] is not None: LoraDB[nodeID][11] = str(info['snr']) + 'dB'
#-------------------------------------------------------------- TNC WA8DED ---------------------------------------------------------------------------
BEACONDELAY = int(config.get('radio', 'beacon_time'))
BEACONTEXT = config.get('radio', 'beacon_text')
ACTIVECHAN = num2byte(0)
ACTCHANNELS = {0: ['CQ','',0]}
# Chn:[My call, Remote call, in/out]
sendqueue = []
channels = config.get('tncinit', '3')
polling = 1
channel_to_read_byte = b'x00'
tlast = 0
tnc_at2 = 0
tnc_at3 = 180000
tnc_frac = 400
ser = serial.Serial()
ser.port = config.get('intertnc', 'serial_port')
ser.baudrate = config.get('intertnc', 'serial_baud')
ser.bytesize = serial.EIGHTBITS # number of bits per bytes
ser.parity = serial.PARITY_NONE # set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE # number of stop bits
# ser.timeout = None # block read
ser.timeout = 0.08 # non blocking read
ser.xonxoff = False # disable software flow control
ser.rtscts = False # disable hardware (RTS/CTS) flow control
ser.dsrdtr = False # disable hardware (DSR/DTR) flow control
ser.writeTimeout = 2 # timeout for write
def logheard(call, cmd, info):
tnow = int(time.time())
# 'callsign' = ['name','jo locator if known',first heard,last heard,heard count,first connect, last connect, connect count];
# 0 1 2 3 4 5 6 7
sindex = call.find('-')
if sindex > 1:
call = call[:sindex]
# testcallsign = re.search('[\\d]{0,1}[A-Z]{1,2}\\d([A-Z]{1,4}|\\d{3,3}|\\d{1,3}[A-Z])[A-Z]{0,5}', call.upper())
if len(call) < 7:
# call = testcallsign.group(0)
if cmd == 5:
if call in MHeard:
MHeard[call][3] = tnow
MHeard[call][4] += 1
else:
MHeard[call] = ['', '', tnow, tnow, 1, 0, 0, 0]
if MyCall == call:
MHeard[call][0] = config.get('radio', 'sysop')
else:
sendqueue.append([0,'New station registered with station call ' + call])
elif cmd == 6:
if call in MHeard:
if len(info) >= len(MHeard[call][1]) and str(info) != '':
MHeard[call][1] = str(info)
else:
MHeard[call] = ['', str(info), tnow, tnow, 1, 0, 0, 0]
MHeard[call][4] += 1
MHeard[call][3] = tnow
elif cmd == 3:
if call in MHeard:
MHeard[call][6] = tnow
MHeard[call][4] += 1
MHeard[call][7] += 1
if MHeard[call][5] == 0:
MHeard[call][5] = tnow
else:
print('[ DEBUG ] Log Error, Not a callsign > ' + call)
# Set TNC in WA8DED Hostmode
def init_tncinWa8ded():
try:
ser.open()
except Exception as e:
print("[Serial!] \33[1;31m" + repr(e))
sys.exit()
ser.write(b'\x11\x18\x1b')
ser.readline()
ser.write(b'\x4a\x48\x4f\x53\x54\x31\x0d')
print('\33[0;33mSetting TNC in hostmode...\33[0m')
statustnc = ser.readline().decode(encoding='ASCII').rstrip()
if statustnc != "JHOST1":
print(statustnc)
print("[ DEBUG ] NO TNC!!!!")
sys.exit()
else:
print("[ DEBUG ] \33[0;32m" + statustnc + '\33[0m')
ser.write(b'\x00\x01\x00\x56')
print("[ DEBUG ] \33[0;32m" + ser.readline().decode(encoding='ASCII')[2:] + '\33[0m')
def send_init_tnc(command, chan, cmd):
length_command = len(command) - 1
if length_command < 10:
hex_length_command = '0' + str(length_command)
else:
hex_length_command = str(length_command)
start_hex = '%02d' % (chan,) + '%02d' % (cmd,)
bytes_command = bytearray(command, 'utf-8')
hex_begin_bytes = bytearray.fromhex(start_hex)
hex_length_bytes = bytearray.fromhex(hex_length_command)
start_bytes = hex_begin_bytes + hex_length_bytes
all_bytes = start_bytes + bytes_command
return all_bytes
def send_tnc(command, channel):
allbytes = []
allbytes.append('%02d' % channel)
allbytes.append('%02d' % 0) # Info/CMD
txt_len = int(len(command) - 1)
allbytes.append(hex(txt_len)[2:].zfill(2))
for char in command:
ascii_val = ord(char)
allbytes.append(hex(ascii_val)[2:].zfill(2))
return bytearray.fromhex(''.join(allbytes))
def send_tncC(command, channel):
allbytes = []
allbytes.append('%02d' % channel)
allbytes.append('%02d' % 1) # Info/CMD
txt_len = int(len(command) - 1)
allbytes.append(hex(txt_len)[2:].zfill(2))
for char in command:
ascii_val = ord(char)
allbytes.append(hex(ascii_val)[2:].zfill(2))
return bytearray.fromhex(''.join(allbytes))
def init_tncConfig():
global ACTCHANNELS
ACTCHANNELS = {}
print('\33[0;33mSetting TNC Configs...\33[0m')
x = 0
for x in range(1, 18):
if x == 2:
ACTCHANNELS[0] = [config.get('tncinit', '2')[2:], '', 0]
if x == 3:
all_bytes = send_init_tnc(config.get('tncinit', str(x)),0,1)
ser.write(all_bytes)
ser.readline()
# Set Callsign I for every channel Y
callsign_str = 'I ' + MyCall
callsign_len = len(callsign_str)
callsign_len_hex = '0' + str(callsign_len -1)
callsign_len_byte = bytearray.fromhex(callsign_len_hex)
callsign_in_bytes = bytearray(callsign_str, 'utf-8')
chan_i = 0
for x in range(1, int(channels[2:]) + 1):
chan_i = chan_i + 1
incremented_hex_value = num2byte(chan_i)
ser.write(incremented_hex_value + b'\x01' + callsign_len_byte + callsign_in_bytes)
ser.readline()
ser.write(incremented_hex_value + b'\x01\x00\x43')
tmp = (ser.readline())[2:-1].decode()
print('[Chan %02d' % (chan_i,) + '] ' + callsign_str + ' > ' + tmp)
ACTCHANNELS[chan_i] = [callsign_str[2:],tmp,0]
elif x == 6:
# Set date for K
date_string = today_date.strftime("%m/%d/%y")
date_today = send_init_tnc("K " + date_string,0,1)
ser.write(date_today)
ser.readline()
elif x == 7:
# Set time for K
time_string = time_now.strftime('%H:%M:%S')
now_time = send_init_tnc("K " + time_string,0,1)
ser.write(now_time)
ser.readline()
else:
if x == 17:
all_bytes = send_init_tnc('I ' + MyCall,0,1)
else:
all_bytes = send_init_tnc(config.get('tncinit', str(x)),0,1)
ser.write(all_bytes)
ser.readline()
print('\33[0;33mTNC Active and listening...\33[0m')
sendqueue.append([0,BEACONTEXT + ' @ ' + time.strftime("%H:%M", time.localtime())])
async def go_serial():
# serial port stuff here
global polling
global ACTIVECHAN
global tlast
polling = 1
queing = 0
sendbuffs = 0
x = 0
while True:
if x < 6:
if polling == 1:
await asyncio.sleep(pdelay)
ser.write(b'\xff\x01\x00G')
polling_data = ser.readline()
# print('IS 0000 > ' + polling_data.hex())
data_hex = polling_data.hex()
if data_hex != 'ff0100':
polling = 0
# How in *** mind can a serial read return absolutly noting....
if data_hex == '': data_hex = '0000'
# ff 01 02 00
if len(data_hex) > 4:
chan_i = int(data_hex[4:6].upper(), 16) - 1
else:
chan_i = int(data_hex[2:4].upper(), 16) - 1 # ?? should need be [0:2]
# these two cases should not happen but happen...
if chan_i > 10 and chan_i < 255 or chan_i < 0:
chan_i = 0
poll_byte = num2byte(chan_i)
await asyncio.sleep(pdelay)
ser.write(poll_byte + b'\x01\x00G')
data = ser.readline()
data_hex = data.hex()
# why we getting ff010100 + plus real package?!
# Seriously need find a good way to repair a `double` serial read
if data_hex[:8] == 'ff010100' and len(data_hex) > 8:
data_hex = data_hex[8:]
data = data[4:]
# How in *** mind can a serial read return absolutly noting....
if data_hex == '': data_hex = '0000'
data_decode = codecs.decode(data, 'cp850')
# data_decode = re.sub(r'[\x00-\x09\xfe-\xff]', '', data_decode)
data_int = int(data_hex[2:4].upper(), 16)
namechan = '[Monitor]'
if chan_i != 0:
namechan = '[Chan %02d]' % (chan_i,)
if data_int == 0:
# Success, no data follows
polling = 1
elif data_int == 1:
# Success, message follows (end with 00)
print('[ DEBUG ] ' + data_hex)
print(namechan + " [1] \33[1;32m" + data_decode[2:] + "\33[0m")
await sendmsg(chan_i,'cmd1',"OK: " + data_decode[2:])
elif data_int == 2:
# Failure, message follows
print(namechan + " [2] \33[1;31m" + data_decode + "\33[0m")
await sendmsg(chan_i,'cmd2',"Error: " + data_decode)
elif data_int == 3:
# Link status
data_decode = data_decode[2:-1]
if 'DISCONNECTED' in data_decode:
# Channel chan_i got disconected
ACTCHANNELS[chan_i][1] = 'CHANNEL NOT CONNECTED'
ACTCHANNELS[chan_i][2] = 0
await sendmsg(0,'cmd3',json.dumps(ACTCHANNELS).replace('\"','\\\"'))
elif 'CONNECTED' in data_decode:
# Channel chan_i got disconected
remote_station = data_decode.split(" ")[3]
logheard(remote_station, 3, '')
if ACTCHANNELS[chan_i][1] != remote_station:
ctext_file = 'ctext-' + remote_station + '.txt'
if os.path.isfile(MyPath + ctext_file):
# We got a personal ctext send it...
sendtext = readfile(ctext_file)
await textchunk(sendtext,chan_i,remote_station)
elif os.path.isfile(MyPath + 'ctext.txt'):
# Send default ctext.txt
sendtext = readfile('ctext.txt')
await textchunk(sendtext,chan_i,remote_station)
else:
print('[ DEBUG ] No ctext.txt file in txtfiles folder!')
ACTCHANNELS[chan_i][1] = remote_station
ACTCHANNELS[chan_i][2] = 2
await sendmsg(0,'cmd3',json.dumps(ACTCHANNELS).replace('\"','\\\"'))
print(namechan + " \33[0;37m" + data_decode + "\33[0m")
await sendmsg(chan_i,'chat',data_decode)
elif data_int == 4:
# Monitor header/no info
data_decode = data_decode[2:-1]
callsign = data_decode.split(" ")[1]
logheard(callsign, 5, '')
print(namechan + " \33[1;37m" + data_decode + "\33[0m")
await sendmsg(chan_i,'cmd4',data_decode)
elif data_int == 5:
# Monitor header/info
#need check mheard if new or not and appent to msg *NEW*
data_decode = data_decode[2:-1]
callsign = data_decode.split(" ")[1]
sindex = callsign.find('-')
if sindex > 1:
callsign = callsign[:sindex]
heardnew = ''
if callsign not in MHeard:
heardnew = ' * NEW *'
logheard(callsign, 5, '')
print(namechan + " \33[1;37m" + data_decode + heardnew + "\33[0m")
await sendmsg(chan_i,'cmd5',data_decode + heardnew)
heardnew = callsign
elif data_int == 6:
# Monitor information
if data_decode[-2:-1] == data_decode[-1:] and data_decode[-1:] == '\n':
data_decode = data_decode[:-1]
data_out = data_decode[3:].splitlines()
sendtext = ''
_print('\33[1;36m', end='')
for lines in data_out:
_print((' ' * 21) + lines)
sendtext += lines + '\r'
_print('\33[0m', end='')
await sendmsg(chan_i,'warn',replacesheet(sendtext))
locator = re.findall(r'[A-R]{2}[0-9]{2}[A-Z]{2}[0-9]{2}', data_decode.upper())
if not locator:
locator = re.findall(r'[A-R]{2}[0-9]{2}[A-Z]{2}', data_decode.upper())
# APRS Location to locator (hopfully) apparently we off by like 5km.. or so
if not locator:
if len(data_decode) >= 20:
ymp = str(re.findall(r'[0-9]{4}[.][0-9]{2}[N,S]/.[0-9]{4}[.][0-9]{2}[E,W]', data_decode))
# we get as example ['5303.96N/00700.39E']
if len(ymp) == 22:
direction = {'N':1, 'S':-1, 'E': 1, 'W':-1}
new = (ymp[2:4] + ' ' + ymp[4:6] + ' ' + ymp[7:9] + ' ' + ymp[9:10]).split()
new_dir = new.pop()
new.extend([0,0,0])
lat = (int(new[0])+int(new[1])/60.0+int(new[2])/3600.0) * direction[new_dir]
new = (ymp[12:14] + ' ' + ymp[14:16] + ' ' + ymp[17:19] + ' ' + ymp[19:20]).split()
new_dir = new.pop()
new.extend([0,0,0])
lon = (int(new[0])+int(new[1])/60.0+int(new[2])/3600.0) * direction[new_dir]
locator = [LatLon2qth(lat, lon)[:-2],12]
if not locator:
logheard(callsign, 6, '')
else:
logheard(callsign, 6, locator[0])
heardnew = ''
elif data_int == 7:
# Connected information
if data_decode[-2:-1] == data_decode[-1:] and data_decode[-1:] == '\n':
data_decode[:-1]
data_out = data_decode[3:].splitlines()
numlines = 0
_print('\33[1;30m', end='')
sendtext = ''
for lines in data_out:
_print((' ' * 21) + lines)
sendtext += lines + '\r'
numlines += 1
_print('\33[0m', end='')
await sendmsg(chan_i,'warn',replacesheet(sendtext))
# deal weith incomming // commands.
if '//' in sendtext and numlines == 1 and ACTCHANNELS[chan_i][1] != 'CHANNEL NOT CONNECTED':
await menuhandle(chan_i, ACTCHANNELS[chan_i][1], sendtext)
else:
data_decode = re.sub(r'[\x00-\x31\xfe-\xff]', '', data_decode) # lets get rid of non printable shiz
data_out = data_decode.splitlines()
print('[ DEBUG ]\33[0;33m Stage Get CMD Unknown : "' + data_hex + '"')
_print('\33[1;30m', end='')
sendtext = ''
numlines = 0
for lines in data_out:
_print((' ' * 21) + data_decode)
sendtext += lines + '\r'
numlines += 1
_print('\33[0m', end='')
await sendmsg(0,'warn',replacesheet(sendtext))
# pass
x += 1
else:
x = 0
queing += 1
if queing == 1:
chan_i = int(ACTIVECHAN.hex(), 16)
ser.write(ACTIVECHAN + b'\x01\x01\x40\x42')
tmp = (codecs.decode(ser.readline()[2:-1], 'cp850'))
await sendmsg(chan_i,'cmd1','@B:' + tmp)
await asyncio.sleep(pdelay)
elif queing == 2:
ser.write(ACTIVECHAN + b'\x01\x00\x4C')
# tmp = ser.readline()[2:-1].decode()
tmp = (codecs.decode(ser.readline()[2:-1], 'cp850'))
await sendmsg(chan_i,'cmd1','L:' + tmp)
tmp = int(psutil.Process(os.getpid()).memory_info().rss)
await sendmsg(chan_i,'cmd1','@MEM:' + str(tmp))
await asyncio.sleep(pdelay)
elif queing > 2:
# Lets check if we have a queue to send and if so, send it
if len(sendqueue) > 0:
todo = (sendqueue[0])
sendqueue.pop(0)
if todo[0] == 9 or todo[0] == 10:
await sendmsg(todo[0],'cmd4',todo[1])
elif todo[0] > 19:
beacon = send_tnc(todo[1], todo[0] - 20)
ser.write(beacon)
tmp = ser.readline().decode()
await asyncio.sleep(pdelay)
else:
beacon = send_tnc(todo[1] + '\r', todo[0])
ser.write(beacon)
tmp = ser.readline().decode()
await asyncio.sleep(pdelay)
queing = 0
# And because we need to, send the mheard ever 15 full sec?
tnow = int(time.time())
if tnow > tlast + 6:
tlast = tnow
sendbuffs += 1
if sendbuffs == 1:
await sendmsg(100,'cmd100',json.dumps(MHeard).replace('\"','\\\"'))
else:
await sendmsg(100,'loraHeard',json.dumps(LoraDB).replace('\"','\\\"'))
sendbuffs = 0
print('str: ' + str(x) + ', queing: ' + str(queing))
def replacesheet(text):
if isinstance(text, bytes):
print('[ DEBUG ] Posible byte arrat')
# Replace all enters to be \n
text = re.sub(r'(\r\n|\n|\r)', '\n', text)
# Lets escape weird characters
text = escape(text)[:-1]
# kay still messed up characters ? lets xml char it
text = str(text.encode('ascii', 'xmlcharrefreplace')).replace('b\'', '')[:-1]
# seriously x00 ~ x29 or likes still in there
text = re.sub(r'\\x[0-9][0-9]', '�',text)
return text
# Handle // Commands...
async def menuhandle(chan, call, cmdtxt):
reqcmd = cmdtxt[2:3].upper()
sendtext = ''
if reqcmd == 'H':
# //HELP send ./txtfiles/help.txt
sendtext = readfile('help.txt')
elif reqcmd == 'M':
# send mHeard info...
sendtext = send_mh()
elif reqcmd == 'C':