Skip to content

Commit 32d969d

Browse files
committed
Add 1-wire and modbus support
1 parent 4245abe commit 32d969d

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed

robopipe_api/models/modbus.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from pydantic import BaseModel, Field
2+
3+
from typing import Annotated
4+
5+
from .device import Device
6+
7+
8+
class Modbus(Device):
9+
value: Annotated[int, Field(ge=0, le=(2**16 - 1))]
10+
save: bool = True
11+
12+
13+
class ModbusUpdate(BaseModel):
14+
value: float

robopipe_api/models/ow.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from pydantic import BaseModel
2+
3+
from typing import Literal
4+
5+
from .device import Device
6+
7+
8+
class OWPower(Device):
9+
value: Literal[0, 1]
10+
11+
12+
class OWPowerUpdate(BaseModel):
13+
value: Literal[0, 1]
14+
15+
16+
class OWBus(Device):
17+
bus: str
18+
interval: int = 10
19+
do_scan: bool
20+
do_reset: bool
21+
scan_interval: int = 60
22+
23+
24+
class OWBusUpdate(BaseModel):
25+
interval: int = 10
26+
do_scan: bool = False
27+
do_reset: bool = False
28+
scan_interval: int = 60

robopipe_api/routers/controller.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
from fastapi import APIRouter
2+
from pydantic import BaseModel
23

34
from dataclasses import asdict
45
from typing import Any, TypeVar
56

6-
from ..controller.devices import AI, AO, DI, DO, LED
7+
from ..controller.devices import AI, AO, DI, DO, LED, OWPOWER, OWBUS, REGISTER
78
from ..models.analog_input import AnalogInput, AnalogInputUpdate
89
from ..models.analog_output import AnalogOutput, AnalogOutputUpdate
910
from ..models.digital_input import DigitalInput, DigitalInputUpdate
1011
from ..models.digital_output import DigitalOutput, DigitalOutputUpdate
1112
from ..models.led import Led, LedUpdate
13+
from ..models.modbus import Modbus, ModbusUpdate
14+
from ..models.ow import OWPower, OWPowerUpdate, OWBus, OWBusUpdate
1215
from .common import DevicesDep, DeviceType, DEVICE_TYPES, DeviceDep
1316

1417
router = APIRouter(prefix="/controller", tags=["controller"])
@@ -32,42 +35,46 @@ def list_all_circuits_of_device_type(devices: DevicesDep, device_type: DeviceTyp
3235
S = TypeVar("S")
3336

3437

35-
def create_endpoints(
36-
device_type: str,
37-
output_type: S,
38-
input_type: T,
39-
):
38+
def create_endpoints(device_type: str, output_type: S, input_type: T):
4039
async def get_all(devices: DevicesDep) -> list[output_type]:
4140
return [device.full() for device in devices.by_name(device_type)]
4241

4342
async def set_all(devices: DevicesDep, body: input_type) -> list[output_type]:
4443
return [
45-
await device.set(**asdict(body)) for device in devices.by_name(device_type)
44+
await device.set(**body.model_dump())
45+
for device in devices.by_name(device_type)
4646
]
4747

4848
async def get(device: DeviceDep) -> output_type:
4949
return device.full()
5050

5151
async def set(device: DeviceDep, body: input_type) -> output_type:
52-
return await device.set(**asdict(body))
52+
return await device.set(**body.model_dump())
5353

5454
return [get_all, set_all, get, set]
5555

5656

5757
DEVICE_ENDPOINTS = [
58-
[AI, AnalogInput, AnalogInputUpdate, "analog input"],
59-
[AO, AnalogOutput, AnalogOutputUpdate, "analog output"],
60-
[DI, DigitalInput, DigitalInputUpdate, "digital input"],
61-
[DO, DigitalOutput, DigitalOutputUpdate, "digital output"],
62-
[LED, Led, LedUpdate, "led"],
58+
[AI, AnalogInput, AnalogInputUpdate, "analog input", None],
59+
[AO, AnalogOutput, AnalogOutputUpdate, "analog output", None],
60+
[DI, DigitalInput, DigitalInputUpdate, "digital input", None],
61+
[DO, DigitalOutput, DigitalOutputUpdate, "digital output", None],
62+
[LED, Led, LedUpdate, "led", None],
63+
[OWPOWER, OWPower, OWPowerUpdate, "one-wire power supply", "supplies"],
64+
[OWBUS, OWBus, OWBusUpdate, "one-wire bus", "buses"],
65+
[REGISTER, Modbus, ModbusUpdate, "modbus register", None],
6366
]
6467

6568

66-
def register_device_endpoints(endpoints: list[tuple[str, Any, Any, str]]):
69+
def register_device_endpoints(endpoints: list[tuple[str, Any, Any, str, str | None]]):
6770
for endpoint in endpoints:
68-
path, out, inp, name = endpoint
71+
path, out, inp, name, last_plural = endpoint
6972
get_all, set_all, get, set = create_endpoints(path, out, inp)
70-
plural_name = name + "s"
73+
plural_name = (
74+
name + "s"
75+
if last_plural is None
76+
else " ".join([*name.split(" ")[:-1], last_plural])
77+
)
7178

7279
router.add_api_route(
7380
rf"/{path}", get_all, name=f"List all {plural_name}", methods=["GET"]

0 commit comments

Comments
 (0)