Exemple #1
0
    def addDeviceToMonitor(self,device_class_name,device_config):
        device_class_name=str(device_class_name)
        
        self.log("Handling Device: %s"%(device_class_name,))
        #print2err("addDeviceToMonitor:\n\tdevice_class: {0}\n\texperiment_device_config:{1}\n".format(device_class_name,device_config))

        DeviceClass=None
        class_name_start=device_class_name.rfind('.')
        iohub_sub_mod='psychopy.iohub.'
        iohub_submod_path_length=len(iohub_sub_mod)
        device_module_path=iohub_sub_mod+'devices.'
        if class_name_start>0:
            device_module_path="{0}{1}".format(device_module_path,device_class_name[:class_name_start].lower())   
            device_class_name=device_class_name[class_name_start+1:]
        else:
            device_module_path="{0}{1}".format(device_module_path,device_class_name.lower())

        #print2err("Processing device, device_class_name: {0}, device_module_path: {1}".format(device_class_name, device_module_path))
         
        dconfigPath=os.path.join(IO_HUB_DIRECTORY,device_module_path[iohub_submod_path_length:].replace('.',os.path.sep),"default_%s.yaml"%(device_class_name.lower()))

        #print2err("dconfigPath: {0}, device_module_path: {1}\n".format(dconfigPath,device_module_path))
        #print2err("Loading Device Defaults file:\n\tdevice_class: {0}\n\tdeviceConfigFile:{1}\n".format(device_class_name,dconfigPath))
        self.log("Loading Device Defaults file: %s"%(device_class_name,))

        _dclass,default_device_config=load(file(dconfigPath,'r'), Loader=Loader).popitem()

        #print2err("Device Defaults:\n\tdevice_class: {0}\n\tdefault_device_config:{1}\n".format(device_class_name,default_device_config))
        
        self.processDeviceConfigDictionary(device_module_path, device_class_name, device_config,default_device_config)

        if device_module_path in self._all_device_config_errors:
            # Complete device config verification.
            print2err("**** ERROR: DEVICE CONFIG ERRORS FOUND ! NOT LOADING DEVICE: ",device_module_path)
            device_config_errors=self._all_device_config_errors[device_module_path]
            for error_type,errors in device_config_errors.iteritems():
                print2err("%s count %d:"%(error_type,len(errors)))
                for error in errors:
                    print2err("\t{0}".format(error))
                print2err("\n")
            return None
        
        DeviceClass,device_class_name,event_classes=import_device(device_module_path,device_class_name)
        #print2err("Updated Experiment Device Config:\n\tdevice_class: {0}\n\tdevice_config:{1}\n".format(device_class_name,default_device_config))
            
        if device_config.get('enable',True):
            self.log("Searching Device Path: %s"%(device_class_name,))
            self.log("Creating Device: %s"%(device_class_name,))
            #print2err("Creating Device: %s"%(device_class_name,))
            
            if DeviceClass._iohub_server is None:
                DeviceClass._iohub_server=self
            
            if device_class_name != 'Display' and DeviceClass._display_device is None:
                DeviceClass._display_device=ioServer.deviceDict['Display']  
                
            deviceInstance=DeviceClass(dconfig=device_config)

            self.log("Device Instance Created: %s"%(device_class_name,))
            #print2err("Device Instance Created: %s"%(device_class_name,))

            self.devices.append(deviceInstance)
            ioServer.deviceDict[device_class_name]=deviceInstance

            if 'device_timer' in device_config:
                interval = device_config['device_timer']['interval']
                self.log("%s has requested a timer with period %.5f"%(device_class_name, interval))
                dPoller=DeviceMonitor(deviceInstance,interval)
                self.deviceMonitors.append(dPoller)

            monitoringEventIDs=[]
            monitor_events_list=device_config.get('monitor_event_types',[])
            if isinstance(monitor_events_list,(list,tuple)):
                for event_class_name in monitor_events_list:
                    event_id = getattr(EventConstants,convertCamelToSnake(event_class_name[:-5],False))
                    monitoringEventIDs.append(event_id)
            self.log("{0} Instance Event IDs To Monitor: {1}".format(device_class_name,monitoringEventIDs))
            #ioHub.print2err("{0} Instance Event IDs To Monitor: {1}".format(device_class_name,eventIDs))

            # add event listeners for streaming events
            if device_config.get('stream_events') is True:
                self.log("Online event access is being enabled for: %s"%device_class_name)
                # add listener for global event queue
                deviceInstance._addEventListener(self,monitoringEventIDs)
                #ioHub.print2err("ioServer event stream listener added: device=%s eventIDs=%s"%(device_class_name,eventIDs))
                self.log("Standard event stream listener added for ioServer for event ids %s"%(str(monitoringEventIDs),))
                # add listener for device event queue
                deviceInstance._addEventListener(deviceInstance,monitoringEventIDs)
                #  ioHub.print2err("%s event stream listener added: eventIDs=%s"%(device_class_name,eventIDs))
                self.log("Standard event stream listener added for class %s for event ids %s"%(device_class_name,str(monitoringEventIDs)))

            return deviceInstance,device_config,monitoringEventIDs,event_classes
