package com.yzx.impl; import com.netsdk.common.Res; import com.netsdk.demo.frame.Gate.CardManegerDialog; import com.netsdk.demo.module.DeviceLogModule; import com.netsdk.demo.module.GateModule; import com.netsdk.demo.module.LoginModule; import com.netsdk.demo.module.TalkModule; import com.netsdk.lib.NetSDKLib; import com.netsdk.lib.ToolKits; import com.netsdk.lib.structure.DH_DEVICE_LOG_ITEM_EX; import com.netsdk.lib.structure.NET_TIME; import com.sun.jna.Memory; import com.sun.jna.Pointer; import com.yzx.IHardwareEquipment; import com.yzx.pojo.FaceRecrodInfo; import com.yzx.pojo.UserFaceInfo; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.*; public class DaHua implements IHardwareEquipment { // 设备断线通知回调 private static DisConnect disConnect = new DisConnect(); // 网络连接恢复 private static HaveReConnect haveReConnect = new HaveReConnect(); // 订阅句柄 public static NetSDKLib.LLong m_hAttachHandle = new NetSDKLib.LLong(0); private AnalyzerDataCB analyzerCallback = new AnalyzerDataCB(); private HardwareEquipmentInfo info; static{ //初始化 LoginModule.init(disConnect, haveReConnect); // 打开工程,初始化 } public DaHua(HardwareEquipmentInfo info){ this.info = info; } public boolean openDoor(){ this.login(info.getIp(),info.getPort(),info.getUsername(),info.getPassword()); NetSDKLib.NET_CTRL_ACCESS_OPEN openInfo = new NetSDKLib.NET_CTRL_ACCESS_OPEN(); openInfo.nChannelID = 0; openInfo.emOpenDoorType = NetSDKLib.EM_OPEN_DOOR_TYPE.EM_OPEN_DOOR_TYPE_REMOTE; Pointer pointer = new Memory(openInfo.size()); ToolKits.SetStructDataToPointer(openInfo, pointer, 0); boolean ret = LoginModule.netsdk.CLIENT_ControlDeviceEx(LoginModule.m_hLoginHandle, NetSDKLib.CtrlType.CTRLTYPE_CTRL_ACCESS_OPEN, pointer, null, 10000); if (!ret) { return false; } this.logout(); return true; } /** * 获取系统日志 * @param startTime 开始时间格式 yyyy/MM/dd/hh/mm/ss * @param endTime 结束时间格式 yyyy/MM/dd/hh/mm/ss * @return */ public boolean getSystemLog(String startTime,String endTime){ this.login(info.getIp(),info.getPort(),info.getUsername(),info.getPassword()); NET_TIME netTimeStart = new NET_TIME(startTime); NET_TIME netTimeEnd = new NET_TIME(endTime); DH_DEVICE_LOG_ITEM_EX[] systemLog = DeviceLogModule.getSystemLog(netTimeStart, netTimeEnd); //退出登录 this.logout(); return true; } /** * 获取开门记录 * @param time * @return */ public List getAllRecord(NetSDKLib.NET_TIME time){ List faceRecrodInfoList = new ArrayList<>(); this.login(info.getIp(),info.getPort(),info.getUsername(),info.getPassword()); int count = 0; int index = 0; int nFindCount = 100; // 日期: 为空,查询所有开门信息 // 获取查询句柄 if(!GateModule.findRecord(time)) { return faceRecrodInfoList; } // 查询具体信息 while(true) { NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC[] pstRecord = GateModule.findNextRecord(nFindCount); if(pstRecord == null) { break; } for(int i = 0; i < pstRecord.length; i++) { index = i + count * nFindCount; try { FaceRecrodInfo faceRecrodInfo = new FaceRecrodInfo(); faceRecrodInfo.setIndex(index); faceRecrodInfo.setRecordNo(pstRecord[i].nRecNo); faceRecrodInfo.setCardNo(new String(pstRecord[i].szCardNo)); faceRecrodInfo.setEmployeeNo(new String(pstRecord[i].szUserID).trim()); faceRecrodInfo.setEmployeeName(new String(pstRecord[i].szCardName, "GBK").trim()); faceRecrodInfo.setTime(pstRecord[i].stuTime.toStringTimeEx()); faceRecrodInfo.setSnapUrl(new String(pstRecord[i].szSnapFtpUrl).trim()); faceRecrodInfoList.add(faceRecrodInfo); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } if (pstRecord.length < nFindCount) { break; } else { count ++; } } // 关闭查询接口 GateModule.findRecordClose(); //退出登录 this.logout(); return faceRecrodInfoList; } /** * 添加或修改用户 * @return */ public boolean addOrEditPersion(UserFaceInfo userFaceInfo){ this.login(info.getIp(),info.getPort(),info.getUsername(),info.getPassword()); //查询用户 String employeeNo = userFaceInfo.getEmployeeNo(); int recordNo = this.findRecordNo(employeeNo); if(recordNo != 0){//编辑 if(this.updateUserCard(userFaceInfo) && this.updateUserFace(employeeNo,userFaceInfo.getImageBytes())){ return true; } }else{//添加 if(this.addUserCard(userFaceInfo) && this.updateUserFace(employeeNo,userFaceInfo.getImageBytes())){ return true; } } this.logout(); return false; } public boolean deletePerson(UserFaceInfo userFaceInfo){ this.login(info.getIp(),info.getPort(),info.getUsername(),info.getPassword()); //查询用户 String employeeNo = userFaceInfo.getEmployeeNo(); int recordNo = this.findRecordNo(employeeNo); if(recordNo != 0){ if(GateModule.deleteCard(recordNo) && GateModule.deleteFaceInfo(employeeNo)){ return true; } } this.logout(); return false; } /** * 添加人脸 * @param userId * @param imageBytes * @return */ private boolean addUserFace(String userId,byte[] imageBytes){ Memory memory = ToolKits.readPictureBytes(imageBytes); boolean b = GateModule.addFaceInfo(userId, memory); return b; } /** * 更新人脸 * @param userId * @param imageBytes * @return */ private boolean updateUserFace(String userId,byte[] imageBytes){ Memory memory = ToolKits.readPictureBytes(imageBytes); boolean b = GateModule.modifyFaceInfo(userId, memory); return b; } /** * 添加用户卡 * @param userFaceInfo * @return */ private boolean addUserCard(UserFaceInfo userFaceInfo){ String employeeNo = userFaceInfo.getEmployeeNo(); String employeeName = userFaceInfo.getEmployeeName(); String startTime = userFaceInfo.getStartTime(); String endTime = userFaceInfo.getEndTime(); String cardNo = userFaceInfo.getCardNo(); this.login(info.getIp(),info.getPort(),info.getUsername(),info.getPassword()); boolean b = GateModule.insertCard(cardNo, employeeNo, employeeName, "", 0, 0, 0, 0, 1, startTime, endTime); return b; } /** * 更新用户卡 * @param userFaceInfo * @return */ private boolean updateUserCard(UserFaceInfo userFaceInfo){ String employeeNo = userFaceInfo.getEmployeeNo(); String employeeName = userFaceInfo.getEmployeeName(); String startTime = userFaceInfo.getStartTime(); String endTime = userFaceInfo.getEndTime(); String cardNo = userFaceInfo.getCardNo(); this.login(info.getIp(),info.getPort(),info.getUsername(),info.getPassword()); //根据userId 查询记录集编号 int recordNo = this.findRecordNo(employeeNo); boolean b = GateModule.modifyCard(recordNo,cardNo, employeeNo, employeeName, "", 0, 0, 0, 0, 1, startTime, endTime); return b; } private int findRecordNo(String employeeNo) { int nFindCount = 1; // 卡号: 为空,查询所有的卡信息 // 获取查询句柄 if(!GateModule.findCard("",employeeNo)) { return 0; } // 查询具体信息 while(true) { NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARD[] pstRecord = GateModule.findNextCard(nFindCount); if(pstRecord == null) { break; } Optional any = Arrays.stream(pstRecord).findAny(); if(any.isPresent()){ return any.get().nRecNo; } if (pstRecord.length < nFindCount) { break; } } // 关闭查询接口 GateModule.findCardClose(); return 0; } public List findAllCard() { this.login(info.getIp(),info.getPort(),info.getUsername(),info.getPassword()); List userFaceInfoList = new ArrayList<>(); int nFindCount = 1; // 卡号: 为空,查询所有的卡信息 // 获取查询句柄 if(!GateModule.findCard("","")) { return userFaceInfoList; } // 查询具体信息 while(true) { NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARD[] pstRecord = GateModule.findNextCard(nFindCount); if(pstRecord == null) { break; } for(int i = 0; i < pstRecord.length; i++) { UserFaceInfo userFaceInfo = new UserFaceInfo(); try { userFaceInfo.setCardNo(new String(pstRecord[i].szCardNo).trim()); userFaceInfo.setEmployeeName(new String(pstRecord[i].szCardName, "GBK").trim()); userFaceInfo.setEmployeeNo(new String(pstRecord[i].szUserID).trim()); userFaceInfo.setStartTime(pstRecord[i].stuValidStartTime.toStringTimeEx()); userFaceInfo.setEndTime(pstRecord[i].stuValidEndTime.toStringTimeEx()); userFaceInfoList.add(userFaceInfo); }catch(UnsupportedEncodingException e){ e.printStackTrace(); } } if (pstRecord.length < nFindCount) { break; } } // 关闭查询接口 GateModule.findCardClose(); this.logout(); return userFaceInfoList; } public boolean clearAllPerson(){ this.login(info.getIp(),info.getPort(),info.getUsername(),info.getPassword()); if(GateModule.clearCard() && GateModule.clearFaceInfo()){ return true; } this.logout(); return false; } // 登录 public boolean login(String ip,short port,String username,String password) { if(LoginModule.login(ip, port, username, password)) { // 登陆成功,将通道添加到控件 } else { return false; } return true; } // 登出 public void logout() { LoginModule.logout(); } private static class DisConnect implements NetSDKLib.fDisConnect { public void invoke(NetSDKLib.LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) { System.out.printf("Device[%s] Port[%d] DisConnect!\n", pchDVRIP, nDVRPort); // 断线提示 System.out.println(Res.string().getGate() + " : " + Res.string().getDisConnectReconnecting()); } } // 网络连接恢复,设备重连成功回调 // 通过 CLIENT_SetAutoReconnect 设置该回调函数,当已断线的设备重连成功时,SDK会调用该函数 private static class HaveReConnect implements NetSDKLib.fHaveReConnect { public void invoke(NetSDKLib.LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) { System.out.printf("ReConnect Device[%s] Port[%d]\n", pchDVRIP, nDVRPort); // 重连提示 System.out.println(Res.string().getGate() + " : " + Res.string().getOnline()); } } //消息订阅 private class AnalyzerDataCB implements NetSDKLib.fAnalyzerDataCallBack { private BufferedImage gateBufferedImage = null; public int invoke(NetSDKLib.LLong lAnalyzerHandle, int dwAlarmType, Pointer pAlarmInfo, Pointer pBuffer, int dwBufSize, Pointer dwUser, int nSequence, Pointer reserved) { if (lAnalyzerHandle.longValue() == 0 || pAlarmInfo == null) { return -1; } File path = new File("./GateSnapPicture/"); if (!path.exists()) { path.mkdir(); } ///< 门禁事件 if(dwAlarmType == NetSDKLib.EVENT_IVS_ACCESS_CTL) { NetSDKLib.DEV_EVENT_ACCESS_CTL_INFO msg = new NetSDKLib.DEV_EVENT_ACCESS_CTL_INFO(); ToolKits.GetPointerData(pAlarmInfo, msg); // 保存图片,获取图片缓存 String snapPicPath = path + "\\" + System.currentTimeMillis() + "GateSnapPicture.jpg"; // 保存图片地址 byte[] buffer = pBuffer.getByteArray(0, dwBufSize); ByteArrayInputStream byteArrInputGlobal = new ByteArrayInputStream(buffer); try { gateBufferedImage = ImageIO.read(byteArrInputGlobal); if(gateBufferedImage != null) { ImageIO.write(gateBufferedImage, "jpg", new File(snapPicPath)); } } catch (IOException e2) { e2.printStackTrace(); } // 图片以及门禁信息界面显示 } return 0; } } @Override public void cleanup() { } @Override public String getDeployInfo() { return null; } @Override public int getStatus() { return 0; } @Override public int setCardTemplate(CardTemplateInfo info) { return 0; } @Override public void modifyUserInfo(List userFaceInfos) { } }