DeviceSyncScheduled.java
3.6 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
package com.viontech.scheduled;
import com.viontech.constant.Gb1400Constants;
import com.viontech.constant.RedisConstants;
import com.viontech.constant.SystemConstants;
import com.viontech.handler.TrafficDataConvertHandler;
import com.viontech.service.Gb1400Service;
import com.viontech.service.TrafficEventService;
import com.viontech.utils.DateUtil;
import com.viontech.utils.JsonMessage;
import com.viontech.vo.gb1400.SubscribeNotificationsObj;
import com.viontech.vo.traffic.DeviceModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
* @author msl
* @date 2022/5/7
*/
@Slf4j
@Component
public class DeviceSyncScheduled {
@Resource
private TrafficEventService trafficEventService;
@Resource
private Gb1400Service gb1400Service;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private TrafficDataConvertHandler trafficDataConvertHandler;
@Scheduled(cron = "0 */2 * * * ?")
public void handleIllegal() {
log.info("开始处理设备数据");
if (!Gb1400Constants.LinkStatus) {
return;
}
Object o = redisTemplate.opsForValue().get(RedisConstants.FANXING_TO_GB1400_DEVICE_LASTTIME);
//上次对接到的设备记录时间
String lastTimeUtc = "";
if (o == null) {
//初次查询一小时前的数据,-12,-8:转UTC时间,总-20
lastTimeUtc = DateUtil.formatDate(DateUtil.addHours(new Date(), -20), DateUtil.TIMESTAMP_FORMAT);
} else {
lastTimeUtc = (String) o;
}
String currentTimeUtc = DateUtil.formatDate(DateUtil.addHours(new Date(), -8), DateUtil.TIMESTAMP_FORMAT);
if (StringUtils.isBlank(lastTimeUtc) || StringUtils.isBlank(currentTimeUtc)) {
log.warn("lastTimeUtc or currentTimeUtc isBlank");
return;
}
//查询
DeviceModel deviceQry = new DeviceModel();
deviceQry.setModifyTime_gte(lastTimeUtc);
deviceQry.setModifyTime_lte(currentTimeUtc);
deviceQry.setOffset(0);
deviceQry.setLimit(20);
List<DeviceModel> deviceModels = trafficEventService.selectDeviceDatas(deviceQry);
log.info("查询范围(lastTimeUtc,currentTimeUtc]=({},{}],本批次设备待同步数据总{}条。", lastTimeUtc, currentTimeUtc, deviceModels.size());
if (!CollectionUtils.isEmpty(deviceModels)) {
//List<DeviceModel> 转 SubscribeNotificationsObj
SubscribeNotificationsObj subscribeNotificationsObj = trafficDataConvertHandler.deviceToSubscribeNotifications(deviceModels);
//将设备数据同步gb1400
if (subscribeNotificationsObj != null) {
JsonMessage resultVo = gb1400Service.subscribeNotifications(subscribeNotificationsObj);
if (SystemConstants.APP_CODE_SUCCESS == resultVo.getCode()) {
redisTemplate.opsForValue().set(RedisConstants.FANXING_TO_GB1400_DEVICE_LASTTIME, DateUtil.formatDate(deviceModels.get(deviceModels.size() - 1).getModifyTime(), DateUtil.TIMESTAMP_FORMAT));
}
} else {
log.error("trafficDataConvertHandler.deviceToSubscribeNotifications is null");
}
}
log.info("结束处理设备数据");
}
}