Add type field
This commit is contained in:
@@ -17,22 +17,23 @@ class MQTTForwarder:
|
||||
self._central_broker = dst_mqtt
|
||||
self._qos = qos
|
||||
|
||||
def start(self, src_topic: str, dst_topic: str):
|
||||
def start(self, src_topic: str, dst_topic: str, sensor_type: str):
|
||||
try:
|
||||
def forward_handler(topic: str, msg: str):
|
||||
forwarded_msg = self.__wrap_data(msg)
|
||||
forwarded_msg = self.__wrap_data(msg, sensor_type)
|
||||
self._central_broker.publish(dst_topic, forwarded_msg, self._qos)
|
||||
|
||||
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}")
|
||||
|
||||
def __wrap_data(self, msg : str):
|
||||
def __wrap_data(self, msg : str, sensor_type: str):
|
||||
print(repr(msg))
|
||||
result = {}
|
||||
data = json.loads(msg)
|
||||
result["timestamp"] = int(time.time())
|
||||
result["systemId"] = self._client_id
|
||||
result["type"] = sensor_type
|
||||
result["data"] = {}
|
||||
for keys in data:
|
||||
result["data"][keys] = data[keys]
|
||||
|
||||
Reference in New Issue
Block a user