Example #1
0
 def checkIfSessionCodeExists(self,sessionCode):
     try:
         if self.iohub.emrt_file:
             return self.iohub.emrt_file.checkIfSessionCodeExists(sessionCode)
         return False
     except:
         printExceptionDetailsToStdErr()
Example #2
0
def run(rootScriptPathDir,configFilePath):
    import tempfile
    from iohub import IO_HUB_DIRECTORY
    from iohub.util import updateDict
    tdir=tempfile.gettempdir()
    cdir,cfile=os.path.split(configFilePath)

    if tdir==cdir:
        tf=open(configFilePath)
        ioHubConfig=json.loads(tf.read())
        tf.close()
        os.remove(configFilePath)
    else:
        ioHubConfig=load(file(configFilePath,'r'), Loader=Loader)

 

    hub_defaults_config=load(file(os.path.join(IO_HUB_DIRECTORY,'default_config.yaml'),'r'), Loader=Loader)
    updateDict(ioHubConfig,hub_defaults_config)


    try:
        s = ioServer(rootScriptPathDir, ioHubConfig)
    except Exception,e:
        printExceptionDetailsToStdErr()
        sys.stdout.flush()
        
        try:
            s.shutdown()
        except:
            pass
        
        return -1
Example #3
0
    def shutdown(self):
        try:
            self._running = False

            if Computer.system == 'linux2':
                if self._hookManager:
                    self._hookManager.cancel()

            while len(self.deviceMonitors) > 0:
                m = self.deviceMonitors.pop(0)
                m.running = False
            if self.eventBuffer:
                self.clearEventBuffer()
            try:
                self.closeDataStoreFile()
            except:
                pass
            while len(self.devices) > 0:
                d = self.devices.pop(0)
                try:
                    if d is not None:
                        d._close()
                except:
                    pass
            gevent.sleep()
        except:
            print2err("Error in ioSever.shutdown():")
            printExceptionDetailsToStdErr()
Example #4
0
 def shutdown(self):
     try:
         self._running=False
 
         if Computer.system=='linux2':
             if self._hookManager:
                 self._hookManager.cancel()
 
         while len(self.deviceMonitors) > 0:
             m=self.deviceMonitors.pop(0)
             m.running=False
         if self.eventBuffer:
             self.clearEventBuffer()
         try:
             self.closeDataStoreFile()
         except:
             pass
         while len(self.devices) > 0:
             d=self.devices.pop(0)
             try:
                 if d is not None:
                     d._close()
             except:
                     pass
         gevent.sleep()
     except:
         print2err("Error in ioSever.shutdown():")
         printExceptionDetailsToStdErr()
Example #5
0
def run(rootScriptPathDir, configFilePath):
    import tempfile
    from iohub import IO_HUB_DIRECTORY
    from iohub.util import updateDict
    tdir = tempfile.gettempdir()
    cdir, cfile = os.path.split(configFilePath)

    if tdir == cdir:
        tf = open(configFilePath)
        ioHubConfig = json.loads(tf.read())
        tf.close()
        os.remove(configFilePath)
    else:
        ioHubConfig = load(file(configFilePath, 'r'), Loader=Loader)

    hub_defaults_config = load(file(
        os.path.join(IO_HUB_DIRECTORY, 'default_config.yaml'), 'r'),
                               Loader=Loader)
    updateDict(ioHubConfig, hub_defaults_config)

    try:
        s = ioServer(rootScriptPathDir, ioHubConfig)
    except Exception, e:
        printExceptionDetailsToStdErr()
        sys.stdout.flush()

        try:
            s.shutdown()
        except:
            pass

        return -1
Example #6
0
 def checkIfSessionCodeExists(self, sessionCode):
     try:
         if self.iohub.emrt_file:
             return self.iohub.emrt_file.checkIfSessionCodeExists(
                 sessionCode)
         return False
     except:
         printExceptionDetailsToStdErr()
Example #7
0
 def shutDown(self):
     try:
         self.disableHighPriority()
         self.iohub.shutdown()
         self._running = False
         self.stop()
     except:
         print2err("Error in ioSever.shutdown():")
         printExceptionDetailsToStdErr()
         sys.exit(1)
Example #8
0
 def shutDown(self):
     try:
         self.disableHighPriority()
         self.iohub.shutdown()
         self._running=False
         self.stop()
     except:
         print2err("Error in ioSever.shutdown():")
         printExceptionDetailsToStdErr()
         sys.exit(1)
