Ejemplo n.º 1
0
class Device(QtCore.QObject):
    """Abstract class defining the standard interface for Device subclasses."""
    def __init__(self, deviceManager, config, name):
        QtCore.QObject.__init__(self)
        self._lock_ = Mutex(QtCore.QMutex.Recursive)  ## no, good idea
        self._lock_tb_ = None
        self.dm = deviceManager
        self.dm.declareInterface(name, ['device'], self)
        #self.config = config
        self._name = name
    
    def name(self):
        return self._name
    
    def createTask(self, cmd, task):
        ### Read configuration, configure tasks
        ### Return a handle unique to this task
        pass
    
    #def __del__(self):
        #self.quit()
        
    def quit(self):
        pass
    
    def deviceInterface(self, win):
        """Return a widget with a UI to put in the device rack"""
        return None
        
    def taskInterface(self, task):
        """Return a widget with a UI to put in the task rack"""
        return TaskGui(self, task)
        

    def reserve(self, block=True, timeout=20):
        #print "Device %s attempting lock.." % self.name()
        if block:
            l = self._lock_.tryLock(int(timeout*1000))
            if not l:
                print "Timeout waiting for device lock for %s" % self.name()
                print "  Device is currently locked from:"
                print self._lock_tb_
                raise Exception("Timed out waiting for device lock for %s" % self.name())
        else:
            l = self._lock_.tryLock()
            if not l:
                #print "Device %s lock failed." % self.name()
                return False
                #print "  Device is currently locked from:"
                #print self._lock_tb_
                #raise Exception("Could not acquire lock", 1)  ## 1 indicates failed non-blocking attempt
        self._lock_tb_ = ''.join(traceback.format_stack()[:-1])
        #print "Device %s lock ok" % self.name()
        return True
        
    def release(self):
        try:
            self._lock_.unlock()
            self._lock_tb_ = None
        except:
            printExc("WARNING: Failed to release device lock for %s" % self.name())
            
    def getTriggerChannel(self, daq):
        """Return the name of the channel on daq that this device raises when it starts.
        Allows the DAQ to trigger off of this device."""
        return None
Ejemplo n.º 2
0
class Device(QtCore.QObject):
    """Abstract class defining the standard interface for Device subclasses."""
    def __init__(self, deviceManager, config, name):
        QtCore.QObject.__init__(self)

        # task reservation lock -- this is a recursive lock to allow a task to run its own subtasks
        # (for example, setting a holding value before exiting a task).
        # However, under some circumstances we might try to run two concurrent tasks from the same
        # thread (eg, due to calling processEvents() while waiting for the task to complete). We
        # don't have a good solution for this problem at present..
        self._lock_ = Mutex(QtCore.QMutex.Recursive)
        self._lock_tb_ = None
        self.dm = deviceManager
        self.dm.declareInterface(name, ['device'], self)
        self._name = name

    def name(self):
        return self._name

    def createTask(self, cmd, task):
        ### Read configuration, configure tasks
        ### Return a handle unique to this task
        pass

    def quit(self):
        pass

    def deviceInterface(self, win):
        """Return a widget with a UI to put in the device rack"""
        return None

    def taskInterface(self, task):
        """Return a widget with a UI to put in the task rack"""
        return TaskGui(self, task)

    def configPath(self):
        """Return the path used for storing configuration data for this device.

        This path should resolve to `acq4/config/devices/DeviceName_config`.
        """
        return os.path.join('devices', self.name() + '_config')

    def configFileName(self, filename):
        """Return the full path to a config file for this device.
        """
        filename = os.path.join(self.configPath(), filename)
        return self.dm.configFileName(filename)

    def readConfigFile(self, filename):
        """Read a config file from this device's configuration directory.
        """
        fileName = os.path.join(self.configPath(), filename)
        return self.dm.readConfigFile(fileName)

    def writeConfigFile(self, data, filename):
        """Write data to a config file in this device's configuration directory.
        """
        fileName = os.path.join(self.configPath(), filename)
        return self.dm.writeConfigFile(data, fileName)

    def appendConfigFile(self, data, filename):
        """Append data to a config file in this device's configuration directory.
        """
        fileName = os.path.join(self.configPath(), filename)
        return self.dm.appendConfigFile(data, fileName)

    def reserve(self, block=True, timeout=20):
        #print "Device %s attempting lock.." % self.name()
        if block:
            l = self._lock_.tryLock(int(timeout * 1000))
            if not l:
                print "Timeout waiting for device lock for %s" % self.name()
                print "  Device is currently locked from:"
                print self._lock_tb_
                raise Exception("Timed out waiting for device lock for %s" %
                                self.name())
        else:
            l = self._lock_.tryLock()
            if not l:
                #print "Device %s lock failed." % self.name()
                return False
                #print "  Device is currently locked from:"
                #print self._lock_tb_
                #raise Exception("Could not acquire lock", 1)  ## 1 indicates failed non-blocking attempt
        self._lock_tb_ = ''.join(traceback.format_stack()[:-1])
        #print "Device %s lock ok" % self.name()
        return True

    def release(self):
        try:
            self._lock_.unlock()
            self._lock_tb_ = None
        except:
            printExc("WARNING: Failed to release device lock for %s" %
                     self.name())

    def getTriggerChannel(self, daq):
        """Return the name of the channel on daq that this device raises when it starts.
        Allows the DAQ to trigger off of this device."""
        return None

    def __repr__(self):
        return '<%s "%s">' % (self.__class__.__name__, self.name())