def runPipeline(self): self.activateTiles() self.statemat = self.floormat.get_floormat_states(key=1) self.heatmap = createHeatMap(self.statemat, self.weightMap, self.dims[1], self.dims[0]) while not self.isConnected: if not self.isConnecting: self.isConnecting = True self.connectDevice(self.scanDevices(), FLOORMAT_MAC, NOTIFY_UUID) time.sleep(1) if self.isConnected: break while not thread_stop_event.isSet(): try: if self.peripheral.peripheral.waitForNotifications(3.0): print("========================= ACQUIRING DATA ========================") self.updateFloormat() socketio.sleep(self.delay) print("=================================================================") else: print("Nothing new has arrived") pass except Exception as e: self.peripheral.disconnect() self.peripheral = blePeripheral(FLOORMAT_MAC, MTU_SIZE) self.peripheral.peripheral.setDelegate(PeripheralDelegate(self.peripheral)) self.peripheral.enableNotify(uuid=NOTIFY_UUID) self.floormat.set_isTared(False) self.floormat.set_completeTared(False) print("Peripheral successfully re-connected!")
def connectDevice(self, devices, mac=None, notifyUUID=None, counter=0): print("Finding device {}, counter at: {}".format(mac, counter)) if not devices: return False elif counter >= 3: print("isConnecting: {}, isConnected: {}".format(self.isConnecting, self.isConnected)) print("Max connection counter reached. Will restart after 5 seconds") time.sleep(5) self.connectDevice(self.scanDevices(), mac, notifyUUID, 0) for device in devices: if device.addr == mac and mac and not self.isConnected and not self.isConnecting and counter < 3: try: self.peripheral = blePeripheral(device.addr, MTU_SIZE) self.peripheral.peripheral.setDelegate(PeripheralDelegate(self.peripheral)) self.isConnecting = True self.queue.put(POST) self.queue.put(EMIT) self.queue.join() if notifyUUID: try: self.peripheral.enableNotify(uuid=notifyUUID) except Exception as e: raise(e) self.queue.put(SERVICE) self.queue.put(CHARACTERISTIC) self.queue.join() self.isConnected = True print("Connected successfully to {}".format(mac)) except Exception as e: raise(e) #if isinstance(e, bluepy.btle.BTLEException): # print("Device cannot be connected to. It may be connected currently.") # time.sleep(5) # self.connectDevice(self.scanDevices(), mac, notifyUUID, counter+1) #else: # raise(e) if counter < 3 and not self.isConnected: self.isConnecting = False self.connectDevice(self.scanDevices(), mac, notifyUUID, counter+1) return counter < 3
def run(self): while True: key = self.queue.get() try: if key == SERVICE: self.master.services = self.master.peripheral.acquireService() elif key == CHARACTERISTIC: self.master.characteristics = self.master.peripheral.getCharacteristics() elif key == ACQUIRE: self.master.floormat.update_tile_state(self.master.tiles) self.master.peripheral.setDateTime(datetime.datetime.now()) self.master.statemat = self.master.floormat.get_floormat_states(key=1) self.master.heatmap = createHeatMap(self.master.statemat, self.master.weightMap, self.master.dims[0], self.master.dims[1]) elif key == EMIT: self.master.emitHeatmap() elif key == POST: self.master.postHeatmap() elif key == WRITE: self.master.writeToCSV() elif key == LOG: print('statemat: {}'.format(self.master.statemat)) print('heatmap: {}'.format(self.master.heatmap)) print('datetime: {}'.format(self.master.peripheral.getDateTime())) print('isTared: {}'.format(self.master.floormat.tareStatus()[0])) print('completeTared: {}'.format(self.master.floormat.tareStatus()[1])) print('writeFlag: {}'.format(self.master.writeFlag)) except Exception as e: self.master.peripheral.disconnect() self.master.peripheral = blePeripheral(FLOORMAT_MAC, MTU_SIZE) self.master.peripheral.peripheral.setDelegate(PeripheralDelegate(self.master.peripheral)) self.master.peripheral.enableNotify(uuid=NOTIFY_UUID) self.master.floormat.set_isTared(False) self.master.floormat.set_completeTared(False) print("Peripheral successfully re-connected!") except KeyboardInterrupt: self.master.peripheral.disconnect() sys.exit(1) finally: self.queue.task_done()
def demo_bleScan(): weightMap = demo_acquireSensorClassMapping() bleScanner = Scanner().withDelegate(ScanDelegate()) devices = bleScanner.scan() for dev in devices: print(dev.addr, dev.getScanData()) if dev.addr == '8c:aa:b5:86:4a:2a': peripheral = blePeripheral(dev.addr) services = peripheral.acquireService() print("services: {}".format(services)) characteristics = peripheral.getCharacteristics() print("characteristics: {}".format(characteristics)) print() print("Peripheral: {}".format(peripheral.getAddress())) print("=================================") for service in services: print("UUID: {}".format(service.uuid)) print("UUID: {}".format(service.uuid.getCommonName())) print("UUID: {}".format(service.uuid.binVal)) c = peripheral.getCharacteristics(uuid=service.uuid) print("c: {}".format(c)) print("=================================") for uuid, chars in characteristics.items(): print("UUID: {}, {}".format(uuid.getCommonName(), uuid.binVal)) for char in chars: print("Characteristic: {}, {}, {}".format( char.uuid, char.propertiesToString(), char.getHandle())) print() peripheral.enableNotify( uuid="e514ae34-a8c5-11ea-bb37-0242ac130002") while True: if peripheral.peripheral.waitForNotifications(3.0): print("{}: {}".format(peripheral.getDateTime(), peripheral.getData())) else: print("nada nada yada haha") print("Waiting") break
def tareCal(flag): if (flag == 1 or flag == 2) and not thread.floormat.tareStatus()[0]: try: thread.floormat.set_isTared(True) thread.peripheral.writeData(uuid="809a3309-2e5c-4d68-acef-196222cf9886", data=bytes(str(flag), encoding='utf-8')) thread.floormat.set_completeTared(True) except Exception as e: thread.peripheral.disconnect() thread.peripheral = blePeripheral(FLOORMAT_MAC, MTU_SIZE) thread.peripheral.peripheral.setDelegate(PeripheralDelegate(thread.peripheral)) thread.peripheral.enableNotify(uuid=NOTIFY_UUID) thread.floormat.set_isTared(False) thread.floormat.set_completeTared(False) print("Peripheral successfully re-connected!") else: socketio.emit('midtarecal')