Example #9
0
 def log(self, text, level=None):
     try:
         log_time = currentSec()
         exp = self.deviceDict.get('Experiment', None)
         if exp and self._session_id and self._experiment_id:
             while len(self._logMessageBuffer):
                 lm = self._logMessageBuffer.popleft()
                 #print2err('>>>!!! Logging BACKLOGGED LogEvent: ',lm,", ",(exp, self._session_id, self._experiment_id))
                 exp.log(*lm)
             #print2err('>>>!!! Logging LogEvent: ',(text,level,log_time),", ",(exp, self._session_id, self._experiment_id))
             exp.log(text, level, log_time)
         else:
             #print2err('>>>!!! Adding LogEvent to _logMessageBuffer: ',(text,level,log_time),", ",(exp, self._session_id, self._experiment_id))
             self._logMessageBuffer.append((text, level, log_time))
     except:
         printExceptionDetailsToStdErr()
Example #10
0
 def log(self,text,level=None):
     try:
         log_time=currentSec()
         exp=self.deviceDict.get('Experiment',None)
         if exp and self._session_id and self._experiment_id:
             while len(self._logMessageBuffer):
                 lm=self._logMessageBuffer.popleft()
                 #print2err('>>>!!! Logging BACKLOGGED LogEvent: ',lm,", ",(exp, self._session_id, self._experiment_id))
                 exp.log(*lm)
             #print2err('>>>!!! Logging LogEvent: ',(text,level,log_time),", ",(exp, self._session_id, self._experiment_id))
             exp.log(text,level,log_time)
         else:
             #print2err('>>>!!! Adding LogEvent to _logMessageBuffer: ',(text,level,log_time),", ",(exp, self._session_id, self._experiment_id))
             self._logMessageBuffer.append((text,level,log_time))
     except:
         printExceptionDetailsToStdErr()
Example #11
0
    def sendResponse(self, data, address):
        packet_data = None
        try:
            max_size = iohub.net.MAX_PACKET_SIZE / 2 - 20
            packet_data = self.pack(data) + '\r\n'
            packet_data_length = len(packet_data)
            if packet_data_length >= max_size:
                num_packets = len(packet_data) / max_size + 1
                self.sendResponse(('IOHUB_MULTIPACKET_RESPONSE', num_packets),
                                  address)
                for p in xrange(num_packets - 1):
                    self.socket.sendto(
                        packet_data[p * max_size:(p + 1) * max_size], address)
                self.socket.sendto(
                    packet_data[(p + 1) * max_size:packet_data_length],
                    address)
            else:
                self.socket.sendto(packet_data, address)
        except:
            print2err('Error trying to send data to experiment process:')
            print2err('data length:', len(data))
            print2err("=============================")
            printExceptionDetailsToStdErr()
            print2err("=============================")

            first_data_element = "NO_DATA_AVAILABLE"
            if data:
                print2err('Data was [{0}]'.format(data))
                try:
                    first_data_element = data[0]
                except:
                    pass

            packet_data_length = 0
            if packet_data:
                packet_data_length = len(packet_data)
                print2err('packet Data length: ', len(packet_data))

            data = createErrorResult(
                'IOHUB_SERVER_RESPONSE_ERROR',
                msg="The ioHub Server Failed to send the intended response.",
                first_data_element=str(first_data_element),
                packet_data_length=packet_data_length,
                max_packet_size=max_size)
            packet_data = self.pack(data) + '\r\n'
            packet_data_length = len(packet_data)
            self.socket.sendto(packet_data, address)
Example #12
0
 def _processDeviceEventIteration(self):
     for device in self.devices:
         try:
             events=device._getNativeEventBuffer()
             #if events and len(events)>0:
             #    ioHub.print2err("_processDeviceEventIteration.....", device._event_listeners)
             while len(events)>0:
                 evt=events.popleft()
                 e=device._getIOHubEventObject(evt)
                 if e is not None:
                     for l in device._getEventListeners(e[DeviceEvent.EVENT_TYPE_ID_INDEX]):
                         l._handleEvent(e)
         except:
             printExceptionDetailsToStdErr()
             print2err("Error in processDeviceEvents: ", device, " : ", len(events), " : ", e)
             print2err("Event type ID: ",e[DeviceEvent.EVENT_TYPE_ID_INDEX], " : " , EventConstants.getName(e[DeviceEvent.EVENT_TYPE_ID_INDEX]))
             print2err("--------------------------------------")
