From 9da60f3815b92ac2f0391c9e26a679a09c8578b5 Mon Sep 17 00:00:00 2001 From: zbeacon Date: Tue, 7 Apr 2020 09:54:45 +0300 Subject: [PATCH] Added RPC params processing for Modbus and removed orjson from packages #280 #279 --- setup.cfg | 2 +- setup.py | 3 +- thingsboard-gateway.spec | 4 +- thingsboard_gateway/config/modbus.json | 5 +- .../modbus/bytes_modbus_downlink_converter.py | 23 ++++-- .../connectors/modbus/modbus_connector.py | 78 ++++++++++--------- .../gateway/tb_gateway_service.py | 3 +- .../storage/event_storage_reader.py | 3 +- .../tb_client/tb_device_mqtt.py | 3 +- .../tb_client/tb_gateway_mqtt.py | 3 +- thingsboard_gateway/tb_utility/tb_utility.py | 3 +- 11 files changed, 70 insertions(+), 60 deletions(-) diff --git a/setup.cfg b/setup.cfg index ce93af314..0c0fb8e9e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = thingsboard-gateway -version = 2.2.4.1 +version = 2.2.4.2 description = Thingsboard Gateway for IoT devices. long_description= file: README.md license = Apache Software License (Apache Software License 2.0) diff --git a/setup.py b/setup.py index 180a1d33d..03afaa560 100644 --- a/setup.py +++ b/setup.py @@ -38,12 +38,11 @@ 'pytz', 'PyYAML', 'simplejson', - 'orjson>=2.6.3', 'pyrsistent', 'requests', 'python-can' ], - download_url='https://github.com/thingsboard/thingsboard-gateway/archive/2.2.4.1.tar.gz', + download_url='https://github.com/thingsboard/thingsboard-gateway/archive/2.2.4.2.tar.gz', entry_points={ 'console_scripts': [ 'thingsboard-gateway = thingsboard_gateway.tb_gateway:daemon' diff --git a/thingsboard-gateway.spec b/thingsboard-gateway.spec index bf0d219bd..71a406a9e 100644 --- a/thingsboard-gateway.spec +++ b/thingsboard-gateway.spec @@ -1,6 +1,6 @@ %define name thingsboard-gateway -%define version 2.2.4.1 -%define unmangled_version 2.2.4.1 +%define version 2.2.4.2 +%define unmangled_version 2.2.4.2 %define release 1 Summary: Thingsboard Gateway for IoT devices. diff --git a/thingsboard_gateway/config/modbus.json b/thingsboard_gateway/config/modbus.json index f9b3909cf..86ceda9fc 100644 --- a/thingsboard_gateway/config/modbus.json +++ b/thingsboard_gateway/config/modbus.json @@ -55,11 +55,12 @@ "value": false }, "setCPUFanSpeed": { + "type": "int", "value": 42, "functionCode": 16, - "address": 0, + "address": 1, "byteOrder": "BIG", - "registerCount": 1 + "registerCount": 2 }, "getCPULoad": { "type": "int", diff --git a/thingsboard_gateway/connectors/modbus/bytes_modbus_downlink_converter.py b/thingsboard_gateway/connectors/modbus/bytes_modbus_downlink_converter.py index 2336e4c16..930c420a4 100644 --- a/thingsboard_gateway/connectors/modbus/bytes_modbus_downlink_converter.py +++ b/thingsboard_gateway/connectors/modbus/bytes_modbus_downlink_converter.py @@ -40,11 +40,16 @@ def convert(self, config, data): "16float": builder.add_16bit_float, "32float": builder.add_32bit_float, "64float": builder.add_64bit_float} - - value = config["value"] - lower_type = config["tag"].lower() + value = None + if data.get("data") and data["data"].get("params") is not None: + value = data["data"]["params"] + else: + value = config["value"] + lower_type = config.get("type", config.get("tag", "error")).lower() + if lower_type == "error": + log.error('"type" and "tag" - not found in configuration.') variable_size = config.get("registerCount", 1) * 8 - if lower_type in ["integer", "dword", "dword/integer", "word"]: + if lower_type in ["integer", "dword", "dword/integer", "word", "int"]: lower_type = str(variable_size) + "int" assert builder_functions.get(lower_type) is not None builder_functions[lower_type](value) @@ -68,6 +73,8 @@ def convert(self, config, data): log.debug(bits) builder.add_bits(bits) return builder.to_string() + else: + log.error("Unknown variable type") builder_converting_functions = {5: builder.to_coils, 15: builder.to_coils, @@ -77,6 +84,10 @@ def convert(self, config, data): function_code = config["functionCode"] if function_code in builder_converting_functions: - return builder_converting_functions[function_code]() - log.warning("Unsupported function code, for the device %s in the Modbus Downlink converter", self.__config["deviceName"]) + builder = builder_converting_functions[function_code]() + if "Exception" in str(builder): + log.exception(builder) + builder = str(builder) + return builder + log.warning("Unsupported function code, for the device %s in the Modbus Downlink converter", config["device"]) return None diff --git a/thingsboard_gateway/connectors/modbus/modbus_connector.py b/thingsboard_gateway/connectors/modbus/modbus_connector.py index e6934d890..1178743b6 100644 --- a/thingsboard_gateway/connectors/modbus/modbus_connector.py +++ b/thingsboard_gateway/connectors/modbus/modbus_connector.py @@ -231,44 +231,48 @@ def __function_to_device(self, config, unit_id): else: log.error("Unknown Modbus function with code: %i", function_code) log.debug("With result %s", str(result)) - + if "Exception" in str(result): + log.exception(result) + result = str(result) return result def server_side_rpc_handler(self, content): - log.debug("Modbus connector received rpc request for %s with content: %s", self.get_name(), content) - rpc_command_config = self.__devices[content["device"]]["config"]["rpc"].get(content["data"].get("method")) - - if rpc_command_config is not None: - rpc_command_config["unitId"] = self.__devices[content["device"]]["config"]["unitId"] - if rpc_command_config.get('bit') is not None: - rpc_command_config["functionCode"] = 6 - if rpc_command_config.get("functionCode") in (5, 6, 15, 16): - rpc_command_config["payload"] = self.__devices[content["device"]]["downlink_converter"].convert(rpc_command_config, content) - response = None - try: - response = self.__function_to_device(rpc_command_config, rpc_command_config["unitId"]) - except Exception as e: - log.exception(e) - - if isinstance(response, ReadRegistersResponseBase): - to_converter = {"rpc": {content["data"]["method"]: {"data_sent": rpc_command_config, - "input_data": response}}} - response = self.__devices[content["device"]]["converter"].convert(config=None, data=to_converter) - log.debug("Received RPC method: %s, result: %r", content["data"]["method"], response) - elif isinstance(response, (WriteMultipleRegistersResponse, - WriteMultipleCoilsResponse, - WriteSingleCoilResponse, - WriteSingleRegisterResponse)): - response = str(response) - log.debug("Write %r", response) - response = False if response is None else response - self.__gateway.send_rpc_reply(content["device"], - content["data"]["id"], - {content["data"]["method"]: response}) + if content.get("device") is not None: + log.debug("Modbus connector received rpc request for %s with content: %s", content["device"], content) + rpc_command_config = self.__devices[content["device"]]["config"]["rpc"].get(content["data"].get("method")) + if rpc_command_config is not None: + rpc_command_config["unitId"] = self.__devices[content["device"]]["config"]["unitId"] + if rpc_command_config.get('bit') is not None: + rpc_command_config["functionCode"] = 6 + if rpc_command_config.get("functionCode") in (5, 6, 15, 16): + rpc_command_config["payload"] = self.__devices[content["device"]]["downlink_converter"].convert(rpc_command_config, content) + response = None + try: + response = self.__function_to_device(rpc_command_config, rpc_command_config["unitId"]) + except Exception as e: + log.exception(e) + if isinstance(response, ReadRegistersResponseBase): + to_converter = {"rpc": {content["data"]["method"]: {"data_sent": rpc_command_config, + "input_data": response}}} + response = self.__devices[content["device"]]["converter"].convert(config=None, data=to_converter) + log.debug("Received RPC method: %s, result: %r", content["data"]["method"], response) + elif isinstance(response, (WriteMultipleRegistersResponse, + WriteMultipleCoilsResponse, + WriteSingleCoilResponse, + WriteSingleRegisterResponse)): + response = str(response) + log.debug("Write %r", response) + response = False if response is None else response + response = str(response) if isinstance(response, Exception) else response + self.__gateway.send_rpc_reply(content["device"], + content["data"]["id"], + {content["data"]["method"]: response}) + else: + log.error("Received rpc request, but method %s not found in config for %s.", + content["data"].get("method"), + self.get_name()) + self.__gateway.send_rpc_reply(content["device"], + content["data"]["id"], + {content["data"]["method"]: "METHOD NOT FOUND!"}) else: - log.error("Received rpc request, but method %s not found in config for %s.", - content["data"].get("method"), - self.get_name()) - self.__gateway.send_rpc_reply(content["device"], - content["data"]["id"], - {content["data"]["method"]: "METHOD NOT FOUND!"}) + log.debug("Received RPC to connector: %r", content) diff --git a/thingsboard_gateway/gateway/tb_gateway_service.py b/thingsboard_gateway/gateway/tb_gateway_service.py index a6174dae6..92bc9223e 100644 --- a/thingsboard_gateway/gateway/tb_gateway_service.py +++ b/thingsboard_gateway/gateway/tb_gateway_service.py @@ -24,8 +24,7 @@ from threading import Thread, RLock from yaml import safe_load -from simplejson import load, dumps -from orjson import loads +from simplejson import load, dumps, loads from thingsboard_gateway.gateway.tb_client import TBClient from thingsboard_gateway.gateway.tb_logger import TBLoggerHandler diff --git a/thingsboard_gateway/storage/event_storage_reader.py b/thingsboard_gateway/storage/event_storage_reader.py index fe62622b4..30307c68f 100644 --- a/thingsboard_gateway/storage/event_storage_reader.py +++ b/thingsboard_gateway/storage/event_storage_reader.py @@ -16,8 +16,7 @@ from os import remove from os.path import exists from base64 import b64decode -from simplejson import load, JSONDecodeError -from orjson import dumps +from simplejson import load, JSONDecodeError, dumps from thingsboard_gateway.storage.file_event_storage import log from thingsboard_gateway.storage.event_storage_files import EventStorageFiles from thingsboard_gateway.storage.file_event_storage_settings import FileEventStorageSettings diff --git a/thingsboard_gateway/tb_client/tb_device_mqtt.py b/thingsboard_gateway/tb_client/tb_device_mqtt.py index d48eec5d0..150ff47b4 100644 --- a/thingsboard_gateway/tb_client/tb_device_mqtt.py +++ b/thingsboard_gateway/tb_client/tb_device_mqtt.py @@ -21,8 +21,7 @@ import paho.mqtt.client as paho -# from simplejson import dumps -from orjson import dumps +from simplejson import dumps # from jsonschema import Draft7Validator # from jsonschema import ValidationError diff --git a/thingsboard_gateway/tb_client/tb_gateway_mqtt.py b/thingsboard_gateway/tb_client/tb_gateway_mqtt.py index 33d012ca3..eb8e798a7 100644 --- a/thingsboard_gateway/tb_client/tb_gateway_mqtt.py +++ b/thingsboard_gateway/tb_client/tb_gateway_mqtt.py @@ -14,8 +14,7 @@ import logging import time -# from simplejson import dumps -from orjson import dumps +from simplejson import dumps from thingsboard_gateway.tb_client.tb_device_mqtt import TBDeviceMqttClient from thingsboard_gateway.tb_utility.tb_utility import TBUtility diff --git a/thingsboard_gateway/tb_utility/tb_utility.py b/thingsboard_gateway/tb_utility/tb_utility.py index 06f484f8e..af7281cf8 100644 --- a/thingsboard_gateway/tb_utility/tb_utility.py +++ b/thingsboard_gateway/tb_utility/tb_utility.py @@ -17,8 +17,7 @@ from inspect import getmembers, isclass from importlib import util from logging import getLogger -# from simplejson import dumps, loads -from orjson import loads, dumps +from simplejson import dumps, loads from jsonpath_rw import parse