SchduleTask.java
7.94 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
package com.sincere.haikang;
import com.sincere.haikang.async.SendUserAsync;
import com.sincere.haikang.bean.AttendanceBean;
import com.sincere.haikang.bean.StudentBean;
import com.sincere.haikang.dao.DeviceDao;
import com.sincere.haikang.dao.UserDao;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
@Component
@EnableScheduling
public class SchduleTask {
File saveFile = new File("C:\\save");
File savePth = new File("C:\\save\\save.txt");
@Autowired
UserDao userDao;
@Value("${clint_type}")
private String clint_type;
@Autowired
SendUserAsync sendUserAsync;
@Autowired
DeviceDao deviceDao;
private boolean isSending = false;
/**
* 定时任务,取更新的学生信息
*/
@Scheduled(fixedRate = 5000)
public synchronized void getUpdateStudentInfo() {
if (!isSending) {
isSending = true;
try {
if (!saveFile.exists()) {
try {
saveFile.mkdirs();
File saveFile_txt = new File(saveFile, "save.txt");
if (!saveFile_txt.exists()) saveFile_txt.createNewFile();
} catch (IOException e) {
System.out.println("e:" + e.toString());
e.printStackTrace();
}
}
FileInputStream fileInputStream = new FileInputStream(savePth);
byte[] readByte = new byte[1024];
fileInputStream.read(readByte);
//存储的数据库下标
String index = new String(readByte, 0, readByte.length).trim();
// System.out.println("定时任务取得下标:" + index);
List<StudentBean> studentBeans = null;
if (StringUtils.isEmpty(index)) {//没记录,取前十条
studentBeans = userDao.getStudents();
// System.out.println("getStudents:"+studentBeans.toString());
} else {
studentBeans = userDao.getAllStudents(Long.parseLong(index), 2);
// System.out.println("getAllStudents:"+studentBeans.toString());
}
// System.out.println("定时任务:" + studentBeans.toString());
System.out.println("studentBeans:" + studentBeans.size());
if (sendUserAsync != null && studentBeans != null && studentBeans.size() > 0) {
sendUserAsync.sendStu(studentBeans, 1);
for (int i = studentBeans.size() - 1; i >= 0; i--) {
Thread.sleep(200);
StudentBean studentBean = studentBeans.get(i);
// System.out.println("studentBeans:"+studentBean.toString());
if (null != studentBean.getStudentCode() && 0 != studentBean.getSchoolId() && null != studentBean.getCard()) {
sendCImg(studentBean);
sendCard(studentBean);
FileOutputStream fileOutputStream = null;
fileOutputStream = new FileOutputStream(savePth);
System.out.println("getId:" + studentBeans.get(0).getID());
fileOutputStream.write((studentBean.getID() + "").getBytes());
fileInputStream.close();
fileOutputStream.close();
}
}
}
/*if (studentBeans.size() > 0 && CMSServer.deviceAndLoginIdMap.keySet().size() >= 0) {
FileOutputStream fileOutputStream = null;
fileOutputStream = new FileOutputStream(savePth);
System.out.println("getId:"+studentBeans.get(0).getID());
fileOutputStream.write((studentBeans.get(0).getID() + "").getBytes());
fileInputStream.close();
fileOutputStream.close();
}*/
} catch (Exception e) {
e.printStackTrace();
}
isSending = false;
}
}
private void sendCImg(StudentBean studentBean) {
int SchoolId = studentBean.getSchoolId();
List<AttendanceBean> attendanceBeans = deviceDao.getAttendancesWithSchoolId(SchoolId);
int type = -1;//18为海康,22为大华
String clientIds = "";
for (int i = 0; i < attendanceBeans.size(); i++) {
if (i < attendanceBeans.size() - 1) {
if (attendanceBeans.get(i).getClint_type().equals("22")) {
type = 22;
clientIds += attendanceBeans.get(i).getClint_id() + ",";
} else if (attendanceBeans.get(i).getClint_type().equals("18")) {
type = 18;
clientIds += attendanceBeans.get(i).getClint_id();
}
}
}
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
String card = TextUtils.isEmpty(studentBean.getCard()) ? studentBean.getOldCard() : studentBean.getCard();
String imgFile = "E://wwwhtdocs//SmartCampus//face17e5//School" + SchoolId + "//Student//" + studentBean.getStudentCode() + ".png";
if (type == 18 && !TextUtils.isEmpty(card)) {
String url = "http://114.55.30.100:8089/facereco/sendFaceToDevices?card=" + card + "&deviceIds=" + clientIds
+ "&file=" + imgFile + "&name=" + studentBean.getName() + "&userType=2";
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
System.out.println("下发人脸:" + responseEntity.getBody());
} else if (type == 22 && !TextUtils.isEmpty(card)) {
String url = "http://121.40.109.21:8991/user/uploadImgAndUserInfo?clint_type=22&file=" + imgFile + "&schoolId=" + SchoolId + "&studentCode=" + studentBean.getStudentCode();
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
System.out.println("下发人脸:" + responseEntity.getBody());
}
// String result = restTemplate.getForObject(url, String.class);
// System.out.println("result:" + result.getBody() + " studdentCode:" + studentBean.getStudentCode());
}
private void sendCard(StudentBean studentBean) {
int SchoolId = studentBean.getSchoolId();
String card = TextUtils.isEmpty(studentBean.getCard()) ? studentBean.getOldCard() : studentBean.getCard();
if (!TextUtils.isEmpty(card)) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
String url = "http://campus.myjxt.com/api/OneCard/SendSingle?SchoolId=" + SchoolId + "&Card=" + card + "&ClassId=" + studentBean.getClassId();
System.out.println("下发卡号url:" + url);
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
System.out.println("下发卡号:" + responseEntity.getBody());
}
}
}