Example #13
0
    def sendResponse(self,data,address):
        packet_data=None
        try:
            max_size=iohub.net.MAX_PACKET_SIZE/2-20
            packet_data=self.pack(data)+'\r\n'
            packet_data_length=len(packet_data)
            if packet_data_length>= max_size:
                num_packets=len(packet_data)/max_size+1
                self.sendResponse(('IOHUB_MULTIPACKET_RESPONSE',num_packets),address)
                for p in xrange(num_packets-1):
                    self.socket.sendto(packet_data[p*max_size:(p+1)*max_size],address)
                self.socket.sendto(packet_data[(p+1)*max_size:packet_data_length],address)
            else:
                self.socket.sendto(packet_data,address)
        except:
            print2err('Error trying to send data to experiment process:')
            print2err('data length:',len(data))
            print2err("=============================")            
            printExceptionDetailsToStdErr()
            print2err("=============================")            

            first_data_element="NO_DATA_AVAILABLE"            
            if data:
                print2err('Data was [{0}]'.format(data))     
                try:    
                    first_data_element=data[0]
                except:
                    pass
                    
            packet_data_length=0
            if packet_data:
                packet_data_length=len(packet_data)
                print2err('packet Data length: ',len(packet_data))

            data=createErrorResult('IOHUB_SERVER_RESPONSE_ERROR',       
                                   msg="The ioHub Server Failed to send the intended response.",
                                   first_data_element=str(first_data_element),
                                   packet_data_length=packet_data_length,
                                   max_packet_size=max_size)
            packet_data=self.pack(data)+'\r\n'
            packet_data_length=len(packet_data)            
            self.socket.sendto(packet_data,address)
Example #14
0
 def _processDeviceEventIteration(self):
     for device in self.devices:
         try:
             events = device._getNativeEventBuffer()
             #if events and len(events)>0:
             #    ioHub.print2err("_processDeviceEventIteration.....", device._event_listeners)
             while len(events) > 0:
                 evt = events.popleft()
                 e = device._getIOHubEventObject(evt)
                 if e is not None:
                     for l in device._getEventListeners(
                             e[DeviceEvent.EVENT_TYPE_ID_INDEX]):
                         l._handleEvent(e)
         except:
             printExceptionDetailsToStdErr()
             print2err("Error in processDeviceEvents: ", device, " : ",
                       len(events), " : ", e)
             print2err(
                 "Event type ID: ", e[DeviceEvent.EVENT_TYPE_ID_INDEX],
                 " : ",
                 EventConstants.getName(e[DeviceEvent.EVENT_TYPE_ID_INDEX]))
             print2err("--------------------------------------")
