11from fastapi import APIRouter
2+ from pydantic import BaseModel
23
34from dataclasses import asdict
45from 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
78from ..models .analog_input import AnalogInput , AnalogInputUpdate
89from ..models .analog_output import AnalogOutput , AnalogOutputUpdate
910from ..models .digital_input import DigitalInput , DigitalInputUpdate
1011from ..models .digital_output import DigitalOutput , DigitalOutputUpdate
1112from ..models .led import Led , LedUpdate
13+ from ..models .modbus import Modbus , ModbusUpdate
14+ from ..models .ow import OWPower , OWPowerUpdate , OWBus , OWBusUpdate
1215from .common import DevicesDep , DeviceType , DEVICE_TYPES , DeviceDep
1316
1417router = APIRouter (prefix = "/controller" , tags = ["controller" ])
@@ -32,42 +35,46 @@ def list_all_circuits_of_device_type(devices: DevicesDep, device_type: DeviceTyp
3235S = 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
5757DEVICE_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