需求背景介绍:
工厂有10台中控考勤机用于员工考勤,10台机器里面的人员信息,大致一样(只是为了上班时更快的打卡,采用多台考勤机)。但由于工厂工人流动性大,人事文员删除10台指纹信息,工作量大, 所以有了需求,程序控制批量删除。(这里的批量是指机器的同步删除, 中控提供的管理程序一时间只能操作一台。)
解决方案
采用中控提供的sdk, 写程序连接考勤机,进行操作。目前学习的几种语言中,只有python能够完美操作这套sdk。并且,平台需要时windows。
安装sdk操作
在官方下载的sdk包中, 根据自己电脑系统的架构,直接Register即可。
python代码
'''
Date: 2021-06-01 08:31:03
LastEditors: Mok.CH
LastEditTime: 2021-07-26 16:01:25
FilePath: /pyzk/zk.py
'''
import win32com.client
import pythoncom
instances = dict()
def create(deviceNo: int, ip: str, port: int = 4370):
global instances
if instances.get(ip, None) != None:
instance = instances.get(ip)
if instance.checkStatus():
return instance
else:
del instance
instance = ZKTimeDevice(deviceNo, ip, port)
instances[ip] = instance
return instance
class ZKTimeDevice():
'''中控考勤机链接实例
Attributes:
deviceNo: 设备编号
ip: 设备IP地址
port: 设备端口号
'''
def __init__(self, deviceNo: int, ip: str, port: int = 4370) -> None:
self.deviceNo = deviceNo
self.ip = ip
self.port = port
pythoncom.CoInitialize()
self._zk = win32com.client.Dispatch('zkemkeeper.ZKEM.1')
if not self._zk.Connect_Net(ip, port):
raise Exception('cannot connect to device')
pass
def __del__(self):
self._zk.Disconnect()
def getAllUserInfo(self) -> list:
'''get all user info
获取所有用户基础信息
'''
userData = list()
self._zk.ReadAllUserID(self.deviceNo)
while True:
hasData, enRollNum, userName, pwd, privilege, enable = self._zk.SSR_GetAllUserInfo(
self.deviceNo)
if not hasData:
break
userData.append({
'enRollNum': enRollNum,
'userName': userName,
'password': pwd,
'privilege': privilege,
'enable': enable,
'deviceId': self.deviceNo
})
return userData
def deleteUser(self, enRollNum: str) -> bool:
'''delete user
根据考勤号, 删除用户所有信息, 包括指纹、面容识别、基本信息
Attributes:
enRollNum: 考勤号
'''
return self._zk.SSR_DeleteEnrollDataExt(self.deviceNo, enRollNum, 12)
def enableUser(self, enRollNum: str, state: bool) -> bool:
return self._zk.SSR_EnableUser(self.deviceNo, enRollNum, state)
def checkStatus(self) -> bool:
res, _ = self._zk.GetDeviceStatus(self.deviceNo, 8)
return res
pass
pass
def getUserTmpStr(self, enRollNum):
for i in [6, 1, 7, 8, 9, 0, 2, 3, 4, 5]:
res = self._zk.SSR_GetUserTmpStr(
self.deviceNo, enRollNum, i)
if res[0]:
break
return res
pass
使用
以上功能需要集成到现有的系统中(web方式的系统)。所以需要做成web api 接口的方式供系统调用。以下使用flask编写,只提供获取人员的例子。
from flask import Flask, jsonify, request
from flask_cors import CORS
import zk
app = Flask(__name__)
cors = CORS(app)
@app.route('/users', methods=['POST'])
def get_users():
if request.is_json:
req_data = request.get_json(silent=True)
else:
req_data = request.values
deviceNo = req_data.get('deviceNo')
ip = req_data.get('ip')
port = req_data.get('port')
if deviceNo == None or ip == None or port == None:
return jsonify({'status': 'faild', 'msg': 'param required'})
device = zk.create(deviceNo, ip, int(port))
data = device.getAllUserInfo()
return jsonify(data)