Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed gateway as a slave init #1633

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions thingsboard_gateway/connectors/modbus/backward_compability_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Copyright 2024. ThingsBoard
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from logging import getLogger
from simplejson import dumps


class BackwardCompatibilityAdapter:
config_files_count = 1
CONFIG_PATH = None

def __init__(self, config, config_dir, logger=None):
if logger:
self._log = logger
else:
self._log = getLogger('BackwardCompatibilityAdapter')

self.__config = config
self.__config_dir = config_dir
BackwardCompatibilityAdapter.CONFIG_PATH = self.__config_dir
self.__keys = ['host', 'port', 'type', 'method', 'timeout', 'byteOrder', 'wordOrder', 'retries', 'retryOnEmpty',
'retryOnInvalid', 'baudrate']

@staticmethod
def __save_json_config_file(config):
with open(
f'{BackwardCompatibilityAdapter.CONFIG_PATH}modbus_new_{BackwardCompatibilityAdapter.config_files_count}.json',
'w') as file:
file.writelines(dumps(config, sort_keys=False, indent=' ', separators=(',', ': ')))
BackwardCompatibilityAdapter.config_files_count += 1

def __check_slaves_type_connection(self, config):
is_tcp_or_udp_connection = False
is_serial_connection = False

for slave in config['master']['slaves']:
if slave['type'] == 'tcp' or slave['type'] == 'udp':
is_tcp_or_udp_connection = True
elif slave['type'] == 'serial':
is_serial_connection = True

if is_tcp_or_udp_connection and is_serial_connection:
self._log.warning('It seems that your slaves using different connection type (tcp/udp and serial). '
'It is recommended to separate tcp/udp slaves and serial slaves in different connectors '
'to avoid problems with reading data.')

@staticmethod
def _convert_slave_configuration(slave_config):
if not slave_config:
return {}

values = slave_config.pop('values', {})
slave_config['values'] = {}
if len(values):
for (key, value) in values.items():
if isinstance(value, list):
value = value[0]
for section_name, section_value in value.items():
if slave_config['values'].get(key) is None:
slave_config['values'][key] = {section_name: section_value}
else:
slave_config['values'][key].update({section_name: section_value})

return slave_config

def convert(self):
if not self.__config.get('server'):
# check if slaves are similar type connection
self.__check_slaves_type_connection(self.__config)
converted_slave_config = self._convert_slave_configuration(self.__config.get('slave'))
self.__config['slave'] = converted_slave_config
return self.__config

self._log.warning(
'You are using old configuration structure for Modbus connector. It will be DEPRECATED in the future '
'version! New config file "modbus_new.json" was generated in %s folder. Please, use it.', self.CONFIG_PATH)
self._log.warning('You have to manually connect the new generated config file to tb_gateway.json!')

slaves = []
for device in self.__config['server'].get('devices', []):
slave = {**device}

for key in self.__keys:
if not device.get(key):
slave[key] = self.__config['server'].get(key)

slave['pollPeriod'] = slave['timeseriesPollPeriod']

slaves.append(slave)

result_dict = {'master': {'slaves': slaves}, 'slave': self.__config.get('slave')}

# check if slaves are similar type connection
self.__check_slaves_type_connection(result_dict)

self.__save_json_config_file(result_dict)

return result_dict
7 changes: 5 additions & 2 deletions thingsboard_gateway/connectors/modbus/modbus_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
from thingsboard_gateway.connectors.modbus.slave import Slave
from thingsboard_gateway.connectors.modbus.entities.bytes_downlink_converter_config import \
BytesDownlinkConverterConfig
from thingsboard_gateway.connectors.modbus.backward_compability_adapter import BackwardCompatibilityAdapter

