63 lines
2.3 KiB
Python
63 lines
2.3 KiB
Python
import json, os, time
|
|
from datetime import datetime as dt
|
|
from common.Logger import logger
|
|
from quickfaas.remotebus import publish
|
|
from quickfaas.global_dict import get as get_params
|
|
from quickfaas.global_dict import _set_global_args
|
|
|
|
def reboot():
|
|
logger.info("!" * 10 + "REBOOTING DEVICE" + "!"*10)
|
|
r = os.popen("kill -s SIGHUP `cat /var/run/python/supervisord.pid`").read()
|
|
logger.info(f"REBOOT : {r}")
|
|
|
|
|
|
|
|
# Helper function to split the payload into chunks
|
|
def chunk_payload(payload, chunk_size=20):
|
|
chunked_values = list(payload["values"].items())
|
|
for i in range(0, len(chunked_values), chunk_size):
|
|
yield {
|
|
"ts": payload["ts"],
|
|
"values": dict(chunked_values[i:i+chunk_size])
|
|
}
|
|
|
|
|
|
def sendData(message):
|
|
#logger.debug(message)
|
|
payload = {"ts": (round(dt.timestamp(dt.now())/600)*600)*1000, "values": {}}
|
|
for measure in message["measures"]:
|
|
try:
|
|
logger.debug(measure)
|
|
if measure["name"] in ["pump_1_run_status", "pump_2_run_status", "charge_pump_run_status"]:
|
|
logger.info(measure["name"])
|
|
logger.debug("Converting DINT/BOOL to STRING")
|
|
value = convert_int(measure["name"], measure["value"])
|
|
logger.debug("Converted {} to {}".format(measure["value"], value))
|
|
payload["values"][measure["name"]] = value
|
|
payload["values"][measure["name"] + "_int"] = measure["value"]
|
|
else:
|
|
payload["values"][measure["name"]] = measure["value"]
|
|
except Exception as e:
|
|
logger.error(e)
|
|
logger.info(payload)
|
|
for chunk in chunk_payload(payload=payload):
|
|
publish(__topic__, json.dumps(chunk), __qos__, cloud_name="ThingsBoard")
|
|
time.sleep(2)
|
|
publish("v1/devices/me/attributes", json.dumps({"latestReportTime": (round(dt.timestamp(dt.now())/600)*600)*1000}), __qos__, cloud_name="ThingsBoard")
|
|
|
|
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")
|
|
|
|
|