コード例 #1
0
    def convert(self, _, body):
        try:
            data = body["data"]["value"]
            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)
            if self.__config.get("extension-config") is not None:
                for telemetry_key in self.__config["extension-config"]:
                    value = None
                    byteorder = telemetry_key.get("byteorder", "big").lower()
                    signed = telemetry_key.get("signed", True)
                    if telemetry_key.get("byteAddress") is None:
                        interest_bytes = converted_bytes[
                            telemetry_key["fromByte"]:telemetry_key["toByte"]]
                        if telemetry_key["type"] == "float":
                            value = struct.unpack(
                                ">f" if byteorder == "big" else "<f",
                                interest_bytes)
                            value = value[0] if isinstance(value,
                                                           list) else None
                        if telemetry_key["type"] == "int":
                            value = int.from_bytes(interest_bytes,
                                                   byteorder=byteorder,
                                                   signed=signed)
                    else:
                        interest_byte = converted_bytes[
                            telemetry_key["byteAddress"]]
                        bits = "{0:{fill}8b}".format(interest_byte, fill='0')
                        bits = bits if byteorder == "big" else bits[::-1]
                        value = int(
                            bits[::-1][telemetry_key.get("fromBit"):
                                       telemetry_key.get("toBit")][::-1], 2)
                    if value is not None:
                        value = value * telemetry_key.get("multiplier", 1)
                        telemetry_to_send = {
                            telemetry_key["key"]: value
                        }  # creating telemetry data for sending into Thingsboard
                        # current_byte_position += self.__config["extension-config"][telemetry_key]
                        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.error(
                'Error in converter, for config: \n%s\n and message: \n%s\n',
                dumps(self.__config), body)
            log.exception(e)
コード例 #2
0
    def convert(self, config, data):
        if isinstance(data, (bytes, str)):
            data = loads(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, expression_instead_none=True)
            else:
                log.error("The expression \"%s\" 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)
            else:
                log.error("The expression for looking \"deviceType\" not found in config %s", dumps(self.__config))
        except Exception as e:
            log.exception(e)


        try:
            for datatype in self.__datatypes:
                current_datatype = self.__datatypes[datatype]
                for datatype_object_config in self.__config["converter"].get(datatype, []):
                    datatype_object_config_key = TBUtility.get_value(datatype_object_config["key"], data, datatype_object_config["type"], expression_instead_none=True)
                    datatype_object_config_value = TBUtility.get_value(datatype_object_config["value"], data, datatype_object_config["type"])
                    if datatype_object_config_key is not None and datatype_object_config_value is not None:
                        dict_result[current_datatype].append({datatype_object_config_key: datatype_object_config_value})
                    else:
                        error_string = "Cannot find the key in the input data" if datatype_object_config_key is None else "Cannot find the value from the input data"
                        log.error(error_string)
        except Exception as e:
            log.exception(e)

        return dict_result
コード例 #3
0
    def convert(self, config, data):
        if isinstance(data, (bytes, str)):
            data = loads(data)

        dict_result = {
            "deviceName": None,
            "deviceType": None,
            "attributes": [],
            "telemetry": []
        }

        try:
            if self.__config['converter'].get(
                    "deviceNameJsonExpression") is not None:
                device_name_tags = TBUtility.get_values(
                    self.__config['converter'].get("deviceNameJsonExpression"),
                    data,
                    get_tag=True)
                device_name_values = TBUtility.get_values(
                    self.__config['converter'].get("deviceNameJsonExpression"),
                    data,
                    get_tag=True)
                dict_result["deviceName"] = self.__config['converter'].get(
                    "deviceNameJsonExpression")
                for (device_name_tag,
                     device_name_value) in zip(device_name_tags,
                                               device_name_values):
                    is_valid_key = "${" in self.__config['converter'].get("deviceNameJsonExpression") and "}" in \
                                   self.__config['converter'].get("deviceNameJsonExpression")
                    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['converter']))
            if self.__config['converter'].get(
                    "deviceTypeJsonExpression") is not None:
                device_type_tags = TBUtility.get_values(
                    self.__config['converter'].get("deviceTypeJsonExpression"),
                    data,
                    get_tag=True)
                device_type_values = TBUtility.get_values(
                    self.__config['converter'].get("deviceTypeJsonExpression"),
                    data,
                    expression_instead_none=True)
                dict_result["deviceType"] = self.__config['converter'].get(
                    "deviceTypeJsonExpression")
                for (device_type_tag,
                     device_type_value) in zip(device_type_tags,
                                               device_type_values):
                    is_valid_key = "${" in self.__config['converter'].get("deviceTypeJsonExpression") and "}" in \
                                   self.__config['converter'].get("deviceTypeJsonExpression")
                    dict_result["deviceType"] = dict_result["deviceType"].replace('${' + str(device_type_tag) + '}',
                                                                                  str(device_type_value)) \
                        if is_valid_key else device_type_tag
            else:
                log.error(
                    "The expression for looking \"deviceType\" not found in config %s",
                    dumps(self.__config['converter']))
        except Exception as e:
            log.exception(e)

        try:
            for datatype in self.__datatypes:
                dict_result[self.__datatypes[datatype]] = []
                for datatype_object_config in self.__config["converter"].get(
                        datatype, []):
                    values = TBUtility.get_values(
                        datatype_object_config["value"],
                        data,
                        datatype_object_config["type"],
                        expression_instead_none=True)
                    values_tags = TBUtility.get_values(
                        datatype_object_config["value"],
                        data,
                        datatype_object_config["type"],
                        get_tag=True)

                    keys = TBUtility.get_values(datatype_object_config["key"],
                                                data,
                                                datatype_object_config["type"],
                                                expression_instead_none=True)
                    keys_tags = TBUtility.get_values(
                        datatype_object_config["key"], data, get_tag=True)

                    full_key = datatype_object_config["key"]
                    for (key, key_tag) in zip(keys, keys_tags):
                        is_valid_key = "${" in datatype_object_config["key"] and "}" in \
                                       datatype_object_config["key"]
                        full_key = full_key.replace(
                            '${' + str(key_tag) +
                            '}', str(key)) if is_valid_key else key_tag

                    full_value = datatype_object_config["value"]
                    for (value, value_tag) in zip(values, values_tags):
                        is_valid_value = "${" in datatype_object_config["value"] and "}" in \
                                         datatype_object_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.__datatypes[datatype]].append({
                            "ts":
                            data.get('ts', data.get('timestamp', int(time()))),
                            'values': {
                                full_key: full_value
                            }
                        })
                    else:
                        dict_result[self.__datatypes[datatype]].append(
                            {full_key: full_value})

        except Exception as e:
            log.exception(e)

        return dict_result
コード例 #4
0
 def convert(self, config, data):
     if type(data) in (bytes, str):
         data = loads(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,
                 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,
                 expression_instead_none=True)
         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.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"])
                 if attribute_value is not None:
                     dict_result["attributes"].append(
                         {attribute["key"]: attribute_value})
                 else:
                     log.debug(
                         "%s key not found in response: %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("telemetry"):
             for ts in self.__config.get("telemetry"):
                 ts_value = TBUtility.get_value(ts["value"], data,
                                                ts["type"])
                 if ts_value is not None:
                     dict_result["telemetry"].append({ts["key"]: ts_value})
                 else:
                     log.debug(
                         "%s key not found in response: %s",
                         ts["value"].replace("${", '"').replace("}",
                                                                '"'), data)
     except Exception as e:
         log.exception(e)
     return dict_result