Skip to content

Commit 0f89d97

Browse files
committed
http: api: devices: add create vucm and create lua
1 parent e233cd4 commit 0f89d97

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
from enapter import cli
44

55
from .device_assign_blueprint_command import DeviceAssignBlueprintCommand
6+
from .device_create_lua_command import DeviceCreateLuaCommand
67
from .device_create_standalone_command import DeviceCreateStandaloneCommand
8+
from .device_create_vucm_command import DeviceCreateVUCMCommand
79
from .device_delete_command import DeviceDeleteCommand
810
from .device_generate_communication_config_command import (
911
DeviceGenerateCommunicationConfigCommand,
@@ -24,7 +26,9 @@ def register(parent: cli.Subparsers) -> None:
2426
for command in [
2527
DeviceAssignBlueprintCommand,
2628
DeviceCreateStandaloneCommand,
29+
DeviceCreateVUCMCommand,
2730
DeviceDeleteCommand,
31+
DeviceCreateLuaCommand,
2832
DeviceGenerateCommunicationConfigCommand,
2933
DeviceGetCommand,
3034
DeviceListCommand,
@@ -35,6 +39,8 @@ def register(parent: cli.Subparsers) -> None:
3539
@staticmethod
3640
async def run(args: argparse.Namespace) -> None:
3741
match args.device_command:
42+
case "create-lua":
43+
await DeviceCreateLuaCommand.run(args)
3844
case "assign-blueprint":
3945
await DeviceAssignBlueprintCommand.run(args)
4046
case "create-standalone":
@@ -49,5 +55,7 @@ async def run(args: argparse.Namespace) -> None:
4955
await DeviceListCommand.run(args)
5056
case "update":
5157
await DeviceUpdateCommand.run(args)
58+
case "create-vucm":
59+
await DeviceCreateVUCMCommand.run(args)
5260
case _:
5361
raise NotImplementedError(args.device_command)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import argparse
2+
import json
3+
4+
from enapter import cli, http
5+
6+
7+
class DeviceCreateLuaCommand(cli.Command):
8+
9+
@staticmethod
10+
def register(parent: cli.Subparsers) -> None:
11+
parser = parent.add_parser(
12+
"create-lua", formatter_class=argparse.ArgumentDefaultsHelpFormatter
13+
)
14+
parser.add_argument("-r", "--runtime-id", help="Runtime ID of the Lua device")
15+
parser.add_argument(
16+
"-b", "--blueprint-id", help="Blueprint ID of the Lua device"
17+
)
18+
parser.add_argument("-s", "--slug", help="Slug of the Lua device")
19+
parser.add_argument("name", help="Name of the Lua device to create")
20+
21+
@staticmethod
22+
async def run(args: argparse.Namespace) -> None:
23+
async with http.api.Client(http.api.Config.from_env()) as client:
24+
device = await client.devices.create_lua(
25+
name=args.name,
26+
runtime_id=args.runtime_id,
27+
blueprint_id=args.blueprint_id,
28+
slug=args.slug,
29+
)
30+
print(json.dumps(device.to_dto()))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import argparse
2+
import json
3+
4+
from enapter import cli, http
5+
6+
7+
class DeviceCreateVUCMCommand(cli.Command):
8+
9+
@staticmethod
10+
def register(parent: cli.Subparsers) -> None:
11+
parser = parent.add_parser(
12+
"create-vucm", formatter_class=argparse.ArgumentDefaultsHelpFormatter
13+
)
14+
parser.add_argument("-s", "--site-id", help="Site ID to create device in")
15+
parser.add_argument("--hardware-id", help="Hardware ID of the VUCM device")
16+
parser.add_argument("name", help="Name of the VUCM device to create")
17+
18+
@staticmethod
19+
async def run(args: argparse.Namespace) -> None:
20+
async with http.api.Client(http.api.Config.from_env()) as client:
21+
device = await client.devices.create_vucm(
22+
name=args.name, site_id=args.site_id, hardware_id=args.hardware_id
23+
)
24+
print(json.dumps(device.to_dto()))

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import random
12
from typing import AsyncGenerator
23

34
import httpx
@@ -17,7 +18,37 @@ def __init__(self, client: httpx.AsyncClient) -> None:
1718

1819
async def create_standalone(self, name: str, site_id: str | None = None) -> Device:
1920
url = "v3/provisioning/standalone"
20-
response = await self._client.post(url, json={"name": name, "site_id": site_id})
21+
response = await self._client.post(url, json={"slug": name, "site_id": site_id})
22+
api.check_error(response)
23+
return await self.get(device_id=response.json()["device_id"])
24+
25+
async def create_vucm(
26+
self, name: str, hardware_id: str | None = None, site_id: str | None = None
27+
) -> Device:
28+
if hardware_id is None:
29+
hardware_id = random_hardware_id()
30+
url = "v3/provisioning/vucm"
31+
response = await self._client.post(
32+
url, json={"name": name, "hardware_id": hardware_id, "site_id": site_id}
33+
)
34+
api.check_error(response)
35+
return await self.get(
36+
device_id=response.json()["device_id"], expand_communication=True
37+
)
38+
39+
async def create_lua(
40+
self, name: str, runtime_id: str, blueprint_id: str, slug: str | None = None
41+
) -> Device:
42+
url = "v3/provisioning/lua_device"
43+
response = await self._client.post(
44+
url,
45+
json={
46+
"name": name,
47+
"runtime_id": runtime_id,
48+
"blueprint_id": blueprint_id,
49+
"slug": slug,
50+
},
51+
)
2152
api.check_error(response)
2253
return await self.get(device_id=response.json()["device_id"])
2354

@@ -99,3 +130,7 @@ async def generate_communication_config(
99130
response = await self._client.post(url, json={"protocol": mqtt_protocol.value})
100131
api.check_error(response)
101132
return CommunicationConfig.from_dto(response.json()["config"])
133+
134+
135+
def random_hardware_id() -> str:
136+
return "V" + "".join(f"{b:02X}" for b in random.randbytes(16))

0 commit comments

Comments
 (0)