def testPositionNotifications(self): NotificationCenter().addObserver( self, method=self.handleDid, notificationName=LinearMotionNotification.didGetPosition) (x, y, z) = self.device.position() self.assertTrue(self.didNotificationReceived) NotificationCenter().removeObserver(self)
def removeDevice(self, device): with self.lock: NotificationCenter().postNotification( DeviceManagerNotification.willRemoveDevice, notifyingObject=self, userInfo=device) self.devices.remove(device) NotificationCenter().postNotification( DeviceManagerNotification.didRemoveDevice, notifyingObject=self, userInfo=device)
def addDevice(self, device): with self.lock: NotificationCenter().postNotification( DeviceManagerNotification.willAddDevice, notifyingObject=self, userInfo=device) self.devices.add(device) NotificationCenter().postNotification( DeviceManagerNotification.didAddDevice, notifyingObject=self, userInfo=device)
def stop(self): if self.isCapturing: NotificationCenter().postNotification( CameraDeviceNotification.willStopCapture, notifyingObject=self) with self.lock: self.quitLoop = True self.mainLoop.join() self.mainLoop = None NotificationCenter().postNotification( CameraDeviceNotification.didStopCapture, notifyingObject=self) else: raise RuntimeError("No monitoring loop running")
def setUp(self): # DeviceManager().devices = [] DeviceManager() del DeviceManager._instance DeviceManager._instance = None NotificationCenter() del NotificationCenter._instance NotificationCenter._instance = None self.lock = RLock() self.notificationsToReceive = [] self.assertEqual(NotificationCenter().observersCount(), 0)
def captureLoopThread(self): frame = None NotificationCenter().postNotification( notificationName=CameraDeviceNotification.didStartCapture, notifyingObject=self) while (True): frame = self.doCaptureFrame() NotificationCenter().postNotification( CameraDeviceNotification.imageCaptured, self, frame) if self.quitLoop: return
def testAddObserverWrongSender(self): someObject = NotificationCenter() nc = NotificationCenter() nc.addObserver(self, method=self.handle, notificationName=TestNotificationName.test, observedObject=someObject) nc.postNotification(notificationName=TestNotificationName.test, notifyingObject=self, userInfo="1234") self.assertFalse(self.notificationReceived) self.assertNotEqual(self.postedUserInfo, "1234") nc.removeObserver(self) self.assertEqual(nc.observersCount(), 0)
def testRemoveIncorrectObject(self): nc = NotificationCenter() someObject = NotificationCenter() nc.addObserver(self, self.handle, TestNotificationName.test, someObject) nc.removeObserver(someObject) self.assertEqual(nc.observersCount(), 1)
def testAddObserverRemoveObserver(self): nc = NotificationCenter() nc.addObserver(observer=self, method=self.handle, notificationName=TestNotificationName.test) self.assertEqual(nc.observersCount(), 1) nc.removeObserver(observer=self) self.assertEqual(nc.observersCount(), 0)
def measureAbsolutePower(self): self.doGetAbsolutePower() power = self.absolutePower NotificationCenter().postNotification( PowerMeterNotification.didMeasure, notifyingObject=self, userInfo=power) return power
def testAddObserverAnySenderAndPostithObject(self): nc = NotificationCenter() nc.addObserver(observer=self, method=self.handle, notificationName=TestNotificationName.test) nc.postNotification(notificationName=TestNotificationName.test, notifyingObject=self) self.assertTrue(self.notificationReceived) nc.removeObserver(self)
def monitoringLoop(self, duration=1e7): startTime = time.time() endTime = startTime + duration NotificationCenter().postNotification( DeviceManagerNotification.didStartMonitoring, notifyingObject=self) while time.time() < endTime: currentDevices = self.updateConnectedDevices() NotificationCenter().postNotification( DeviceManagerNotification.status, notifyingObject=self, userInfo=currentDevices) with self.lock: if self.quitMonitoring: break time.sleep(0.3) NotificationCenter().postNotification( DeviceManagerNotification.didStopMonitoring, notifyingObject=self)
def testAddObserverWrongNotification(self): nc = NotificationCenter() nc.addObserver(observer=self, method=self.handle, notificationName=TestNotificationName.wrong) nc.postNotification(notificationName=TestNotificationName.test, notifyingObject=self, userInfo="1234") self.assertFalse(self.notificationReceived) self.assertNotEqual(self.postedUserInfo, "1234") nc.removeObserver(self)
def testAddObserverAnySenderAndPostWithUserInfo(self): nc = NotificationCenter() nc.addObserver(observer=self, method=self.handle, notificationName=TestNotificationName.test) nc.postNotification(notificationName=TestNotificationName.test, notifyingObject=self, userInfo="1234") self.assertTrue(self.notificationReceived) self.assertEqual(self.postedUserInfo, "1234") nc.removeObserver(self)
def setUp(self): super().setUp() DeviceManager().updateConnectedDevices() self.device = None self.isRunning = False self.notificationReceived = None DeviceManager().destroy() NotificationCenter().destroy()
def testDeviceMoveByNotifications(self): NotificationCenter().addObserver( self, method=self.handleWill, notificationName=LinearMotionNotification.willMove) NotificationCenter().addObserver( self, method=self.handleDid, notificationName=LinearMotionNotification.didMove) self.assertFalse(self.willNotificationReceived) self.assertFalse(self.didNotificationReceived) self.device.moveBy((-1000, -2000, -3000)) self.assertTrue(self.willNotificationReceived) self.assertTrue(self.didNotificationReceived) NotificationCenter().removeObserver(self)
def testDeviceMoveNotifications(self): NotificationCenter().addObserver( self, method=self.handleWill, notificationName=LinearMotionNotification.willMove) NotificationCenter().addObserver( self, method=self.handleDid, notificationName=LinearMotionNotification.didMove) self.assertFalse(self.willNotificationReceived) self.assertFalse(self.didNotificationReceived) destination = (4000, 5000, 6000) self.device.moveTo(destination) self.assertTrue(self.willNotificationReceived) self.assertTrue(self.didNotificationReceived) NotificationCenter().removeObserver(self)
def captureLoopSynchronous(self): frame = None NotificationCenter().postNotification( notificationName=CameraDeviceNotification.didStartCapture, notifyingObject=self) while (True): frame = self.doCaptureFrame() NotificationCenter().postNotification( CameraDeviceNotification.imageCaptured, self, frame) cv2.imshow('Preview (Q to quit)', frame) if cv2.waitKey(1) & 0xFF == ord('q'): NotificationCenter().postNotification( CameraDeviceNotification.willStopCapture, notifyingObject=self) break cv2.destroyAllWindows() NotificationCenter().postNotification( CameraDeviceNotification.didStopCapture, notifyingObject=self)
def stopMonitoring(self): if self.isMonitoring: NotificationCenter().postNotification( DeviceManagerNotification.willStopMonitoring, notifyingObject=self) with self.lock: self.quitMonitoring = True self.monitoring.join() self.removeAllDevices() self.monitoring = None else: raise RuntimeError("No monitoring loop running")
def start(self): with self.lock: if not self.isCapturing: self.quitLoop = False self.mainLoop = Thread(target=self.captureLoop, name="Camera-CaptureLoop") NotificationCenter().postNotification( notificationName=CameraDeviceNotification.willStartCapture, notifyingObject=self) self.mainLoop.start() else: raise RuntimeError("Capture loop already running")
def startMonitoring(self): with self.lock: if not self.isMonitoring: self.quitMonitoring = False self.monitoring = Thread(target=self.monitoringLoop, name="DeviceManager-RunLoop") NotificationCenter().postNotification( notificationName=DeviceManagerNotification. willStartMonitoring, notifyingObject=self) self.monitoring.start() else: raise RuntimeError("Monitoring loop already running")
def testObserverInfo(self): nc = NotificationCenter() observer = ObserverInfo(observer=self, method=self.handle, notificationName=TestNotificationName.test, observedObject=nc) self.assertTrue(observer.matches(ObserverInfo(observer=self))) self.assertTrue( observer.matches( ObserverInfo(observer=self, notificationName=TestNotificationName.test))) self.assertTrue( observer.matches(ObserverInfo(observer=self, notificationName=None))) self.assertTrue( observer.matches( ObserverInfo(observer=self, notificationName=TestNotificationName.test, observedObject=nc))) self.assertTrue( observer.matches( ObserverInfo(observer=self, notificationName=None, observedObject=nc))) self.assertFalse(observer.matches(ObserverInfo(observer=nc))) self.assertFalse( observer.matches( ObserverInfo(observer=nc, notificationName=TestNotificationName.test, observedObject=nc))) self.assertFalse( observer.matches( ObserverInfo(observer=nc, notificationName=TestNotificationName.other, observedObject=nc))) self.assertFalse( observer.matches( ObserverInfo(observer=nc, notificationName=TestNotificationName.other, observedObject=None))) self.assertFalse( observer.matches( ObserverInfo(observer=self, notificationName=TestNotificationName.other, observedObject=None)))
def testPostNotificationDidInitialize(self): nc = NotificationCenter() nc.addObserver(observer=self, method=self.handle, notificationName=PhysicalDeviceNotification. didInitializeDevice, observedObject=self.device) self.assertIsNone(self.notificationReceived) self.device.initializeDevice() self.assertIsNotNone(self.notificationReceived) nc.removeObserver(self)
def usbDeviceDisconnected(self, usbDevice): descriptor = None for aDescriptor in self.usbDeviceDescriptors: if aDescriptor.usbDevice == usbDevice: descriptor = aDescriptor break if descriptor is None: print("Unable to find descriptor matching {0}".format(usbDevice)) NotificationCenter().postNotification( DeviceManagerNotification.usbDeviceDidDisconnect, notifyingObject=self, userInfo=descriptor) currentDevices = list(self.devices) for device in currentDevices: if descriptor.matchesPhysicalDevice(device): self.removeDevice(device)
def usbDeviceConnected(self, usbDevice): descriptor = USBDeviceDescriptor.fromUSBDevice(usbDevice) NotificationCenter().postNotification( DeviceManagerNotification.usbDeviceDidConnect, notifyingObject=self, userInfo=descriptor) if descriptor not in self.usbDeviceDescriptors: self.usbDeviceDescriptors.append(descriptor) candidates = PhysicalDevice.candidates(descriptor.idVendor, descriptor.idProduct) for candidateClass in candidates: # This may throw if incompatible deviceInstance = candidateClass( serialNumber=descriptor.serialNumber, idProduct=descriptor.idProduct, idVendor=descriptor.idVendor) deviceInstance.initializeDevice() deviceInstance.shutdownDevice() self.addDevice(deviceInstance)
def testAddObserver(self): nc = NotificationCenter() nc.addObserver(observer=self, method=self.handle, notificationName=TestNotificationName.test)
def home(self) -> (): NotificationCenter().postNotification(LinearMotionNotification.willMove, notifyingObject=self) self.doHome() NotificationCenter().postNotification(LinearMotionNotification.didMove, notifyingObject=self)
def position(self) -> (): position = self.doGetPosition() NotificationCenter().postNotification(LinearMotionNotification.didGetPosition, notifyingObject=self, userInfo=position) return position
def moveBy(self, displacement): NotificationCenter().postNotification(LinearMotionNotification.willMove, notifyingObject=self, userInfo=displacement) self.doMoveBy(displacement) NotificationCenter().postNotification(LinearMotionNotification.didMove, notifyingObject=self, userInfo=displacement)
def moveTo(self, position): NotificationCenter().postNotification(LinearMotionNotification.willMove, notifyingObject=self, userInfo=position) self.doMoveTo(position) NotificationCenter().postNotification(LinearMotionNotification.didMove, notifyingObject=self, userInfo=position)