-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublishSavedLog.py
39 lines (30 loc) · 1 KB
/
publishSavedLog.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
38
39
from datetime import datetime
from time import sleep
import paho.mqtt.client as mqtt
from dotenv import load_dotenv
from mqtt import createMqttPublisherClient
from config import loadConfig
def parseAndPublishLines(lines: list[str], mqttClient: mqtt.Client):
"""Parse a list of lines, waiting for the timestamp to pass"""
lastTimestamp = None
for line in lines:
data = line.split("\t")
timestamp = data[0]
nmeaMessage = data[1].strip()
parsedTimestamp = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S.%f")
if lastTimestamp is not None:
delta = parsedTimestamp - lastTimestamp
sleep(delta.total_seconds() / 1)
print(nmeaMessage)
mqttClient.publish("gnss/rawMessages", nmeaMessage, qos=0)
lastTimestamp = parsedTimestamp
def main():
"""Main function"""
load_dotenv()
config = loadConfig()
mqttClient = createMqttPublisherClient(config)
with open("test.nmea", "r", encoding="utf-8") as f:
lines = f.readlines()
parseAndPublishLines(lines, mqttClient)
if __name__ == "__main__":
main()