Exemplo n.º 1
0
 def __new_device_processing(self, device):
     default_services_on_device = [service for service in self.__devices_around[device]['services'].keys() if int(service.split('-')[0], 16) in self.__default_services]
     log.debug('Default services found on device %s :%s', device, default_services_on_device)
     converter = BytesBLEUplinkConverter(self.__devices_around[device]['device_config'])
     converted_data = None
     for service in default_services_on_device:
         characteristics = [char for char in self.__devices_around[device]['services'][service].keys() if self.__devices_around[device]['services'][service][char]['characteristic'].supportsRead()]
         for char in characteristics:
             read_config = {'characteristicUUID': char,
                            'method': 'READ',
                            }
             try:
                 self.__check_and_reconnect(device)
                 data = self.__service_processing(device, read_config)
                 attribute = capitaliseName(UUID(char).getCommonName())
                 read_config['key'] = attribute
                 read_config['byteFrom'] = 0
                 read_config['byteTo'] = -1
                 converter_config = [{"type": "attributes",
                                      "clean": False,
                                      "section_config": read_config}]
                 for interest_information in converter_config:
                     try:
                         converted_data = converter.convert(interest_information, data)
                         log.debug(converted_data)
                     except Exception as e:
                         log.debug(e)
             except Exception as e:
                 log.debug('Cannot process %s', e)
                 continue
     if converted_data is not None:
         # self.__gateway.add_device(converted_data["deviceName"], {"connector": self})
         self.__gateway.send_to_storage(self.get_name(), converted_data)
Exemplo n.º 2
0
 def __fill_interest_devices(self):
     if self.__config.get('devices') is None:
         log.error('Devices not found in configuration file. BLE Connector stopped.')
         self._connected = False
         return None
     for interest_device in self.__config.get('devices'):
         keys_in_config = ['attributes', 'telemetry']
         if interest_device.get('MACAddress') is not None:
             default_converter = BytesBLEUplinkConverter(interest_device)
             interest_uuid = {}
             for key_type in keys_in_config:
                 for type_section in interest_device.get(key_type):
                     if type_section.get("characteristicUUID") is not None:
                         converter = None
                         if type_section.get('converter') is not None:
                             try:
                                 module = TBUtility.check_and_import(self.__connector_type,
                                                                     type_section['converter'])
                                 if module is not None:
                                     log.debug('Custom converter for device %s - found!',
                                               interest_device['MACAddress'])
                                     converter = module(interest_device)
                                 else:
                                     log.error(
                                         "\n\nCannot find extension module for device %s .\nPlease check your configuration.\n",
                                         interest_device['MACAddress'])
                             except Exception as e:
                                 log.exception(e)
                         else:
                             converter = default_converter
                         if converter is not None:
                             if interest_uuid.get(type_section["characteristicUUID"].upper()) is None:
                                 interest_uuid[type_section["characteristicUUID"].upper()] = [
                                     {'section_config': type_section,
                                      'type': key_type,
                                      'converter': converter}]
                             else:
                                 interest_uuid[type_section["characteristicUUID"].upper()].append(
                                     {'section_config': type_section,
                                      'type': key_type,
                                      'converter': converter})
                     else:
                         log.error("No characteristicUUID found in configuration section for %s:\n%s\n", key_type,
                                   pformat(type_section))
             if self.__devices_around.get(interest_device['MACAddress'].upper()) is None:
                 self.__devices_around[interest_device['MACAddress'].upper()] = {}
             self.__devices_around[interest_device['MACAddress'].upper()]['device_config'] = interest_device
             self.__devices_around[interest_device['MACAddress'].upper()]['interest_uuid'] = interest_uuid
         else:
             log.error("Device address not found, please check your settings.")
Exemplo n.º 3
0
    def test_ble_getting_values(self):
        test_ble_config = {
            "name":
            "Temperature and humidity sensor",
            "MACAddress":
            "4C:65:A8:DF:85:C0",
            "telemetry": [{
                "key": "temperature",
                "method": "notify",
                "characteristicUUID": "226CAA55-6476-4566-7562-66734470666D",
                "byteFrom": 2,
                "byteTo": 6
            }, {
                "key": "humidity",
                "method": "notify",
                "characteristicUUID": "226CAA55-6476-4566-7562-66734470666D",
                "byteFrom": 9,
                "byteTo": 13
            }],
            "attributes": [{
                "key": "name",
                "characteristicUUID": "00002A00-0000-1000-8000-00805F9B34FB",
                "method": "read",
                "byteFrom": 0,
                "byteTo": -1
            }]
        }
        test_data_list = [b'T=54.7 H=37.0', b'T=54.7 H=37.0', b'Some string']
        test_configs = [{
            "section_config": {
                "key": "temperature",
                "byteFrom": 2,
                "byteTo": 6
            },
            "type": "telemetry",
            "clean": False
        }, {
            "section_config": {
                "key": "humidity",
                "byteFrom": 9,
                "byteTo": 13
            },
            "type": "telemetry",
            "clean": False
        }, {
            "section_config": {
                "key": "name",
                "byteFrom": 0,
                "byteTo": -1
            },
            "type": "attributes",
            "clean": False
        }]
        test_result = {
            'deviceName': 'Temperature and humidity sensor',
            'deviceType': 'BLEDevice',
            'telemetry': [{
                'temperature': '54.7'
            }, {
                'humidity': '37.0'
            }],
            'attributes': [{
                'name': 'Some string'
            }]
        }

        result = {}

        converter = BytesBLEUplinkConverter(test_ble_config)
        for index, config in enumerate(test_configs):
            result = converter.convert(config, test_data_list[index])
        self.assertDictEqual(result, test_result)