Example #15
0
    def createNewMonitoredDevice(self, device_class_name, deviceConfig):
        #print2err("#### createNewMonitoredDevice: ",device_class_name)
        self._all_device_config_errors = dict()

        try:
            device_instance = None
            device_config = None
            device_event_ids = None
            event_classes = None

            device_instance_and_config = self.addDeviceToMonitor(
                device_class_name, deviceConfig)
            if device_instance_and_config:
                device_instance, device_config, device_event_ids, event_classes = device_instance_and_config
                DeviceConstants.addClassMapping(device_instance.__class__)
                EventConstants.addClassMappings(device_instance.__class__,
                                                device_event_ids,
                                                event_classes)
            else:
                print2err('## Device was not started by the ioHub Server: ',
                          device_class_name)
                raise ioHubError("Device config validation failed")

        except:
            print2err("Error during device creation ....")
            printExceptionDetailsToStdErr()
            raise ioHubError("Error during device creation ....")

        # Update DataStore Structure if required.
        try:
            if self.emrt_file is not None:
                self.emrt_file.updateDataStoreStructure(
                    device_instance, event_classes)
        except:
            print2err("Error while updating datastore for device addition:",
                      device_instance, device_event_ids)
            printExceptionDetailsToStdErr()

        self.log("Adding ioServer and DataStore event listeners......")

        # add event listeners for saving events
        if self.emrt_file is not None:
            if device_config['save_events']:
                device_instance._addEventListener(self.emrt_file,
                                                  device_event_ids)
                self.log(
                    "DataStore listener for device added: device: %s eventIDs: %s"
                    % (device_instance.__class__.__name__, device_event_ids))
                #print2err("DataStore listener for device added: device: %s eventIDs: %s"%(device_instance.__class__.__name__,device_event_ids))
            else:
                #print2err("DataStore saving disabled for device: %s"%(device_instance.__class__.__name__,))
                self.log("DataStore saving disabled for device: %s" %
                         (device_instance.__class__.__name__, ))
        else:
            #print2err("DataStore Not Evabled. No events will be saved.")
            self.log("DataStore Not Enabled. No events will be saved.")

        # Add Device Monitor for Keyboard or Mouse device type
        deviceDict = ioServer.deviceDict
        iohub = self
        if device_class_name in ('Mouse', 'Keyboard'):
            if Computer.system == 'win32':
                if self._hookDevice is None:
                    iohub.log("Creating pyHook Monitors....")

                    #print2err("Creating pyHook Monitor....")

                    class pyHookDevice(object):
                        def __init__(self):
                            import pyHook
                            self._hookManager = pyHook.HookManager()

                            self._mouseHooked = False
                            self._keyboardHooked = False

                            if device_class_name == 'Mouse':
                                #print2err("Hooking Mouse.....")
                                self._hookManager.MouseAll = deviceDict[
                                    'Mouse']._nativeEventCallback
                                self._hookManager.HookMouse()
                                self._mouseHooked = True
                            elif device_class_name == 'Keyboard':
                                #print2err("Hooking Keyboard.....")
                                self._hookManager.KeyAll = deviceDict[
                                    'Keyboard']._nativeEventCallback
                                self._hookManager.HookKeyboard()
                                self._keyboardHooked = True

                            #iohub.log("WindowsHook PumpEvents Periodic Timer Created.")

                        def _poll(self):
                            import pythoncom
                            # PumpWaitingMessages returns 1 if a WM_QUIT message was received, else 0
                            if pythoncom.PumpWaitingMessages() == 1:
                                raise KeyboardInterrupt()

                    #print2err("Creating pyHook Monitor......")
                    self._hookDevice = pyHookDevice()
                    hookMonitor = DeviceMonitor(self._hookDevice, 0.00375)
                    self.deviceMonitors.append(hookMonitor)

                    #print2err("Created pyHook Monitor.")
                else:
                    #print2err("UPDATING pyHook Monitor....")
                    if device_class_name == 'Mouse' and self._hookDevice._mouseHooked is False:
                        #print2err("Hooking Mouse.....")
                        self._hookDevice._hookManager.MouseAll = deviceDict[
                            'Mouse']._nativeEventCallback
                        self._hookDevice._hookManager.HookMouse()
                        self._hookDevice._mouseHooked = True
                    elif device_class_name == 'Keyboard' and self._hookDevice._keyboardHooked is False:
                        #print2err("Hooking Keyboard.....")
                        self._hookDevice._hookManager.KeyAll = deviceDict[
                            'Keyboard']._nativeEventCallback
                        self._hookDevice._hookManager.HookKeyboard()
                        self._hookDevice._keyboardHooked = True

                    #print2err("Finished Updating pyHook Monitor.")

            elif Computer.system == 'linux2':
                # TODO: consider switching to xlib-ctypes implementation of xlib
                # https://github.com/garrybodsworth/pyxlib-ctypes
                from .devices import pyXHook
                if self._hookManager is None:
                    #iohub.log("Creating pyXHook Monitors....")
                    self._hookManager = pyXHook.HookManager()
                    self._hookManager._mouseHooked = False
                    self._hookManager._keyboardHooked = False

                    if device_class_name == 'Keyboard':
                        #print2err("Hooking Keyboard.....")
                        self._hookManager.HookKeyboard()
                        self._hookManager.KeyDown = deviceDict[
                            'Keyboard']._nativeEventCallback
                        self._hookManager.KeyUp = deviceDict[
                            'Keyboard']._nativeEventCallback
                        self._hookManager._keyboardHooked = True
                    elif device_class_name == 'Mouse':
                        #print2err("Hooking Mouse.....")
                        self._hookManager.HookMouse()
                        self._hookManager.MouseAllButtonsDown = deviceDict[
                            'Mouse']._nativeEventCallback
                        self._hookManager.MouseAllButtonsUp = deviceDict[
                            'Mouse']._nativeEventCallback
                        self._hookManager.MouseAllMotion = deviceDict[
                            'Mouse']._nativeEventCallback
                        self._hookManager._mouseHooked = True

                    #print2err("Starting pyXHook.HookManager.....")
                    self._hookManager.start()
                    #iohub.log("pyXHook Thread Created.")
                    #print2err("pyXHook.HookManager thread created.")
                else:
                    #iohub.log("Updating pyXHook Monitor....")
                    if device_class_name == 'Keyboard' and self._hookManager._keyboardHooked is False:
                        #print2err("Hooking Keyboard.....")
                        self._hookManager.HookKeyboard()
                        self._hookManager.KeyDown = deviceDict[
                            'Keyboard']._nativeEventCallback
                        self._hookManager.KeyUp = deviceDict[
                            'Keyboard']._nativeEventCallback
                        self._hookManager._keyboardHooked = True
                    if device_class_name == 'Mouse' and self._hookManager._mouseHooked is False:
                        #print2err("Hooking Mouse.....")
                        self._hookManager.HookMouse()
                        self._hookManager.MouseAllButtonsDown = deviceDict[
                            'Mouse']._nativeEventCallback
                        self._hookManager.MouseAllButtonsUp = deviceDict[
                            'Mouse']._nativeEventCallback
                        self._hookManager.MouseAllMotion = deviceDict[
                            'Mouse']._nativeEventCallback
                        self._hookManager._mouseHooked = True
                    #iohub.log("Finished Updating pyXHook Monitor....")

            else:  # OSX
                if self._hookDevice is None:
                    self._hookDevice = []

                if device_class_name == 'Mouse' and 'Mouse' not in self._hookDevice:
                    #print2err("Hooking OSX Mouse.....")
                    mouseHookMonitor = DeviceMonitor(deviceDict['Mouse'],
                                                     0.004)
                    self.deviceMonitors.append(mouseHookMonitor)
                    deviceDict['Mouse']._CGEventTapEnable(
                        deviceDict['Mouse']._tap, True)
                    self._hookDevice.append('Mouse')
                    #print2err("Done Hooking OSX Mouse.....")
                if device_class_name == 'Keyboard' and 'Keyboard' not in self._hookDevice:
                    #print2err("Hooking OSX Keyboard.....")
                    kbHookMonitor = DeviceMonitor(deviceDict['Keyboard'],
                                                  0.004)
                    self.deviceMonitors.append(kbHookMonitor)
                    deviceDict['Keyboard']._CGEventTapEnable(
                        deviceDict['Keyboard']._tap, True)
                    self._hookDevice.append('Keyboard')
                    #print2err("DONE Hooking OSX Keyboard.....")

            return [
                device_class_name, device_config['name'],
                device_instance._getRPCInterface()
            ]
