Skip to content

Commit

Permalink
Fix for numbers less 0 for errors count
Browse files Browse the repository at this point in the history
  • Loading branch information
imbeacon committed Nov 25, 2024
1 parent a0fcb58 commit de67148
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
20 changes: 10 additions & 10 deletions tests/integration/connectors/modbus/test_modbus_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def setUp(self) -> None:
self.gateway.tb_client = self.tb_client
self.tb_logger = Mock(spec=TbLogger)
self.gateway.get_devices.return_value = []
self.connector = None
self.connector: AsyncModbusConnector = None
self.config = None

def tearDown(self):
Expand All @@ -53,7 +53,7 @@ def _create_connector(self, config_file_name, test_patch):
self.config['master']['slaves'][0]['downlink_converter'] = BytesModbusDownlinkConverter(
{**self.config['master']['slaves'][0], 'deviceName': 'Test'}, logger=self.tb_logger)
self.connector = AsyncModbusConnector(self.gateway, self.config, "modbus")
self.connector._ModbusConnector__log = Mock()
self.connector._AsyncModbusConnector__log = Mock()
self.connector.open()
sleep(1) # some time to init
except Exception as e:
Expand Down Expand Up @@ -83,8 +83,8 @@ def test_read_input_registers(self):

modbus_connector_results = []
for item in attrs:
modbus_connector_results.append(self.connector._ModbusConnector__function_to_device(
self.connector._ModbusConnector__slaves[0],
modbus_connector_results.append(self.connector._AsyncModbusConnector__function_to_device(
self.connector._AsyncModbusConnector__slaves[0],
{'address': item['address'], 'objectsCount': item['objectsCount'],
'functionCode': 4}).registers)

Expand All @@ -101,8 +101,8 @@ def test_read_holding_registers(self):

modbus_connector_results = []
for item in attrs:
modbus_connector_results.append(self.connector._ModbusConnector__function_to_device(
self.connector._ModbusConnector__slaves[0],
modbus_connector_results.append(self.connector._AsyncModbusConnector__function_to_device(
self.connector._AsyncModbusConnector__slaves[0],
{'address': item['address'], 'objectsCount': item['objectsCount'],
'functionCode': 3}).registers)

Expand All @@ -120,8 +120,8 @@ def test_read_discrete_inputs(self):

modbus_connector_results = []
for item in attrs:
modbus_connector_results.append(self.connector._ModbusConnector__function_to_device(
self.connector._ModbusConnector__slaves[0],
modbus_connector_results.append(self.connector._AsyncModbusConnector__function_to_device(
self.connector._AsyncModbusConnector__slaves[0],
{'address': item['address'], 'objectsCount': item['objectsCount'],
'functionCode': 2}).bits)

Expand All @@ -139,8 +139,8 @@ def test_read_coils_inputs(self):

modbus_connector_results = []
for item in attrs:
rc = self.connector._ModbusConnector__function_to_device(
self.connector._ModbusConnector__slaves[0],
rc = self.connector._AsyncModbusConnector__function_to_device(
self.connector._AsyncModbusConnector__slaves[0],
{'address': item['address'], 'objectsCount': item['objectsCount'],
'functionCode': 1})
if rc and hasattr(rc, 'bits'):
Expand Down
4 changes: 2 additions & 2 deletions thingsboard_gateway/tb_utility/tb_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def connector_name(self, value):

def reset(self):
with TbLogger.ERRORS_MUTEX:
TbLogger.ALL_ERRORS_COUNT = TbLogger.ALL_ERRORS_COUNT - self.errors
TbLogger.ALL_ERRORS_COUNT = max(0, TbLogger.ALL_ERRORS_COUNT - self.errors)
self.errors = 0
self._update_errors_batch()

Expand Down Expand Up @@ -181,7 +181,7 @@ def _update_errors_batch(self, error_attr_name=None):
error_attr_name = error_attr_name + '_ERRORS_COUNT'
else:
error_attr_name = self.attr_name
TbLogger.ERRORS_BATCH[error_attr_name] = self.errors
TbLogger.ERRORS_BATCH[error_attr_name] = max(0, self.errors)

@classmethod
def send_errors_if_needed(cls, gateway):
Expand Down

0 comments on commit de67148

Please sign in to comment.