55from ..constants import ExcCodes
66from ..exceptions import NoSuchIdException
77from ..logging import Log
8- from .store import BaseModbusDataBlock
8+ from .sequential import ModbusSequentialDataBlock
9+ from .sparse import ModbusSparseDataBlock
910
1011
1112# pylint: disable=missing-type-doc
1213
13- class ModbusBaseDeviceContext :
14- """Interface for a modbus device data context."""
15-
16- _fx_mapper = {2 : "d" , 4 : "i" }
17- _fx_mapper .update ([(i , "h" ) for i in (3 , 6 , 16 , 22 , 23 )])
18- _fx_mapper .update ([(i , "c" ) for i in (1 , 5 , 15 )])
19-
20- def decode (self , fx ):
21- """Convert the function code to the datastore to.
22-
23- :param fx: The function we are working with
24- :returns: one of [d(iscretes),i(nputs),h(olding),c(oils)
25- """
26- return self ._fx_mapper .get (fx , "x" )
27-
28- async def async_getValues (self , func_code : int , address : int , count : int = 1 ) -> list [int ] | list [bool ] | ExcCodes :
29- """Get `count` values from datastore.
30-
31- :param func_code: The function we are working with
32- :param address: The starting address
33- :param count: The number of values to retrieve
34- :returns: The requested values from a:a+c
35- """
36- Log .error ("getValues({},{},{}) not implemented!" , func_code , address , count )
37- return ExcCodes .ILLEGAL_FUNCTION
38-
39- async def async_setValues (self , func_code : int , address : int , values : list [int ] | list [bool ] ) -> None | ExcCodes :
40- """Set the datastore with the supplied values.
41-
42- :param func_code: The function we are working with
43- :param address: The starting address
44- :param values: The new values to be set
45- """
46- Log .error ("setValues({},{},{}) not implemented!" , func_code , address , values )
47- return ExcCodes .ILLEGAL_FUNCTION
48-
49-
50- # ---------------------------------------------------------------------------#
51- # Device Contexts
52- # ---------------------------------------------------------------------------#
53- class ModbusDeviceContext (ModbusBaseDeviceContext ):
14+ class ModbusDeviceContext :
5415 """Create a modbus data model with data stored in a block.
5516
5617 :param di: discrete inputs initializer ModbusDataBlock
@@ -59,11 +20,15 @@ class ModbusDeviceContext(ModbusBaseDeviceContext):
5920 :param ir: input registers initializer ModbusDataBlock
6021 """
6122
23+ _fx_mapper = {2 : "d" , 4 : "i" }
24+ _fx_mapper .update ([(i , "h" ) for i in (3 , 6 , 16 , 22 , 23 )])
25+ _fx_mapper .update ([(i , "c" ) for i in (1 , 5 , 15 )])
26+
6227 def __init__ (self , * _args ,
63- di : BaseModbusDataBlock | None = None ,
64- co : BaseModbusDataBlock | None = None ,
65- ir : BaseModbusDataBlock | None = None ,
66- hr : BaseModbusDataBlock | None = None ,
28+ di : ModbusSequentialDataBlock | ModbusSparseDataBlock | None = None ,
29+ co : ModbusSequentialDataBlock | ModbusSparseDataBlock | None = None ,
30+ ir : ModbusSequentialDataBlock | ModbusSparseDataBlock | None = None ,
31+ hr : ModbusSequentialDataBlock | ModbusSparseDataBlock | None = None ,
6732 ):
6833 """Initialize the datastores."""
6934 self .store = {
@@ -73,7 +38,7 @@ def __init__(self, *_args,
7338 "h" : hr ,
7439 }
7540
76- async def async_getValues (self , func_code , address , count = 1 ) -> list [int ] | list [bool ] | ExcCodes :
41+ async def async_OLD_getValues (self , func_code , address , count = 1 ) -> list [int ] | list [bool ] | ExcCodes :
7742 """Get `count` values from datastore.
7843
7944 :param func_code: The function we are working with
@@ -83,11 +48,11 @@ async def async_getValues(self, func_code, address, count=1) -> list[int] | list
8348 """
8449 address += 1
8550 Log .debug ("getValues: fc-[{}] address-{}: count-{}" , func_code , address , count )
86- if dt := self .store [self .decode (func_code )]:
87- return await dt .async_getValues (address , count )
51+ if dt := self .store [self ._fx_mapper . get (func_code , "x" )]:
52+ return await dt .async_OLD_getValues (address , count )
8853 return ExcCodes .ILLEGAL_ADDRESS
8954
90- async def async_setValues (self , func_code , address , values ) -> None | ExcCodes :
55+ async def async_OLD_setValues (self , func_code , address , values ) -> None | ExcCodes :
9156 """Set the datastore with the supplied values.
9257
9358 :param func_code: The function we are working with
@@ -96,8 +61,8 @@ async def async_setValues(self, func_code, address, values) -> None | ExcCodes:
9661 """
9762 address += 1
9863 Log .debug ("setValues[{}] address-{}: count-{}" , func_code , address , len (values ))
99- if dt := self .store [self .decode (func_code )]:
100- return await dt .async_setValues (address , values )
64+ if dt := self .store [self ._fx_mapper . get (func_code , "x" )]:
65+ return await dt .async_OLD_setValues (address , values )
10166 return ExcCodes .ILLEGAL_ADDRESS
10267
10368
@@ -141,7 +106,7 @@ async def async_getValues(self, device_id: int, func_code: int, address: int, co
141106 :returns: The requested values from a:a+c
142107 """
143108 dev = self .__get_device (device_id )
144- return await dev .async_getValues (func_code , address , count )
109+ return await dev .async_OLD_getValues (func_code , address , count )
145110
146111 async def async_setValues (self , device_id : int , func_code : int , address : int , values : list [int ] | list [bool ] ) -> None | ExcCodes :
147112 """Set the datastore with the supplied values.
@@ -152,7 +117,7 @@ async def async_setValues(self, device_id: int, func_code: int, address: int, va
152117 :param values: The new values to be set
153118 """
154119 dev = self .__get_device (device_id )
155- return await dev .async_setValues (func_code , address , values )
120+ return await dev .async_OLD_setValues (func_code , address , values )
156121
157122 def device_ids (self ):
158123 """Get the configured device ids."""
0 commit comments