Skip to content

Commit

Permalink
Improvements for converters
Browse files Browse the repository at this point in the history
  • Loading branch information
imbeacon committed Feb 7, 2020
1 parent 1cc382e commit 4ddd022
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions thingsboard_gateway/tb_utility/tb_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 4ddd022

Please sign in to comment.