def convert(self, config, data): datatypes = {"attributes": "attributes", "timeseries": "telemetry"} 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, expression_instead_none=True) elif self.__config.get("deviceNameTopicExpression") is not None: search_result = search(self.__config["deviceNameTopicExpression"], config) if search_result is not None: dict_result["deviceName"] = search_result.group(0) else: log.debug("Regular expression result is None. deviceNameTopicExpression parameter will be interpreted as a deviceName\n Topic: %s\nRegex: %s", config, self.__config.get("deviceNameTopicExpression")) dict_result["deviceName"] = self.__config.get("deviceNameTopicExpression") 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, expression_instead_none=True) elif self.__config.get("deviceTypeTopicExpression") is not None: search_result = search(self.__config["deviceTypeTopicExpression"], config) if search_result is not None: dict_result["deviceType"] = search_result.group(0) else: log.debug("Regular expression result is None. deviceTypeTopicExpression will be interpreted as a deviceType\n Topic: %s\nRegex: %s", config, self.__config.get("deviceTypeTopicExpression")) dict_result["deviceType"] = self.__config.get("deviceTypeTopicExpression") else: log.error("The expression for looking \"deviceType\" not found in config %s", dumps(self.__config)) 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 datatypes: dict_result[datatypes[datatype]] = [] for datatype_config in self.__config.get(datatype, []): value = TBUtility.get_value(datatype_config["value"], data, datatype_config["type"], expression_instead_none=True) value_tag = TBUtility.get_value(datatype_config["value"], data, datatype_config["type"], get_tag=True) key = TBUtility.get_value(datatype_config["key"], data, datatype_config["type"], expression_instead_none=True) key_tag = TBUtility.get_value(datatype_config["key"], data, get_tag=True) if ("${" not in str(value) and "}" not in str(value)) \ and ("${" not in str(key) and "}" not in str(key)): is_valid_key = isinstance(key, str) and "${" in datatype_config["key"] and "}" in datatype_config["key"] is_valid_value = isinstance(value, str) and "${" in datatype_config["value"] and "}" in datatype_config["value"] full_key = datatype_config["key"].replace('${' + key_tag + '}', key) if is_valid_key else key_tag full_value = datatype_config["value"].replace('${' + value_tag + '}', value) if is_valid_value else value if datatype == 'timeseries' and (data.get("ts") is not None or data.get("timestamp") is not None): dict_result[datatypes[datatype]].append({"ts": data.get('ts', data.get('timestamp', int(time()))), 'values': {full_key: full_value}}) else: dict_result[datatypes[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
def convert(self, topic, body): try: self.dict_result["deviceName"] = topic.split("/")[-1] # getting all data after last '/' symbol in this case: if topic = 'devices/temperature/sensor1' device name will be 'sensor1'. self.dict_result["deviceType"] = "Accelerometer" # just hardcode this self.dict_result["telemetry"] = [] # template for telemetry array bytes_to_read = body.replace("0x", "") # Replacing the 0x (if '0x' in body), needs for converting to bytearray converted_bytes = bytearray.fromhex(bytes_to_read) # Converting incoming data to bytearray output_dict = {} output_dict["ts"] = 0 output_dict["values"] = {} data_order = ["ts", "x001", "x002"] if self.__config.get("extension-config") is not None: remainder="" if 'tsBytes' in config["extension-config"]: chunk = bytes_to_read[:config["extension-config"]["tsBytes"]*2] remainder = bytes_to_read[config["extension-config"]["tsBytes"]*2:] ts_value = str(struct.unpack("l", bytearray.fromhex(chunk))[0]) ts_value = int(ts_value[:13]) output_dict["ts"] = ts_value else: pass #TODO: Throw exception on missing tsBytes array_format = "d"*(len(config["extension-config"])-1) #array_format = "" #for _ in range(len(config["extension-config"])-1): #iterate over number of objects excluding timestamp # array_format += "d" data_array = struct.unpack(array_format, bytearray.fromhex(remainder)) for telemetry_key in self.__config["extension-config"]: # Processing every telemetry key in config for extension struct_key = telemetry_key.replace("Bytes", "") if struct_key == "ts": pass else: output_dict["values"][struct_key] = data_array[data_order.index(struct_key)-1] self.dict_result["telemetry"].append(output_dict) # adding data to telemetry array else: self.dict_result["telemetry"] = {"data": int(body, 0)} # if no specific configuration in config file - just send data which received return self.dict_result except Exception as e: log.exception('Error in converter, for config: \n%s\n and message: \n%s\n', dumps(self.__config), body) log.exception(e)
def convert(self, topic, body): try: # Example hab topic is /Habs/Hab1/temperature self.dict_result["deviceName"] = topic.split( "/" )[1] # getting all data after last '/' symbol in this case: if topic = 'devices/temperature/sensor1' device name will be 'sensor1'. self.dict_result[ "deviceType"] = "Hab_controller" # just hardcode this self.dict_result["telemetry"] = [] # template for telemetry array data_type = "" if len(topic.split("/")) == 3: data_type = topic.split("/")[-1] else: data_type = topic.split("/")[-2] + '-' + topic.split("/")[-1] # bytes_to_read = body.replace("0x", "") # Replacing the 0x (if '0x' in body), needs for converting to bytearray # converted_bytes = bytearray.fromhex(bytes_to_read) # Converting incoming data to bytearray if self.__config.get("extension-config") is not None: # for telemetry_key in self.__config["extension-config"]: # Processing every telemetry key in config for extension # value = 0 # for _ in range(self.__config["extension-config"][telemetry_key]): # reading every value with value length from config # value = value*256 + converted_bytes.pop(0) # process and remove byte from processing # telemetry_to_send = {telemetry_key.replace("Bytes", ""): value} # creating telemetry data for sending into Thingsboard # self.dict_result["telemetry"].append(telemetry_to_send) # adding data to telemetry array self.dict_result["telemetry"] = {data_type: int(body, 0)} else: self.dict_result["telemetry"] = [ { data_type: body } ] # if no specific configuration in config file - just send data which received return self.dict_result except Exception as e: log.exception( 'Error in converter, for config: \n%s\n and message: \n%s\n', dumps(self.__config), body) log.exception(e) # print(len('Habs/Hab3/Temperature'.split("/")))
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) elif self.__config.get("deviceNameTopicExpression") is not None: search_result = search(self.__config["deviceNameTopicExpression"], config) if search_result is not None: dict_result["deviceName"] = search_result.group(0) else: log.debug("Regular expression result is None. deviceNameTopicExpression parameter will be interpreted as a deviceName\n Topic: %s\nRegex: %s", config, self.__config.get("deviceNameTopicExpression")) dict_result["deviceName"] = self.__config.get("deviceNameTopicExpression") 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) elif self.__config.get("deviceTypeTopicExpression") is not None: search_result = search(self.__config["deviceTypeTopicExpression"], config) if search_result is not None: dict_result["deviceType"] = search_result.group(0) else: log.debug("Regular expression result is None. deviceTypeTopicExpression will be interpreted as a deviceType\n Topic: %s\nRegex: %s", config, self.__config.get("deviceTypeTopicExpression")) dict_result["deviceType"] = self.__config.get("deviceTypeTopicExpression") else: log.error("The expression for looking \"deviceType\" not found in config %s", dumps(self.__config)) dict_result["attributes"] = [] dict_result["telemetry"] = [] 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: if self.__config.get("attributes"): for attribute in self.__config.get("attributes"): attribute_value = TBUtility.get_value(attribute["value"], data, attribute["type"]) 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) 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: if self.__config.get("timeseries"): for ts in self.__config.get("timeseries"): ts_value = TBUtility.get_value(ts["value"], data, ts["type"]) 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: 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, topic, body): try: ''' getting device name from topic, next line will get all data after last '/' symbol in this case: if topic = 'devices/temperature/sensor1' device name will be 'sensor1'.''' self.dict_result["deviceName"] = topic.split("/")[-1] self.dict_result["deviceType"] = "Thermostat" # just hardcode this self.dict_result["telemetry"] = [] # template for telemetry array bytes_to_read = body.replace("0x", "") # Replacing the 0x (if '0x' in body), needs for converting to bytearray converted_bytes = bytearray.fromhex(bytes_to_read) # Converting incoming data to bytearray if self.__config.get("extension-config") is not None: for telemetry_key in self.__config["extension-config"]: # Processing every telemetry key in config for extension value = 0 for current_byte_position in range(self.__config["extension-config"][telemetry_key]): # reading every value with value length from config value = value*256 + converted_bytes.pop(0) # process and remove byte from processing telemetry_to_send = {telemetry_key.replace("Bytes", ""): value} # creating telemetry data for sending into Thingsboard self.dict_result["telemetry"].append(telemetry_to_send) # adding data to telemetry array else: self.dict_result["telemetry"] = {"data": int(body, 0)} # if no specific configuration in config file - just send data which received return self.dict_result except Exception as e: log.exception('Error in converter, for config: \n%s\n and message: \n%s\n', dumps(self.__config), body) log.error(e)
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) elif self.__config.get("deviceNameTopicExpression") is not None: dict_result["deviceName"] = search( self.__config["deviceNameTopicExpression"], config) 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) elif self.__config.get("deviceTypeTopicExpression") is not None: dict_result["deviceType"] = search( self.__config["deviceTypeTopicExpression"], config) else: log.error( "The expression for looking \"deviceType\" not found in config %s", dumps(self.__config)) dict_result["attributes"] = [] dict_result["telemetry"] = [] 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: if self.__config.get("attributes"): for attribute in self.__config.get("attributes"): dict_result["attributes"].append({ attribute["key"]: TBUtility.get_value(attribute["value"], data, attribute["type"]) }) 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: if self.__config.get("timeseries"): for ts in self.__config.get("timeseries"): dict_result["telemetry"].append({ ts["key"]: TBUtility.get_value(ts["value"], data, ts["type"]) }) 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