Skip to content

Commit dd910e7

Browse files
authored
Merge pull request #17 from Enapter/rnovatorov/dev
Split Enapter MQTT API Logic Into A Separate Package
2 parents 5b2f221 + 6dbc1db commit dd910e7

File tree

19 files changed

+74
-59
lines changed

19 files changed

+74
-59
lines changed

enapter/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.8.0"
1+
__version__ = "0.9.0"
22

33
from . import async_, log, mdns, mqtt, types, vucm
44

enapter/mqtt/__init__.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1+
from . import api
12
from .client import Client
2-
from .command import CommandRequest, CommandResponse, CommandState
33
from .config import Config
4-
from .device_channel import DeviceChannel, DeviceLogSeverity
54

65
__all__ = [
6+
"api",
77
"Client",
8-
"CommandRequest",
9-
"CommandResponse",
10-
"CommandState",
118
"Config",
12-
"DeviceChannel",
13-
"DeviceLogSeverity",
149
]

enapter/mqtt/api/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from .command import CommandRequest, CommandResponse, CommandState
2+
from .device_channel import DeviceChannel
3+
from .log_severity import LogSeverity
4+
5+
__all__ = [
6+
"CommandRequest",
7+
"CommandResponse",
8+
"CommandState",
9+
"DeviceChannel",
10+
"LogSeverity",
11+
]
File renamed without changes.

enapter/mqtt/device_channel.py renamed to enapter/mqtt/api/device_channel.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import enum
21
import json
32
import logging
43
import time
54

6-
from .. import async_
5+
import enapter
6+
77
from .command import CommandRequest
88

99
LOGGER = logging.getLogger(__name__)
@@ -29,7 +29,7 @@ def _new_logger(hardware_id, channel_id):
2929
extra = {"hardware_id": hardware_id, "channel_id": channel_id}
3030
return logging.LoggerAdapter(LOGGER, extra=extra)
3131

32-
@async_.generator
32+
@enapter.async_.generator
3333
async def subscribe_to_command_requests(self):
3434
async with self._subscribe("v1/command/requests") as messages:
3535
async for msg in messages:
@@ -72,10 +72,3 @@ async def _publish(self, path, payload, **kwargs):
7272
await self._client.publish(topic, payload, **kwargs)
7373
except Exception as e:
7474
self._logger.error("failed to publish %s: %r", path, e)
75-
76-
77-
class DeviceLogSeverity(enum.Enum):
78-
DEBUG = "debug"
79-
INFO = "info"
80-
WARNING = "warning"
81-
ERROR = "error"

enapter/mqtt/api/log_severity.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import enum
2+
3+
4+
class LogSeverity(enum.Enum):
5+
DEBUG = "debug"
6+
INFO = "info"
7+
WARNING = "warning"
8+
ERROR = "error"

enapter/mqtt/client.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77

88
import aiomqtt
99

10-
from .. import async_, mdns
11-
from .device_channel import DeviceChannel
10+
import enapter
1211

1312
LOGGER = logging.getLogger(__name__)
1413

1514

16-
class Client(async_.Routine):
15+
class Client(enapter.async_.Routine):
1716
def __init__(self, config):
1817
self._logger = self._new_logger(config)
1918
self._config = config
20-
self._mdns_resolver = mdns.Resolver()
19+
self._mdns_resolver = enapter.mdns.Resolver()
2120
self._tls_context = self._new_tls_context(config)
2221
self._client = None
2322
self._client_ready = asyncio.Event()
@@ -31,16 +30,11 @@ def _new_logger(config):
3130
def config(self):
3231
return self._config
3332

34-
def device_channel(self, hardware_id, channel_id):
35-
return DeviceChannel(
36-
client=self, hardware_id=hardware_id, channel_id=channel_id
37-
)
38-
3933
async def publish(self, *args, **kwargs):
4034
client = await self._wait_client()
4135
await client.publish(*args, **kwargs)
4236

43-
@async_.generator
37+
@enapter.async_.generator
4438
async def subscribe(self, topic):
4539
while True:
4640
client = await self._wait_client()

enapter/vucm/app.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import asyncio
22

3-
from .. import async_, log, mqtt
3+
import enapter
4+
45
from .config import Config
56
from .ucm import UCM
67

78

89
async def run(device_factory):
9-
log.configure(level=log.LEVEL or "info")
10+
enapter.log.configure(level=enapter.log.LEVEL or "info")
1011

