Format JSON payload
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
import datetime
|
||||
import json
|
||||
import time
|
||||
|
||||
from services.mqtt_service import MQTTService
|
||||
|
||||
class MQTTForwarder:
|
||||
@@ -14,8 +18,19 @@ class MQTTForwarder:
|
||||
def start(self, src_topic: str, dst_topic: str):
|
||||
try:
|
||||
def forward_handler(topic: str, msg: str):
|
||||
self.central_broker.publish(dst_topic, msg)
|
||||
forwarded_msg = self.__wrapData(msg)
|
||||
self.central_broker.publish(dst_topic, forwarded_msg)
|
||||
|
||||
self.local_broker.subscribe(src_topic, forward_handler)
|
||||
except Exception as e:
|
||||
print(f"An error occurred while forwarding from {src_topic} to {dst_topic}: {e}")
|
||||
print(f"An error occurred while forwarding from {src_topic} to {dst_topic}: {e}")
|
||||
|
||||
def __wrapData(self, msg : str):
|
||||
result = {}
|
||||
data = json.loads(msg)
|
||||
result["timestamp"] = int(time.time())
|
||||
result["systemId"] = self.client_id
|
||||
result["data"] = {}
|
||||
for keys in data:
|
||||
result["data"][keys] = data[keys]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user