Skip to content

Commit 3c1c1e3

Browse files
committed
move logging from mqtt.api.device.channel to standalone.mqtt_adapter
1 parent 633aa3a commit 3c1c1e3

File tree

3 files changed

+52
-31
lines changed

3 files changed

+52
-31
lines changed

src/enapter/mqtt/api/device/channel.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import logging
21
from typing import AsyncContextManager, AsyncGenerator
32

43
from enapter import async_, mqtt
@@ -9,14 +8,11 @@
98
from .properties import Properties
109
from .telemetry import Telemetry
1110

12-
LOGGER = logging.getLogger(__name__)
13-
1411

1512
class Channel:
1613

1714
def __init__(self, client: mqtt.Client, hardware_id: str, channel_id: str) -> None:
1815
self._client = client
19-
self._logger = self._new_logger(hardware_id, channel_id)
2016
self._hardware_id = hardware_id
2117
self._channel_id = channel_id
2218

@@ -28,11 +24,6 @@ def hardware_id(self) -> str:
2824
def channel_id(self) -> str:
2925
return self._channel_id
3026

31-
@staticmethod
32-
def _new_logger(hardware_id: str, channel_id: str) -> logging.LoggerAdapter:
33-
extra = {"hardware_id": hardware_id, "channel_id": channel_id}
34-
return logging.LoggerAdapter(LOGGER, extra=extra)
35-
3627
@async_.generator
3728
async def subscribe_to_command_requests(
3829
self,
@@ -62,7 +53,4 @@ def _subscribe(
6253

6354
async def _publish(self, path: str, payload: str, **kwargs) -> None:
6455
topic = f"v1/from/{self._hardware_id}/{self._channel_id}/{path}"
65-
try:
66-
await self._client.publish(topic, payload, **kwargs)
67-
except Exception as e:
68-
self._logger.error("failed to publish %s: %r", path, e)
56+
await self._client.publish(topic, payload, **kwargs)

src/enapter/standalone/mqtt_adapter.py

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
import asyncio
22
import contextlib
3+
import logging
34
import time
45
import traceback
56

67
from enapter import async_, mqtt
78

89
from .device_protocol import DeviceProtocol
910

11+
LOGGER = logging.getLogger(__name__)
12+
1013

1114
class MQTTAdapter(async_.Routine):
1215

1316
def __init__(
1417
self,
15-
device_channel: mqtt.api.device.Channel,
18+
hardware_id: str,
19+
channel_id: str,
20+
mqtt_api_client: mqtt.api.Client,
1621
device: DeviceProtocol,
1722
task_group: asyncio.TaskGroup | None,
1823
) -> None:
1924
super().__init__(task_group=task_group)
20-
self._device_channel = device_channel
25+
self._logger = logging.LoggerAdapter(
26+
LOGGER, extra={"hardware_id": hardware_id, "channel_id": channel_id}
27+
)
28+
self._device_channel = mqtt_api_client.device_channel(hardware_id, channel_id)
2129
self._device = device
2230

2331
async def _run(self) -> None:
@@ -33,36 +41,63 @@ async def _stream_properties(self) -> None:
3341
async for properties in stream:
3442
properties = properties.copy()
3543
timestamp = properties.pop("timestamp", int(time.time()))
36-
await self._device_channel.publish_properties(
37-
properties=mqtt.api.device.Properties(
38-
timestamp=timestamp, values=properties
39-
)
44+
await self._publish_properties(
45+
mqtt.api.device.Properties(timestamp=timestamp, values=properties)
4046
)
4147

48+
async def _publish_properties(self, properties: mqtt.api.device.Properties) -> None:
49+
try:
50+
await self._device_channel.publish_properties(properties=properties)
51+
except Exception as e:
52+
self._logger.error("failed to publish properties: %s", e)
53+
4254
async def _stream_telemetry(self) -> None:
4355
async with contextlib.aclosing(self._device.stream_telemetry()) as stream:
4456
async for telemetry in stream:
4557
telemetry = telemetry.copy()
4658
timestamp = telemetry.pop("timestamp", int(time.time()))
4759
alerts = telemetry.pop("alerts", None)
48-
await self._device_channel.publish_telemetry(
49-
telemetry=mqtt.api.device.Telemetry(
60+
await self._publish_telemetry(
61+
mqtt.api.device.Telemetry(
5062
timestamp=timestamp, alerts=alerts, values=telemetry
5163
)
5264
)
5365

66+
async def _publish_telemetry(self, telemetry: mqtt.api.device.Telemetry) -> None:
67+
try:
68+
await self._device_channel.publish_telemetry(telemetry=telemetry)
69+
except Exception as e:
70+
self._logger.error("failed to publish telemetry: %s", e)
71+
5472
async def _stream_logs(self) -> None:
5573
async with contextlib.aclosing(self._device.stream_logs()) as stream:
5674
async for log in stream:
57-
await self._device_channel.publish_log(
58-
log=mqtt.api.device.Log(
75+
match log.severity:
76+
case "debug":
77+
self._logger.debug(log.message)
78+
case "info":
79+
self._logger.info(log.message)
80+
case "warning":
81+
self._logger.warning(log.message)
82+
case "error":
83+
self._logger.error(log.message)
84+
case _:
85+
raise NotImplementedError(log.severity)
86+
await self._publish_log(
87+
mqtt.api.device.Log(
5988
timestamp=int(time.time()),
6089
severity=mqtt.api.device.LogSeverity(log.severity),
6190
message=log.message,
6291
persist=log.persist,
6392
)
6493
)
6594

95+
async def _publish_log(self, log: mqtt.api.device.Log) -> None:
96+
try:
97+
await self._device_channel.publish_log(log=log)
98+
except Exception as e:
99+
self._logger.error("failed to publish log: %s", e)
100+
66101
async def _execute_commands(self) -> None:
67102
async with asyncio.TaskGroup() as tg:
68103
async with self._device_channel.subscribe_to_command_requests() as requests:

src/enapter/standalone/run.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,19 @@ async def run(device: DeviceProtocol) -> None:
1919
)
2020
_ = await stack.enter_async_context(
2121
MQTTAdapter(
22-
device_channel=mqtt_api_client.device_channel(
23-
hardware_id=config.communication.hardware_id,
24-
channel_id=config.communication.channel_id,
25-
),
22+
hardware_id=config.communication.hardware_id,
23+
channel_id=config.communication.channel_id,
24+
mqtt_api_client=mqtt_api_client,
2625
device=device,
2726
task_group=task_group,
2827
)
2928
)
3029
if config.communication.ucm_needed:
3130
_ = await stack.enter_async_context(
3231
MQTTAdapter(
33-
device_channel=mqtt_api_client.device_channel(
34-
hardware_id=config.communication.hardware_id,
35-
channel_id="ucm",
36-
),
32+
hardware_id=config.communication.hardware_id,
33+
channel_id="ucm",
34+
mqtt_api_client=mqtt_api_client,
3735
device=UCM(),
3836
task_group=task_group,
3937
)

0 commit comments

Comments
 (0)