Skip to content

Commit

Permalink
Request/Response: change execute to be async method
Browse files Browse the repository at this point in the history
contains only async/await edits, no functional changes otherwise
  • Loading branch information
ilkka-ollakka committed Apr 2, 2024
1 parent 6b728ec commit 2162e3d
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 110 deletions.
4 changes: 2 additions & 2 deletions pymodbus/bit_read_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def __init__(self, address=None, count=None, slave=0, **kwargs):
"""
ReadBitsRequestBase.__init__(self, address, count, slave, **kwargs)

def execute(self, context):
async def execute(self, context):
"""Run a read coils request against a datastore.
Before running the request, we make sure that the request is in
Expand Down Expand Up @@ -223,7 +223,7 @@ def __init__(self, address=None, count=None, slave=0, **kwargs):
"""
ReadBitsRequestBase.__init__(self, address, count, slave, **kwargs)

def execute(self, context):
async def execute(self, context):
"""Run a read discrete input request against a datastore.
Before running the request, we make sure that the request is in
Expand Down
4 changes: 2 additions & 2 deletions pymodbus/bit_write_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def decode(self, data):
self.address, value = struct.unpack(">HH", data)
self.value = value == ModbusStatus.ON

def execute(self, context):
async def execute(self, context):
"""Run a write coil request against a datastore.
:param context: The datastore to request from
Expand Down Expand Up @@ -212,7 +212,7 @@ def decode(self, data):
values = unpack_bitstring(data[5:])
self.values = values[:count]

def execute(self, context):
async def execute(self, context):
"""Run a write coils request against a datastore.
:param context: The datastore to request from
Expand Down
36 changes: 18 additions & 18 deletions pymodbus/diag_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def __init__(self, data=0x0000, **kwargs):
DiagnosticStatusRequest.__init__(self, **kwargs)
self.message = data

def execute(self, *args):
async def execute(self, *args):
"""Raise if not implemented."""
raise NotImplementedException("Diagnostic Message Has No Execute Method")

Expand Down Expand Up @@ -245,7 +245,7 @@ def __init__(self, message=b"\x00\x00", slave=None, **kwargs):
raise ModbusException(f"message({type(message)}) must be bytes")
self.message = message

def execute(self, *_args):
async def execute(self, *_args):
"""Execute the loopback request (builds the response).
:returns: The populated loopback response message
Expand Down Expand Up @@ -301,7 +301,7 @@ def __init__(self, toggle=False, slave=None, **kwargs):
else:
self.message = [ModbusStatus.OFF]

def execute(self, *_args):
async def execute(self, *_args):
"""Clear event log and restart.
:returns: The initialized response message
Expand Down Expand Up @@ -343,7 +343,7 @@ class ReturnDiagnosticRegisterRequest(DiagnosticStatusSimpleRequest):

sub_function_code = 0x0002

def execute(self, *args):
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
Expand Down Expand Up @@ -377,7 +377,7 @@ class ChangeAsciiInputDelimiterRequest(DiagnosticStatusSimpleRequest):

sub_function_code = 0x0003

def execute(self, *args):
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
Expand Down Expand Up @@ -412,7 +412,7 @@ class ForceListenOnlyModeRequest(DiagnosticStatusSimpleRequest):

sub_function_code = 0x0004

def execute(self, *args):
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
Expand Down Expand Up @@ -451,7 +451,7 @@ class ClearCountersRequest(DiagnosticStatusSimpleRequest):

sub_function_code = 0x000A

def execute(self, *args):
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
Expand Down Expand Up @@ -482,7 +482,7 @@ class ReturnBusMessageCountRequest(DiagnosticStatusSimpleRequest):

sub_function_code = 0x000B

def execute(self, *args):
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
Expand Down Expand Up @@ -515,7 +515,7 @@ class ReturnBusCommunicationErrorCountRequest(DiagnosticStatusSimpleRequest):

sub_function_code = 0x000C

def execute(self, *args):
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
Expand Down Expand Up @@ -548,7 +548,7 @@ class ReturnBusExceptionErrorCountRequest(DiagnosticStatusSimpleRequest):

sub_function_code = 0x000D

def execute(self, *args):
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
Expand Down Expand Up @@ -581,7 +581,7 @@ class ReturnSlaveMessageCountRequest(DiagnosticStatusSimpleRequest):

sub_function_code = 0x000E

def execute(self, *args):
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
Expand Down Expand Up @@ -614,7 +614,7 @@ class ReturnSlaveNoResponseCountRequest(DiagnosticStatusSimpleRequest):

sub_function_code = 0x000F

def execute(self, *args):
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
Expand Down Expand Up @@ -648,7 +648,7 @@ class ReturnSlaveNAKCountRequest(DiagnosticStatusSimpleRequest):

sub_function_code = 0x0010

def execute(self, *args):
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
Expand Down Expand Up @@ -682,7 +682,7 @@ class ReturnSlaveBusyCountRequest(DiagnosticStatusSimpleRequest):

sub_function_code = 0x0011

def execute(self, *args):
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
Expand Down Expand Up @@ -717,7 +717,7 @@ class ReturnSlaveBusCharacterOverrunCountRequest(DiagnosticStatusSimpleRequest):

sub_function_code = 0x0012

def execute(self, *args):
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
Expand Down Expand Up @@ -750,7 +750,7 @@ class ReturnIopOverrunCountRequest(DiagnosticStatusSimpleRequest):

sub_function_code = 0x0013

def execute(self, *args):
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
Expand Down Expand Up @@ -783,7 +783,7 @@ class ClearOverrunCountRequest(DiagnosticStatusSimpleRequest):

sub_function_code = 0x0014