Example #16
0
    def __init__(self, rootScriptPathDir, config=None):
        self._session_id = None
        self._experiment_id = None

        self.log("Server Time Offset: {0}".format(
            Computer.globalClock.getLastResetTime()))

        self._hookManager = None
        self.emrt_file = None
        self.config = config
        self.devices = []
        self.deviceMonitors = []
        self.sessionInfoDict = None
        self.experimentInfoList = None
        self.filterLookupByInput = {}
        self.filterLookupByOutput = {}
        self.filterLookupByName = {}
        self._hookDevice = None

        import iohub
        ioServer.eventBuffer = deque(
            maxlen=config.get('global_event_buffer', 2048))

        self._running = True

        # start UDP service
        self.udpService = udpServer(self, ':%d' % config.get('udp_port', 9000))

        # read temp paths file
        iohub.data_paths = None
        try:
            expJsonPath = os.path.join(rootScriptPathDir, 'exp.paths')
            f = open(expJsonPath, 'r')
            iohub.data_paths = json.loads(f.read())
            f.flush()
            f.close()
            os.remove(expJsonPath)
        except:
            pass

        try:
            # initial dataStore setup
            if 'data_store' in config:
                experiment_datastore_config = config.get('data_store')

                default_datastore_config_path = os.path.join(
                    iohub.IO_HUB_DIRECTORY, 'dataStore',
                    'default_datastore.yaml')
                _dslabel, default_datastore_config = load(
                    file(default_datastore_config_path,
                         'r'), Loader=Loader).popitem()

                for default_key, default_value in default_datastore_config.iteritems(
                ):
                    if default_key not in experiment_datastore_config:
                        experiment_datastore_config[
                            default_key] = default_value

                if experiment_datastore_config.get('enable', True):
                    #print2err("Creating ioDataStore....")

                    if iohub.data_paths is None:
                        resultsFilePath = rootScriptPathDir
                    else:
                        resultsFilePath = iohub.data_paths[u'IOHUB_DATA']
                    self.createDataStoreFile(
                        experiment_datastore_config.get('filename', 'events') +
                        '.hdf5', resultsFilePath, 'a',
                        experiment_datastore_config)

                    #print2err("Created ioDataStore.")
        except:
            print2err("Error during ioDataStore creation....")
            printExceptionDetailsToStdErr()

        #built device list and config from initial yaml config settings
        try:
            for iodevice in config.get('monitor_devices', ()):
                for device_class_name, deviceConfig in iodevice.iteritems():
                    #print2err("======================================================")
                    #print2err("Started load process for: {0}".format(device_class_name))
                    self.createNewMonitoredDevice(device_class_name,
                                                  deviceConfig)
        except:
            print2err("Error during device creation ....")
            printExceptionDetailsToStdErr()
            raise ioHubError("Error during device creation ....")
