|
15 | 15 | from aiomqtt import MqttCodeError |
16 | 16 | from paho.mqtt.client import MQTT_ERR_NO_CONN |
17 | 17 |
|
18 | | -from smartmeter_datacollector.sinks.mqtt_sink import MqttConfig, MqttDataSink |
| 18 | +from smartmeter_datacollector.sinks.mqtt_sink import MqttConfig, MqttDataSink, MqttSinkRlDsp |
19 | 19 | from smartmeter_datacollector.smartmeter.meter_data import MeterDataBundle, MeterDataPoint, MeterDataPointType |
20 | 20 | from smartmeter_datacollector.smartmeter.obis import OBISCode |
21 | 21 |
|
22 | 22 | TEST_DATA_POINT_TYPE = MeterDataPointType("TEST_TYPE", "test type", "unit") |
23 | 23 | TEST_OBIS = OBISCode(0, 1, 2, 3, 4, 5) |
| 24 | +TEST_OBIS_2 = OBISCode(1, 1, 2, 8, 0, 255) |
24 | 25 |
|
25 | 26 |
|
26 | 27 | @pytest.fixture(autouse=True) |
@@ -190,3 +191,84 @@ def test_mqtt_config_encrypted_authorized_client_cert(_: mock.MagicMock): |
190 | 191 |
|
191 | 192 | with mock.patch("smartmeter_datacollector.sinks.mqtt_sink.ssl"): |
192 | 193 | sink = MqttDataSink(cfg) |
| 194 | + |
| 195 | + |
| 196 | +@pytest.mark.asyncio |
| 197 | +async def test_mqtt_sink_rldsp_send_publishes_single_message(mocked_mqtt_client: mock.MagicMock): |
| 198 | + config = MqttConfig("localhost") |
| 199 | + sink = MqttSinkRlDsp(config) |
| 200 | + |
| 201 | + timestamp = datetime(2024, 1, 15, 10, 30, 0, tzinfo=timezone.utc) |
| 202 | + data_point = MeterDataPoint(TEST_DATA_POINT_TYPE, 42.0, TEST_OBIS) |
| 203 | + data_bundle = MeterDataBundle("meter1", timestamp, [data_point]) |
| 204 | + |
| 205 | + await sink.send(data_bundle) |
| 206 | + |
| 207 | + assert mocked_mqtt_client.publish.await_count == 1 |
| 208 | + |
| 209 | + |
| 210 | +@pytest.mark.asyncio |
| 211 | +async def test_mqtt_sink_rldsp_send_uses_correct_topic(mocked_mqtt_client: mock.MagicMock): |
| 212 | + config = MqttConfig("localhost") |
| 213 | + sink = MqttSinkRlDsp(config) |
| 214 | + |
| 215 | + timestamp = datetime(2024, 1, 15, 10, 30, 0, tzinfo=timezone.utc) |
| 216 | + data_point = MeterDataPoint(TEST_DATA_POINT_TYPE, 42.0, TEST_OBIS) |
| 217 | + data_bundle = MeterDataBundle("meter1", timestamp, [data_point]) |
| 218 | + expected_topic = "dt/building/meter1/ds" |
| 219 | + |
| 220 | + await sink.send(data_bundle) |
| 221 | + |
| 222 | + mocked_mqtt_client.publish.assert_awaited_once_with(expected_topic, mock.ANY) |
| 223 | + |
| 224 | + |
| 225 | +@pytest.mark.asyncio |
| 226 | +async def test_mqtt_sink_rldsp_send_multiple_datapoints_single_publish(mocked_mqtt_client: mock.MagicMock): |
| 227 | + config = MqttConfig("localhost") |
| 228 | + sink = MqttSinkRlDsp(config) |
| 229 | + |
| 230 | + timestamp = datetime(2024, 1, 15, 10, 30, 0, tzinfo=timezone.utc) |
| 231 | + data_points = [ |
| 232 | + MeterDataPoint(TEST_DATA_POINT_TYPE, 100.0, TEST_OBIS), |
| 233 | + MeterDataPoint(TEST_DATA_POINT_TYPE, 200.0, TEST_OBIS_2), |
| 234 | + ] |
| 235 | + data_bundle = MeterDataBundle("meter1", timestamp, data_points) |
| 236 | + |
| 237 | + await sink.send(data_bundle) |
| 238 | + |
| 239 | + assert mocked_mqtt_client.publish.await_count == 1 |
| 240 | + |
| 241 | + |
| 242 | +def test_mqtt_sink_rldsp_to_mqtt_payload_contains_timestamp(): |
| 243 | + timestamp = datetime(2024, 1, 15, 10, 30, 0, tzinfo=timezone.utc) |
| 244 | + data_point = MeterDataPoint(TEST_DATA_POINT_TYPE, 42.0, TEST_OBIS) |
| 245 | + data_bundle = MeterDataBundle("meter1", timestamp, [data_point]) |
| 246 | + |
| 247 | + payload = json.loads(MqttSinkRlDsp.to_mqtt_payload(data_bundle)) |
| 248 | + |
| 249 | + assert payload["meter"]["ts"] == timestamp.isoformat() |
| 250 | + |
| 251 | + |
| 252 | +def test_mqtt_sink_rldsp_to_mqtt_payload_contains_obis_values(): |
| 253 | + timestamp = datetime(2024, 1, 15, 10, 30, 0, tzinfo=timezone.utc) |
| 254 | + data_points = [ |
| 255 | + MeterDataPoint(TEST_DATA_POINT_TYPE, 100.0, TEST_OBIS), |
| 256 | + MeterDataPoint(TEST_DATA_POINT_TYPE, 200.0, TEST_OBIS_2), |
| 257 | + ] |
| 258 | + data_bundle = MeterDataBundle("meter1", timestamp, data_points) |
| 259 | + |
| 260 | + payload = json.loads(MqttSinkRlDsp.to_mqtt_payload(data_bundle)) |
| 261 | + |
| 262 | + assert payload["meter"][TEST_OBIS.to_short_str()] == 100.0 |
| 263 | + assert payload["meter"][TEST_OBIS_2.to_short_str()] == 200.0 |
| 264 | + |
| 265 | + |
| 266 | +def test_mqtt_sink_rldsp_build_topic_name(): |
| 267 | + config = MqttConfig("localhost") |
| 268 | + sink = MqttSinkRlDsp(config) |
| 269 | + timestamp = datetime(2024, 1, 15, 10, 30, 0, tzinfo=timezone.utc) |
| 270 | + data_bundle = MeterDataBundle("my_meter", timestamp, []) |
| 271 | + |
| 272 | + topic = sink.build_topic_name(data_bundle) |
| 273 | + |
| 274 | + assert topic == "dt/building/my_meter/ds" |
0 commit comments