108 lines
3.7 KiB
Python
108 lines
3.7 KiB
Python
import json, os, time
|
|
from common.Logger import logger
|
|
from quickfaas.remotebus import publish
|
|
from datetime import datetime as dt
|
|
from mobiuspi_lib.gps import GPS
|
|
from quickfaas.global_dict import get as get_params
|
|
from quickfaas.global_dict import _set_global_args
|
|
|
|
def getGPS():
|
|
# Create a gps instance
|
|
gps = GPS()
|
|
|
|
# Retrieve GPS information
|
|
position_status = gps.get_position_status()
|
|
logger.debug("position_status: ")
|
|
logger.debug(position_status)
|
|
latitude = position_status["latitude"].split(" ")
|
|
longitude = position_status["longitude"].split(" ")
|
|
lat_dec = int(latitude[0][:-1]) + (float(latitude[1][:-1])/60)
|
|
lon_dec = int(longitude[0][:-1]) + (float(longitude[1][:-1])/60)
|
|
if latitude[2] == "S":
|
|
lat_dec = lat_dec * -1
|
|
if longitude[2] == "W":
|
|
lon_dec = lon_dec * -1
|
|
#lat_dec = round(lat_dec, 7)
|
|
#lon_dec = round(lon_dec, 7)
|
|
logger.info("HERE IS THE GPS COORDS")
|
|
logger.info(f"LATITUDE: {lat_dec}, LONGITUDE: {lon_dec}")
|
|
speedKnots = position_status["speed"].split(" ")
|
|
speedMPH = float(speedKnots[0]) * 1.151
|
|
return (f"{lat_dec:.8f}",f"{lon_dec:.8f}",f"{speedMPH:.2f}")
|
|
|
|
def chunk_payload(payload, chunk_size=20):
|
|
if "values" in payload:
|
|
# Original format: {"ts": ..., "values": {...}}
|
|
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])
|
|
}
|
|
else:
|
|
# New format: {"key1": "value1", "key2": "value2"}
|
|
chunked_keys = list(payload.keys())
|
|
for i in range(0, len(chunked_keys), chunk_size):
|
|
yield {k: payload[k] for k in chunked_keys[i:i+chunk_size]}
|
|
|
|
def sendData(message):
|
|
#logger.debug(message)
|
|
|
|
|
|
payload = {"ts": (round(dt.timestamp(dt.now())/600)*600)*1000, "values": {}}
|
|
attributes_payload = {}
|
|
for measure in message["measures"]:
|
|
try:
|
|
logger.debug(measure)
|
|
if measure["health"] == 1:
|
|
if measure["name"] in ["auto_manual", "auto_control_mode", "device_status"]:
|
|
logger.debug("Converting DINT/BOOL to STRING")
|
|
payload["values"][measure["name"]] = convert_int(measure["name"], measure["value"])
|
|
payload["values"][measure["name"] + "_int"] = measure["value"]
|
|
else:
|
|
payload["values"][measure["name"]] = measure["value"]
|
|
except Exception as e:
|
|
logger.error(e)
|
|
|
|
try:
|
|
payload["values"]["latitude"], payload["values"]["longitude"], payload["values"]["speed"] = getGPS()
|
|
except:
|
|
logger.error("Could not get GPS coordinates")
|
|
|
|
for chunk in chunk_payload(payload=payload):
|
|
publish(__topic__, json.dumps(chunk), __qos__, cloud_name="ThingsBoard")
|
|
time.sleep(2)
|
|
|
|
attributes_payload["latestReportTime"] = (round(dt.timestamp(dt.now())/600)*600)*1000
|
|
for chunk in chunk_payload(payload=attributes_payload):
|
|
publish("v1/devices/me/attributes", json.dumps(chunk), __qos__, cloud_name="ThingsBoard")
|
|
time.sleep(2)
|
|
|
|
|
|
def convert_int(plc_tag, value):
|
|
auto_manual = {
|
|
0: "Manual",
|
|
1: "Auto"
|
|
}
|
|
|
|
auto_control_mode = {
|
|
0: "Pressure",
|
|
1: "Flow"
|
|
}
|
|
|
|
device_status = {
|
|
1: "Running",
|
|
64: "Idle",
|
|
128: "Overpressure",
|
|
1024: "Faulted"
|
|
}
|
|
|
|
plc_tags = {
|
|
"auto_manual": auto_manual.get(value, "Invalid Code"),
|
|
"auto_control_mode": auto_control_mode.get(value, "Invalid Code"),
|
|
"device_status": device_status.get(value, "Invalid Code"),
|
|
}
|
|
|
|
return plc_tags.get(plc_tag, "Invalid Tag")
|
|
|
|
|