Exemple #2
0
    def addDeviceToMonitor(self, device_class_name, device_config):
        device_class_name = str(device_class_name)

        self.log("Handling Device: %s" % (device_class_name, ))
        #print2err("addDeviceToMonitor:\n\tdevice_class: {0}\n\texperiment_device_config:{1}\n".format(device_class_name,device_config))

        DeviceClass = None
        class_name_start = device_class_name.rfind('.')
        iohub_sub_mod = 'psychopy.iohub.'
        iohub_submod_path_length = len(iohub_sub_mod)
        device_module_path = iohub_sub_mod + 'devices.'
        if class_name_start > 0:
            device_module_path = "{0}{1}".format(
                device_module_path,
                device_class_name[:class_name_start].lower())
            device_class_name = device_class_name[class_name_start + 1:]
        else:
            device_module_path = "{0}{1}".format(device_module_path,
                                                 device_class_name.lower())

        #print2err("Processing device, device_class_name: {0}, device_module_path: {1}".format(device_class_name, device_module_path))

        dconfigPath = os.path.join(
            IO_HUB_DIRECTORY,
            device_module_path[iohub_submod_path_length:].replace(
                '.',
                os.path.sep), "default_%s.yaml" % (device_class_name.lower()))

        #print2err("dconfigPath: {0}, device_module_path: {1}\n".format(dconfigPath,device_module_path))
        #print2err("Loading Device Defaults file:\n\tdevice_class: {0}\n\tdeviceConfigFile:{1}\n".format(device_class_name,dconfigPath))
        self.log("Loading Device Defaults file: %s" % (device_class_name, ))

        _dclass, default_device_config = load(file(dconfigPath, 'r'),
                                              Loader=Loader).popitem()

        #print2err("Device Defaults:\n\tdevice_class: {0}\n\tdefault_device_config:{1}\n".format(device_class_name,default_device_config))

        self.processDeviceConfigDictionary(device_module_path,
                                           device_class_name, device_config,
                                           default_device_config)

        if device_module_path in self._all_device_config_errors:
            # Complete device config verification.
            print2err(
                "**** ERROR: DEVICE CONFIG ERRORS FOUND ! NOT LOADING DEVICE: ",
                device_module_path)
            device_config_errors = self._all_device_config_errors[
                device_module_path]
            for error_type, errors in device_config_errors.iteritems():
                print2err("%s count %d:" % (error_type, len(errors)))
                for error in errors:
                    print2err("\t{0}".format(error))
                print2err("\n")
            return None

        DeviceClass, device_class_name, event_classes = import_device(
            device_module_path, device_class_name)
        #print2err("Updated Experiment Device Config:\n\tdevice_class: {0}\n\tdevice_config:{1}\n".format(device_class_name,default_device_config))

        if device_config.get('enable', True):
            self.log("Searching Device Path: %s" % (device_class_name, ))
            self.log("Creating Device: %s" % (device_class_name, ))
            #print2err("Creating Device: %s"%(device_class_name,))

            if DeviceClass._iohub_server is None:
                DeviceClass._iohub_server = self

            if device_class_name != 'Display' and DeviceClass._display_device is None:
                DeviceClass._display_device = ioServer.deviceDict['Display']

            deviceInstance = DeviceClass(dconfig=device_config)

            self.log("Device Instance Created: %s" % (device_class_name, ))
            #print2err("Device Instance Created: %s"%(device_class_name,))

            self.devices.append(deviceInstance)
            ioServer.deviceDict[device_class_name] = deviceInstance

            if 'device_timer' in device_config:
                interval = device_config['device_timer']['interval']
                self.log("%s has requested a timer with period %.5f" %
                         (device_class_name, interval))
                dPoller = DeviceMonitor(deviceInstance, interval)
                self.deviceMonitors.append(dPoller)

            monitoringEventIDs = []
            monitor_events_list = device_config.get('monitor_event_types', [])
            if isinstance(monitor_events_list, (list, tuple)):
                for event_class_name in monitor_events_list:
                    event_id = getattr(
                        EventConstants,
                        convertCamelToSnake(event_class_name[:-5], False))
                    monitoringEventIDs.append(event_id)
            self.log("{0} Instance Event IDs To Monitor: {1}".format(
                device_class_name, monitoringEventIDs))
            #ioHub.print2err("{0} Instance Event IDs To Monitor: {1}".format(device_class_name,eventIDs))

            # add event listeners for streaming events
            if device_config.get('stream_events') is True:
                self.log("Online event access is being enabled for: %s" %
                         device_class_name)
                # add listener for global event queue
                deviceInstance._addEventListener(self, monitoringEventIDs)
                #ioHub.print2err("ioServer event stream listener added: device=%s eventIDs=%s"%(device_class_name,eventIDs))
                self.log(
                    "Standard event stream listener added for ioServer for event ids %s"
                    % (str(monitoringEventIDs), ))
                # add listener for device event queue
                deviceInstance._addEventListener(deviceInstance,
                                                 monitoringEventIDs)
                #  ioHub.print2err("%s event stream listener added: eventIDs=%s"%(device_class_name,eventIDs))
                self.log(
                    "Standard event stream listener added for class %s for event ids %s"
                    % (device_class_name, str(monitoringEventIDs)))

            return deviceInstance, device_config, monitoringEventIDs, event_classes