Skip to content

Commit

Permalink
Merge pull request #64 from scs/bugfix/unnecessary-fallback-to-system…
Browse files Browse the repository at this point in the history
…time

Bugfix/unnecessary fallback to systemtime
  • Loading branch information
raymar9 authored Mar 13, 2024
2 parents 4830565 + 6413b9a commit d497a3e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions smartmeter_datacollector/smartmeter/hdlc_dlms_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def convert_dlms_bundle_to_reader_data(self, dlms_objects: List[GXDLMSObject],

timestamp = None
if self._use_system_time:
timestamp = datetime.utcnow()
timestamp = datetime.now(timezone.utc)

if not timestamp:
timestamp = self._cosem.retrieve_time_from_dlms_registers(obis_obj_pairs)
Expand All @@ -131,7 +131,7 @@ def convert_dlms_bundle_to_reader_data(self, dlms_objects: List[GXDLMSObject],
if not timestamp:
LOGGER.warning("Unable to get timestamp from message. Falling back to system time.")
self._use_system_time = True
timestamp = datetime.utcnow()
timestamp = datetime.now(timezone.utc)

if not timestamp.tzinfo:
# if timezone info not set, assume UTC
Expand Down
4 changes: 3 additions & 1 deletion smartmeter_datacollector/smartmeter/meter.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ def _data_received(self, received_data: bytes) -> None:

self._parser.append_to_hdlc_buffer(received_data)
if self._parser.extract_data_from_hdlc_frames():
message_time = self._parser.extract_message_time()
dlms_objects = self._parser.parse_to_dlms_objects()
if not dlms_objects:
return
message_time = self._parser.extract_message_time()
data_points = self._parser.convert_dlms_bundle_to_reader_data(dlms_objects, message_time)
self._notify_observers(data_points)
10 changes: 5 additions & 5 deletions tests/test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
import asyncio
import sys
from datetime import datetime
from datetime import datetime, timezone

import pytest
from pytest_mock import MockerFixture
Expand All @@ -27,7 +27,7 @@ def test_type() -> MeterDataPointType:
async def test_collector_with_one_sink(mocker: MockerFixture, test_type: MeterDataPointType):
coll = Collector()
sink = mocker.AsyncMock(DataSink)
data_point = MeterDataPoint(test_type, 0.0, "test_source", datetime.utcnow())
data_point = MeterDataPoint(test_type, 0.0, "test_source", datetime.now(timezone.utc))

coll.register_sink(sink)
coll.notify([data_point])
Expand All @@ -46,8 +46,8 @@ async def test_collector_with_one_sink_multiple_data_points(mocker: MockerFixtur
sink = mocker.AsyncMock(DataSink)

coll.register_sink(sink)
point0 = MeterDataPoint(test_type, 0.0, "test_source", datetime.utcnow())
point1 = MeterDataPoint(test_type, 1.0, "test_source", datetime.utcnow())
point0 = MeterDataPoint(test_type, 0.0, "test_source", datetime.now(timezone.utc))
point1 = MeterDataPoint(test_type, 1.0, "test_source", datetime.now(timezone.utc))
coll.notify([point0, point1])
routine = coll.process_queue()

Expand All @@ -65,7 +65,7 @@ async def test_collector_with_two_sinks(mocker: MockerFixture, test_type: MeterD
sink0 = mocker.AsyncMock(DataSink)
sink1 = mocker.AsyncMock(DataSink)

data_point = MeterDataPoint(test_type, 0.0, "test_source", datetime.utcnow())
data_point = MeterDataPoint(test_type, 0.0, "test_source", datetime.now(timezone.utc))

coll.register_sink(sink0)
coll.register_sink(sink1)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_meter_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
# See LICENSES/README.md for more information.
#
import json
from datetime import datetime
from datetime import datetime, timezone

from smartmeter_datacollector.smartmeter.meter_data import MeterDataPoint, MeterDataPointType


def test_meter_data_serialize():
test_type = MeterDataPointType("TEST_TYPE", "test type", "unit")
data_point = MeterDataPoint(test_type, 1.0, "TestSource", datetime.utcnow())
data_point = MeterDataPoint(test_type, 1.0, "TestSource", datetime.now(timezone.utc))

data_point_json = data_point.to_json()

Expand Down
6 changes: 3 additions & 3 deletions tests/test_mqtt_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import configparser
import json
import sys
from datetime import datetime
from datetime import datetime, timezone
from unittest import mock

import pytest
Expand Down Expand Up @@ -42,7 +42,7 @@ async def test_mqtt_sink_send_point_when_started(mocker: MockerFixture):
config = MqttConfig("localhost")
sink = MqttDataSink(config)
client_mock = mocker.patch.object(sink, "_client", autospec=True)
data_point = MeterDataPoint(TEST_TYPE, 1.0, "test_source", datetime.utcnow())
data_point = MeterDataPoint(TEST_TYPE, 1.0, "test_source", datetime.now(timezone.utc))
expected_topic = f"smartmeter/test_source/{TEST_TYPE.identifier}"
expected_payload = json.dumps({
"value": data_point.value,
Expand All @@ -61,7 +61,7 @@ async def test_mqtt_sink_send_reconnect_when_not_started(mocker: MockerFixture):
config = MqttConfig("localhost")
sink = MqttDataSink(config)
client_mock = mocker.patch.object(sink, "_client", autospec=True)
data_point = MeterDataPoint(TEST_TYPE, 1.0, "test_source", datetime.utcnow())
data_point = MeterDataPoint(TEST_TYPE, 1.0, "test_source", datetime.now(timezone.utc))

client_mock.publish.side_effect = MqttCodeError(MQTT_ERR_NO_CONN)
await sink.send(data_point)
Expand Down

0 comments on commit d497a3e

Please sign in to comment.