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)
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)