from pymodbus.exceptions import ConnectionException
from pymodbus.bit_read_message import ReadBitsResponseBase
Expand All @@ -80,8 +81,7 @@ def __init__(self, gateway, config, connector_type):
super().__init__()
self.__gateway = gateway
self._connector_type = connector_type
self.__config = config
self.name = self.__config.get("name", 'Modbus Connector ' + ''.join(choice(ascii_lowercase) for _ in range(5)))
self.name = config.get("name", 'Modbus Connector ' + ''.join(choice(ascii_lowercase) for _ in range(5)))
self.__log = init_logger(self.__gateway, config.get('name', self.name),
config.get('logLevel', 'INFO'),
enable_remote_logging=config.get('enableRemoteLogging', False),
Expand All @@ -90,6 +90,9 @@ def __init__(self, gateway, config, connector_type):
config.get('logLevel', 'INFO'),
enable_remote_logging=config.get('enableRemoteLogging', False),
is_converter_logger=True, attr_name=self.name)
self.__backward_compatibility_adapter = BackwardCompatibilityAdapter(config, gateway.get_config_path(),
logger=self.__log)
self.__config = self.__backward_compatibility_adapter.convert()
self.__log.info('Starting Modbus Connector...')
self.__id = self.__config.get('id')
self.__connected = False
Expand Down
49 changes: 29 additions & 20 deletions thingsboard_gateway/connectors/modbus/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,26 +189,35 @@ def __get_server_context(self, config):
converter = BytesModbusDownlinkConverter({}, self.__log)
for section in ('attributes', 'timeseries', 'attributeUpdates', 'rpc'):
for item in value.get(section, []):
function_code = FUNCTION_CODE_SLAVE_INITIALIZATION[key][0] if item['objectsCount'] <= 1 else \
FUNCTION_CODE_SLAVE_INITIALIZATION[key][1]
converter_config = BytesDownlinkConverterConfig(
device_name=config.get('deviceName', 'Gateway'),
byte_order=config['byteOrder'],
word_order=config.get('wordOrder', 'LITTLE'),
repack=config.get('repack', False),
objects_count=item['objectsCount'],
function_code=function_code,
lower_type=item.get('type', item.get('tag', 'error')),
address=item.get('address', 0)
)
converted_value = converter.convert(converter_config, {'data': {'params': item['value']}})
if converted_value is not None:
values[item['address'] + 1] = converted_value
else:
self.__log.error("Failed to convert value %s with type %s, skipping...", item['value'],
item['type'])
if len(values):
blocks[FUNCTION_TYPE[key]] = ModbusSparseDataBlock(values)
try:
function_code = FUNCTION_CODE_SLAVE_INITIALIZATION[key][0] if item['objectsCount'] <= 1 else \
FUNCTION_CODE_SLAVE_INITIALIZATION[key][1]
converter_config = BytesDownlinkConverterConfig(
device_name=config.get('deviceName', 'Gateway'),
byte_order=config['byteOrder'],
word_order=config.get('wordOrder', 'LITTLE'),
repack=config.get('repack', False),
objects_count=item['objectsCount'],
function_code=function_code,
lower_type=item.get(
'type', item.get('tag', 'error')),
address=item.get('address', 0)
)
converted_value = converter.convert(
converter_config, {'data': {'params': item['value']}})
if converted_value is not None:
values[item['address'] + 1] = converted_value
else:
self.__log.error("Failed to convert value %s with type %s, skipping...", item['value'],
item['type'])
except Exception as e:
self.__log.error("Failed to configure value %s with error: %s, skipping...", item['value'], e)

try:
if len(values):
blocks[FUNCTION_TYPE[key]] = ModbusSparseDataBlock(values)
except Exception as e:
self.__log.error("Failed to configure block %s with error: %s", key, e)

if not len(blocks):
self.__log.info("%s - will be initialized without values", config.get('deviceName', 'Modbus Slave'))
Expand Down
2 changes: 1 addition & 1 deletion thingsboard_gateway/connectors/modbus/slave.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, connector, logger, config):
self.port = config['port']
self.method = config['method']
self.tls = config.get('tls', {})
self.timeout = config.get('timeout')
self.timeout = config.get('timeout', 30)
self.retry_on_empty = config.get('retryOnEmpty', False)
self.retry_on_invalid = config.get('retryOnInvalid', False)
self.retries = config.get('retries', 3)
Expand Down
Loading