Skip to content

Commit f9cc124

Browse files
committed
http: api: implement expand device communication info
1 parent 66414eb commit f9cc124

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

src/enapter/cli/http/api/device_get_command.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ def register(parent: cli.Subparsers) -> None:
3232
action="store_true",
3333
help="Expand device connectivity information",
3434
)
35+
parser.add_argument(
36+
"-u",
37+
"--communication",
38+
action="store_true",
39+
help="Expand device communication information",
40+
)
3541

3642
@staticmethod
3743
async def run(args: argparse.Namespace) -> None:
@@ -41,5 +47,6 @@ async def run(args: argparse.Namespace) -> None:
4147
expand_manifest=args.manifest,
4248
expand_properties=args.properties,
4349
expand_connectivity=args.connectivity,
50+
expand_communication=args.communication,
4451
)
4552
print(json.dumps(device.to_dto()))

src/enapter/http/api/devices/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .client import Client
33
from .communication_config import CommunicationConfig
44
from .device import Device
5+
from .device_communication import DeviceCommunication, DeviceCommunicationType
56
from .device_connectivity import DeviceConnectivity, DeviceConnectivityStatus
67
from .device_type import DeviceType
78
from .mqtt_credentials import MQTTCredentials
@@ -17,6 +18,8 @@
1718
"DeviceType",
1819
"DeviceConnectivity",
1920
"DeviceConnectivityStatus",
21+
"DeviceCommunication",
22+
"DeviceCommunicationType",
2023
"MQTTCredentials",
2124
"MQTTProtocol",
2225
"MQTTSCredentials",

src/enapter/http/api/devices/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ async def get(
2727
expand_manifest: bool = False,
2828
expand_properties: bool = False,
2929
expand_connectivity: bool = False,
30+
expand_communication: bool = False,
3031
) -> Device:
3132
url = f"v3/devices/{device_id}"
3233
expand = {
3334
"manifest": expand_manifest,
3435
"properties": expand_properties,
3536
"connectivity": expand_connectivity,
37+
"communication": expand_communication,
3638
}
3739
params = {"expand": ",".join(k for k, v in expand.items() if v)}
3840
response = await self._client.get(url, params=params)

src/enapter/http/api/devices/device.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Any, Self
44

55
from .authorized_role import AuthorizedRole
6+
from .device_communication import DeviceCommunication
67
from .device_connectivity import DeviceConnectivity
78
from .device_type import DeviceType
89

@@ -21,6 +22,7 @@ class Device:
2122
manifest: dict[str, Any] | None = None
2223
properties: dict[str, Any] | None = None
2324
connectivity: DeviceConnectivity | None = None
25+
communication: DeviceCommunication | None = None
2426

2527
@classmethod
2628
def from_dto(cls, dto: dict[str, Any]) -> Self:
@@ -40,6 +42,11 @@ def from_dto(cls, dto: dict[str, Any]) -> Self:
4042
if dto.get("connectivity") is not None
4143
else None
4244
),
45+
communication=(
46+
DeviceCommunication.from_dto(dto["communication"])
47+
if dto.get("communication") is not None
48+
else None
49+
),
4350
)
4451

4552
def to_dto(self) -> dict[str, Any]:
@@ -57,4 +64,7 @@ def to_dto(self) -> dict[str, Any]:
5764
"connectivity": (
5865
self.connectivity.to_dto() if self.connectivity is not None else None
5966
),
67+
"communication": (
68+
self.communication.to_dto() if self.communication is not None else None
69+
),
6070
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import dataclasses
2+
import enum
3+
from typing import Any
4+
5+
6+
class DeviceCommunicationType(str, enum.Enum):
7+
8+
MQTT_V1_PLAINTEXT = "MQTT_V1_PLAINTEXT"
9+
MQTT_V1_TLS = "MQTT_V1_TLS"
10+
MQTT_V1_LOCALHOST = "MQTT_V1_LOCALHOST"
11+
UCM_LUA = "UCM_LUA"
12+
UCM_EMBEDDED = "UCM_EMBEDDED"
13+
LINK = "LINK"
14+
15+
16+
@dataclasses.dataclass
17+
class DeviceCommunication:
18+
19+
type: DeviceCommunicationType
20+
upstream_id: str | None
21+
hardware_id: str | None
22+
channel_id: str | None
23+
24+
@classmethod
25+
def from_dto(cls, dto: dict[str, Any]) -> "DeviceCommunication":
26+
return cls(
27+
type=DeviceCommunicationType(dto["type"]),
28+
upstream_id=dto.get("upstream_id"),
29+
hardware_id=dto.get("hardware_id"),
30+
channel_id=dto.get("channel_id"),
31+
)
32+
33+
def to_dto(self) -> dict[str, Any]:
34+
return {
35+
"type": self.type.value,
36+
"upstream_id": self.upstream_id,
37+
"hardware_id": self.hardware_id,
38+
"channel_id": self.channel_id,
39+
}

0 commit comments

Comments
 (0)