Skip to content

Commit

Permalink
Change factory methods to class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rgov committed Jul 25, 2020
1 parent dcf35a1 commit ba7c73b
Showing 1 changed file with 60 additions and 58 deletions.
118 changes: 60 additions & 58 deletions umodbus/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,16 @@ def request_pdu(self):
return struct.pack('>BHH', self.function_code, self.starting_address,
self.quantity)

@staticmethod
def create_from_request_pdu(pdu):
@classmethod
def create_from_request_pdu(cls, pdu):
""" Create instance from request PDU.
:param pdu: A request PDU.
:return: Instance of this class.
"""
_, starting_address, quantity = struct.unpack('>BHH', pdu)

instance = ReadCoils()
instance = cls()
instance.starting_address = starting_address
instance.quantity = quantity

Expand Down Expand Up @@ -324,8 +324,8 @@ def create_response_pdu(self, data):
fmt = '>BB' + self.format_character * len(bytes_)
return struct.pack(fmt, self.function_code, len(bytes_), *bytes_)

@staticmethod
def create_from_response_pdu(resp_pdu, req_pdu):
@classmethod
def create_from_response_pdu(cls, resp_pdu, req_pdu):
""" Create instance from response PDU.
Response PDU is required together with the quantity of coils read.
Expand All @@ -334,7 +334,7 @@ def create_from_response_pdu(resp_pdu, req_pdu):
:param quantity: Number of coils read.
:return: Instance of :class:`ReadCoils`.
"""
read_coils = ReadCoils()
read_coils = cls()
read_coils.quantity = struct.unpack('>H', req_pdu[-2:])[0]
byte_count = struct.unpack('>B', resp_pdu[1:2])[0]

Expand All @@ -360,7 +360,7 @@ def execute(self, slave_id, route_map):
""" Execute the Modbus function registered for a route.
:param slave_id: Slave id.
:param eindpoint: Instance of modbus.route.Map.
:param route_map: Instance of modbus.route.Map.
:return: Result of call to endpoint.
"""
try:
Expand Down Expand Up @@ -491,16 +491,16 @@ def request_pdu(self):
return struct.pack('>BHH', self.function_code, self.starting_address,
self.quantity)

@staticmethod
def create_from_request_pdu(pdu):
@classmethod
def create_from_request_pdu(cls, pdu):
""" Create instance from request PDU.
:param pdu: A request PDU.
:return: Instance of this class.
"""
_, starting_address, quantity = struct.unpack('>BHH', pdu)

instance = ReadDiscreteInputs()
instance = cls()
instance.starting_address = starting_address
instance.quantity = quantity

Expand Down Expand Up @@ -537,8 +537,8 @@ def create_response_pdu(self, data):
fmt = '>BB' + self.format_character * len(bytes_)
return struct.pack(fmt, self.function_code, len(bytes_), *bytes_)

@staticmethod
def create_from_response_pdu(resp_pdu, req_pdu):
@classmethod
def create_from_response_pdu(cls, resp_pdu, req_pdu):
""" Create instance from response PDU.
Response PDU is required together with the quantity of inputs read.
Expand All @@ -547,7 +547,7 @@ def create_from_response_pdu(resp_pdu, req_pdu):
:param quantity: Number of inputs read.
:return: Instance of :class:`ReadDiscreteInputs`.
"""
read_discrete_inputs = ReadDiscreteInputs()
read_discrete_inputs = cls()
read_discrete_inputs.quantity = struct.unpack('>H', req_pdu[-2:])[0]
byte_count = struct.unpack('>B', resp_pdu[1:2])[0]

Expand All @@ -573,7 +573,7 @@ def execute(self, slave_id, route_map):
""" Execute the Modbus function registered for a route.
:param slave_id: Slave id.
:param eindpoint: Instance of modbus.route.Map.
:param route_map: Instance of modbus.route.Map.
:return: Result of call to endpoint.
"""
try:
Expand Down Expand Up @@ -695,16 +695,16 @@ def request_pdu(self):
return struct.pack('>BHH', self.function_code, self.starting_address,
self.quantity)

@staticmethod
def create_from_request_pdu(pdu):

@classmethod
def create_from_request_pdu(cls, pdu):
""" Create instance from request PDU.
:param pdu: A request PDU.
:return: Instance of this class.
"""
_, starting_address, quantity = struct.unpack('>BHH', pdu)

instance = ReadHoldingRegisters()
instance = cls()
instance.starting_address = starting_address
instance.quantity = quantity

Expand All @@ -729,8 +729,8 @@ def create_response_pdu(self, data):

