MyTask.java
13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package com.example.dahua;
import com.example.dahua.Gate.Gate;
import com.example.dahua.alarmListen.AlarmListenModule;
import com.example.dahua.bean.CardBean;
import com.example.dahua.bean.DeviceInfoBean;
import com.example.dahua.bean.TeacherBean;
import com.example.dahua.bean.UserInfoBean;
import com.example.dahua.dao.UserDao;
import com.example.dahua.lib.NetSDKLib;
import com.example.dahua.lib.ToolKits;
import com.example.dahua.lib.Utils;
import com.example.dahua.module.AutoRegisterModule;
import com.example.dahua.module.GateModule;
import com.example.dahua.module.LoginModule;
import com.example.dahua.service.AttendanceService;
import com.example.dahua.utils.FileUtils;
import com.example.dahua.xiananDao.SendRecordDao;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
@Component
public class MyTask implements ApplicationRunner {
// 主动注册监听回调
private ServiceCB servicCallback = new ServiceCB();
// 设备断线通知回调
private DisConnect disConnect = new DisConnect();
// 网络连接恢复
private HaveReConnect haveReConnect = new HaveReConnect();
//设备登录账号
public static String strUser = "admin";
//设备登密码
public static String password = "q12345678";
//在线设备信息集合
private List<DeviceInfoBean> deviceInfoBeans = new ArrayList<>();
//存放登录句柄
public static Map<String, NetSDKLib.LLong> lLongMap = new HashMap<>();
@Autowired
SendRecordDao sendRecordDao;
@Autowired
UserDao userDao;
public boolean isHasNewDevice = false;//判断是否有新设备上线
@Override
public void run(ApplicationArguments args) throws Exception {
// 121.40.109.21
LoginModule.init(disConnect, haveReConnect); // 打开工程,初始化
autoRegister();//自注册
// System.out.println(" sendRecordDao:"+sendRecordDao.getSenSuccess());
}
/**
* 设备自注册
*/
private void autoRegister() {
/**
* 开启监听服务
*/
AutoRegisterModule.startServer(new Utils().getHostAddress(), 9500, servicCallback);
}
// 设备断线回调: 通过 CLIENT_Init 设置该回调函数,当设备出现断线时,SDK会调用该函数
private 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);
String deviceId = getDeviceId(pchDVRIP, nDVRPort);
if (!StringUtils.isEmpty(deviceId)) {
attendanceService.updateConnectStateWithDevid(0, deviceId);
GateModule.stopRealLoadPic(lLongMap.get(deviceId));
lLongMap.remove(deviceId);
// devMap.remove(deviceId);
removeDevice(deviceId);
isHasNewDevice = true;
}
// AutoRegisterModule.logout()
// 断线提示
}
}
// 网络连接恢复,设备重连成功回调
// 通过 CLIENT_SetAutoReconnect 设置该回调函数,当已断线的设备重连成功时,SDK会调用该函数
private 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);
// 重连提示
String deviceId = getDeviceId(pchDVRIP, nDVRPort);
if (!StringUtils.isEmpty(deviceId)) {
attendanceService.updateConnectStateWithDevid(1, deviceId);
// devMap.put(deviceId,"");
DeviceInfoBean deviceInfoBean = new DeviceInfoBean();
deviceInfoBean.setDevcieId(deviceId);
deviceInfoBean.setDeviceIp(pchDVRIP);
deviceInfoBean.setDevicePort(nDVRPort);
deviceInfoBean.setLoginHandle(m_hLoginHandle);
deviceInfoBean.setPassword(password);
deviceInfoBean.setUsername(strUser);
deviceInfoBeans.add(deviceInfoBean);
lLongMap.put(deviceId, m_hLoginHandle);
isHasNewDevice = true;
}
}
}
/**
* 根据ip和port获取设备id
*
* @param pchDVRIP
* @param nDVRPort
* @return
*/
private String getDeviceId(String pchDVRIP, int nDVRPort) {
for (DeviceInfoBean deviceInfoBean :
deviceInfoBeans) {
if (pchDVRIP.equals(deviceInfoBean.getDeviceIp()) && nDVRPort == deviceInfoBean.getDevicePort()) {
return deviceInfoBean.getDevcieId();
}
}
return "";
}
/**
* 移除设备
*
* @param deviceId
*/
private void removeDevice(String deviceId) {
DeviceInfoBean deviceInfoBean = null;
for (DeviceInfoBean dev :
deviceInfoBeans) {
if (dev.getDevcieId().equals(deviceId)) deviceInfoBean = dev;
}
if (null != deviceInfoBean) deviceInfoBeans.remove(deviceInfoBean);
}
@Autowired
private AttendanceService attendanceService;//设备控制服务
/**
* 侦听服务器回调函数
*/
public class ServiceCB implements NetSDKLib.fServiceCallBack {
@Override
public int invoke(NetSDKLib.LLong lHandle, final String pIp, final int wPort,
int lCommand, Pointer pParam1, int dwParamLen,
Pointer dwUserData) {
// 将 pParam 转化为序列号
byte[] buffer1 = new byte[dwParamLen];
pParam1.read(0, buffer1, 0, dwParamLen);
//设备id
String deviceId = "";
try {
deviceId = new String(buffer1, "GBK").trim();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.printf("Register Device Info [Device address %s][port %s][DeviceID %s] \n", pIp, wPort, deviceId);
switch (lCommand) {
case NetSDKLib.EM_LISTEN_TYPE.NET_DVR_DISCONNECT: { // 验证期间设备断线回调
System.out.println("EM_LISTEN_TYPE:验证期间设备断线回调");
break;
}
case NetSDKLib.EM_LISTEN_TYPE.NET_DVR_SERIAL_RETURN: { // 设备注册携带序列号
System.out.println("EM_LISTEN_TYPE:设备注册携带序列号");
/**
* 主动注册调用的登录接口,获取登录句柄
*/
NetSDKLib.LLong loginHandleLong = AutoRegisterModule.login(pIp, wPort, strUser, password, deviceId);
// FileUtils.getInstance().writeLogs("设备注册:"+deviceId+" 登录句柄:"+loginHandleLong,FileUtils.devices);
if (loginHandleLong.longValue() != 0) {
lLongMap.put(deviceId, loginHandleLong);
isHasNewDevice = true;
String inTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
int index = attendanceService.insert(deviceId, "22", pIp, wPort + "", inTime, "-1", "1");
DeviceInfoBean deviceInfoBean = new DeviceInfoBean();
deviceInfoBean.setDevcieId(deviceId);
deviceInfoBean.setDeviceIp(pIp);
deviceInfoBean.setDevicePort(wPort);
deviceInfoBean.setLoginHandle(loginHandleLong);
deviceInfoBeans.add(deviceInfoBean);
} else {
// System.err.println(String.format("Login Failed[Device IP %s] [Port %s][DeviceID %s] %s", pIp,
// wPort, deviceId));
}
break;
}
default:
break;
}
return 0;
}
}
public synchronized void reloadPic() {
isHasNewDevice = false;
if (null!=deviceInfoBeans&&deviceInfoBeans.size()>0){
for (DeviceInfoBean dev :
deviceInfoBeans) {
// FileUtils.getInstance().writeLogs(dev.getDevcieId() + "------" + lLongMap.get(dev.getDevcieId()), FileUtils.device_login);
if (lLongMap.get(dev.getDevcieId()).intValue() > 0){
NetSDKLib.LLong lLong = GateModule.realLoadPic(0, analyzerCallback, lLongMap.get(dev.getDevcieId()));
if (lLong.intValue()!=-1){
deviceInfoBeans.remove(dev);
}
}
}
}
}
//智能订阅
private AnalyzerDataCB analyzerCallback = new AnalyzerDataCB();
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;
}
byte[] bufferBytes = new byte[dwBufSize];
pBuffer.read(0, bufferBytes, 0, dwBufSize);
File path = new File(".\\FaceRecoder");
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);
System.out.println("sda:" + new String(msg.szCardNo) + " 抓拍照片存储地址:");
try {
String card = new String(msg.szCardNo).trim();
if (!StringUtils.isEmpty(card)) {
// 保存图片,获取图片缓存
String snapPicPath = path + "\\" + System.currentTimeMillis() + ".png"; // 保存图片地址
byte[] buffer = pBuffer.getByteArray(0, dwBufSize);
ByteArrayInputStream byteArrInputGlobal = new ByteArrayInputStream(buffer);
gateBufferedImage = ImageIO.read(byteArrInputGlobal);
if (gateBufferedImage != null) {
ImageIO.write(gateBufferedImage, "png", new File(snapPicPath));
}
card=cardNo(card);
CardBean cardBean = userDao.getCards(card);//根据卡号获取身份信息
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
snapPicPath = snapPicPath.replaceFirst("\\.", "http://121.40.109.21:8991");
snapPicPath = snapPicPath.replace("FaceRecoder","image");
if (cardBean.getType() == 0) {//老师
TeacherBean teacherBean = userDao.getTeacherWithId(cardBean.getUser_id());
sendRecordDao.addFaceRecoder("", teacherBean.getUser_id(), teacherBean.getName(), snapPicPath, 1, time, card);
} else if (cardBean.getType() == 2) {//学生
UserInfoBean userInfoBean = userDao.getStudentWithid(cardBean.getUser_id());
sendRecordDao.addFaceRecoder("", userInfoBean.getUser_id(), userInfoBean.getName(), snapPicPath, 1, time, card);
}
}
} catch (IOException e2) {
e2.printStackTrace();
}
// 图片以及门禁信息界面显示
// EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
// if (eventQueue != null) {
// eventQueue.postEvent( new GateFrame.AccessEvent(target,
// gateBufferedImage,
// msg));
// }
}
return 0;
}
}
/**
* 卡号两两取反
*
* @param cardDex
* @return
*/
public String cardNo(String cardDex) {
String cardR = "";
int length = cardDex.length();
if (length != 8) {
System.out.println("卡号格式不正确:" + cardDex);
return cardDex;
}
while (length > 0) {
length -= 2;
cardR += cardDex.substring(length, length + 2);
}
return cardR;
}
}