Files
HP_InHand_IG502/Pub_Sub/abbflow/v1/sub/receiveCommand.py
Nico Melone 8f83dbbf96 initial
2022-02-16 21:46:01 -06:00

41 lines
1.5 KiB
Python

# Enter your python code.
import json
from quickfaas.measure import recall
from common.Logger import logger
def sync(mac, wizard_api):
#get new values and send
try:
data = recall()#json.loads(recall().decode("utf-8"))
except Exception as e:
logger.error(e)
logger.info(data)
for controller in data:
for measure in controller["measures"]:
#publish measure
topic = "meshify/db/194/_/abbflow/" + mac + "/" + measure["name"]
payload = [{"value": measure["value"]}]
logger.debug("Sending on topic: {}".format(topic))
logger.debug("Sending value: {}".format(payload))
wizard_api.mqtt_publish(topic, json.dumps(payload))
def receiveCommand(topic, payload, wizard_api):
logger.debug(topic)
logger.debug(json.loads(payload))
p = json.loads(payload)[0]
command = p["payload"]["name"].split(".")[1]
commands = {
"sync": sync
}
commands[command](p["mac"].lower(), wizard_api)
#logger.debug(command)
ack(p["msgId"], p["mac"], p["payload"]["name"].split(".")[1], p["payload"]["value"], wizard_api)
def ack(msgid, mac, name, value, wizard_api):
#logger.debug(mac)
mac = "".join(mac.split(":")[:-2])
#logger.debug(msgid)
#logger.debug(mac)
#logger.debug(name)
#logger.debug(value)
wizard_api.mqtt_publish("meshify/responses/" + str(msgid), json.dumps([{"value": "{} Success Setting: {} To: {}".format(mac,name, value), "msgid": str(msgid)}]))