Esempio n. 1
0
    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, 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(self, config, msg):
     try:
         basic_msg = FromServiceMessage()
         message_types = config.get("message_type")
         additional_message = config.get("additional_message")
         if not isinstance(message_types, list):
             message_types = [message_types]
         for message_type in message_types:
             self.__conversion_methods[message_type](basic_msg, msg,
                                                     additional_message)
         return basic_msg
     except Exception as e:
         log.exception("[GRPC] ", e)
         return None
Esempio n. 4
0
    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
Esempio n. 5
0
 def convert(self, config, data):
     result = {
         "deviceName": self.__config["deviceName"],
         "deviceType": self.__config["deviceType"],
         "attributes": [],
         "telemetry": []
     }
     try:
         if isinstance(data, dict):
             result[config[0]].append({
                 config[1]["key"]:
                 {str(k): str(v)
                  for k, v in data.items()}
             })
         elif isinstance(data, list):
             if isinstance(data[0], str):
                 result[config[0]].append(
                     {config[1]["key"]: ','.join(data)})
             elif isinstance(data[0], dict):
                 res = {}
                 for item in data:
                     res.update(**item)
                 result[config[0]].append({
                     config[1]["key"]:
                     {str(k): str(v)
                      for k, v in res.items()}
                 })
         elif isinstance(data, str):
             result[config[0]].append({config[1]["key"]: data})
         elif isinstance(data, bytes):
             result[config[0]].append(
                 {config[1]["key"]: data.decode("UTF-8")})
         else:
             result[config[0]].append({config[1]["key"]: data})
         log.debug(result)
     except Exception as e:
         log.exception(e)
     return result
Esempio n. 6
0
    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