Skip to content

Commit

Permalink
Fix for not updated slave configuration during remote configuration u…
Browse files Browse the repository at this point in the history
…pdate
  • Loading branch information
imbeacon committed Jan 3, 2024
1 parent 61988e7 commit f93c50d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
34 changes: 21 additions & 13 deletions thingsboard_gateway/connectors/modbus/modbus_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
TBUtility.install_package('twisted')
from twisted.internet import reactor

from twisted.internet import reactor
from pymodbus.bit_write_message import WriteSingleCoilResponse, WriteMultipleCoilsResponse
from pymodbus.register_write_message import WriteMultipleRegistersResponse, WriteSingleRegisterResponse
from pymodbus.register_read_message import ReadRegistersResponseBase
Expand Down Expand Up @@ -137,15 +136,16 @@ def __init__(self, gateway, config, connector_type):
self.__max_msg_number_for_worker = config.get('maxMessageNumberPerWorker', 10)
self.__max_number_of_workers = config.get('maxNumberOfWorkers', 100)

self.__slaves = []
self.__slave_thread = None

if self.__config.get('slave'):
self.__slave_thread = Thread(target=self.__configure_and_run_slave, args=(self.__config['slave'],),
daemon=True, name='Gateway modbus slave')
self.__slave_thread.start()

if config['slave'].get('sendDataToThingsBoard', False):
self.__modify_main_config()

self.__slaves = []
self.__load_slaves()

def is_connected(self):
Expand Down Expand Up @@ -206,7 +206,7 @@ def __configure_and_run_slave(self, config):
converted_value = converter.convert(
{**val,
'device': config.get('deviceName', 'Gateway'), 'functionCode': function_code,
'byteOrder': config['byteOrder'], 'wordOrder': config.get('wordOrder', 'BIG')},
'byteOrder': config['byteOrder'], 'wordOrder': config.get('wordOrder', 'LITTLE')},
{'data': {'params': val['value']}})
if converted_value is not None:
values[val['address'] + 1] = converted_value
Expand Down Expand Up @@ -244,10 +244,9 @@ def __modify_main_config(self):
self.__config['master']['slaves'].append(device)

def __load_slaves(self):
self.__slaves = [
Slave(**{**device, 'connector': self, 'gateway': self.__gateway, 'logger': self.__log,
'callback': ModbusConnector.callback}) for
device in self.__config.get('master', {'slaves': []}).get('slaves', [])]
for device in self.__config.get('master', {'slaves': []}).get('slaves', []):
self.__slaves.append(Slave(**{**device, 'connector': self, 'gateway': self.__gateway, 'logger': self.__log,
'callback': ModbusConnector.callback}))

@classmethod
def callback(cls, slave):
Expand All @@ -274,7 +273,7 @@ def __thread_manager(self):
elif number_of_needed_threads < threads_count and threads_count > 1:
worker: ModbusConnector.ConverterWorker = self.__workers_thread_pool[-1]
if not worker.in_progress:
worker.stopped = True
worker.close()
self.__workers_thread_pool.remove(worker)

def __convert_data(self, params):
Expand Down Expand Up @@ -330,8 +329,14 @@ def close(self):
for slave in self.__slaves:
slave.close()

if reactor.running:
if self.__slave_thread is not None:
ServerStop()

# Stop all workers
for worker in self.__workers_thread_pool:
worker.close()

# self.__slave_thread.join()
self.__log.info('%s has been stopped.', self.get_name())
self.__log.reset()
self.__stopping = False
Expand All @@ -340,7 +345,7 @@ def get_name(self):
return self.name

def __process_slaves(self):
while True:
while not self.__stopped:
if not self.__stopped and not ModbusConnector.process_requests.empty():
device = ModbusConnector.process_requests.get()

Expand Down Expand Up @@ -738,15 +743,15 @@ class ConverterWorker(Thread):
def __init__(self, name, incoming_queue, send_result, logger):
super().__init__()
self._log = logger
self.stopped = False
self.__stopped = False
self.setName(name)
self.setDaemon(True)
self.__msg_queue = incoming_queue
self.in_progress = False
self.__send_result = send_result

def run(self):
while not self.stopped:
while not self.__stopped:
if not self.__msg_queue.empty():
self.in_progress = True
convert_function, params = self.__msg_queue.get(True, 10)
Expand All @@ -757,3 +762,6 @@ def run(self):
self.in_progress = False
else:
sleep(.001)

def close(self):
self.__stopped = True
3 changes: 3 additions & 0 deletions thingsboard_gateway/connectors/modbus/slave.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def __init__(self, **kwargs):
self.daemon = True
self.stop = False

self.name = "Modbus slave processor for unit " + str(self.config['unitId']) + " on host " + str(
self.config['host']) + ":" + str(self.config['port'])

self.start()

def timer(self):
Expand Down

0 comments on commit f93c50d

Please sign in to comment.