def testMatching1(self):
        dm = DeviceManager()
        device = DebugLinearMotionDevice()
        dm.addDevice(device)

        matched = dm.matchPhysicalDevicesOfType(DebugLinearMotionDevice)
        self.assertTrue(len(matched) == 1)
        self.assertTrue(matched[0] == device)
    def testMatching2(self):
        dm = DeviceManager()
        device1 = DebugLinearMotionDevice()
        device2 = DebugLinearMotionDevice()
        dm.addDevice(device1)
        dm.addDevice(device2)

        matched = dm.matchPhysicalDevicesOfType(DebugLinearMotionDevice)
        self.assertTrue(len(matched) == 2)
    def testSendCommand(self):
        DeviceManager().updateConnectedDevices()
        devices = list(DeviceManager().devices)

        if len(devices) > 0:
            devices[0].initializeDevice()

            _ = DeviceManager().sendCommand("VERSION", deviceIdentifier=0)
            _ = DeviceManager().sendCommand("GETWAVELENGTH",
                                            deviceIdentifier=0)

            device[0].shutdownDevice()
    def testRestartRunLoop(self):
        dm = DeviceManager()

        startTime = time.time()
        expectedMaxEndTime = startTime + 3.0
        dm.startMonitoring()
        dm.stopMonitoring()
        self.assertTrue(expectedMaxEndTime > time.time())

        startTime = time.time()
        expectedMaxEndTime = startTime + 3.0
        dm.startMonitoring()
        dm.stopMonitoring()
        self.assertTrue(expectedMaxEndTime > time.time())
    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 testNotificationReceivedWhileAddingDevices(self):
        dm = DeviceManager()
        nc = NotificationCenter()
        nc.addObserver(self, self.handle,
                       DeviceManagerNotification.willStartMonitoring)
        nc.addObserver(self, self.handle,
                       DeviceManagerNotification.didStartMonitoring)
        nc.addObserver(self, self.handle,
                       DeviceManagerNotification.willStopMonitoring)
        nc.addObserver(self, self.handle,
                       DeviceManagerNotification.didStopMonitoring)
        nc.addObserver(self, self.handleStatus,
                       DeviceManagerNotification.status)
        self.notificationsToReceive = [
            DeviceManagerNotification.willStartMonitoring,
            DeviceManagerNotification.didStartMonitoring,
            DeviceManagerNotification.willStopMonitoring,
            DeviceManagerNotification.didStopMonitoring
        ]

        dm.startMonitoring()
        time.sleep(0.5)
        self.addRemoveManyDevices()
        time.sleep(0.5)
        dm.stopMonitoring()

        self.assertTrue(len(self.notificationsToReceive) == 0)
        nc.removeObserver(self)
    def testMatching4WithSubclass(self):
        dm = DeviceManager()
        device1 = DebugLinearMotionDevice()
        dm.addDevice(device1)
        device2 = SutterDevice("debug")
        dm.addDevice(device2)

        matched = dm.matchPhysicalDevicesOfType(LinearMotionDevice)
        self.assertTrue(len(matched) == 2)
        self.assertTrue(device1 in matched)
        self.assertTrue(device2 in matched)

        matched = dm.matchPhysicalDevicesOfType(DebugLinearMotionDevice)
        self.assertTrue(len(matched) == 1)
        self.assertTrue(device1 in matched)

        matched = dm.matchPhysicalDevicesOfType(SutterDevice)
        self.assertTrue(len(matched) == 1)
        self.assertTrue(device2 in matched)
 def testStartRunLoop(self):
     dm = DeviceManager()
     dm.startMonitoring()
     startTime = time.time()
     expectedMaxEndTime = startTime + 3.0
     time.sleep(2.0)
     # self.assertEqual(len(dm.devices), 1)
     dm.stopMonitoring()
     self.assertTrue(expectedMaxEndTime > time.time())
    def addRemoveManyDevices(N=1000):
        dm = DeviceManager()
        devices = []
        for i in range(N):
            device = DebugLinearMotionDevice()
            dm.addDevice(device)
            devices.append(device)

        for device in devices:
            dm.removeDevice(device)
Exemplo n.º 10
0
    def testMatching3WithSerial(self):
        dm = DeviceManager()
        device1 = DebugLinearMotionDevice()
        device2 = DebugLinearMotionDevice()
        device2.serialNumber = "debug2"
        dm.addDevice(device1)
        dm.addDevice(device2)

        matched = dm.matchPhysicalDevicesOfType(DebugLinearMotionDevice,
                                                serialNumber="debug")
        self.assertTrue(len(matched) == 2)

        matched = dm.matchPhysicalDevicesOfType(DebugLinearMotionDevice,
                                                serialNumber="debu")
        self.assertTrue(len(matched) == 2)

        matched = dm.matchPhysicalDevicesOfType(DebugLinearMotionDevice,
                                                serialNumber="debug2")
        self.assertTrue(len(matched) == 1)
Exemplo n.º 11
0
    def testNotificationReceivedFromAddingDevices(self):
        dm = DeviceManager()
        nc = NotificationCenter()

        dm.startMonitoring()
        time.sleep(1.0)  # let newlyConnected devices be added.

        nc.addObserver(self, self.handle,
                       DeviceManagerNotification.willAddDevice)
        nc.addObserver(self, self.handle,
                       DeviceManagerNotification.didAddDevice)
        nc.addObserver(self, self.handle,
                       DeviceManagerNotification.willRemoveDevice)
        nc.addObserver(self, self.handle,
                       DeviceManagerNotification.didRemoveDevice)

        N = 1000
        self.notificationsToReceive = [
            DeviceManagerNotification.willAddDevice,
            DeviceManagerNotification.didAddDevice
        ] * N
        self.notificationsToReceive.extend([
            DeviceManagerNotification.willRemoveDevice,
            DeviceManagerNotification.didRemoveDevice
        ] * N)

        time.sleep(0.5)
        self.addRemoveManyDevices(N)
        time.sleep(0.5)

        with self.lock:
            self.assertEqual(len(self.notificationsToReceive), 0)

        nc.removeObserver(self)

        dm.stopMonitoring()
Exemplo n.º 12
0
 def testEmpty(self):
     self.assertEqual(len(DeviceManager().devices), 0)
Exemplo n.º 13
0
 def testSingleton(self):
     dm1 = DeviceManager()
     dm2 = DeviceManager()
     self.assertEqual(dm1, dm2)
Exemplo n.º 14
0
 def testStopRunLoopTwice(self):
     dm = DeviceManager()
     with self.assertRaises(Exception):
         dm.stopMonitoring()
Exemplo n.º 15
0
 def testInstantiate(self):
     self.assertIsNotNone(DeviceManager())