Esempio n. 1
0
 def __init__(self):
     '''
     '''
     log.info('Initialized!')
     self.conf = ConfSaver("." + self.__class__.__name__)
     self.running = False
     self.data_catcher = MovCatcher()
     self.data_filter = DataFilter()
     self.__thread__ = threading.Thread(target = self.run)
     self.last_raw_data = None
     self.__exit = False
     self.__init_values__()
     self.stats = ['STOP', 'FINDED','CONFIGURED','CALIBRATED']
     self.actual_stat = 'STOP'
     self.actions = ['FIND', 'CONFIGURE', 'CALIBRATE']
Esempio n. 2
0
class Application():
    __default={'TIME_BETWEEN_SAMPLES': 0.1 #Time between samples in seconds
               }
    
    def __init__(self):
        '''
        '''
        log.info('Initialized!')
        self.conf = ConfSaver("." + self.__class__.__name__)
        self.running = False
        self.data_catcher = MovCatcher()
        self.data_filter = DataFilter()
        self.__thread__ = threading.Thread(target = self.run)
        self.last_raw_data = None
        self.__exit = False
        self.__init_values__()
        self.stats = ['STOP', 'FINDED','CONFIGURED','CALIBRATED']
        self.actual_stat = 'STOP'
        self.actions = ['FIND', 'CONFIGURE', 'CALIBRATE']
    
    def exec_action(self, action):
        '''
        State Machine
        '''
        if action == INIT:
            self.__newstate__(INIT)
        elif action == SEARCH:
            self.__newstate__(SEARCHING)
            self.__setup_devices__()
            data = self.data_catcher.get_devices_state()
            if len(data):
                self.__newstate__(FOUND)
            else:
                self.exec_action(INIT)
        
    def __init_values__(self):
        '''
        '''
        self.time_between_samples = self.conf.init_set("time_between_samples", Application.__default['TIME_BETWEEN_SAMPLES'])
    
    def system_calibrate(self):
        '''
        '''
        
        self.show_info(messages['CALIBRATION'])
        answer = self.make_question("Are you OK?")
        
        print answer
    
    def system_part_calibrate(self):
        pass
    
    def __exec_filter__(state):
        '''
        '''
        def function(function_wrapped):
            def function2(self,*args,**kwargs):
                if  self.stats.index(self.actual_stat) not in state:
                    return function_wrapped(self,*args,**kwargs)
                else:
                    msg = "Not Valid instruction in actual State: %s" % str(self.actual_stat)
                    log.error(msg)
                    raise Exception(msg)
            return function2
        return function
    
    def __setup_devices__(self):
        '''
        Description:
        
        Arguments:
        
        Return:
        
        Exceptions
        '''
        self.data_catcher.find_devices()
        self.data_catcher.connect_all_devices()
        self.catchers_info = self.data_catcher.get_devices_state()
        for keys, value in self.catchers_info.items():
            if value['mode'] == ACCELERATION:
                self.data_filter.add_accelerometer_controller(keys)
            if value['mode'] == IR:
                self.data_filter.add_movement_controller(keys)
            if value['mode'] == GYRO:
                self.data_filter.add_gyroscope_controller(keys)
    
    @__exec_filter__(['FINDED','CONFIGURED','CALIBRATED'])
    def list_devices(self):
        return self.data_catcher
    
    @__exec_filter__(['FINDED'])
    def set_report_mode(self,index,report,state):
        self.data_catcher.is_found(index)
        self.data_catcher.set_report_mode(index,report,state)
    
    @__exec_filter__(['FINDED'])
    def disconnect_device(self):
        pass
    
    @__exec_filter__(['FINDED','CONFIGURED','CALIBRATED'])
    def get_device_status(self):
        return self.data_catcher.get_devices_state()
    
    def get_last_raw_data(self):
        return self.last_raw_data
    
    @__exec_filter__(['STOP'])
    def set_time_between_samples(self,time):
        self.conf.setval('time_between_samples',time)
        self.time_between_samples = time
        
    def get_time_between_samples(self):
        return self.time_between_samples
    
    def start(self):
        self.__thread__.start()
        
    def stop(self):
        if self.running:
            self.__exit = True
            self.__thread__.join()
            self.__thread__ = threading.Thread(target = self.run)
            self.running = False
            
    def run(self):
        self.running = True
        while not self.__exit:
            self.last_raw_data = self.read_one_value()
            #self.data_filter.analize_data(self.last_raw_data)
            self.__represent_raw_value()
            time.sleep(self.time_between_samples)
            print self.last_raw_data
    
    def read_one_value(self):
        return self.data_catcher.get_alldata()
        
    def exit_app(self):
        if self.running:
            self.__exit = True
            self.__thread__.join()
        self.data_catcher.disconnect_all_devices()

    def __represent_raw_value(self, rawdata):
        '''
        This method should be overloaded to show raw data information
        '''
        print rawdata
    
    def __newstate__(self, state):
        '''
        Overload this method to warn we are in another state
        '''
        print "New State: ", state