MyScheduledTask.java
9.25 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
package com.example.dahua.async;
import com.example.dahua.MyTask;
import com.example.dahua.bean.SendRecordBean;
import com.example.dahua.bean.StudentBean;
import com.example.dahua.bean.UserInfoBean;
import com.example.dahua.bean.WGBean;
import com.example.dahua.dao.UserDao;
import com.example.dahua.lib.Utils;
import com.example.dahua.mqtt.MqttManager;
import com.example.dahua.utils.FileUtils;
import com.example.dahua.xiananDao.SendRecordDao;
import org.json.JSONException;
import org.json.JSONObject;
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.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.config.ScheduledTask;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@Component
public class MyScheduledTask {
@Autowired
SendRecordDao sendRecordDao;
@Autowired
UserDao userDao;
@Autowired
SendUserInfoTask sendUserInfoTask;
@Autowired
MyTask myTasks;
// @Value("${haikangpic}")
// private String haikangpic;
public static boolean isSendHaikang = false;
public static boolean isSendWeigeng = false;
public static boolean isSendDahua = false;
private boolean isClose = true, isCloseYT = true;//判断是否关机
private String deleteDate = "";//删除一周前的图片用的
private String imgFilPath = "C:\\EhomePicServer";//抓拍图片路径
@Scheduled(fixedRate = 5000)
private void sendErrorInfo() throws InterruptedException {
// System.out.println("---------------------------sendErrorInfo:");
// System.out.println("定时任务执行中....");
deleteFaceImg();
/**
* 目前分三个时间段(9:00-10:00) (13:00-14:00)(22:00-5:00)进行补发操作
*/
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH");
int hour = Integer.parseInt(simpleDateFormat.format(new Date()));
/* if (hour >= 9 && hour < 11) {
dealData();
} else if (hour >= 13 && hour < 14) {
dealData();
} else if (hour >= 20 || hour < 5) {
dealData();
}*/
if (myTasks.isHasNewDevice) myTasks.reloadPic();
}
/**
* 定期删除人脸抓拍图片
*/
private void deleteFaceImg() {
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
// System.out.println("deleteDate:" + deleteDate);
if (!deleteDate.equals(date)) {
deleteDate = date;
File file = new File(imgFilPath);
File[] files = file.listFiles();
Date currentDate = new Date();
if (null != files)
for (int i = 0; i < files.length; i++) {
File imgFile = files[i];
String name = imgFile.getName().split("\\.")[0];
String time = name.substring(32);
if (time.length() > 8 && !time.contains("-")) time = time.substring(0, 8);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd");
try {
Date date1 = null;
if (time.contains("-")) date1 = simpleDateFormat.parse(time);
else date1 = simpleDateFormat1.parse(time);
long timecj = currentDate.getTime() - date1.getTime();
if (timecj > (7 * 24 * 60 * 60 * 1000)) {//大于七天的删除
System.out.println("imgFile:" + imgFile.getAbsolutePath() + imgFile.exists());
imgFile.delete();
}
} catch (ParseException e) {
imgFile.delete();
// e.printStackTrace();
}
}
}
}
private synchronized void dealData() {
String ip = new Utils().getHostAddress();
System.out.println("ip:" + ip);
if (ip.startsWith("192") || ip.contains("172.16.247.64"))
return;
/**
* 微耕
*/
//System.out.println("isSendWeigeng:"+isSendWeigeng+"isSendHaikang:"+isSendHaikang+"isSendDahua:"+isSendDahua);
if (!isSendWeigeng) {
isSendWeigeng = true;
sendUserInfoTask.addWeiGen();
}
/**
* 海康
*/
if (!isSendHaikang) {
isSendHaikang = true;
sendUserInfoTask.addHaikangface();
}
/**
* 大华人脸
*/
if (!isSendDahua) {
isSendDahua = true;
sendUserInfoTask.addDahuaFace();
}
}
boolean isSendMQ = false;
boolean isInitMQ = false;
int readLineNum = -1;
Map<String, Integer> readLineMap = new HashMap<>();
MqttManager mqttManager = new MqttManager();
@Scheduled(fixedRate = 1000)
public void sendMq() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH");
if (!isInitMQ) initMQ();
if (!isSendMQ) {
isSendMQ = true;
String fileName = simpleDateFormat.format(new Date());
String filePath = "D:\\wg_log\\info\\kaoInfo\\" + fileName + ".log";
// String filePath = "C:\\Users\\Administrator\\Desktop\\2020-04-30 08.log";
if (!readLineMap.containsKey(fileName)) readLineNum = -1;//更新文件后从头开始
File file = new File(filePath);
try {
if (!file.exists()) {
// System.out.println("文件不存在:" + filePath);
return;
}
FileInputStream fileInputStream = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, "GB2312"));
String content = null;
int indexLineNum = 0;//当前文件的行
while ((content = bufferedReader.readLine()) != null) {
indexLineNum++;
// System.out.println("indexLineNum:"+indexLineNum+" ----------readLineNum:"+readLineNum);
if (indexLineNum >= readLineNum) {
readLineNum++;
sendMQMess(content);
}
}
readLineMap.put(fileName, readLineNum);
fileInputStream.close();
bufferedReader.close();
} catch (FileNotFoundException e) {
System.out.println("文件不存在:" + e.toString());
e.printStackTrace();
} catch (IOException e) {
System.out.println("IO异常:" + e.toString());
e.printStackTrace();
}
} else {
isSendMQ = false;
}
}
private void initMQ() {
if (null != mqttManager) mqttManager.init();
isInitMQ = true;
}
public void sendMQMess(String content) {
String cardNum = content.substring(content.indexOf("卡号") + 3, content.indexOf("卡号") + 11);
// System.out.println("cardNum:" + cardNum);
String inOrOut = content.substring(content.indexOf("方向:") + 3, content.indexOf("方向:") + 5).endsWith("进门") ? "0" : "1";
String qiandaoDevId = content.substring(content.indexOf("设备") + 3, content.indexOf("设备") + 12);
//获取寝室关联的场景id
String placeId = userDao.getPlaceIdWithKaoqinDevid(qiandaoDevId);
// System.out.println("qiandaoDevId:" + qiandaoDevId);
// System.out.println("placeId:" + placeId);
if (null != placeId) {
//关联的显示看板设备
List<String> kanbanIds = userDao.getKanBanIdWithPlaceId(placeId);
for (int i = 0; i < kanbanIds.size(); i++) {
String kanbanId = kanbanIds.get(i);
String data = "{\"cmd\":\"" + 34 + "\",\"clientId\":\"" + kanbanId + "\",\"data\":{\"cardNum\":\"" + cardNum + "\",\"inOrOut\":\"" + inOrOut + "\"}}";
mqttManager.sendMqQD(kanbanId, data);
}
} else {
String schoolId = userDao.getSchoolIdWidthCardNum(cardNum);
if(StringUtils.isEmpty(schoolId)||Integer.parseInt(schoolId) <=0)return;
List<String> clintIds = userDao.getClintIds(schoolId);
for (int i = 0; i < clintIds.size(); i++) {
String clintId = clintIds.get(i);
String data = "{\"cmd\":\"" + 34 + "\",\"clientId\":\"" + clintId + "\",\"data\":{\"cardNum\":\"" + cardNum + "\",\"inOrOut\":\"" + inOrOut + "\"}}";
mqttManager.sendMqQD(clintId, data);
}
}
}
}