def clear_attendance(self): for info in self: try: machine_ip = info.name zk_port = info.port_no timeout = 30 try: zk = ZK(machine_ip, port=zk_port, timeout=timeout, password=0, force_udp=False, ommit_ping=True) except NameError: raise UserError(_("Please install it with 'pip3 install pyzk'.")) conn = self.device_connect(zk) if conn: conn.enable_device() clear_data = zk.get_attendance() if clear_data: # conn.clear_attendance() self._cr.execute("""delete from zk_machine_attendance""") conn.disconnect() raise UserError(_('Attendance Records Deleted.')) else: raise UserError(_('Unable to clear Attendance log. Are you sure attendance log is not empty.')) else: raise UserError( _('Unable to connect to Attendance Device. Please use Test Connection button to verify.')) except: raise ValidationError( 'Unable to clear Attendance log. Are you sure attendance device is connected & record is not empty.')
class ZKUtil(object): def __init__(self, logger, ip, port=4370): self.log = logger self.sep = ',' self.zk_sttg = None self.zk_ip = ip self.zk_port = port self.zk_conn = None self.timeout = 5 self.passwd = 0 self.fc_udp = False self.om_png = False def _zk_connect(self): ret = 1 try: self.zk_conn = ZK(self.zk_ip, self.zk_port, self.timeout, self.passwd, self.fc_udp, self.om_png) self.zk_conn = self.zk_conn.connect() self.log.info(f" connected to machine, ip {self.zk_ip}") ret = 0 except Exception as e: self.log.error( f"Except error to connect -> {self.zk_ip}, detail -> {e}") self.log.error(f"Except zk error, {sys.exc_info()}") finally: return ret def _zk_is_connected(self): try: if self.zk_conn: return True else: self.log.error(f"zk conn is {self.zk_conn}") return False except Exception as e: self.log.info(f"Except -> {e} ") self.log.error(f" Except {sys.exc_info()}") def _zk_disconnect(self): ret = 1 try: if self._zk_is_connected(): self.zk_conn.disconnect() self.log.info( f" disconnect from fingerprint, ip => {self.zk_ip}") ret = 0 except Exception as e: self.log.info(f"Except -> {e} ") self.log.error(f" Except {sys.exc_info()}") finally: return ret def _zk_firmware(self): ret = 1 try: if self._zk_is_connected(): ret = self.zk_conn.get_firmware_version() except Exception as e: self.log.error(f" Except error , detail -> {e}") self.log.error(f" Except {sys.exc_info()}") finally: return ret def _zk_live_attendance(self): ret = 1 try: if self._zk_is_connected(): ret = self.zk_conn.get_firmware_version() except Exception as e: self.log.error(f" Except error , detail -> {e}") self.log.error(f" Except {sys.exc_info()}") finally: return ret def _zk_get_attendance(self): ret = [] tmp = { '1': "47714351", '2': "46373211", '4': "47598830", '5': "45448470", '6': "46027680" } try: if self._zk_is_connected(): ret = self.zk_conn.get_attendance() if len(ret) == 0: self.log.error(f"attendance is len = 0, {attendance}") except Exception as e: self.log.error(f" Except error , detail -> {e}") self.log.error(f" Except {sys.exc_info()}") finally: return ret def _zk_clear_attendance(self): ret = 1 try: if self._zk_is_connected(): self.zk_conn.clear_attendance() ret = 0 except Exception as e: self.log.error(f"Except error, detail -> {e}") self.log.error(f"Except {sys.exc_info()}") finally: return ret def _zk_reboot_machine(self): ret = 1 try: if self._zk_is_connected(): self.zk_conn.restart() ret = 0 except Exception as e: self.log.error(f" Except error , detail -> {e}") self.log.error(f" Except {sys.exc_info()}") finally: return ret
zk = zklib.ZKLib("192.168.1.201", 4370) zkt = ZK('192.168.1.201', port=4370, timeout=5) zkt.connect() conZkt = zkt.is_connect # Pulling data while True: statusConnect = zk.connect() if statusConnect: print("Connected to device") else: print("No connected to devive") print("Pulling") if statusConnect: users = zkt.get_users() attendances = zkt.get_attendance() tempDate = zk.getTime().date() zkt.get_serialnumber() for us in users: for att in attendances: if us.user_id == att.user_id: dataTime = ({ "iduser": format(us.user_id), "name": us.name, "date": format(att.timestamp.date()), "time": format(att.timestamp.time()), "method_swipe": format(att.status) }) cur.execute( "INSERT INTO datatable (iduser,name,date,time,method_swipe) VALUES (%(iduser)s, %(name)s, %(date)s, %(time)s, %(method_swipe)s)", dataTime)