diff --git a/src/preprocess.py b/src/preprocess.py index 3965e92..0154f3f 100644 --- a/src/preprocess.py +++ b/src/preprocess.py @@ -15,6 +15,10 @@ class ApplicationNotFoundException (Exception): def __init__(self, deviceId): self.deviceId = deviceId +class SensorTypeNotFoundException (Exception): + def __init__(self, sensorType): + self.sensorType = sensorType + class DbOp(object): def __init__(self, config): self.conn = None @@ -91,23 +95,35 @@ def mqttOnMessageCallback(client, userdata, message): application = dbh.getApplication(device_id) frame = base64.b64decode(parse_payload['uplink_message']['frm_payload']) + measurement = {} + + match application['sensor_type']: + case 'LDDS75': + battery = struct.unpack('>H', frame[0:2])[0] + distance = struct.unpack('>H', frame[2:4])[0] + status = struct.unpack('?', frame[7:8])[0] + logger.debug(f"{frame=}, {battery=}, {distance=}, {status=}") - battery = struct.unpack('>H', frame[0:2])[0] - distance = struct.unpack('>H', frame[2:4])[0] - status = struct.unpack('?', frame[7:8])[0] - logger.debug(f"{frame=}, {battery=}, {distance=}, {status=}") + statusText = 'Ok' + if not status: + statusText = 'No sensor' + elif distance == 20: + statusText = 'Too close' + + measurement = { + 'application_name': application['label'], + 'raw_level': distance, + 'level': application['ground_level'] - distance, + 'status': statusText, + 'battery': battery / 1000 + } + case _: + raise SensorTypeNotFoundException(application['sensor_type']) - measurement = { - 'application_name': application['label'], - 'raw_level': distance, - 'level': application['ground_level'] - distance, - 'status': 'Ok' if status else 'No sensor', - 'battery': battery / 100 - } logger.debug(f"{measurement=}") - dbh.storeMeasurement(measurement) - + except SensorTypeNotFoundException as e: + logger.error(f"application has unknown sensor type {e.sensorType}") except ApplicationNotFoundException as e: logger.error(f"message from unknown device {e.deviceId}") except Exception as e: