add sensor type handling
This commit is contained in:
@ -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 = {}
|
||||||
|
|
||||||
battery = struct.unpack('>H', frame[0:2])[0]
|
match application['sensor_type']:
|
||||||
distance = struct.unpack('>H', frame[2:4])[0]
|
case 'LDDS75':
|
||||||
status = struct.unpack('?', frame[7:8])[0]
|
battery = struct.unpack('>H', frame[0:2])[0]
|
||||||
logger.debug(f"{frame=}, {battery=}, {distance=}, {status=}")
|
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=}")
|
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:
|
||||||
|
Reference in New Issue
Block a user