1112
config = Config.from_env()
1213

1314
async with App(config=config, device_factory=device_factory) as app:
1415
await app.join()
1516

1617

17-
class App(async_.Routine):
18+
class App(enapter.async_.Routine):
1819
def __init__(self, config, device_factory):
1920
self._config = config
2021
self._device_factory = device_factory
@@ -23,7 +24,7 @@ async def _run(self):
2324
tasks = set()
2425

2526
mqtt_client = await self._stack.enter_async_context(
26-
mqtt.Client(config=self._config.mqtt)
27+
enapter.mqtt.Client(config=self._config.mqtt)
2728
)
2829
tasks.add(mqtt_client.task())
2930

@@ -35,7 +36,7 @@ async def _run(self):
3536

3637
device = await self._stack.enter_async_context(
3738
self._device_factory(
38-
channel=mqtt_client.device_channel(
39+
channel=enapter.mqtt.api.DeviceChannel(
3940
hardware_id=self._config.hardware_id,
4041
channel_id=self._config.channel_id,
4142
)

enapter/vucm/config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
import os
44

5-
from .. import mqtt
5+
import enapter
66

77

88
class Config:
@@ -23,7 +23,7 @@ def from_env(cls, prefix="ENAPTER_VUCM_", env=os.environ):
2323
hardware_id = os.environ[prefix + "HARDWARE_ID"]
2424
channel_id = os.environ[prefix + "CHANNEL_ID"]
2525

26-
mqtt_config = mqtt.Config.from_env(prefix=prefix, env=env)
26+
mqtt_config = enapter.mqtt.Config.from_env(prefix=prefix, env=env)
2727

2828
start_ucm = os.environ.get(prefix + "START_UCM", "1") != "0"
2929

@@ -38,7 +38,7 @@ def from_env(cls, prefix="ENAPTER_VUCM_", env=os.environ):
3838
def from_blob(cls, blob):
3939
payload = json.loads(base64.b64decode(blob))
4040

41-
mqtt_config = mqtt.Config(
41+
mqtt_config = enapter.mqtt.Config(
4242
host=payload["mqtt_host"],
4343
port=int(payload["mqtt_port"]),
4444
tls_ca_cert=payload["mqtt_ca"],

enapter/vucm/device.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import traceback
55
from typing import Optional, Set
66

7-
from .. import async_, mqtt, types
7+
import enapter
8+
89
from .logger import Logger
910

1011

11-
class Device(async_.Routine):
12+
class Device(enapter.async_.Routine):
1213
def __init__(
1314
self,
1415
channel,
@@ -28,7 +29,9 @@ def __init__(
2829
self.log = Logger(channel=channel)
2930
self.alerts: Set[str] = set()
3031

31-
async def send_telemetry(self, telemetry: Optional[types.JSON] = None) -> None:
32+
async def send_telemetry(
33+
self, telemetry: Optional[enapter.types.JSON] = None
34+
) -> None:
3235
if telemetry is None:
3336
telemetry = {}
3437
else:
@@ -38,7 +41,9 @@ async def send_telemetry(self, telemetry: Optional[types.JSON] = None) -> None:
3841

3942
await self.__channel.publish_telemetry(telemetry)
4043

41-
async def send_properties(self, properties: Optional[types.JSON] = None) -> None:
44+
async def send_properties(
45+
self, properties: Optional[enapter.types.JSON] = None
46+
) -> None:
4247
if properties is None:
4348
properties = {}
4449
else:
@@ -104,9 +109,11 @@ async def __execute_command(self, req):
104109
try:
105110
cmd = getattr(self, self.__cmd_prefix + req.name)
106111
except AttributeError:
107-
return mqtt.CommandState.ERROR, {"reason": "unknown command"}
112+
return enapter.mqtt.api.CommandState.ERROR, {"reason": "unknown command"}
108113

109114
try:
110-
return mqtt.CommandState.COMPLETED, await cmd(**req.args)
115+
return enapter.mqtt.api.CommandState.COMPLETED, await cmd(**req.args)
111116
except:
112-
return mqtt.CommandState.ERROR, {"traceback": traceback.format_exc()}
117+
return enapter.mqtt.api.CommandState.ERROR, {
118+
"traceback": traceback.format_exc()
119+
}

0 commit comments

Comments
 (0)