-
Notifications
You must be signed in to change notification settings - Fork 3
/
print-packets-json-log.py
37 lines (32 loc) · 1.22 KB
/
print-packets-json-log.py
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
import meshtastic.serial_interface
from pubsub import pub
import json
import time
import os
interface = meshtastic.serial_interface.SerialInterface()
local_node_id = interface.getNode('^local')
file_path = 'received_data.json'
def onReceive(packet, interface):
if packet['from'] is not local_node_id:
json_packet = json.dumps(packet, indent=4, default=lambda s: " ".join(str(s).split()))
if os.path.exists(file_path):
with open(file_path, 'r+') as json_file:
content = json_file.read().strip()
if content:
if content.endswith(']'):
content = content[:-1].rstrip() + ',\n'
else:
content = '['
json_file.seek(0)
json_file.write(content + json_packet + '\n]')
else:
json_file.seek(0)
json_file.write('[' + json_packet + '\n]')
json_file.truncate()
else:
with open(file_path, 'w') as json_file:
json_file.write('[\n' + json_packet + '\n]')
print(f"{json_packet}\n\n")
pub.subscribe(onReceive, 'meshtastic.receive')
while True:
time.sleep(1)