因为公司觉得中控考勤机的数据管理软件不好用,于是决定自己做一个.
一,首先需要下载中控机的API JAR包,我这里是下载好后放在项目里直接MAVEVN引用的.具体方法可以见我之前的博客.
jacob
jacob
3.0.1
system
${project.basedir}/libs/jacob.jar
二.打通中控考勤机的代码
工具类
package com.cosun.cosunp.tool;
import com.cosun.cosunp.entity.ZhongKongBean;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @author:homey Wong
* @Date: 2019/10/7 上午 11:20
* @Description:
* @Modified By:
* @Modified-date:
*/
public class ZkemSDKUtils {
private static ActiveXComponent zkem = new ActiveXComponent("zkemkeeper.ZKEM.1");
public static boolean connect(String address, int port) {
boolean result = zkem.invoke("Connect_NET", address, port).getBoolean();
return result;
}
public static boolean readGeneralLogData() {
boolean result = zkem.invoke("ReadGeneralLogData", 1).getBoolean();
return result;
}
public static boolean readLastestLogData(Date lastest) {
boolean result = zkem.invoke("ReadLastestLogData", 2018 - 07 - 24).getBoolean();
return result;
}
public static List getGeneralLogData(String fromDate, String endDate) {
Variant dwMachineNumber = new Variant(1, true);//机器号
Variant dwEnrollNumber = new Variant("", true);
Variant dwVerifyMode = new Variant(0, true);
Variant dwInOutMode = new Variant(0, true);
Variant dwYear = new Variant(0, true);
Variant dwMonth = new Variant(0, true);
Variant dwDay = new Variant(0, true);
Variant dwHour = new Variant(0, true);
Variant dwMinute = new Variant(0, true);
Variant dwSecond = new Variant(0, true);
Variant dwWorkCode = new Variant(0, true);
List strList = new ArrayList();
boolean newresult = false;
try {
do {
Variant vResult = Dispatch.call(zkem, "SSR_GetGeneralLogData", dwMachineNumber, dwEnrollNumber, dwVerifyMode, dwInOutMode, dwYear, dwMonth, dwDay, dwHour, dwMinute, dwSecond, dwWorkCode);
newresult = vResult.getBoolean();
if (newresult) {
String enrollNumber = dwEnrollNumber.getStringRef();
//如果没有编号,则跳过。
if (enrollNumber == null || enrollNumber.trim().length() == 0)
continue;
String month = dwMonth.getIntRef() + "";
String day = dwDay.getIntRef() + "";
if (dwMonth.getIntRef() < 10) {
month = "0" + dwMonth.getIntRef();
}
if (dwDay.getIntRef() < 10) {
day = "0" + dwDay.getIntRef();
}
String validDate = dwYear.getIntRef() + "-" + month + "-" + day;
SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
Date fromDatedateTime = sm.parse(fromDate);
Date endDatedateTime = sm.parse(endDate);
Date valiDateDate = sm.parse(validDate);
if ((endDatedateTime.after(valiDateDate) && fromDatedateTime.before(valiDateDate))) {
ZhongKongBean m = new ZhongKongBean();
m.setEnrollNumber(Integer.valueOf(enrollNumber));
m.setTime(dwYear.getIntRef() + "-" + dwMonth.getIntRef() + "-" + dwDay.getIntRef() + " " + dwHour.getIntRef() + ":" + dwMinute.getIntRef() + ":" + dwSecond.getIntRef());
m.setDateStr(dwYear.getIntRef() + "-" + dwMonth.getIntRef() + "-" + dwDay.getIntRef());
m.setTimeStr(dwHour.getIntRef() + ":" + dwMinute.getIntRef() + ":" + dwSecond.getIntRef());
m.setVerifyMode(dwVerifyMode.getIntRef());
m.setInOutMode(dwInOutMode.getIntRef());
m.setYear(dwYear.getIntRef());
m.setMonth(dwMonth.getIntRef() + "");
m.setDay(dwDay.getIntRef());
m.setHour(dwHour.getIntRef());
m.setMinute(dwMinute.getIntRef());
m.setSecond(dwSecond.getIntRef());
strList.add(m);
}
}
} while (newresult == true);
} catch (Exception e) {
e.printStackTrace();
}
return strList;
}
public static List getGeneralLogData(String beforDay, Integer num) {
Variant dwMachineNumber = new Variant(1, true);//机器号
Variant dwEnrollNumber = new Variant("", true);
Variant dwVerifyMode = new Variant(0, true);
Variant dwInOutMode = new Variant(0, true);
Variant dwYear = new Variant(0, true);
Variant dwMonth = new Variant(0, true);
Variant dwDay = new Variant(0, true);
Variant dwHour = new Variant(0, true);
Variant dwMinute = new Variant(0, true);
Variant dwSecond = new Variant(0, true);
Variant dwWorkCode = new Variant(0, true);
List strList = new ArrayList();
boolean newresult = false;
try {
do {
Variant vResult = Dispatch.call(zkem, "SSR_GetGeneralLogData", dwMachineNumber, dwEnrollNumber, dwVerifyMode, dwInOutMode, dwYear, dwMonth, dwDay, dwHour, dwMinute, dwSecond, dwWorkCode);
newresult = vResult.getBoolean();
if (newresult) {
String enrollNumber = dwEnrollNumber.getStringRef();
//如果没有编号,则跳过。
if (enrollNumber == null || enrollNumber.trim().length() == 0)
continue;
String month = dwMonth.getIntRef() + "";
String day = dwDay.getIntRef() + "";
if (dwMonth.getIntRef() < 10) {
month = "0" + dwMonth.getIntRef();
}
if (dwDay.getIntRef() < 10) {
day = "0" + dwDay.getIntRef();
}
String validDate = dwYear.getIntRef() + "-" + month + "-" + day;
SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat ti = new SimpleDateFormat("HH:mm:ss");
Date endDatedateTime = sm.parse(beforDay);
Date valiDateDate = sm.parse(validDate);
if (valiDateDate.equals(endDatedateTime)) {
ZhongKongBean m = new ZhongKongBean();
m.setEnrollNumber(Integer.valueOf(enrollNumber));
m.setTime(dwYear.getIntRef() + "-" + dwMonth.getIntRef() + "-" + dwDay.getIntRef() + " " + dwHour.getIntRef() + ":" + dwMinute.getIntRef() + ":" + dwSecond.getIntRef());
m.setDateStr(dwYear.getIntRef() + "-" + dwMonth.getIntRef() + "-" + dwDay.getIntRef());
m.setTimeStr(dwHour.getIntRef() + ":" + dwMinute.getIntRef() + ":" + dwSecond.getIntRef());
m.setTimeTime(ti.parse(dwHour.getIntRef() + ":" + dwMinute.getIntRef() + ":" + dwSecond.getIntRef()));
m.setYearMonth(dwYear.getIntRef() + "-" + dwMonth.getIntRef());
m.setVerifyMode(dwVerifyMode.getIntRef());
m.setInOutMode(dwInOutMode.getIntRef());
m.setYear(dwYear.getIntRef());
m.setMonth(dwMonth.getIntRef() + "");
m.setDay(dwDay.getIntRef());
m.setHour(dwHour.getIntRef());
m.setMinute(dwMinute.getIntRef());
m.setSecond(dwSecond.getIntRef());
m.setMachineNum(num);
strList.add(m);
}
}
} while (newresult == true);
} catch (Exception e) {
e.printStackTrace();
}
return strList;
}
public static List
从中控机上拿数据代码
public void getBeforeDayZhongKongData() throws Exception {
//String beforDay = DateUtil.getBeforeDay();
String beforDay = "2019-11-08";
String[] afterDay = beforDay.split("-");
Map map = new HashMap();
boolean connFlag = ZkemSDKUtils.connect("192.168.2.12", 4370);
// 1办公室 0.202 2 2号厂房钣金部 2.10 3 3号厂房门口 2.11 4.3号厂房精工部 2.12
List strList = new ArrayList();
if (connFlag) {
boolean flag = ZkemSDKUtils.readGeneralLogData();
strList.addAll(ZkemSDKUtils.getGeneralLogData(beforDay, 4));
}
boolean connFlag1 = ZkemSDKUtils.connect("192.168.2.10", 4370);
if (connFlag1) {
boolean flag = ZkemSDKUtils.readGeneralLogData();
strList.addAll(ZkemSDKUtils.getGeneralLogData(beforDay, 2));
}
boolean connFlag2 = ZkemSDKUtils.connect("192.168.2.11", 4370);
if (connFlag2) {
boolean flag = ZkemSDKUtils.readGeneralLogData();
strList.addAll(ZkemSDKUtils.getGeneralLogData(beforDay, 3));
}
boolean connFlag3 = ZkemSDKUtils.connect("192.168.0.202", 4370);
if (connFlag3) {
boolean flag = ZkemSDKUtils.readGeneralLogData();
strList.addAll(ZkemSDKUtils.getGeneralLogData(beforDay, 1));
}
//后续可以对集合进行加工并保存在数据库,这代码我就不贴上了
}