Example #17
0
    def createNewMonitoredDevice(self,device_class_name,deviceConfig):
        #print2err("#### createNewMonitoredDevice: ",device_class_name)
        self._all_device_config_errors=dict()

        try:
            device_instance=None
            device_config=None
            device_event_ids=None
            event_classes=None
            
            device_instance_and_config=self.addDeviceToMonitor(device_class_name,deviceConfig)
            if device_instance_and_config:
                device_instance,device_config,device_event_ids,event_classes=device_instance_and_config 
                DeviceConstants.addClassMapping(device_instance.__class__)
                EventConstants.addClassMappings(device_instance.__class__,device_event_ids,event_classes)
            else:
                print2err('## Device was not started by the ioHub Server: ',device_class_name)
                raise ioHubError("Device config validation failed")
                
        except:
            print2err("Error during device creation ....")
            printExceptionDetailsToStdErr()
            raise ioHubError("Error during device creation ....")


        # Update DataStore Structure if required.
        try:            
            if self.emrt_file is not None:
                self.emrt_file.updateDataStoreStructure(device_instance,event_classes)
        except:
            print2err("Error while updating datastore for device addition:",device_instance,device_event_ids)
            printExceptionDetailsToStdErr()


        self.log("Adding ioServer and DataStore event listeners......")

        # add event listeners for saving events
        if self.emrt_file is not None:
            if device_config['save_events']:
                device_instance._addEventListener(self.emrt_file,device_event_ids)
                self.log("DataStore listener for device added: device: %s eventIDs: %s"%(device_instance.__class__.__name__,device_event_ids))
                #print2err("DataStore listener for device added: device: %s eventIDs: %s"%(device_instance.__class__.__name__,device_event_ids))
            else:
                #print2err("DataStore saving disabled for device: %s"%(device_instance.__class__.__name__,))
                self.log("DataStore saving disabled for device: %s"%(device_instance.__class__.__name__,))
        else:
            #print2err("DataStore Not Evabled. No events will be saved.")
            self.log("DataStore Not Enabled. No events will be saved.")
    

        # Add Device Monitor for Keyboard or Mouse device type 
        deviceDict=ioServer.deviceDict
        iohub=self
        if device_class_name in ('Mouse','Keyboard'):
            if Computer.system == 'win32':  
                if self._hookDevice is None:
                    iohub.log("Creating pyHook Monitors....")
                    #print2err("Creating pyHook Monitor....")
    
                    class pyHookDevice(object):
                        def __init__(self):
                            import pyHook
                            self._hookManager=pyHook.HookManager()
                            
                            self._mouseHooked=False
                            self._keyboardHooked=False
                            
                            if device_class_name == 'Mouse':
                                #print2err("Hooking Mouse.....")
                                self._hookManager.MouseAll = deviceDict['Mouse']._nativeEventCallback
                                self._hookManager.HookMouse()
                                self._mouseHooked=True
                            elif device_class_name == 'Keyboard':
                                #print2err("Hooking Keyboard.....")
                                self._hookManager.KeyAll = deviceDict['Keyboard']._nativeEventCallback
                                self._hookManager.HookKeyboard()
                                self._keyboardHooked=True
    
                            #iohub.log("WindowsHook PumpEvents Periodic Timer Created.")
                
                        def _poll(self):
                            import pythoncom
                            # PumpWaitingMessages returns 1 if a WM_QUIT message was received, else 0
                            if pythoncom.PumpWaitingMessages() == 1:
                                raise KeyboardInterrupt()               
        
                    #print2err("Creating pyHook Monitor......")
                    self._hookDevice=pyHookDevice()
                    hookMonitor=DeviceMonitor(self._hookDevice,0.00375)
                    self.deviceMonitors.append(hookMonitor)
                
                    #print2err("Created pyHook Monitor.")
                else:
                    #print2err("UPDATING pyHook Monitor....")
                    if device_class_name == 'Mouse' and self._hookDevice._mouseHooked is False:
                        #print2err("Hooking Mouse.....")
                        self._hookDevice._hookManager.MouseAll = deviceDict['Mouse']._nativeEventCallback
                        self._hookDevice._hookManager.HookMouse()
                        self._hookDevice._mouseHooked=True
                    elif device_class_name == 'Keyboard' and self._hookDevice._keyboardHooked is False:
                        #print2err("Hooking Keyboard.....")
                        self._hookDevice._hookManager.KeyAll = deviceDict['Keyboard']._nativeEventCallback
                        self._hookDevice._hookManager.HookKeyboard()
                        self._hookDevice._keyboardHooked=True
                
                    #print2err("Finished Updating pyHook Monitor.")
                
            elif Computer.system == 'linux2':
                # TODO: consider switching to xlib-ctypes implementation of xlib
                # https://github.com/garrybodsworth/pyxlib-ctypes
                from .devices import pyXHook
                if self._hookManager is None:
                    #iohub.log("Creating pyXHook Monitors....")
                    self._hookManager = pyXHook.HookManager()
                    self._hookManager._mouseHooked=False
                    self._hookManager._keyboardHooked=False

                    if device_class_name == 'Keyboard':
                        #print2err("Hooking Keyboard.....")
                        self._hookManager.HookKeyboard()
                        self._hookManager.KeyDown = deviceDict['Keyboard']._nativeEventCallback
                        self._hookManager.KeyUp = deviceDict['Keyboard']._nativeEventCallback
                        self._hookManager._keyboardHooked=True
                    elif device_class_name == 'Mouse':                
                        #print2err("Hooking Mouse.....")
                        self._hookManager.HookMouse()
                        self._hookManager.MouseAllButtonsDown = deviceDict['Mouse']._nativeEventCallback
                        self._hookManager.MouseAllButtonsUp = deviceDict['Mouse']._nativeEventCallback
                        self._hookManager.MouseAllMotion = deviceDict['Mouse']._nativeEventCallback
                        self._hookManager._mouseHooked=True
    
                    #print2err("Starting pyXHook.HookManager.....")
                    self._hookManager.start()
                    #iohub.log("pyXHook Thread Created.")
                    #print2err("pyXHook.HookManager thread created.")
                else:
                    #iohub.log("Updating pyXHook Monitor....")
                    if device_class_name == 'Keyboard' and self._hookManager._keyboardHooked is False:
                        #print2err("Hooking Keyboard.....")
                        self._hookManager.HookKeyboard()
                        self._hookManager.KeyDown = deviceDict['Keyboard']._nativeEventCallback
                        self._hookManager.KeyUp = deviceDict['Keyboard']._nativeEventCallback
                        self._hookManager._keyboardHooked=True
                    if device_class_name == 'Mouse' and self._hookManager._mouseHooked is False:                
                        #print2err("Hooking Mouse.....")
                        self._hookManager.HookMouse()
                        self._hookManager.MouseAllButtonsDown = deviceDict['Mouse']._nativeEventCallback
                        self._hookManager.MouseAllButtonsUp = deviceDict['Mouse']._nativeEventCallback
                        self._hookManager.MouseAllMotion = deviceDict['Mouse']._nativeEventCallback
                        self._hookManager._mouseHooked=True
                    #iohub.log("Finished Updating pyXHook Monitor....")
                    

            else: # OSX
                if self._hookDevice is None:
                    self._hookDevice=[]
                    
                if  device_class_name == 'Mouse' and 'Mouse' not in self._hookDevice:
                    #print2err("Hooking OSX Mouse.....")
                    mouseHookMonitor=DeviceMonitor(deviceDict['Mouse'],0.004)
                    self.deviceMonitors.append(mouseHookMonitor)
                    deviceDict['Mouse']._CGEventTapEnable(deviceDict['Mouse']._tap, True)
                    self._hookDevice.append('Mouse')
                    #print2err("Done Hooking OSX Mouse.....")
                if device_class_name == 'Keyboard'  and 'Keyboard' not in self._hookDevice:
                    #print2err("Hooking OSX Keyboard.....")
                    kbHookMonitor=DeviceMonitor(deviceDict['Keyboard'],0.004)
                    self.deviceMonitors.append(kbHookMonitor)
                    deviceDict['Keyboard']._CGEventTapEnable(deviceDict['Keyboard']._tap, True)
                    self._hookDevice.append('Keyboard')
                    #print2err("DONE Hooking OSX Keyboard.....")


            return [device_class_name, device_config['name'], device_instance._getRPCInterface()]
