Skip to content

Commit

Permalink
Merge branch 'master' of github.com:thingsboard/thingsboard-gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
imbeacon committed Nov 22, 2024
2 parents 511dc8b + 14037ec commit 1497926
Showing 1 changed file with 9 additions and 27 deletions.
36 changes: 9 additions & 27 deletions thingsboard_gateway/connectors/modbus/modbus_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,12 @@ async def __poll_device(self, slave):
else:
self.__log.error('Socket is closed, connection is lost, for device %s', slave)
self.__delete_device_from_platform(slave)
except (ConnectionException, asyncio.exceptions.TimeoutError):
except ConnectionException:
self.__delete_device_from_platform(slave)
await asyncio.sleep(5)
self.__log.error('Failed to connect to device %s', slave)
except asyncio.exceptions.TimeoutError:
self.__log.error('Timeout error for device %s', slave)
except Exception as e:
self.__delete_device_from_platform(slave)
self.__log.error('Failed to poll %s device: %s', slave, exc_info=e)
Expand All @@ -263,32 +265,12 @@ async def __read_slave_data(self, slave: Slave):

for config_section in ('attributes', 'telemetry'):
for config in getattr(slave.uplink_converter_config, config_section):
response = await slave.read(config['functionCode'], config['address'], config['objectsCount'])

if 'Exception' in str(response) or 'Error' in str(response):
self.__log.error("Reading failed for device %s function code %s address %s unit id %s",
slave.device_name, config['functionCode'], config[ADDRESS_PARAMETER],
slave.unit_id)
self.__log.error("Reading failed with exception:", exc_info=result)
self.__log.info("Trying to reconnect to device %s", slave.device_name)
if slave.master.connected():
await slave.master.close()
connected, just_added = await slave.connect()
if just_added:
self.__manage_device_connectivity_to_platform(slave)
if connected:
self.__log.info("Reconnected to device %s", slave.device_name)
response = await slave.read(config['functionCode'], config['address'], config['objectsCount'])
if "Exception" in str(result) or "Error" in str(result):
self.__log.error("Reading failed for device %s function code %s address %s unit id %s",
slave.device_name, config['functionCode'], config[ADDRESS_PARAMETER],
slave.unit_id)
self.__log.error("Reading failed with exception:", exc_info=result)

if slave.master.connected():
await slave.master.close()

self.__log.info("Will try to connect to device %s later", slave.device_name)
try:
response = await slave.read(config['functionCode'], config['address'], config['objectsCount'])
except asyncio.exceptions.TimeoutError:
self.__log.error("Timeout error for device %s function code %s address %s",
slave.device_name, config['functionCode'], config[ADDRESS_PARAMETER])
continue

result[config_section][config['tag']] = response

Expand Down

0 comments on commit 1497926

Please sign in to comment.