Fix light sensor parsing
This commit is contained in:
0
rpi/hardware/light/__init__.py
Normal file
0
rpi/hardware/light/__init__.py
Normal file
27
rpi/hardware/light/lora_light_sensor_reader.py
Normal file
27
rpi/hardware/light/lora_light_sensor_reader.py
Normal 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())
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user