Skip to content

Commit

Permalink
Merge pull request #66 from scs/bugfix/obis-local-timestamp
Browse files Browse the repository at this point in the history
Bugfix/obis local timestamp
  • Loading branch information
raymar9 authored Apr 5, 2024
2 parents 02299b5 + 87c7b20 commit 11e80de
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 10 deletions.
3 changes: 2 additions & 1 deletion smartmeter_datacollector/smartmeter/cosem.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ def retrieve_time_from_dlms_registers(self, dlms_objects: Dict[OBISCode, Any]) -
if clock_obj and isinstance(clock_obj, GXDLMSClock):
timestamp = self._extract_datetime(clock_obj)
if timestamp:
return timestamp
# assume local time if tzinfo is None
return timestamp.astimezone(timestamp.tzinfo)
return None

def get_register(self, obis: OBISCode) -> Optional[RegisterCosem]:
Expand Down
11 changes: 6 additions & 5 deletions smartmeter_datacollector/smartmeter/hdlc_dlms_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ def extract_data_from_hdlc_frames(self) -> bool:
def extract_message_time(self) -> Optional[datetime]:
if not isinstance(self._dlms_data.time, GXDateTime):
return None
if isinstance(self._dlms_data.time.value, datetime):
return self._dlms_data.time.value
dt = self._dlms_data.time.value
if isinstance(dt, datetime):
# assume local time if tzinfo is None
return dt.astimezone(dt.tzinfo)
return None

def parse_to_dlms_objects(self) -> List[GXDLMSObject]:
Expand Down Expand Up @@ -133,9 +135,8 @@ def convert_dlms_bundle_to_reader_data(self, dlms_objects: List[GXDLMSObject],
self._use_system_time = True
timestamp = datetime.now(timezone.utc)

if not timestamp.tzinfo:
# if timezone info not set, assume UTC
timestamp = timestamp.replace(tzinfo=timezone.utc)
# convert to UTC format
timestamp = timestamp.astimezone(timezone.utc)

# Extract register data
data_points: List[MeterDataPoint] = []
Expand Down
3 changes: 2 additions & 1 deletion tests/test_hdlc_dlms_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def test_parse_dlms_to_meter_data(self, unencrypted_valid_data_lg: List[bytes],
assert any(data.type == MeterDataPointTypes.ACTIVE_POWER_N.value for data in meter_data)
assert all(isinstance(data.value, float) for data in meter_data)
assert all(data.source == "LGZ1030655933512" for data in meter_data)
assert all(data.timestamp.strftime(r"%m/%d/%y %H:%M:%S") == "07/06/21 14:58:18" for data in meter_data)
assert all(data.timestamp.astimezone().strftime(r"%m/%d/%y %H:%M:%S")
== "07/06/21 14:58:18" for data in meter_data)

def test_parse_dlms_to_meter_data2(self, unencrypted_valid_data_lg2: List[bytes], cosem_config_lg: Cosem):
parser = prepare_parser(unencrypted_valid_data_lg2, cosem_config_lg)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_iskraam550.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ def data_received():
assert any(data.type == MeterDataPointTypes.REACTIVE_ENERGY_Q4.value for data in values)
assert any(data.type == MeterDataPointTypes.POWER_FACTOR.value for data in values)
assert all(data.source == "ISK1030775213859" for data in values)
assert all(data.timestamp.strftime(r"%m/%d/%y %H:%M:%S") == "08/15/20 06:19:45" for data in values)
# message time comes with timezone info (+02:00)
assert all(data.timestamp.strftime(r"%m/%d/%y %H:%M:%S") == "08/15/20 04:19:45" for data in values)
2 changes: 1 addition & 1 deletion tests/test_lge450.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def data_received():
assert any(data.type == MeterDataPointTypes.REACTIVE_ENERGY_Q4.value for data in values)
assert any(data.type == MeterDataPointTypes.POWER_FACTOR.value for data in values)
assert all(data.source == "LGZ1030655933512" for data in values)
assert all(data.timestamp.strftime(r"%m/%d/%y %H:%M:%S") == "07/06/21 14:58:18" for data in values)
assert all(data.timestamp.astimezone().strftime(r"%m/%d/%y %H:%M:%S") == "07/06/21 14:58:18" for data in values)


@pytest.mark.asyncio
Expand Down
1 change: 0 additions & 1 deletion tests/test_lge570.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from .utils import *


@pytest.mark.skipif(sys.version_info < (3, 8), reason="Python3.7 does not support AsyncMock.")
@pytest.mark.asyncio
async def test_lge570_parse_and_provide_encrypted_data(mocker: MockerFixture,
encrypted_valid_data_lge570: List[bytes]):
Expand Down

0 comments on commit 11e80de

Please sign in to comment.