73 lines
2.7 KiB
Python
73 lines
2.7 KiB
Python
# Enter your python code.
|
|
import json
|
|
from quickfaas.measure import recall
|
|
from common.Logger import logger
|
|
|
|
def sync(mac,value, 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/_/dual_flowmeter/" + mac + "/" + measure["name"]
|
|
if measure["name"] in ["pump_1_run_status", "pump_2_run_status", "charge_pump_run_status"]:
|
|
payload = [{"value": convert_int(measure["name"], measure["value"])}]
|
|
else:
|
|
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 writeplctag(mac, value, wizard_api):
|
|
try:
|
|
value = json.loads(value.replace("'",'"'))
|
|
logger.debug(value)
|
|
message = {"advvfdipp":{value["tag"]: value["val"]}}
|
|
wizard_api.write_plc_values(message)
|
|
except Exception as e:
|
|
logger.debug(e)
|
|
|
|
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,
|
|
"writeplctag": writeplctag,
|
|
}
|
|
commands[command](p["mac"].lower(),p["payload"]["value"], wizard_api)
|
|
#logger.debug(command)
|
|
ack(p["msgId"], p["mac"], command, p["payload"]["name"].split(".")[1], p["payload"]["value"], wizard_api)
|
|
|
|
def ack(msgid, mac, name, command, value, wizard_api):
|
|
#logger.debug(mac)
|
|
macsquish = "".join(mac.split(":")[:-2])
|
|
maclower = ":".join(mac.split(":")[:-2])
|
|
maclower = maclower.lower()
|
|
#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(macsquish,name, value), "msgid": str(msgid)}]))
|
|
wizard_api.mqtt_publish("meshify/db/194/_/mainMeshify/" + maclower + ":00:00/commands", json.dumps([{"value": {"status": "success", "value": str(value), "channel": command}, "msgid": str(msgid)}]))
|
|
|
|
def convert_int(plc_tag, value):
|
|
|
|
status_codes = {
|
|
0: "Off",
|
|
1: "On"
|
|
}
|
|
|
|
plc_tags = {
|
|
"pump_1_run_status": status_codes.get(value, "Invalid Code"),
|
|
"pump_2_run_status": status_codes.get(value, "Invalid Code"),
|
|
"charge_pump_run_status": status_codes.get(value, "Invalid Code")
|
|
}
|
|
|
|
return plc_tags.get(plc_tag, "Invalid Tag")
|
|
|
|
|