45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
# Enter your python code.
|
|
import json
|
|
from datetime import datetime as dt
|
|
from common.Logger import logger
|
|
from quickfaas.remotebus import publish
|
|
|
|
|
|
def sendData(message):
|
|
#logger.debug(message)
|
|
# Extract measures and group by ctrlName
|
|
grouped_data = {}
|
|
grouped_attributes = {}
|
|
now = (round(dt.timestamp(dt.now())/600)*600)*1000
|
|
for measure in message['measures']:
|
|
ctrlName = " ".join(measure['ctrlName'].split("_"))
|
|
name = measure['name']
|
|
value = measure['value']
|
|
health = measure['health']
|
|
#Add controller for telemetry if it doesn't exist
|
|
if ctrlName not in grouped_data:
|
|
grouped_data[ctrlName] = {}
|
|
#Add controller for attributes if it doesn't exist
|
|
if ctrlName not in grouped_attributes:
|
|
grouped_attributes[ctrlName] = {}
|
|
#Add data to temp payload if datapoint health is good
|
|
if health:
|
|
grouped_data[ctrlName][name] = value
|
|
grouped_attributes[ctrlName]["latestReportTime"] = now
|
|
#print(grouped_data)
|
|
# Transform the grouped data to desired structure
|
|
payload = {}
|
|
|
|
for key, value in grouped_data.items():
|
|
if value:
|
|
payload[key] = [{"ts": now ,"values": value}]
|
|
attributes_payload = {}
|
|
for key, value in grouped_attributes.items():
|
|
if value:
|
|
attributes_payload[key] = value
|
|
|
|
|
|
|
|
#logger.debug(payload)
|
|
publish(__topic__, json.dumps(payload), __qos__)
|
|
publish("v1/gateway/attributes", json.dumps(attributes_payload), __qos__) |