Example #18
0
    def __init__(self, rootScriptPathDir, config=None):
        self._session_id=None
        self._experiment_id=None

        self.log("Server Time Offset: {0}".format(Computer.globalClock.getLastResetTime()))

        self._hookManager=None
        self.emrt_file=None
        self.config=config
        self.devices=[]
        self.deviceMonitors=[]
        self.sessionInfoDict=None
        self.experimentInfoList=None
        self.filterLookupByInput={}
        self.filterLookupByOutput={}
        self.filterLookupByName={}  
        self._hookDevice=None

        import iohub        
        ioServer.eventBuffer=deque(maxlen=config.get('global_event_buffer',2048))

        self._running=True
        
        # start UDP service
        self.udpService=udpServer(self,':%d'%config.get('udp_port',9000))

        # read temp paths file
        iohub.data_paths=None
        try:
            expJsonPath=os.path.join(rootScriptPathDir,'exp.paths')
            f=open(expJsonPath,'r')
            iohub.data_paths=json.loads(f.read())
            f.flush()
            f.close()
            os.remove(expJsonPath)
        except:
            pass


        try:
            # initial dataStore setup
            if 'data_store' in config:
                experiment_datastore_config=config.get('data_store')

                default_datastore_config_path=os.path.join(iohub.IO_HUB_DIRECTORY,'dataStore','default_datastore.yaml')
                _dslabel,default_datastore_config=load(file(default_datastore_config_path,'r'), Loader=Loader).popitem()

                for default_key,default_value in default_datastore_config.iteritems():
                    if default_key not in experiment_datastore_config:
                        experiment_datastore_config[default_key]=default_value
                                
                if experiment_datastore_config.get('enable', True):
                    #print2err("Creating ioDataStore....")

                    if iohub.data_paths is None:
                        resultsFilePath=rootScriptPathDir
                    else:
                        resultsFilePath=iohub.data_paths[u'IOHUB_DATA']
                    self.createDataStoreFile(experiment_datastore_config.get('filename','events')+'.hdf5',resultsFilePath,'a',experiment_datastore_config)

                    #print2err("Created ioDataStore.")
        except:
            print2err("Error during ioDataStore creation....")
            printExceptionDetailsToStdErr()


        #built device list and config from initial yaml config settings
        try:
            for iodevice in config.get('monitor_devices',()):
                for device_class_name,deviceConfig in iodevice.iteritems():
                    #print2err("======================================================")
                    #print2err("Started load process for: {0}".format(device_class_name))
                    self.createNewMonitoredDevice(device_class_name,deviceConfig)
        except:
            print2err("Error during device creation ....")
            printExceptionDetailsToStdErr()
            raise ioHubError("Error during device creation ....")
