add sensor type handling

This commit is contained in:
2023-02-23 10:45:59 +01:00
parent 95ddfed391
commit f303ea7950

View File

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