Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tedge_modbus/reader/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ class MappedMessage:

data: str = ""
topic: str = ""
time: str = datetime.now(timezone.utc).isoformat()

def serialize(self):
"""Serialize message adding time if not present"""
if "/cmd/" in self.topic:
return self.data
out = json.loads(self.data)
if "time" not in out:
out["time"] = self.time
return json.dumps(out)

def extend_data(self, other_message):
"""Combine Json data of two messages with the same topic"""
Expand All @@ -40,6 +50,10 @@ def merge(d1: dict, d2: dict) -> dict:
d2 = json.loads(other_message.data)
# Merge the dictionaries
merged = merge(d1, d2)

if "time" not in merged:
merged["time"] = self.time

# Convert the merged dictionary back to a JSON string and update self.data
self.data = json.dumps(merged)

Expand Down
4 changes: 2 additions & 2 deletions tedge_modbus/reader/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,9 @@ def send_tedge_message(
self, msg: MappedMessage, retain: bool = False, qos: int = 0
):
"""Send a thin-edge.io message via MQTT"""
self.logger.debug("sending message %s to topic %s", msg.data, msg.topic)
self.logger.debug("sending message %s to topic %s", msg.serialize(), msg.topic)
self.tedge_client.publish(
topic=msg.topic, payload=msg.data, retain=retain, qos=qos
topic=msg.topic, payload=msg.serialize(), retain=retain, qos=qos
)

def on_connect(
Expand Down