93 lines
3.5 KiB
Python
93 lines
3.5 KiB
Python
# Enter your python code.
|
|
import json
|
|
from quickfaas.measure import recall
|
|
from quickfaas.remotebus import publish
|
|
from common.Logger import logger
|
|
from quickfaas.measure import write_plc_values
|
|
|
|
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:
|
|
if controller["name"] == mac[:-6]:
|
|
if controller["health"] == 1:
|
|
publish("meshify/db/194/_/plcfreshwater/" + mac + "/plc_ping", json.dumps({"value": "OK"}))
|
|
else:
|
|
publish("meshify/db/194/_/plcfreshwater/" + mac + "/plc_ping", json.dumps({"value": "Comms Error to PLC"}))
|
|
for measure in controller["measures"]:
|
|
#publish measure
|
|
topic = "meshify/db/194/_/plcfreshwater/" + mac + "/" + measure["name"]
|
|
if measure["name"] in ["raw_hand_input", "raw_auto_input", "raw_run_status", "raw_local_start","raw_overload_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))
|
|
publish(topic, json.dumps(payload))
|
|
def writeplctag(mac, value, wizard_api):
|
|
try:
|
|
value = json.loads(value.replace("'",'"'))
|
|
logger.debug(value)
|
|
if value["tag"] == "CMD_Cloud_Control":
|
|
value["tag"] = "cmd_cloud_control"
|
|
message = {mac[:-6]:{value["tag"]: value["val"]}}
|
|
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)
|
|
publish("meshify/responses/" + str(msgid), json.dumps([{"value": "{} Success Setting: {} To: {}".format(macsquish,name, value), "msgid": str(msgid)}]))
|
|
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):
|
|
input_codes = {
|
|
0: "Off",
|
|
1: "On"
|
|
}
|
|
|
|
run_status_codes = {
|
|
0: "Stopped",
|
|
1: "Running"
|
|
}
|
|
|
|
overload_codes = {
|
|
0: "Good",
|
|
1: "Down on Overload Tripped"
|
|
}
|
|
|
|
plc_tags = {
|
|
"raw_hand_input": input_codes.get(value, "Invalid Code"),
|
|
"raw_local_start": input_codes.get(value, "Invalid Code"),
|
|
"raw_auto_input": input_codes.get(value, "Invalid Code"),
|
|
"raw_run_status": run_status_codes.get(value, "Invalid Code"),
|
|
"raw_overload_status": overload_codes.get(value, "Invalid Code")
|
|
}
|
|
|
|
return plc_tags.get(plc_tag, "Invalid Tag")
|