def _refresh(self): 'Refresh the list of Computers' self._btnRefresh.setEnabled(False) self._btnRefresh.setText('Scanning...') self._model.clear() typ = self.wizard().field('type') if typ == len(ComputerTypes): # Custom Computer Type didx = self.wizard().field('driver') drvr = list_drivers().keys()[didx] dopt = self.wizard().field('driveropt') else: # Predefined Computer Type drvr = ComputerTypes[typ]['driver'] dopt = ComputerTypes[typ]['driveropt'] dclass = list_drivers()[drvr]['class'] doptions = [] if dopt == '' else dopt.split(':') thread = QThread(self) #FIXME: ZOMG HAX: Garbage Collector will eat DiscoveryWorker when moveToThread is called #NOTE: Qt.QueuedConnection is important... self.worker = None self.worker = DiscoveryWorker(dclass, doptions) self.worker.moveToThread(thread) thread.started.connect(self.worker.start, Qt.QueuedConnection) self.worker.foundDevice.connect(self._model.addItem, Qt.QueuedConnection) self.worker.finished.connect(self._discoverFinished, Qt.QueuedConnection) self.worker.finished.connect(self.worker.deleteLater, Qt.QueuedConnection) self.worker.finished.connect(thread.deleteLater, Qt.QueuedConnection) thread.start()
def RunWizard(cls, parent=None): 'Run the Add Dive Computer Wizard and return a DiveComputer object' wiz = cls(parent) if wiz.exec_() == QDialog.Rejected: return None # Handle Wizard Fields typ = wiz.field('type') if typ == len(ComputerTypes): # Custom Computer Type didx = wiz.field('driver') pidx = wiz.field('parser') drvr = list_drivers().keys()[didx] prsr = list_parsers().keys()[pidx] dopt = wiz.field('driveropt') popt = wiz.field('parseropt') else: # Predefined Computer Type drvr = ComputerTypes[typ]['driver'] prsr = ComputerTypes[typ]['parser'] dopt = ComputerTypes[typ]['driveropt'] popt = ComputerTypes[typ]['parseropt'] serno = wiz.field('serial') name = wiz.field('name') # Create the DiveComputer model dc = models.DiveComputer() dc.driver = drvr dc.serial = serno dc.name = name dc.parser = prsr dc.driver_args = dopt dc.parser_args = popt return dc
def start(self): 'Run the Transfer' self.status.emit(self.tr('Starting Transfer from %s') % self._dc.name) time.sleep(0.1) # Load the Driver try: d = list_drivers()[self._dc.driver] da = self._dc.driver_args dopts = [] if da is None or da == '' else da.split(':') driver = d['class'](*dopts) self.status.emit(self.tr('Loaded Driver "%s"') % self._dc.driver) except: self.status.emit(self.tr('Error: Cannot load driver "%s"') % self._dc.driver) # Load the Parser and Adapter Class try: p = list_parsers()[self._dc.parser] pa = self._dc.parser_args popts = [] if pa is None or pa == '' else pa.split(':') parser = p['class'](*popts) adapter_cls = p['adapter'] self.status.emit(self.tr('Loaded Parser "%s"') % self._dc.parser) except: self.status.emit(self.tr('Error: Cannot load parser "%s"') % self._dc.parser) # Connect and check Serial Number self.status.emit('Connecting to %s' % self._dc.name) #FIXME: driver.discover() blocks main thread time.sleep(0.1) devs = driver.discover() drv = None for _dev in devs: time.sleep(0.1) try: _drv = d['class'](*dopts) _drv.connect(_dev) if _drv.serial == self._dc.serial: drv = _drv break else: _drv.disconnect() except: self.status.emit('Error: Could not connect to %s (Driver Error)' % self._dc.name) self.finished.emit() return if drv == None: self.status.emit('Error: Could not connect to %s (Device Not Found)' % self._dc.name) time.sleep(0.1) # Transfer Dives drv.set_token(self._dc.token) nbytes = drv.get_bytecount() self.status.emit('Transferring %d bytes...' % nbytes) self.started.emit(nbytes) _dives = drv.transfer(TransferWorker.Reporter(self)) token = drv.issue_token() self.status.emit('Transfer Finished (%d new dives)' % len(_dives)) drv.disconnect() # Parse Dive Data for _dive in _dives: dive = models.Dive() dive.init_from_adapter(adapter_cls(parser.parse(_dive))) dive.computer = self._dc self.status.emit(self.tr('Parsed Dive: %s') % dive.dive_datetime.strftime('%x %X')) self.parsedDive.emit(dive) # Update Dive Computer Token self._dc.token = token # Finished Transferring self.status.emit(self.tr('Transfer Successful')) self.finished.emit()
def __init__(self): super(DriverModel, self).__init__() self._drivers = list_drivers()