return struct.pack(fmt, self.function_code, len(data) * 2, *data)

@staticmethod
def create_from_response_pdu(resp_pdu, req_pdu):
@classmethod
def create_from_response_pdu(cls, resp_pdu, req_pdu):
""" Create instance from response PDU.
Response PDU is required together with the number of registers read.
Expand All @@ -739,7 +739,7 @@ def create_from_response_pdu(resp_pdu, req_pdu):
:param quantity: Number of coils read.
:return: Instance of :class:`ReadCoils`.
"""
read_holding_registers = ReadHoldingRegisters()
read_holding_registers = cls()
read_holding_registers.quantity = struct.unpack('>H', req_pdu[-2:])[0]
read_holding_registers.byte_count = \
struct.unpack('>B', resp_pdu[1:2])[0]
Expand All @@ -753,7 +753,7 @@ def execute(self, slave_id, route_map):
""" Execute the Modbus function registered for a route.
:param slave_id: Slave id.
:param eindpoint: Instance of modbus.route.Map.
:param route_map: Instance of modbus.route.Map.
:return: Result of call to endpoint.
"""
try:
Expand Down Expand Up @@ -875,16 +875,16 @@ def request_pdu(self):
return struct.pack('>BHH', self.function_code, self.starting_address,
self.quantity)

@staticmethod
def create_from_request_pdu(pdu):
@classmethod
def create_from_request_pdu(cls, pdu):
""" Create instance from request PDU.
:param pdu: A request PDU.
:return: Instance of this class.
"""
_, starting_address, quantity = struct.unpack('>BHH', pdu)

instance = ReadInputRegisters()
instance = cls()
instance.starting_address = starting_address
instance.quantity = quantity

Expand All @@ -909,8 +909,8 @@ def create_response_pdu(self, data):

return struct.pack(fmt, self.function_code, len(data) * 2, *data)

@staticmethod
def create_from_response_pdu(resp_pdu, req_pdu):
@classmethod
def create_from_response_pdu(cls, resp_pdu, req_pdu):
""" Create instance from response PDU.
Response PDU is required together with the number of registers read.
Expand All @@ -919,7 +919,7 @@ def create_from_response_pdu(resp_pdu, req_pdu):
:param quantity: Number of coils read.
:return: Instance of :class:`ReadCoils`.
"""
read_input_registers = ReadInputRegisters()
read_input_registers = cls()
read_input_registers.quantity = struct.unpack('>H', req_pdu[-2:])[0]

fmt = '>' + (conf.TYPE_CHAR * read_input_registers.quantity)
Expand All @@ -931,7 +931,7 @@ def execute(self, slave_id, route_map):
""" Execute the Modbus function registered for a route.
:param slave_id: Slave id.
:param eindpoint: Instance of modbus.route.Map.
:param route_map: Instance of modbus.route.Map.
:return: Result of call to endpoint.
"""
try:
Expand Down Expand Up @@ -1044,17 +1044,18 @@ def request_pdu(self):
return struct.pack('>BHH', self.function_code, self.address,
self._value)

@staticmethod
def create_from_request_pdu(pdu):
@classmethod
def create_from_request_pdu(cls, pdu):
""" Create instance from request PDU.
:param pdu: A response PDU.
:return: Instance of this class.
"""
_, address, value = struct.unpack('>BHH', pdu)

value = 1 if value == 0xFF00 else value

instance = WriteSingleCoil()
instance = cls()
instance.address = address
instance.value = value

Expand All @@ -1077,14 +1078,14 @@ def create_response_pdu(self):
fmt = '>BHH'
return struct.pack(fmt, self.function_code, self.address, self._value)

@staticmethod
def create_from_response_pdu(resp_pdu):
@classmethod
def create_from_response_pdu(cls, resp_pdu):
""" Create instance from response PDU.
:param resp_pdu: Byte array with request PDU.
:return: Instance of :class:`WriteSingleCoil`.
"""
write_single_coil = WriteSingleCoil()
write_single_coil = cls()

address, value = struct.unpack('>HH', resp_pdu[1:5])
value = 1 if value == 0xFF00 else value
Expand All @@ -1098,7 +1099,7 @@ def execute(self, slave_id, route_map):
""" Execute the Modbus function registered for a route.
:param slave_id: Slave id.
:param eindpoint: Instance of modbus.route.Map.
:param route_map: Instance of modbus.route.Map.
"""
endpoint = route_map.match(slave_id, self.function_code, self.address)
try:
Expand Down Expand Up @@ -1195,16 +1196,17 @@ def request_pdu(self):
return struct.pack('>BH' + conf.TYPE_CHAR, self.function_code,
self.address, self.value)

