Files
HP_InHand_IG502/Pub_Sub/rr_pipeline/thingsboard/pub/sendAlarm.py
2025-01-14 14:34:21 -06:00

32 lines
1.0 KiB
Python

# Enter your python code.
import json, time
from datetime import datetime as dt
from common.Logger import logger
from quickfaas.global_dict import get as get_params
from quickfaas.remotebus import publish
def convertDStoJSON(ds):
j = dict()
for x in ds:
j[x["key"]] = x["value"]
return j
def controlName(name):
try:
params = convertDStoJSON(get_params())
nameMap = json.loads(params.get("name_map"))
return nameMap.get(name, name)
except Exception as e:
logger.error(e)
return name
def sendAlarm(message, wizard_api, cloudName):
logger.debug("publish topic:%s, payload: %s, cloudName: %s" % (__topic__, message, cloudName))
now = round(time.time() * 1000)
payload = {}
for key, value in message["values"].items():
ctrlName = controlName(value["ctrlName"])
payload[ctrlName] = [{"ts": now, "values": {value["measureName"]: value["value"]}}]
logger.info(json.dumps(payload, indent=4))
publish(__topic__, json.dumps(payload), __qos__, cloud_name=cloudName)