Example #19
0
                        device=dclass,
                        dmethod=dmethod,
                        args=args,
                        kwargs=kwargs,
                        exception=str(e)), replyTo)
                return False
        elif request_type == 'GET_DEVICE_LIST':
            try:
                dev_list = []
                for d in self.iohub.devices:
                    dev_list.append((d.name, d.__class__.__name__))
                self.sendResponse(
                    ('GET_DEV_LIST_RESULT', len(dev_list), dev_list), replyTo)
                return True
            except Exception, e:
                printExceptionDetailsToStdErr()
                self.sendResponse(
                    createErrorResult(
                        'RPC_DEVICE_RUNTIME_ERROR',
                        msg=
                        "An error occurred on the ioHub Server while getting the Device list for the Experiment Process",
                        devices=str(self.iohub.devices),
                        dev_list=str(dev_list),
                        exception=str(e)), replyTo)
                return False

        elif request_type == 'GET_DEV_INTERFACE':
            dclass = request.pop(0)
            data = None
            if dclass in ['EyeTracker', 'DAQ']:
                for dname, hdevice in ioServer.deviceDict.iteritems():
Example #20
0
                                      device=dclass,
                                      dmethod=dmethod,
                                      args=args,
                                      kwargs=kwargs,
                                      exception=str(e))
                                ,replyTo)
                return False
        elif request_type == 'GET_DEVICE_LIST':
            try:            
                dev_list=[]
                for d in self.iohub.devices:
                    dev_list.append((d.name,d.__class__.__name__))
                self.sendResponse(('GET_DEV_LIST_RESULT',len(dev_list),dev_list),replyTo)
                return True
            except Exception, e:
                printExceptionDetailsToStdErr()
                self.sendResponse(createErrorResult('RPC_DEVICE_RUNTIME_ERROR',
                                      msg="An error occurred on the ioHub Server while getting the Device list for the Experiment Process",
                                      devices=str(self.iohub.devices),
                                      dev_list=str(dev_list),
                                      exception=str(e))
                                ,replyTo)
                return False                

        elif request_type == 'GET_DEV_INTERFACE':
            dclass=request.pop(0)
            data=None
            if dclass in ['EyeTracker','DAQ']:
                for dname, hdevice in ioServer.deviceDict.iteritems():
                    if dname.endswith(dclass):
                        data=hdevice._getRPCInterface()