DeviceSyncScheduled.java 4.18 KB
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;
        }
        //查询
        int offset = 0;
        int limit = 20;
        int selectSize = 0;
        int num = 0;
        do {
            DeviceModel deviceQry = new DeviceModel();
            deviceQry.setModifyTime_gte(lastTimeUtc);
            deviceQry.setModifyTime_lte(currentTimeUtc);
            deviceQry.setOffset(offset);
            deviceQry.setLimit(limit);
            List<DeviceModel> deviceModels = trafficEventService.selectDeviceDatas(deviceQry);
            selectSize = deviceModels.size();
            num = num + 1;
            offset = limit*num;

            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()) {
                        Date lastDataUtc = DateUtil.addHours(deviceModels.get(deviceModels.size() - 1).getModifyTime(), -8);
                        redisTemplate.opsForValue().set(RedisConstants.FANXING_TO_GB1400_DEVICE_LASTTIME, DateUtil.formatDate(lastDataUtc, DateUtil.TIMESTAMP_FORMAT));
                    } else {
                        log.error("trafficDataConvertHandler.deviceToSubscribeNotifications.failed=(offset,limit)=({},{})", offset, limit);
                    }
                } else {
                    log.error("trafficDataConvertHandler.deviceToSubscribeNotifications is null");
                }
            }
        } while (selectSize >= limit);
        log.info("结束处理设备数据");
    }
}