Fix light sensor parsing

This commit is contained in:
2025-12-27 14:44:51 +01:00
parent 495868a170
commit a2c3fea4af
4 changed files with 29 additions and 9 deletions

View File

View File

@@ -0,0 +1,27 @@
import json
import re
from re import Pattern
from hardware.generic.serial_reader import SerialReader
class LoraLightSensorReader(SerialReader):
json_pattern : Pattern = None
def __init__(self, port, baudrate):
super().__init__(port, baudrate)
self.json_pattern = re.compile(r'\{.*}')
def __read(self):
while self._run_event.is_set():
line = self.serial.readline().decode('utf-8', errors='ignore').strip()
if not line:
continue
match = self.json_pattern.search(line)
if match:
try:
data = json.loads(match.group())
print("Received JSON:", data)
except json.JSONDecodeError:
print("Received invalid JSON:", match.group())

View File

@@ -1,8 +1,4 @@
#!/usr/bin/env python3
from collections.abc import Callable
from threading import Thread
import threading
from serial import Serial
from hardware.generic.serial_reader import SerialReader