@staticmethod
def create_from_request_pdu(pdu):
@classmethod
def create_from_request_pdu(cls, pdu):
""" Create instance from request PDU.
:param pdu: A response PDU.
:param pdu: A request PDU.
:return: Instance of this class.
"""
_, address, value = \
struct.unpack('>BH' + conf.MULTI_BIT_VALUE_FORMAT_CHARACTER, pdu)

instance = WriteSingleRegister()
instance = cls()
instance.address = address
instance.value = value

Expand All @@ -1222,14 +1224,14 @@ def create_response_pdu(self):
fmt = '>BH' + conf.TYPE_CHAR
return struct.pack(fmt, self.function_code, self.address, self.value)

@staticmethod
def create_from_response_pdu(resp_pdu):
@classmethod
def create_from_response_pdu(cls, resp_pdu):
""" Create instance from response PDU.
:param resp_pdu: Byte array with request PDU.
:return: Instance of :class:`WriteSingleRegister`.
"""
write_single_register = WriteSingleRegister()
write_single_register = cls()

address, value = struct.unpack('>H' + conf.TYPE_CHAR, resp_pdu[1:5])

Expand All @@ -1242,7 +1244,7 @@ def execute(self, slave_id, route_map):
""" Execute the Modbus function registered for a route.
:param slave_id: Slave id.
:param eindpoint: Instance of modbus.route.Map.
:param route_map: Instance of modbus.route.Map.
"""
endpoint = route_map.match(slave_id, self.function_code, self.address)
try:
Expand Down Expand Up @@ -1348,8 +1350,8 @@ def request_pdu(self):
len(self.values), (len(self.values) // 8) + 1,
*bytes_)

@staticmethod
def create_from_request_pdu(pdu):
@classmethod
def create_from_request_pdu(cls, pdu):
""" Create instance from request PDU.
This method requires some clarification regarding the unpacking of
Expand Down Expand Up @@ -1416,7 +1418,7 @@ def create_from_request_pdu(pdu):
# and reverse the list.
res = res + [int(i) for i in fmt.format(value)][::-1]

instance = WriteMultipleCoils()
instance = cls()
instance.starting_address = starting_address
instance.quantity = quantity

Expand All @@ -1441,9 +1443,9 @@ def create_response_pdu(self):
return struct.pack('>BHH', self.function_code, self.starting_address,
len(self.values))

@staticmethod
def create_from_response_pdu(resp_pdu):
write_multiple_coils = WriteMultipleCoils()
@classmethod
def create_from_response_pdu(cls, resp_pdu):
write_multiple_coils = cls()

starting_address, data = struct.unpack('>HH', resp_pdu[1:5])

Expand All @@ -1456,7 +1458,7 @@ def execute(self, slave_id, route_map):
""" Execute the Modbus function registered for a route.
:param slave_id: Slave id.
:param eindpoint: Instance of modbus.route.Map.
:param route_map: Instance of modbus.route.Map.
"""
for index, value in enumerate(self.values):
address = self.starting_address + index
Expand Down Expand Up @@ -1554,8 +1556,8 @@ def request_pdu(self):
len(self.values), len(self.values) * 2,
*self.values)

@staticmethod
def create_from_request_pdu(pdu):
@classmethod
def create_from_request_pdu(cls, pdu):
""" Create instance from request PDU.
:param pdu: A request PDU.
Expand All @@ -1570,7 +1572,7 @@ def create_from_request_pdu(pdu):

values = list(struct.unpack(fmt, pdu[6:]))

instance = WriteMultipleRegisters()
instance = cls()
instance.starting_address = starting_address
instance.values = values

Expand All @@ -1593,9 +1595,9 @@ def create_response_pdu(self):
return struct.pack('>BHH', self.function_code, self.starting_address,
len(self.values))

@staticmethod
def create_from_response_pdu(resp_pdu):
write_multiple_registers = WriteMultipleRegisters()
@classmethod
def create_from_response_pdu(cls, resp_pdu):
write_multiple_registers = cls()

starting_address, data = struct.unpack('>HH', resp_pdu[1:5])

Expand All @@ -1608,7 +1610,7 @@ def execute(self, slave_id, route_map):
""" Execute the Modbus function registered for a route.
:param slave_id: Slave id.
:param eindpoint: Instance of modbus.route.Map.
:param route_map: Instance of modbus.route.Map.
"""
for index, value in enumerate(self.values):
address = self.starting_address + index
Expand Down

0 comments on commit ba7c73b

Please sign in to comment.