From 4ddd022e68fa0d1f69d80e3fc281047f2d9ee402 Mon Sep 17 00:00:00 2001 From: zbeacon Date: Fri, 7 Feb 2020 09:34:41 +0200 Subject: [PATCH] Improvements for converters --- .../extensions/mqtt/custom_mqtt_uplink_converter.py | 2 +- .../request/custom_request_uplink_converter.py | 4 ++-- .../extensions/serial/custom_serial_connector.py | 2 +- setup.cfg | 2 +- setup.py | 2 +- .../connectors/mqtt/json_mqtt_uplink_converter.py | 12 +++++++++--- .../request/json_request_uplink_converter.py | 4 ++-- .../request/custom_request_uplink_converter.py | 4 ++-- thingsboard_gateway/tb_utility/tb_utility.py | 4 ++-- 9 files changed, 21 insertions(+), 15 deletions(-) diff --git a/for_build/etc/thingsboard-gateway/extensions/mqtt/custom_mqtt_uplink_converter.py b/for_build/etc/thingsboard-gateway/extensions/mqtt/custom_mqtt_uplink_converter.py index b7d4f1092..c50a3eda2 100644 --- a/for_build/etc/thingsboard-gateway/extensions/mqtt/custom_mqtt_uplink_converter.py +++ b/for_build/etc/thingsboard-gateway/extensions/mqtt/custom_mqtt_uplink_converter.py @@ -42,5 +42,5 @@ def convert(self, topic, body): return self.dict_result except Exception as e: - log.error('Error in converter, for config: \n%s\n and message: \n%s\n', dumps(self.__config), body) + log.exception('Error in converter, for config: \n%s\n and message: \n%s\n', dumps(self.__config), body) log.error(e) diff --git a/for_build/etc/thingsboard-gateway/extensions/request/custom_request_uplink_converter.py b/for_build/etc/thingsboard-gateway/extensions/request/custom_request_uplink_converter.py index 44010d9c4..287815fa9 100644 --- a/for_build/etc/thingsboard-gateway/extensions/request/custom_request_uplink_converter.py +++ b/for_build/etc/thingsboard-gateway/extensions/request/custom_request_uplink_converter.py @@ -26,8 +26,8 @@ def __init__(self, config): def convert(self, topic, body): try: data = body["data"]["value"] - self.dict_result["deviceName"] = TBUtility.get_value(self.__config.get("deviceNameJsonExpression"), body) - self.dict_result["deviceType"] = TBUtility.get_value(self.__config.get("deviceTypeJsonExpression"), body) + self.dict_result["deviceName"] = TBUtility.get_value(self.__config.get("deviceNameJsonExpression"), body, expression_instead_none=True) + self.dict_result["deviceType"] = TBUtility.get_value(self.__config.get("deviceTypeJsonExpression"), body, expression_instead_none=True) self.dict_result["attributes"] = [] self.dict_result["telemetry"] = [] converted_bytes = bytearray.fromhex(data) diff --git a/for_build/etc/thingsboard-gateway/extensions/serial/custom_serial_connector.py b/for_build/etc/thingsboard-gateway/extensions/serial/custom_serial_connector.py index 1ab77a948..5c051c699 100644 --- a/for_build/etc/thingsboard-gateway/extensions/serial/custom_serial_connector.py +++ b/for_build/etc/thingsboard-gateway/extensions/serial/custom_serial_connector.py @@ -78,7 +78,7 @@ def __connect_to_devices(self): # Function for opening connection and connect log.exception(e) time.sleep(10) else: # if no exception handled - add device and change connection state - self.__gateway.add_device(self.devices[device]["device_config"]["name"], {"connector": self}, device_type=self.devices[device]["device_config"]["type"]) + self.__gateway.add_device(self.devices[device]["device_config"]["name"], {"connector": self}, self.devices[device]["device_config"]["type"]) self.connected = True def open(self): # Function called by gateway on start diff --git a/setup.cfg b/setup.cfg index 427dfbe5c..6162eea7f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = thingsboard-gateway -version = 2.1 +version = 2.1.0.1 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 75b9f6bf5..c4382ec05 100644 --- a/setup.py +++ b/setup.py @@ -38,7 +38,7 @@ 'simplejson', 'pyrsistent' ], - download_url='https://github.com/thingsboard/thingsboard-gateway/archive/2.1.tar.gz', + download_url='https://github.com/thingsboard/thingsboard-gateway/archive/2.1.0.1.tar.gz', entry_points={ 'console_scripts': [ 'thingsboard-gateway = thingsboard_gateway.tb_gateway:daemon' diff --git a/thingsboard_gateway/connectors/mqtt/json_mqtt_uplink_converter.py b/thingsboard_gateway/connectors/mqtt/json_mqtt_uplink_converter.py index dd147266b..2c1c41d7d 100644 --- a/thingsboard_gateway/connectors/mqtt/json_mqtt_uplink_converter.py +++ b/thingsboard_gateway/connectors/mqtt/json_mqtt_uplink_converter.py @@ -14,6 +14,7 @@ from simplejson import dumps from re import search +from time import time from thingsboard_gateway.connectors.mqtt.mqtt_uplink_converter import MqttUplinkConverter, log from thingsboard_gateway.tb_utility.tb_utility import TBUtility @@ -56,7 +57,8 @@ def convert(self, config, data): if self.__config.get("attributes"): for attribute in self.__config.get("attributes"): attribute_value = TBUtility.get_value(attribute["value"], data, attribute["type"]) - if attribute_value is not None: + tag = TBUtility.get_value(attribute["value"], data, attribute["type"], get_tag=True) + if attribute_value is not None and data.get(tag) is not None and attribute_value != attribute["value"]: dict_result["attributes"].append({attribute["key"]: attribute_value}) else: log.debug("%s key not found in message: %s", attribute["value"].replace("${", '"').replace("}", '"'), data) @@ -67,8 +69,12 @@ def convert(self, config, data): if self.__config.get("timeseries"): for ts in self.__config.get("timeseries"): ts_value = TBUtility.get_value(ts["value"], data, ts["type"]) - if ts_value is not None: - dict_result["telemetry"].append({ts["key"]: ts_value}) + tag = TBUtility.get_value(ts["value"], data, ts["type"], get_tag=True) + if ts_value is not None and data.get(tag) is not None and ts_value != ts["value"]: + if data.get('ts') is not None or data.get('timestamp') is not None: + dict_result["telemetry"].append({"ts": data.get('ts', data.get('timestamp', int(time()))), 'values': {ts['key']: ts_value}}) + else: + dict_result["telemetry"].append({ts["key"]: ts_value}) else: log.debug("%s key not found in message: %s", ts["value"].replace("${", '"').replace("}", '"'), data) except Exception as e: diff --git a/thingsboard_gateway/connectors/request/json_request_uplink_converter.py b/thingsboard_gateway/connectors/request/json_request_uplink_converter.py index 34f5f94f9..db1b915c5 100644 --- a/thingsboard_gateway/connectors/request/json_request_uplink_converter.py +++ b/thingsboard_gateway/connectors/request/json_request_uplink_converter.py @@ -27,11 +27,11 @@ def convert(self, config, data): dict_result = {"deviceName": None, "deviceType": None,"attributes": [], "telemetry": []} try: if self.__config.get("deviceNameJsonExpression") is not None: - dict_result["deviceName"] = TBUtility.get_value(self.__config.get("deviceNameJsonExpression"), data) + dict_result["deviceName"] = TBUtility.get_value(self.__config.get("deviceNameJsonExpression"), data, expression_instead_none=True) else: log.error("The expression for looking \"deviceName\" not found in config %s", dumps(self.__config)) if self.__config.get("deviceTypeJsonExpression") is not None: - dict_result["deviceType"] = TBUtility.get_value(self.__config.get("deviceTypeJsonExpression"), data) + dict_result["deviceType"] = TBUtility.get_value(self.__config.get("deviceTypeJsonExpression"), data, expression_instead_none=True) else: log.error("The expression for looking \"deviceType\" not found in config %s", dumps(self.__config)) dict_result["attributes"] = [] diff --git a/thingsboard_gateway/extensions/request/custom_request_uplink_converter.py b/thingsboard_gateway/extensions/request/custom_request_uplink_converter.py index 44010d9c4..287815fa9 100644 --- a/thingsboard_gateway/extensions/request/custom_request_uplink_converter.py +++ b/thingsboard_gateway/extensions/request/custom_request_uplink_converter.py @@ -26,8 +26,8 @@ def __init__(self, config): def convert(self, topic, body): try: data = body["data"]["value"] - self.dict_result["deviceName"] = TBUtility.get_value(self.__config.get("deviceNameJsonExpression"), body) - self.dict_result["deviceType"] = TBUtility.get_value(self.__config.get("deviceTypeJsonExpression"), body) + self.dict_result["deviceName"] = TBUtility.get_value(self.__config.get("deviceNameJsonExpression"), body, expression_instead_none=True) + self.dict_result["deviceType"] = TBUtility.get_value(self.__config.get("deviceTypeJsonExpression"), body, expression_instead_none=True) self.dict_result["attributes"] = [] self.dict_result["telemetry"] = [] converted_bytes = bytearray.fromhex(data) diff --git a/thingsboard_gateway/tb_utility/tb_utility.py b/thingsboard_gateway/tb_utility/tb_utility.py index 9dc9dfa37..e589622b9 100644 --- a/thingsboard_gateway/tb_utility/tb_utility.py +++ b/thingsboard_gateway/tb_utility/tb_utility.py @@ -90,7 +90,7 @@ def check_and_import(extension_type, module_name): log.exception(e) @staticmethod - def get_value(expression, body={}, value_type="string", get_tag=False): + def get_value(expression, body={}, value_type="string", get_tag=False, expression_instead_none=False): if isinstance(body, str): body = loads(body) if not expression: @@ -125,7 +125,7 @@ def get_value(expression, body={}, value_type="string", get_tag=False): full_value = search(expression, body).group(0) except Exception: full_value = None - if full_value is None: + if full_value is None and expression_instead_none: full_value = expression else: try: