mirror of
https://github.com/thingsboard/thingsboard-gateway
synced 2025-10-26 22:31:42 +08:00
TBUtility improvements and refactoring
This commit is contained in:
@@ -17,8 +17,8 @@ from inspect import getmembers, isclass
|
||||
from importlib import util
|
||||
from logging import getLogger
|
||||
from simplejson import dumps, loads
|
||||
from re import search, match, compile
|
||||
from jsonpath_rw import jsonpath, parse
|
||||
from re import search
|
||||
from jsonpath_rw import parse
|
||||
|
||||
|
||||
log = getLogger("service")
|
||||
@@ -40,7 +40,7 @@ class TBUtility:
|
||||
if not data.get("deviceType") or data.get("deviceType") is None:
|
||||
log.error('deviceType is empty in data: %s', json_data)
|
||||
return False
|
||||
if (data.get("attributes") is None and data.get("telemetry") is None) or (not data.get("attributes") and not data.get("telemetry")):
|
||||
if not data.get("attributes") and not data.get("telemetry"):
|
||||
log.error('No telemetry and attributes in data: %s', json_data)
|
||||
return False
|
||||
return True
|
||||
@@ -67,15 +67,12 @@ class TBUtility:
|
||||
module_spec = util.spec_from_file_location(module_name, extension_path + path.sep + file)
|
||||
log.debug(module_spec)
|
||||
if module_spec is None:
|
||||
log.error('Module: {} not found'.format(module_name))
|
||||
log.error('Module: %s not found', module_name)
|
||||
continue
|
||||
else:
|
||||
module = util.module_from_spec(module_spec)
|
||||
log.debug(str(module))
|
||||
try:
|
||||
module_spec.loader.exec_module(module)
|
||||
except Exception as e:
|
||||
log.exception(e)
|
||||
module_spec.loader.exec_module(module)
|
||||
for extension_class in getmembers(module, isclass):
|
||||
if module_name in extension_class:
|
||||
log.debug("Import %s from %s.", module_name, extension_path)
|
||||
@@ -90,7 +87,7 @@ class TBUtility:
|
||||
log.exception(e)
|
||||
|
||||
@staticmethod
|
||||
def get_value(expression, body={}, value_type="string", get_tag=False, expression_instead_none=False):
|
||||
def get_value(expression, body=None, value_type="string", get_tag=False, expression_instead_none=False):
|
||||
if isinstance(body, str):
|
||||
body = loads(body)
|
||||
if not expression:
|
||||
@@ -107,50 +104,23 @@ class TBUtility:
|
||||
return target_str
|
||||
full_value = None
|
||||
try:
|
||||
if value_type == "string":
|
||||
try:
|
||||
full_value = expression[0: min(abs(p1 - 2), 0)] + body[target_str.split()[0]] + expression[p2 + 1:len(expression)]
|
||||
except KeyError:
|
||||
pass
|
||||
if full_value is None:
|
||||
try:
|
||||
jsonpath_expression = parse(target_str)
|
||||
jsonpath_match = jsonpath_expression.find(body)
|
||||
if jsonpath_match is not None:
|
||||
full_value = jsonpath_match[0].value
|
||||
except Exception:
|
||||
pass
|
||||
if full_value is None:
|
||||
try:
|
||||
full_value = search(expression, body).group(0)
|
||||
except Exception:
|
||||
full_value = None
|
||||
if full_value is None and expression_instead_none:
|
||||
full_value = expression
|
||||
else:
|
||||
if full_value is None:
|
||||
try:
|
||||
jsonpath_expression = parse(target_str)
|
||||
jsonpath_match = jsonpath_expression.find(body)
|
||||
if jsonpath_match is not None:
|
||||
full_value = jsonpath_match[0].value
|
||||
except Exception:
|
||||
full_value = None
|
||||
if full_value is None:
|
||||
try:
|
||||
full_value = search(expression, body).group(0)
|
||||
except Exception:
|
||||
full_value = None
|
||||
if full_value is None:
|
||||
try:
|
||||
full_value = body.get(target_str.split()[0])
|
||||
except Exception as e:
|
||||
log.exception(e)
|
||||
except TypeError:
|
||||
if full_value is None:
|
||||
log.error('Value is None - Cannot find the pattern: %s in %s. Expression will be interpreted as value.', target_str, dumps(body))
|
||||
if isinstance(body, dict) and target_str.split()[0] in body:
|
||||
if value_type.lower() == "string":
|
||||
full_value = expression[0: max(abs(p1 - 2), 0)] + body[target_str.split()[0]] + expression[p2 + 1:len(expression)]
|
||||
else:
|
||||
full_value = body.get(target_str.split()[0])
|
||||
elif isinstance(body, dict):
|
||||
jsonpath_expression = parse(target_str)
|
||||
jsonpath_match = jsonpath_expression.find(body)
|
||||
if jsonpath_match:
|
||||
full_value = jsonpath_match[0].value
|
||||
elif isinstance(body, (str, bytes)):
|
||||
search_result = search(expression, body)
|
||||
if search_result.groups():
|
||||
full_value = search_result.group(0)
|
||||
elif expression_instead_none:
|
||||
full_value = expression
|
||||
except Exception as e:
|
||||
log.exception(e)
|
||||
return None
|
||||
return full_value
|
||||
finally:
|
||||
return full_value
|
||||
|
||||
Reference in New Issue
Block a user