Skip to content
Merged
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
5 changes: 2 additions & 3 deletions tedge_modbus/reader/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ 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
out["time"] = datetime.now(timezone.utc).isoformat()
return json.dumps(out)

def extend_data(self, other_message):
Expand All @@ -52,7 +51,7 @@ def merge(d1: dict, d2: dict) -> dict:
merged = merge(d1, d2)

if "time" not in merged:
merged["time"] = self.time
merged["time"] = datetime.now(timezone.utc).isoformat()

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