def live_capture(self, new_timeout=10):
        """
        try live capture of events
        """
        was_enabled = self.is_enabled
        users = self.get_users()
        self.cancel_capture()
        self.verify_user()
        if not self.is_enabled:
            self.enable_device()
        if self.verbose: print ("start live_capture")
        self.reg_event(const.EF_ATTLOG)
        self.__sock.settimeout(new_timeout)
        self.end_live_capture = False
        while not self.end_live_capture:
            try:
                if self.verbose: print ("esperando event")
                data_recv = self.__sock.recv(1032)
                self.__ack_ok()
                if self.tcp:
                    size = unpack('<HHI', data_recv[:8])[2]
                    header = unpack('HHHH', data_recv[8:16])
                    data = data_recv[16:]
                else:
                    size = len(data_recv)
                    header = unpack('<4H', data_recv[:8])
                    data = data_recv[8:]
                if not header[0] == const.CMD_REG_EVENT:
                    if self.verbose: print("not event! %x" % header[0])
                    continue
                if not len(data):
                    if self.verbose: print ("empty")
                    continue
                while len(data) >= 12:
                    if len(data) == 12:
                        user_id, status, punch, timehex = unpack('<IBB6s', data)
                        data = data[12:]
                    elif len(data) == 32:
                        user_id,  status, punch, timehex = unpack('<24sBB6s', data[:32])
                        data = data[32:]
                    elif len(data) == 36:
                        user_id,  status, punch, timehex, _other = unpack('<24sBB6s4s', data[:36])
                        data = data[36:]
                    elif len(data) >= 52:
                        user_id,  status, punch, timehex, _other = unpack('<24sBB6s20s', data[:52])
                        data = data[52:]
                    if isinstance(user_id, int):
                        user_id = str(user_id)
                    else:
                        user_id = (user_id.split(b'\x00')[0]).decode(errors='ignore')
                    timestamp = self.__decode_timehex(timehex)
                    tuser = list(filter(lambda x: x.user_id == user_id, users))
                    if not tuser:
                        uid = int(user_id)
                    else:
                        uid = tuser[0].uid
                    yield Attendance(user_id, timestamp, status, punch, uid)
            except timeout:
                if self.verbose: print ("time out")
                yield None # return to keep watching
            except (KeyboardInterrupt, SystemExit):
                if self.verbose: print ("break")
                break
        if self.verbose: print ("exit gracefully")
        self.__sock.settimeout(self.__timeout)
        self.reg_event(0)
        if not was_enabled:
            self.disable_device()
def live_capture ( self , new_timeout = 10 ) : was_enabled = self . is_enabled users = self . get_users ( ) self . cancel_capture ( ) self . verify_user ( ) if not self . is_enabled : self . enable_device ( ) if self . verbose : print ( "start live_capture" ) self . reg_event ( const . EF_ATTLOG ) self . __sock . settimeout ( new_timeout ) self . end_live_capture = False while not self . end_live_capture : try : if self . verbose : print ( "esperando event" ) data_recv = self . __sock . recv ( 1032 ) self . __ack_ok ( ) if self . tcp : size = unpack ( '<HHI' , data_recv [ : 8 ] ) [ 2 ] header = unpack ( 'HHHH' , data_recv [ 8 : 16 ] ) data = data_recv [ 16 : ] else : size = len ( data_recv ) header = unpack ( '<4H' , data_recv [ : 8 ] ) data = data_recv [ 8 : ] if not header [ 0 ] == const . CMD_REG_EVENT : if self . verbose : print ( "not event! %x" % header [ 0 ] ) continue if not len ( data ) : if self . verbose : print ( "empty" ) continue while len ( data ) >= 12 : if len ( data ) == 12 : user_id , status , punch , timehex = unpack ( '<IBB6s' , data ) data = data [ 12 : ] elif len ( data ) == 32 : user_id , status , punch , timehex = unpack ( '<24sBB6s' , data [ : 32 ] ) data = data [ 32 : ] elif len ( data ) == 36 : user_id , status , punch , timehex , _other = unpack ( '<24sBB6s4s' , data [ : 36 ] ) data = data [ 36 : ] elif len ( data ) >= 52 : user_id , status , punch , timehex , _other = unpack ( '<24sBB6s20s' , data [ : 52 ] ) data = data [ 52 : ] if isinstance ( user_id , int ) : user_id = str ( user_id ) else : user_id = ( user_id . split ( b'\x00' ) [ 0 ] ) . decode ( errors = 'ignore' ) timestamp = self . __decode_timehex ( timehex ) tuser = list ( filter ( lambda x : x . user_id == user_id , users ) ) if not tuser : uid = int ( user_id ) else : uid = tuser [ 0 ] . uid yield Attendance ( user_id , timestamp , status , punch , uid ) except timeout : if self . verbose : print ( "time out" ) yield None except ( KeyboardInterrupt , SystemExit ) : if self . verbose : print ( "break" ) break if self . verbose : print ( "exit gracefully" ) self . __sock . settimeout ( self . __timeout ) self . reg_event ( 0 ) if not was_enabled : self . disable_device ( )
try live capture of events