SendUserInfoTask.java
11.9 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
package com.example.dahua.async;
import com.example.dahua.MyTask;
import com.example.dahua.bean.AttendanceBean;
import com.example.dahua.bean.TeacherBean;
import com.example.dahua.bean.UserInfoBean;
import com.example.dahua.common.Res;
import com.example.dahua.dao.UserDao;
import com.example.dahua.lib.CompressPic;
import com.example.dahua.lib.FilePath;
import com.example.dahua.lib.NetSDKLib;
import com.example.dahua.lib.ToolKits;
import com.example.dahua.module.GateModule;
import com.example.dahua.service.UserService;
import com.sun.jna.Memory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import sun.rmi.runtime.Log;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* 下发卡号的异步操作
*/
@Component
public class SendUserInfoTask {
@Autowired
UserDao userDao;
@Async("taskExecutor")
public void doTaskOne(String file, List<AttendanceBean> attendanceBeans, UserInfoBean userInfoBean) throws Exception {
File studentFile = new File(file);
String studentCode = studentFile.getName().split("\\.")[0];
//判断是否是副卡
String[] studentCodes = studentCode.split("_");
if (studentCodes.length > 1) {//副卡
String cardType = studentCodes[1];
String cardNum = userDao.getCardNum(userInfoBean.getStudent_id(), cardType);
userInfoBean.setStudent_num(cardNum);
}
sendUserInfoToDev(file, attendanceBeans, userInfoBean);
}
@Async("taskExecutor")
public void doTaskSendUserInfos(String schoolId, String clint_type, int type) {
try {
List<AttendanceBean> attendanceBeans = userDao.getAttendanceBeans(schoolId, clint_type);
String filePathStudent = "E:\\wwwhtdocs\\SmartCampus\\face17e50\\School" + schoolId + "\\Student";
String filePathParent = "E:\\wwwhtdocs\\SmartCampus\\face17e50\\School" + schoolId + "\\Parent";
String filePathTeacher = "E:\\wwwhtdocs\\SmartCampus\\face17e50\\School" + schoolId + "\\Teacher";
// String filePathStudent = "F:\\wwwroot\\SmartCampus\\face17e50\\School" + schoolId + "\\Student";
// String filePathParent = "F:\\wwwroot\\SmartCampus\\face17e50\\School" + schoolId + "\\Parent";
// String filePathParent = "C:\\Users\\taohandong\\Desktop\\小视频";
List<File> fileList = new ArrayList<>();
File filePaths = null;
if (type == 0) {//主卡下发
filePaths = new File(filePathStudent);
} else if (type == 1) {//副卡下发
filePaths = new File(filePathParent);
} else if (type == 2) {//教师卡下发
filePaths = new File(filePathTeacher);
}else {
filePaths = new File("");
}
if (!filePaths.exists()){
System.out.println("图片目录不存在");
return;
}
File[] filesStudent = filePaths.listFiles();
if (filesStudent == null) {
System.out.println("没有找到人脸图片");
return;
}
/**
* 添加学生人脸
*/
for (int i = 0; i < filesStudent.length; i++) {
fileList.add(filesStudent[i]);
}
// System.out.println("files:" + fileList.size());
while (fileList.size() > 0) {
File studentFile = fileList.get(0);
String studentCode = studentFile.getName().split("\\.")[0];
UserInfoBean userInfoBean = null;
//判断是否是副卡
String[] studentCodes = studentCode.split("_");
System.out.println("学籍号:" + studentCode);
if (studentCodes.length > 1) {//副卡
String studentcode = studentCodes[0];
String cardType = studentCodes[1];
userInfoBean = userDao.getUserInfo(schoolId, studentcode);
if (!userIsExit(fileList, studentcode, userInfoBean)) continue;
String cardNum = userDao.getCardNum(userInfoBean.getStudent_id(), cardType);
userInfoBean.setStudent_num(cardNum);
} else {
if (type == 0 || type == 1) {//学生信息或家长信息
userInfoBean = userDao.getUserInfo(schoolId, studentCode);
} else if (type == 2) {//教师信息
TeacherBean teacher = userDao.getTeacher(schoolId, studentCode);
if (null != teacher) {
userInfoBean = new UserInfoBean();
userInfoBean.setStudent_num(teacher.getTeacher_num());
userInfoBean.setName(teacher.getName());
userInfoBean.setUser_id(teacher.getUser_id());
userInfoBean.setStudentcode(teacher.getNum());
}
}
//判断用户是否存在
if (!userIsExit(fileList, studentCode, userInfoBean)) continue;
}
boolean sendResult = sendUserInfoToDev(studentFile.getAbsolutePath(), attendanceBeans, userInfoBean);
fileList.remove(0);
}
// System.out.println("全部下发完成:");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 判断用户是否存在
*
* @param fileList
* @param studentcode
* @param userInfoBean
*/
private boolean userIsExit(List<File> fileList, String studentcode, UserInfoBean userInfoBean) {
if (userInfoBean == null) {
System.out.println("用户信息不存在:" + studentcode);
fileList.remove(0);
return false;
}
return true;
}
/**
* 发送人员信息给设备
*
* @param file
* @param attendanceBeans
* @param userInfoBean
* @return
*/
private boolean sendUserInfoToDev(String file, List<AttendanceBean> attendanceBeans, UserInfoBean userInfoBean) {
try {
if (userInfoBean == null) {//"用户不存在"
System.out.println("用户不存在");
return false;
}
if (attendanceBeans.size() == 0) {//"该学校下没有人脸设备"
System.out.println("该学校下没有人脸设备:");
return false;
}
String cardNum = userInfoBean.getStudent_num();
System.out.println("cardNum:" + cardNum);
if (StringUtils.isEmpty(cardNum) || cardNum.equals("null")) {
System.out.println("卡号为空");
return false;
}
/**
* 保存图片到本地
*/
sendTodev(file, userInfoBean, attendanceBeans);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
/**
* @param picSrc 图片地址
* @param userInfoBean 用户信息
* @param attendanceBeans 设备列表
*/
private void sendTodev(String picSrc, UserInfoBean userInfoBean, List<AttendanceBean> attendanceBeans) {
Memory memory = null;//图片缓存
try {
String targPath = FilePath.picPathComp + userInfoBean.getStudentcode() + ".jpg";
CompressPic.CompressPic(picSrc, targPath);//压缩后的图片
memory = ToolKits.readPictureFile(targPath);
System.out.println("下发学生信息:" + "targPath:" + new File(targPath).exists() + targPath + " userInfoBean:" + userInfoBean);
} catch (Exception e) {
e.printStackTrace();
}
for (AttendanceBean attendanceBean :
attendanceBeans) {
pushCardAndFace(attendanceBean, userInfoBean.getUser_id(), userInfoBean.getStudent_num(), userInfoBean.getName(), memory);
}
}
private synchronized void pushCardAndFace(AttendanceBean attendanceBean, String user_id, String student_num, String name, Memory memory) {
String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, 4);
String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
int bCardFlags = -1;//记录集编号
bCardFlags = userDao.getRecordNo(user_id) == null ? -1 : Integer.parseInt(userDao.getRecordNo(user_id));
NetSDKLib.LLong loginHandleLong = MyTask.lLongMap.get(attendanceBean.getClint_id());
boolean bFaceFalgs = false;
String cardNum = cardNo(student_num);
boolean ifExits = GateModule.findCard(cardNum, loginHandleLong);
System.out.println("卡号是否存在:" + ifExits);
if (bCardFlags != -1) {//修改卡信息
GateModule.deleteCard(bCardFlags, loginHandleLong);
userDao.deleteRecordNo(user_id, bCardFlags);
// boolean update = GateModule.modifyCard(bCardFlags, cardNum, userInfoBean.getUser_id(), userInfoBean.getName(), "123456"
// , Res.string().getCardStatusInt(1), Res.string().getCardTypeInt(1), 0, 0
// , 1, startTime, endTime, loginHandleLong);
// System.out.println("update:" + update);
bCardFlags = GateModule.insertCard(cardNum, user_id, name, "123456"
, Res.string().getCardStatusInt(1), Res.string().getCardTypeInt(1), 0, 0
, 1, startTime, endTime, loginHandleLong);
if (bCardFlags != -1) {
int index = userDao.saveRecordNo(user_id, bCardFlags, name);//存储记录集编号
}
bFaceFalgs = GateModule.modifyFaceInfo(user_id, memory, loginHandleLong);
//卡号添加成功,但是人脸不成功的话,就需要新增人脸
if (!bFaceFalgs)
bFaceFalgs = GateModule.addFaceInfo(user_id, memory, loginHandleLong);
} else {//新增卡信息
bCardFlags = GateModule.insertCard(cardNum, user_id, name, "123456"
, Res.string().getCardStatusInt(1), Res.string().getCardTypeInt(1), 0, 0
, 1, startTime, endTime, loginHandleLong);
if (bCardFlags != -1) {
int index = userDao.saveRecordNo(user_id, bCardFlags, name);//存储记录集编号
}
//添加人脸
bFaceFalgs = GateModule.addFaceInfo(user_id, memory, loginHandleLong);
}
if (bCardFlags != -1 && bFaceFalgs) {
System.out.println("人脸下发成功");
}
// 添加卡信息和人脸失败
if (bCardFlags == -1 && !bFaceFalgs) {
System.out.println("添加卡信息和人脸失败");
}
// 添加卡信息成功,添加人脸失败
if (bCardFlags != -1 && !bFaceFalgs) {
// System.out.println("添加卡信息成功,添加人脸失败");
}
// 卡信息已存在,添加人脸成功
if (bCardFlags == -1 && bFaceFalgs) {
System.out.println("卡信息已存在,添加人脸成功");
}
}
private 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 Long.parseLong(cardR, 16) + "";
return cardR;
}
}