Example #1
0
def onThermostatValueWork(msg):
    try:
        msg.payload = msg.payload.decode("utf-8")
        value = utils.parseFloat(msg.payload)
        tags = utils.getTags(msg.topic)
    except:
        logger.error(
            f'The message: "{msg.payload}" cannot be processed. Topic: "{msg.topic}" is malformed. Ignoring data'
        )
        return

    try:
        fields = {tags["endpoint"]: value}
        tagsToSave = ["locationId", "sensorId"]
        measurement = "thermostatData"
        influxDb.writeData(
            measurement,
            utils.selectTags(tagsToSave, tags),
            fields,
            retentionPolicy="3years",
        )
    except:
        logger.error(
            f"onThermostatValueWork message failed. message: {msg.payload}. Exception: ",
            exc_info=True,
        )
Example #2
0
def onStatusWork(msg):
    try:
        msg.payload = msg.payload.decode("utf-8")
        status = utils.decodeStatus(msg.payload)
        tags = utils.getTags(msg.topic)
    except:
        logger.error(
            f'The message: "{msg.payload}" cannot be processed. Topic: "{msg.topic}" is malformed. Ignoring data',
            extra={"area": "status"},
        )
        return

    try:
        fields = {"status": status}
        tagsToSave = ["locationId", "deviceId"]
        measurement = "sensorsData"
        influxDb.writeData(
            measurement,
            utils.selectTags(tagsToSave, tags),
            fields,
            retentionPolicy="3years",
        )
    except:
        logger.error(
            f"onStatusWork message failed. message: {msg.payload}. Exception: ",
            exc_info=True,
            extra={"area": "status"},
        )
Example #3
0
def onValueWork(msg):
    # Avoid string values as mathematical operations cant
    # be made afterwards
    try:
        msg.payload = msg.payload.decode("utf-8")
        value = utils.parseFloat(msg.payload)
        tags = utils.getTags(msg.topic)
    except:
        logger.error(
            f'The message: "{msg.payload}" cannot be processed. Topic: "{msg.topic}" is malformed. Ignoring data',
            extra={"area": "value"},
        )
        return

    try:
        fields = {"value": value}
        tagsToSave = ["locationId", "sensorId"]
        measurement = "sensorsData"
        influxDb.writeData(
            measurement,
            utils.selectTags(tagsToSave, tags),
            fields,
            retentionPolicy="raw",
        )

    except:
        logger.error(
            f"onValueWork message failed. message: {msg.payload}. Exception: ",
            exc_info=True,
            extra={"area": "value"},
        )
Example #4
0
def onDeviceDataWork(msg):
    try:
        msg.payload = msg.payload.decode("utf-8")
        data = msg.payload
        assert data
        tags = utils.getTags(msg.topic)
    except:
        logger.error(
            f'The message: "{msg.payload}" cannot be processed. Topic: "{msg.topic}" is malformed. Ignoring data',
            extra={"area": "IP"},
        )
        return

    match tags["endpoint"]:
        case "ip":
            fields = {"ip": data}
        case "model":
            fields = {"model": data}
        case "version":
            fields = {"version": data}
        case _:
            logger.error(
                f"Endpoint: '{tags['endpoint']}' not recognized. Topic: {msg.topic}. Ignoring data")
            return

    try:
        tagsToSave = ["locationId", "deviceId"]
        measurement = "devicesData"
        influxDb.writeData(
            measurement,
            utils.selectTags(tagsToSave, tags),
            fields,
            retentionPolicy="raw",
        )
    except:
        logger.error(
            f"onIPWork message failed. message: {msg.payload}. Exception: ",
            exc_info=True
        )