def convert(self, config, data: bytes):
     keys = ['attributes', 'telemetry']
     for key in keys:
         self.result_dict[key] = []
         if self.__config.get(key) is not None:
             for config_object in self.__config.get(key):
                 data_to_convert = data
                 if config_object.get('untilDelimiter') is not None:
                     data_to_convert = data.split(
                         config_object.get('untilDelimiter').encode(
                             'UTF-8'))[0]
                 if config_object.get('fromDelimiter') is not None:
                     data_to_convert = data.split(
                         config_object.get('fromDelimiter').encode(
                             'UTF-8'))[1]
                 if config_object.get('toByte') is not None:
                     to_byte = config_object.get('toByte')
                     if to_byte == -1:
                         to_byte = len(data) - 1
                     data_to_convert = data_to_convert[:to_byte]
                 if config_object.get('fromByte') is not None:
                     from_byte = config_object.get('fromByte')
                     data_to_convert = data_to_convert[from_byte:]
                 converted_data = {
                     config_object['key']: data_to_convert.decode('UTF-8')
                 }
                 self.result_dict[key].append(converted_data)
     log.debug("Converted data: %s", self.result_dict)
     return self.result_dict
Beispiel #2
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
 def __convert_response_msg(self, msg: Response):
     log.debug("Converted response: %r", msg.response)