def _convert_slices_view_data(self, data): dict_result, get_device_name_from_data, get_device_type_from_data = self._get_required_data( '[', ']') try: for data_type in self.__data_types: for information in self.__config[data_type]: arr = data.split(self.__config['delimiter']) val = self._get_key_or_value(information['value'], arr) key = self._get_key_or_value(information['key'], arr) dict_result[self.__data_types[data_type]].append( {key: val}) if get_device_name_from_data: if self.__config['devicePatternName'] == information[ 'value']: dict_result['deviceName'] = val if get_device_type_from_data: if self.__config['devicePatternType'] == information[ 'value']: dict_result['deviceType'] = val except Exception as e: log.error( 'Error in converter, for config: \n%s\n and message: \n%s\n', dumps(self.__config), data) log.exception(e) return dict_result
def convert(self, configs, can_data): result = {"attributes": {}, "telemetry": {}} for config in configs: try: tb_key = config["key"] tb_item = "telemetry" if config["is_ts"] else "attributes" data_length = config["length"] if config[ "length"] != -1 else len(can_data) - config["start"] # The 'value' variable is used in eval if config["type"][0] == "b": value = bool(can_data[config["start"]]) elif config["type"][0] == "i" or config["type"][0] == "l": value = int.from_bytes( can_data[config["start"]:config["start"] + data_length], config["byteorder"], signed=config["signed"]) elif config["type"][0] == "f" or config["type"][0] == "d": fmt = ">" + config["type"][0] if config["byteorder"][ 0] == "b" else "<" + config["type"][0] value = struct.unpack_from( fmt, bytes(can_data[config["start"]:config["start"] + data_length]))[0] elif config["type"][0] == "s": value = can_data[config["start"]:config["start"] + data_length].decode(config["encoding"]) elif config["type"][0] == "r": value = "" for hex_byte in can_data[config["start"]:config["start"] + data_length]: value += "%02x" % hex_byte else: log.error( "Failed to convert CAN data to TB %s '%s': unknown data type '%s'", "time series key" if config["is_ts"] else "attribute", tb_key, config["type"]) continue if config.get("expression", ""): result[tb_item][tb_key] = eval( config["expression"], {"__builtins__": {}} if config["strictEval"] else globals(), { "value": value, "can_data": can_data }) else: result[tb_item][tb_key] = value except Exception as e: log.error( "Failed to convert CAN data to TB %s '%s': %s", "time series key" if config["is_ts"] else "attribute", tb_key, str(e)) continue return result
def convert(self, config, data): try: if self.__conversion_methods.get(data.DESCRIPTOR) is not None: return self.__conversion_methods[data.DESCRIPTOR](data) else: log.error("[GRPC] unknown uplink message descriptor: %r", data.DESCRIPTOR) return {} except Exception as e: log.exception("[GRPC] ", e) return {}
def _convert_table_view_data(self, config, data): dict_result, get_device_name_from_data, get_device_type_from_data = self._get_required_data( '${', '}') try: for data_type in self.__data_types: for information in self.__config[data_type]: arr = data.split(self.__config['delimiter']) key_index = information['key'] val_index = information['value'] if '${' in information['key'] and '}' in information['key']: key_index = config['headers'].index( re.sub(r'[^\w]', '', information['key'])) if '${' in information['value'] and '}' in information[ 'value']: val_index = config['headers'].index( re.sub(r'[^\w]', '', information['value'])) dict_result[self. __data_types[data_type]].append({ arr[key_index] if isinstance( key_index, int) else key_index: arr[val_index] if isinstance( val_index, int) else val_index }) if get_device_name_from_data: index = config['headers'].index( re.sub(r'[^\w]', '', self.__config['devicePatternName'])) dict_result['deviceName'] = arr[index] if get_device_type_from_data: index = config['headers'].index( re.sub(r'[^\w]', '', self.__config['devicePatternType'])) dict_result['deviceType'] = arr[index] except Exception as e: log.error( 'Error in converter, for config: \n%s\n and message: \n%s\n', dumps(self.__config), data) log.exception(e) return dict_result
def convert(self, config, data): if isinstance(config, str) and config == "*": return data converted_data = {} for config_item in config: try: if isinstance(config_item, str): converted_data[config_item] = data[config_item] elif isinstance(config_item, dict): if "nameExpression" in config_item: name = eval(config_item["nameExpression"], globals(), data) else: name = config_item["name"] if "column" in config_item: converted_data[name] = data[config_item["column"]] elif "value" in config_item: converted_data[name] = eval(config_item["value"], globals(), data) else: log.error( "Failed to convert SQL data to TB format: no column/value configuration item" ) else: log.error( "Failed to convert SQL data to TB format: unexpected configuration type '%s'", type(config_item)) except Exception as e: log.error("Failed to convert SQL data to TB format: %s", str(e)) return converted_data
def convert(self, config, data): try: if config.get("dataInHex", ""): return list(bytearray.fromhex(config["dataInHex"])) if not isinstance(data, dict) or not data: log.error( "Failed to convert TB data to CAN payload: data is empty or not a dictionary" ) return if data.get("dataInHex", ""): return list(bytearray.fromhex(data["dataInHex"])) if config.get("dataExpression", ""): value = eval(config["dataExpression"], {"__builtins__": {}} if config.get( "strictEval", True) else globals(), data) elif "value" in data: value = data["value"] else: log.error( "Failed to convert TB data to CAN payload: no `value` or `dataExpression` property" ) return can_data = [] if config.get("dataBefore", ""): can_data.extend(bytearray.fromhex(config["dataBefore"])) if isinstance(value, bool): can_data.extend([int(value)]) elif isinstance(value, int) or isinstance(value, float): byteorder = config["dataByteorder"] if config.get( "dataByteorder", "") else "big" if isinstance(value, int): can_data.extend( value.to_bytes(config.get("dataLength", 1), byteorder, signed=(config.get("dataSigned", False) or value < 0))) else: can_data.extend( struct.pack(">f" if byteorder[0] == "b" else "<f", value)) elif isinstance(value, str): can_data.extend( value.encode(config["dataEncoding"] if config. get("dataEncoding", "") else "ascii")) if config.get("dataAfter", ""): can_data.extend(bytearray.fromhex(config["dataAfter"])) return can_data except Exception as e: log.error("Failed to convert TB data to CAN payload: %s", str(e)) return
def _convert_json_file(self, data): dict_result = { "deviceName": None, "deviceType": None, "attributes": [], "telemetry": [] } try: if self.__config.get("devicePatternName") is not None: device_name_tags = TBUtility.get_values( self.__config.get("devicePatternName"), data, get_tag=True) device_name_values = TBUtility.get_values( self.__config.get("devicePatternName"), data, expression_instead_none=True) dict_result["deviceName"] = self.__config.get( "devicePatternName") for (device_name_tag, device_name_value) in zip(device_name_tags, device_name_values): is_valid_key = "${" in self.__config.get("devicePatternName") and "}" in \ self.__config.get("devicePatternName") dict_result['deviceName'] = dict_result['deviceName'].replace('${' + str(device_name_tag) + '}', str(device_name_value)) \ if is_valid_key else device_name_tag else: log.error( "The expression for looking \"deviceName\" not found in config %s", dumps(self.__config)) if self.__config.get("devicePatternType") is not None: device_type_tags = TBUtility.get_values( self.__config.get("devicePatternType"), data, get_tag=True) device_type_values = TBUtility.get_values( self.__config.get("devicePatternType"), data, expression_instead_none=True) dict_result["deviceType"] = self.__config.get( "devicePatternType") for (device_type_tag, device_type_value) in zip(device_type_tags, device_type_values): is_valid_key = "${" in self.__config.get("devicePatternType") and "}" in \ self.__config.get("devicePatternType") dict_result["deviceType"] = dict_result["deviceType"].replace('${' + str(device_type_tag) + '}', str(device_type_value)) \ if is_valid_key else device_type_tag except Exception as e: log.error( 'Error in converter, for config: \n%s\n and message: \n%s\n', dumps(self.__config), data) log.exception(e) try: for datatype in self.__data_types: dict_result[self.__data_types[datatype]] = [] for datatype_config in self.__config.get(datatype, []): values = TBUtility.get_values(datatype_config["value"], data, datatype_config["type"], expression_instead_none=True) values_tags = TBUtility.get_values( datatype_config["value"], data, datatype_config["type"], get_tag=True) keys = TBUtility.get_values(datatype_config["key"], data, datatype_config["type"], expression_instead_none=True) keys_tags = TBUtility.get_values(datatype_config["key"], data, get_tag=True) full_key = datatype_config["key"] for (key, key_tag) in zip(keys, keys_tags): is_valid_key = "${" in datatype_config["key"] and "}" in \ datatype_config["key"] full_key = full_key.replace( '${' + str(key_tag) + '}', str(key)) if is_valid_key else key_tag full_value = datatype_config["value"] for (value, value_tag) in zip(values, values_tags): is_valid_value = "${" in datatype_config["value"] and "}" in \ datatype_config["value"] full_value = full_value.replace( '${' + str(value_tag) + '}', str(value)) if is_valid_value else str(value) if datatype == 'timeseries' and (data.get("ts") is not None or data.get("timestamp") is not None): dict_result[self.__data_types[datatype]].append({ "ts": data.get('ts', data.get('timestamp', int(time()))), 'values': { full_key: full_value } }) else: dict_result[self.__data_types[datatype]].append( {full_key: full_value}) except Exception as e: log.error( 'Error in converter, for config: \n%s\n and message: \n%s\n', dumps(self.__config), str(data)) log.exception(e) return dict_result