def execute(self, *args):
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
Expand Down Expand Up @@ -834,7 +834,7 @@ def get_response_pdu_size(self):
data = 0
return 1 + 2 + 2 + 2 + data

def execute(self, *args):
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
Expand Down
2 changes: 1 addition & 1 deletion pymodbus/mei_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def decode(self, data):
params = struct.unpack(">BBB", data)
self.sub_function_code, self.read_code, self.object_id = params

def execute(self, _context):
async def execute(self, _context):
"""Run a read exception status request against the store.
:returns: The populated response
Expand Down
8 changes: 4 additions & 4 deletions pymodbus/other_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def decode(self, data):
:param data: The incoming data
"""

def execute(self, _context=None):
async def execute(self, _context=None):
"""Run a read exception status request against the store.
:returns: The populated response
Expand Down Expand Up @@ -159,7 +159,7 @@ def decode(self, data):
:param data: The incoming data
"""

def execute(self, _context=None):
async def execute(self, _context=None):
"""Run a read exception status request against the store.
:returns: The populated response
Expand Down Expand Up @@ -270,7 +270,7 @@ def decode(self, data):
:param data: The incoming data
"""

def execute(self, _context=None):
async def execute(self, _context=None):
"""Run a read exception status request against the store.
:returns: The populated response
Expand Down Expand Up @@ -395,7 +395,7 @@ def decode(self, data):
:param data: The incoming data
"""

def execute(self, context=None):
async def execute(self, context=None):
"""Run a report slave id request against the store.
:returns: The populated response
Expand Down
2 changes: 1 addition & 1 deletion pymodbus/pdu.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def decode(self, _data):
def encode(self):
"""Decode so this failure will run correctly."""

def execute(self, _context):
async def execute(self, _context):
"""Build an illegal function request error response.
:returns: The error response packet
Expand Down
6 changes: 3 additions & 3 deletions pymodbus/register_read_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def __init__(self, address=None, count=None, slave=0, **kwargs):
"""
super().__init__(address, count, slave, **kwargs)

def execute(self, context):
async def execute(self, context):
"""Run a read holding request against a datastore.
:param context: The datastore to request from
Expand Down Expand Up @@ -200,7 +200,7 @@ def __init__(self, address=None, count=None, slave=0, **kwargs):
"""
super().__init__(address, count, slave, **kwargs)

def execute(self, context):
async def execute(self, context):
"""Run a read input request against a datastore.
:param context: The datastore to request from
Expand Down Expand Up @@ -310,7 +310,7 @@ def decode(self, data):
register = struct.unpack(">H", data[i : i + 2])[0]
self.write_registers.append(register)

def execute(self, context):
async def execute(self, context):
"""Run a write single register request against a datastore.
:param context: The datastore to request from
Expand Down
6 changes: 3 additions & 3 deletions pymodbus/register_write_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def decode(self, data):
"""
self.address, self.value = struct.unpack(">HH", data)

def execute(self, context):
async def execute(self, context):
"""Run a write single register request against a datastore.
:param context: The datastore to request from
Expand Down Expand Up @@ -200,7 +200,7 @@ def decode(self, data):
for idx in range(5, (self.count * 2) + 5, 2):
self.values.append(struct.unpack(">H", data[idx : idx + 2])[0])

def execute(self, context):
async def execute(self, context):
"""Run a write single register request against a datastore.
:param context: The datastore to request from
Expand Down Expand Up @@ -321,7 +321,7 @@ def decode(self, data):
"""
self.address, self.and_mask, self.or_mask = struct.unpack(">HHH", data)

def execute(self, context):
async def execute(self, context):
"""Run a mask write register request against the store.
:param context: The datastore to request from
Expand Down
2 changes: 1 addition & 1 deletion test/sub_client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def __init__(self, base, req, retries=0):
async def delayed_resp(self):
"""Send a response to a received packet."""
await asyncio.sleep(0.05)
resp = self.req.execute(self.ctx)
resp = await self.req.execute(self.ctx)
pkt = self.base.ctx.framer.buildPacket(resp)
self.base.ctx.data_received(pkt)

Expand Down
12 changes: 6 additions & 6 deletions test/sub_function_codes/test_bit_read_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,29 @@ def test_bit_read_base_requests(self):
for request, expected in iter(messages.items()):
assert request.encode() == expected

def test_bit_read_message_execute_value_errors(self):
async def test_bit_read_message_execute_value_errors(self):
"""Test bit read request encoding."""
context = MockContext()
requests = [
ReadCoilsRequest(1, 0x800),
ReadDiscreteInputsRequest(1, 0x800),
]
for request in requests:
result = request.execute(context)
result = await request.execute(context)
assert ModbusExceptions.IllegalValue == result.exception_code

def test_bit_read_message_execute_address_errors(self):
async def test_bit_read_message_execute_address_errors(self):
"""Test bit read request encoding."""
context = MockContext()
requests = [
ReadCoilsRequest(1, 5),
ReadDiscreteInputsRequest(1, 5),
]
for request in requests:
result = request.execute(context)
result = await request.execute(context)
assert ModbusExceptions.IllegalAddress == result.exception_code

def test_bit_read_message_execute_success(self):
async def test_bit_read_message_execute_success(self):
"""Test bit read request encoding."""
context = MockContext()
context.validate = lambda a, b, c: True
Expand All @@ -116,7 +116,7 @@ def test_bit_read_message_execute_success(self):
ReadDiscreteInputsRequest(1, 5),
]
for request in requests:
result = request.execute(context)
result = await request.execute(context)
assert result.bits == [True] * 5

def test_bit_read_message_get_response_pdu(self):
Expand Down
Loading

0 comments on commit 2162e3d

Please sign in to comment.