def test_get_data_format_5_valid_data(self): test_cases = [ '1F0201061BFF990405138A5F92C4F3FFE4FFDC0414C4F6EC29BBE62EB92E73E5BC', '1F0201061BFF990405138A5F61C4F0FFE4FFDC0414C5B6EC29B3E62EB92E73E5BC', ] for x in test_cases: encoded = DataFormats._get_data_format_5(x) self.assertIsNotNone(encoded)
def test_get_data_format_3_valid_data(self): test_cases = [ '1502010611FF990403651652CAE900080018041C0C8BC6', '1502010611FF990403411540C84AFC72FE2FFFC50B89C6', ] for x in test_cases: encoded = DataFormats._get_data_format_3(x) self.assertIsNotNone(encoded)
def _get_ruuvitag_datas(macs=[], search_duratio_sec=None, run_flag=RunFlag(), bt_device=''): """ Get data from BluetoothCommunication and handle data encoding. Args: macs (list): MAC addresses. Default empty list search_duratio_sec (int): Search duration in seconds. Default None run_flag (object): RunFlag object. Function executes while run_flag.running. Default new RunFlag bt_device (string): Bluetooth device id Yields: tuple: MAC and State of RuuviTag sensor data """ mac_blacklist = Manager().list() start_time = time.time() data_iter = ble.get_datas(mac_blacklist, bt_device) for ble_data in data_iter: # Check duration if search_duratio_sec and time.time( ) - start_time > search_duratio_sec: data_iter.send(StopIteration) break # Check running flag if not run_flag.running: data_iter.send(StopIteration) break # Check MAC whitelist if advertised MAC available if ble_data[0] and macs and not ble_data[0].upper() in map( str.upper, macs): continue (data_format, data) = DataFormats.convert_data(ble_data[1]) # Check that encoded data is valid RuuviTag data and it is sensor data # If data is not valid RuuviTag data add MAC to blacklist if MAC is available if data is not None: decoded = get_decoder(data_format).decode_data(data) if decoded is not None: # If advertised MAC is missing, try to parse it from the payload mac = ble_data[0] if ble_data[0] else \ parse_mac(data_format, decoded['mac']) if decoded['mac'] else None # Check whitelist using MAC from decoded data if advertised MAC is not available if mac and macs and mac.upper() not in map( str.upper, macs): continue yield (mac, decoded) else: log.error('Decoded data is null. MAC: %s - Raw: %s', ble_data[0], ble_data[1]) else: if ble_data[0]: mac_blacklist.append(ble_data[0])
def test_convert_data_valid_data(self): test_cases = [('1502010611FF990403651652CAE900080018041C0C8BC6', 3, '03651652CAE900080018041C0C8B'), ('1502010611FF990403411540C84AFC72FE2FFFC50B89C6', 3, '03411540C84AFC72FE2FFFC50B89')] for x, data_format, result in test_cases: encoded = DataFormats.convert_data(x) print(encoded[1]) self.assertEqual(data_format, encoded[0]) self.assertEqual(result, encoded[1])
def test_get_data_format_2and4_valid_data(self): test_cases = [ '1F0201060303AAFE1716AAFE10F6037275752E76692F234248415A414D576F77C9', '1F0201060303AAFE1716AAFE10F6037275752E76692F234248415A414D576F77C8', '1E0201060303AAFE1616AAFE10EE037275752E76692F23416E4159414D5645CC', '1E0201060303AAFE1616AAFE10EE037275752E76692F23416E4159414D5645C9' ] for x in test_cases: encoded = DataFormats._get_data_format_2and4(x) self.assertIsNotNone(encoded)
def get_data(mac, bt_device=''): """ Get raw data for selected RuuviTag Args: mac (string): MAC address bt_device (string): Bluetooth device id Returns: tuple (int, string): Data Format type and raw Sensor data """ raw = ble.get_data(mac, bt_device) return DataFormats.convert_data(raw)
def _get_ruuvitag_datas(macs=[], search_duratio_sec=None, run_flag=RunFlag(), bt_device=''): """ Get data from BluetoothCommunication and handle data encoding. Args: macs (list): MAC addresses. Default empty list search_duratio_sec (int): Search duration in seconds. Default None run_flag (object): RunFlag object. Function executes while run_flag.running. Default new RunFlag bt_device (string): Bluetooth device id Yields: tuple: MAC and State of RuuviTag sensor data """ mac_blacklist = [] start_time = time.time() data_iter = ble.get_datas(mac_blacklist, bt_device) for ble_data in data_iter: # Check duration if search_duratio_sec and time.time( ) - start_time > search_duratio_sec: data_iter.send(StopIteration) break # Check running flag if not run_flag.running: data_iter.send(StopIteration) break # Check MAC whitelist if macs and not ble_data[0] in macs: continue (data_format, data) = DataFormats.convert_data(ble_data[1]) # Check that encoded data is valid RuuviTag data and it is sensor data # If data is not valid RuuviTag data add MAC to blacklist if data is not None: state = get_decoder(data_format).decode_data(data) if state is not None: yield (ble_data[0], state) else: log.error('Decoded data is null. MAC: %s - Raw: %s', ble_data[0], ble_data[1]) else: mac_blacklist.append(ble_data[0])
def test_convert_data_not_valid(self): encoded = DataFormats.convert_data('not_valid') self.assertIsNone(encoded[0]) self.assertIsNone(encoded[1])
def test_convert_data_not_valid_binary(self): data = b'\x99\x04\x03P\x15]\xceh\xfd\x88\x03\x05\x00\x1b\x0c\x13\x00\x00\x00\x00' encoded = DataFormats.convert_data(data) self.assertIsNone(encoded[0]) self.assertIsNone(encoded[1])
except Exception as err: f = open("/home/pi/Desktop/beacon_scan/activeRpiStatusException.txt", "a") f.write("------ Network issue " + str(datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")) + "-----\n") f.close() objConnectMqtt = connectMqtt() objConnectMqtt.start() client.loop_start() for ble_data in ble.get_datas(): try: if (len(ble_data[1]) == 66): (data_format, encoded) = DataFormats.convert_data(ble_data[1]) if not (data_format == None or encoded == None): sensor_data = get_decoder(data_format).decode_data(encoded) sensor_data["rssi"] = int(ble_data[1][-2:], 16) - 256 sensor_data["battery"] = round( giveBatteryPercentage(sensor_data["battery"]), 2) sensor_data["timestamp"] = int( datetime.datetime.now().timestamp() * 1000) publish_array.append(sensor_data) generateLowCriticalBatteryAlert(sensor_data) generareAccelerometerAlert(sensor_data) except Exception as err: f = open("/home/pi/Desktop/beacon_scan/ruuviScannerExceptionLogs.txt",