def test_qt_schedule_action(self): app = make_app() scheduler = QtScheduler(QtCore) ran = [False] def action(scheduler, state): ran[0] = True scheduler.schedule(action) def done(): app.quit() assert ran[0] QtCore.QTimer.singleShot(50, done) app.exec_()
def test_qt_schedule_action_cancel(self): app = make_app() ran = [False] scheduler = QtScheduler(QtCore) def action(scheduler, state): ran[0] = True d = scheduler.schedule_relative(100, action) d.dispose() def done(): app.quit() assert not ran[0] QtCore.QTimer.singleShot(300, done) app.exec_()
def test_qt_schedule_action_periodic(self): app = make_app() scheduler = QtScheduler(QtCore) period = 50 counter = [3] def action(state): if state: counter[0] -= 1 return state - 1 scheduler.schedule_periodic(period, action, counter[0]) def done(): app.quit() assert counter[0] == 0 QtCore.QTimer.singleShot(300, done) app.exec_()
def test_qt_schedule_action_due(self): app = make_app() scheduler = QtScheduler(QtCore) starttime = datetime.utcnow() endtime = [None] def action(scheduler, state): endtime[0] = datetime.utcnow() scheduler.schedule_relative(200, action) def done(): app.quit() assert endtime[0] diff = endtime[0] - starttime assert diff > timedelta(milliseconds=180) QtCore.QTimer.singleShot(300, done) app.exec_()
def startCalibration(self, sensorName, port): """ Starts calibration and instantiates the EMT system :param sensorName: the uncalibrated sensor :param port: the port the uncalibrated sensor is connected to """ if self.mode == MODE_IDLE: try: config = guiutils.import_default_config_settings() primaryChannels = config['system']['primary_channels'] sensor = utils.get_sensor(sensorName) sensor.channel = utils.get_active_channel(sensor.dof, port, primaryChannels) calibration = EMCalibration(sensor, config) qtScheduler = QtScheduler(QtCore) newScheduler = NewThreadScheduler() self.systemMonitor = Monitor(calibration.anser) self.subscriptions.append(calibration.anser.sampleNotifications.sample(500, scheduler=newScheduler)\ .subscribe(self.systemMonitor.run_system_test)) self.subscriptions.append(self.systemMonitor.systemNotifications.subscribe_on(scheduler=qtScheduler)\ .subscribe(self.SYS_EVENT_SYSTEM_STATUS_NOTIFICATION.emit)) #remove this for i in range(3): calibration.anser.sample_update() self.calibrationThread = CalibrationThread(calibration, self.SYS_EVENT_POINT_CAPTURED, self.SYS_EVENT_READY_TO_CALIBRATE, self.SYS_EVENT_CALIBRATION_COMPLETED) self.calibrationThread.start() self.systemStatus = True self.SYS_EVENT_SYSTEM_STATUS.emit(self.systemStatus) self.SYS_EVENT_MODE_CALIBRATION.emit() self.SYS_EVENT_POINT_CAPTURED.emit(1) self.mode = MODE_CALIBRATING self.SYS_EVENT_MODE_CHANGED.emit(self.mode) logging.info('Started Calibration') except Exception as e: logging.info('Device cannot be accessed. Possible causes: ' '\n - Computer is not connected to DAQ port ' '\n - After plugging device into the USB Port, ' 'wait a few moments to let the driver install' '\n - Ensure device specified is correct. ' '\n (Go to -> Developer Tab, in the configuration file under \'system\' change the \'device_name\' to your DevX indentifier) \n') print(str(e)) elif self.mode == MODE_CALIBRATING: self.stopCalibration() else: logging.info('System is currently in use. Please stop tracking to continue')
def main(): app = QApplication(sys.argv) scheduler = QtScheduler(QtCore) window = Window() window.show() text = 'some text on the screen' labels = [QLabel(char, window) for char in text] def handle_label(i, label): def on_next(pos): x, y = pos label.move(x + i * 12 + 15, y) label.show() window.mousemove.delay(i * 100, scheduler=scheduler).subscribe(on_next) for i, label in enumerate(labels): handle_label(i, label) sys.exit(app.exec_())
def random_stock(x): symbols_names = [ ['ABC', 'abc manufacturing'], ['DEF', 'Desert Inc'], ['GHI', 'Chi Chi Inc'], ['A', 'A plus consulting'], ['GS', 'Great Security'], ['GO', 'Go Go Consulting'], ] stock = random.choice(symbols_names) return [ stock[0], stock[1], round(random.uniform(21, 22), 2), round(random.uniform(20, 21), 2), ] REFRESH_STOCK_INTERVAL = 2000 if __name__ == '__main__': app = QApplication(sys.argv) scheduler = QtScheduler(QtCore) stock_prices = Observable.interval(REFRESH_STOCK_INTERVAL, scheduler).map(random_stock).publish() hello_world = HelloWorld(stock_prices_stream=stock_prices) hello_world.show() stock_prices.connect() sys.exit(app.exec_())
def test_qt_schedule_now(self): scheduler = QtScheduler(QtCore) res = scheduler.now() - datetime.utcnow() assert(res < timedelta(seconds=1))
def startTracking(self, sensorNames, ports, sliderPos): """ Starts EMT tracking and instantiates the EMT system :param sensorNames: a list containing the selected sensor for each of the ports :param ports: a boolean list indicating whether the selected sensors is to be tracked for each of the ports :param sliderPos: the given tracking speed for the EMT system (1-4, where 1 is fastest and 4 is the most accurate) """ if self.mode == MODE_IDLE: selectedSensors = [] selectedSensorNames = [] selectedPorts = [] config = guiutils.import_default_config_settings() if config is not None: primaryChannels = config['system']['primary_channels'] else: logging.info('No configuration file found. Go to -> Developer Tab and select configuration file. Click Make Default.') return for index, (sensorName, port) in enumerate(zip(sensorNames, ports)): if port is True: portNo = index + 1 sensor = utils.get_sensor(sensorName) if sensor is not None: sensor.channel = utils.get_active_channel(sensor.dof, portNo, primary_channels=primaryChannels) selectedSensors.append(sensor) selectedSensorNames.append(sensorName) selectedPorts.append(portNo) if len(selectedSensors) == 0: logging.info('No ports or sensors were selected') elif len(selectedSensorNames) != len(set(selectedSensorNames)): logging.info('Duplicate sensors selected') else: config['system']['speed'] = sliderPos try: self.anser = EMTracker(selectedSensors, config) self.anser.sensors = selectedSensors self.anser.start_acquisition() # system object so we can populate views System_Template = namedtuple('System', ['freq', 'coils', 'sampling_freq', 'num_samples', 'active_ports', 'active_channels', 'sensors']) system = System_Template( freq=[freq / 1000 for freq in self.anser.filter.transFreqs], coils=[True]*8, sampling_freq=self.anser.filter.sampleFreq, num_samples=self.anser.filter.numSamples, active_ports=selectedPorts, active_channels = self.anser.active_channels, sensors=self.anser.sensors) qtScheduler = QtScheduler(QtCore) newScheduler = NewThreadScheduler() self.systemMonitor = Monitor(self.anser) self.subscriptions.append(self.anser.positionNotifications.sample(15, scheduler=qtScheduler).subscribe(self.sendPositions)) self.subscriptions.append(self.anser.sampleNotifications.sample(30, scheduler=qtScheduler).subscribe(self.sendSamples)) self.subscriptions.append(self.anser.sampleNotifications.sample(1300, scheduler=newScheduler)\ .subscribe(on_next=self.systemMonitor.run_system_test)) self.subscriptions.append(self.systemMonitor.systemNotifications.subscribe_on(scheduler=qtScheduler)\ .subscribe(on_next=self.SYS_EVENT_SYSTEM_STATUS_NOTIFICATION.emit)) self.anser.start() self.systemStatus = True self.SYS_EVENT_SYSTEM_STATUS.emit(self.systemStatus) self.SYS_EVENT_MODE_TRACKING.emit(system) self.mode = MODE_TRACKING self.SYS_EVENT_MODE_CHANGED.emit(self.mode) logging.info('Started Tracking') except Exception as e: logging.info('Device cannot be accessed. Possible causes: ' '\n - Computer is not connected to DAQ port ' '\n - After plugging device into the USB Port, ' 'wait a few moments to let the driver install' '\n - Ensure device specified is correct. ' '\n (Go to -> Developer Tab, in the configuration file under \'system\' change the \'device_name\' to your DevX indentifier) \n') print(str(e)) elif self.mode == MODE_TRACKING: self.stopTracking() logging.info('Stopped Tracking') else: logging.info('System is currently in use. Please stop calibration to continue')