def testExecute(self): client = MagicMock() client.framer = self._ascii client.framer._buffer = b'deadbeef' client.framer.processIncomingPacket = MagicMock() client.framer.processIncomingPacket.return_value = None client.framer.buildPacket = MagicMock() client.framer.buildPacket.return_value = b'deadbeef' client.framer.sendPacket = MagicMock() client.framer.sendPacket.return_value = len(b'deadbeef') request = MagicMock() request.get_response_pdu_size.return_value = 10 request.unit_id = 1 tm = ModbusTransactionManager(client) tm._recv = MagicMock(return_value=b'abcdef') self.assertEqual(tm.retries, 3) self.assertEqual(tm.retry_on_empty, False) # tm._transact = MagicMock() # some response # tm._transact.return_value = (b'abcdef', None) tm.getTransaction = MagicMock() tm.getTransaction.return_value = 'response' response = tm.execute(request) self.assertEqual(response, 'response') # No response tm._recv = MagicMock(return_value=b'abcdef') # tm._transact.return_value = (b'', None) tm.transactions = [] tm.getTransaction = MagicMock() tm.getTransaction.return_value = None response = tm.execute(request) self.assertIsInstance(response, ModbusIOException) # No response with retries tm.retry_on_empty = True tm._recv = MagicMock(side_effect=iter([b'', b'abcdef'])) # tm._transact.side_effect = [(b'', None), (b'abcdef', None)] response = tm.execute(request) self.assertIsInstance(response, ModbusIOException) # retry on invalid response tm.retry_on_invalid = True tm._recv = MagicMock(side_effect=iter([b'', b'abcdef', b'deadbe', b'123456'])) # tm._transact.side_effect = [(b'', None), (b'abcdef', None)] response = tm.execute(request) self.assertIsInstance(response, ModbusIOException) # Unable to decode response tm._recv = MagicMock(side_effect=ModbusIOException()) # tm._transact.side_effect = [(b'abcdef', None)] client.framer.processIncomingPacket.side_effect = MagicMock(side_effect=ModbusIOException()) self.assertIsInstance(tm.execute(request), ModbusIOException)
def testExecute(self): client = MagicMock() client.framer = self._ascii client.framer._buffer = b'deadbeef' client.framer.processIncomingPacket = MagicMock() client.framer.processIncomingPacket.return_value = None client.framer.buildPacket = MagicMock() client.framer.buildPacket.return_value = b'deadbeef' client.framer.sendPacket = MagicMock() client.framer.sendPacket.return_value = len(b'deadbeef') request = MagicMock() request.get_response_pdu_size.return_value = 10 request.unit_id = 1 tm = ModbusTransactionManager(client) tm._recv = MagicMock(return_value=b'abcdef') self.assertEqual(tm.retries, 3) self.assertEqual(tm.retry_on_empty, False) # tm._transact = MagicMock() # some response # tm._transact.return_value = (b'abcdef', None) tm.getTransaction = MagicMock() tm.getTransaction.return_value = 'response' response = tm.execute(request) self.assertEqual(response, 'response') # No response tm._recv = MagicMock(return_value=b'abcdef') # tm._transact.return_value = (b'', None) tm.transactions = [] tm.getTransaction = MagicMock() tm.getTransaction.return_value = None response = tm.execute(request) self.assertIsInstance(response, ModbusIOException) # No response with retries tm.retry_on_empty = True tm._recv = MagicMock(side_effect=iter([b'', b'abcdef'])) # tm._transact.side_effect = [(b'', None), (b'abcdef', None)] response = tm.execute(request) self.assertIsInstance(response, ModbusIOException) # Unable to decode response tm._recv = MagicMock(side_effect=ModbusIOException()) # tm._transact.side_effect = [(b'abcdef', None)] client.framer.processIncomingPacket.side_effect = MagicMock(side_effect=ModbusIOException()) self.assertIsInstance(tm.execute(request), ModbusIOException)
class BaseModbusClient(ModbusClientMixin): ''' Inteface for a modbus synchronous client. Defined here are all the methods for performing the related request methods. Derived classes simply need to implement the transport methods and set the correct framer. ''' def __init__(self, framer): ''' Initialize a client instance :param framer: The modbus framer implementation to use ''' self.framer = framer self.transaction = ModbusTransactionManager(self) #-----------------------------------------------------------------------# # Client interface #-----------------------------------------------------------------------# def connect(self): ''' Connect to the modbus remote host :returns: True if connection succeeded, False otherwise ''' raise NotImplementedException( "Method not implemented by derived class") def close(self): ''' Closes the underlying socket connection ''' pass def _send(self, request): ''' Sends data on the underlying socket :param request: The encoded request to send :return: The number of bytes written ''' raise NotImplementedException( "Method not implemented by derived class") def _recv(self, size): ''' Reads data from the underlying descriptor :param size: The number of bytes to read :return: The bytes read ''' raise NotImplementedException( "Method not implemented by derived class") #-----------------------------------------------------------------------# # Modbus client methods #-----------------------------------------------------------------------# def execute(self, request=None): ''' :param request: The request to process :returns: The result of the request execution ''' if self.transaction: return self.transaction.execute(request) raise ConnectionException("Client Not Connected") #-----------------------------------------------------------------------# # The magic methods #-----------------------------------------------------------------------# def __enter__(self): ''' Implement the client with enter block :returns: The current instance of the client ''' if not self.connect(): raise ConnectionException("Failed to connect[%s]" % (self.__str__())) return self def __exit__(self, type, value, traceback): ''' Implement the client with exit block ''' self.close() def __del__(self): ''' Class destructor ''' self.close() def __str__(self): ''' Builds a string representation of the connection :returns: The string representation ''' return "Null Transport"
class BaseModbusClient(ModbusClientMixin): ''' Inteface for a modbus synchronous client. Defined here are all the methods for performing the related request methods. Derived classes simply need to implement the transport methods and set the correct framer. ''' def __init__(self, framer): ''' Initialize a client instance :param framer: The modbus framer implementation to use ''' self.framer = framer self.transaction = ModbusTransactionManager(self) #-----------------------------------------------------------------------# # Client interface #-----------------------------------------------------------------------# def connect(self): ''' Connect to the modbus remote host :returns: True if connection succeeded, False otherwise ''' raise NotImplementedException("Method not implemented by derived class") def close(self): ''' Closes the underlying socket connection ''' pass def _send(self, request): ''' Sends data on the underlying socket :param request: The encoded request to send :return: The number of bytes written ''' raise NotImplementedException("Method not implemented by derived class") def _recv(self, size): ''' Reads data from the underlying descriptor :param size: The number of bytes to read :return: The bytes read ''' raise NotImplementedException("Method not implemented by derived class") #-----------------------------------------------------------------------# # Modbus client methods #-----------------------------------------------------------------------# def execute(self, request=None): ''' :param request: The request to process :returns: The result of the request execution ''' if not self.connect(): raise ConnectionException("Failed to connect[%s]" % (self.__str__())) if self.transaction: return self.transaction.execute(request) raise ConnectionException("Client Not Connected") #-----------------------------------------------------------------------# # The magic methods #-----------------------------------------------------------------------# def __enter__(self): ''' Implement the client with enter block :returns: The current instance of the client ''' if not self.connect(): raise ConnectionException("Failed to connect[%s]" % (self.__str__())) return self def __exit__(self, type, value, traceback): ''' Implement the client with exit block ''' self.close() def __del__(self): ''' Class destructor ''' self.close() def __str__(self): ''' Builds a string representation of the connection :returns: The string representation ''' return "Null Transport"
def testExecute(self, mock_time): mock_time.time.side_effect = count() client = MagicMock() client.framer = self._ascii client.framer._buffer = b'deadbeef' client.framer.processIncomingPacket = MagicMock() client.framer.processIncomingPacket.return_value = None client.framer.buildPacket = MagicMock() client.framer.buildPacket.return_value = b'deadbeef' client.framer.sendPacket = MagicMock() client.framer.sendPacket.return_value = len(b'deadbeef') client.framer.decode_data = MagicMock() client.framer.decode_data.return_value = { "unit": 1, "fcode": 222, "length": 27 } request = MagicMock() request.get_response_pdu_size.return_value = 10 request.unit_id = 1 request.function_code = 222 tm = ModbusTransactionManager(client) tm._recv = MagicMock(return_value=b'abcdef') self.assertEqual(tm.retries, 3) self.assertEqual(tm.retry_on_empty, False) # tm._transact = MagicMock() # some response # tm._transact.return_value = (b'abcdef', None) tm.getTransaction = MagicMock() tm.getTransaction.return_value = 'response' response = tm.execute(request) self.assertEqual(response, 'response') # No response tm._recv = MagicMock(return_value=b'abcdef') # tm._transact.return_value = (b'', None) tm.transactions = [] tm.getTransaction = MagicMock() tm.getTransaction.return_value = None response = tm.execute(request) self.assertIsInstance(response, ModbusIOException) # No response with retries tm.retry_on_empty = True tm._recv = MagicMock(side_effect=iter([b'', b'abcdef'])) # tm._transact.side_effect = [(b'', None), (b'abcdef', None)] response = tm.execute(request) self.assertIsInstance(response, ModbusIOException) # wrong handle_local_echo tm._recv = MagicMock( side_effect=iter([b'abcdef', b'deadbe', b'123456'])) client.handle_local_echo = True tm.retry_on_empty = False tm.retry_on_invalid = False self.assertEqual( tm.execute(request).message, '[Input/Output] Wrong local echo') client.handle_local_echo = False # retry on invalid response tm.retry_on_invalid = True tm._recv = MagicMock( side_effect=iter([b'', b'abcdef', b'deadbe', b'123456'])) # tm._transact.side_effect = [(b'', None), (b'abcdef', None)] response = tm.execute(request) self.assertIsInstance(response, ModbusIOException) # Unable to decode response tm._recv = MagicMock(side_effect=ModbusIOException()) # tm._transact.side_effect = [(b'abcdef', None)] client.framer.processIncomingPacket.side_effect = MagicMock( side_effect=ModbusIOException()) self.assertIsInstance(tm.execute(request), ModbusIOException) # Broadcast client.broadcast_enable = True request.unit_id = 0 response = tm.execute(request) self.assertEqual(response, b'Broadcast write sent - ' b'no response expected')