TransitHandleService.java 4.74 KB
package cn.com.glsx.kafka.service;

import cn.com.glsx.kafka.common.enums.TopicsEnum;
import cn.com.glsx.kafka.queue.remote.RemoteDeviceLocationService;
import cn.com.glsx.stdinterface.dto.DeviceLocationDto;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.glsx.plat.redis.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Gps和基站数据处理Service
 *
 * @author Yanglinlong
 * @date 2021/12/17 19:43
 */
@Slf4j
@Service
public class TransitHandleService {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private RemoteDeviceLocationService remoteDeviceLocationService;
    /**
     * 缓存 map
     */
    private static ConcurrentHashMap<String, DeviceLocationDto> deviceLocationMap = new ConcurrentHashMap<>();

    /**
     * 缓存数据入库时间差
     */
    private static long tempTime = System.currentTimeMillis();

    /**
     * 间隔时间
     */
    private static final int STEP = 60000;


    /**
     * 处理GPS和基站的数据
     *
     * @param deviceLocationDto Gps和基站的数据
     */
    public void handleGpsAndBsData(DeviceLocationDto deviceLocationDto, Integer topicId) {
        DeviceLocationDto deviceLocationCache = (DeviceLocationDto) redisUtils.get(setKey(deviceLocationDto.getSn()));
        if (ObjectUtil.isNull(deviceLocationCache)) {
            // 缓存中没有 就是第一次插入数据 设置创建时间同步更新时间
            deviceLocationDto.setCreatedTime(new Date());
            log.info("报警消费数据===sn:" + deviceLocationDto.getSn() + "Gps时间:" + DateUtil.formatDateTime(deviceLocationDto.getLastGpsTime()) + "基站时间:" + DateUtil.formatDateTime(deviceLocationDto.getLastBsTime()));
            redisUtils.set(setKey(deviceLocationDto.getSn()), deviceLocationDto);
            deviceLocationMap.put(deviceLocationDto.getSn(), deviceLocationDto);
        } else {
            // gps时间比缓存中Gps的时间大就更新缓存的时间
            Boolean flag = false;
            Date lastGpsTime = deviceLocationDto.getLastGpsTime();
            Date lastGpsTimeCache = deviceLocationCache.getLastGpsTime();
            if (judgeDate(lastGpsTime, lastGpsTimeCache) && lastGpsTime.after(lastGpsTimeCache)) {
                deviceLocationCache.setLastGpsTime(lastGpsTime);
                flag = true;
            }

            //基站时间比缓存中基站的时间大就更新缓存的时间
            Date lastBsTime = deviceLocationDto.getLastBsTime();
            Date lastBsTimeCache = deviceLocationCache.getLastBsTime();
            if (judgeDate(lastBsTime, lastBsTimeCache) && lastBsTime.after(lastBsTimeCache)) {
                deviceLocationCache.setLastBsTime(lastBsTime);
                flag = true;
            }

            if (TopicsEnum.DEVICE_STATE.getId().equals(topicId)) {
                deviceLocationCache.setStayStatus(deviceLocationDto.getStayStatus());
                deviceLocationCache.setStayStatusUpdatetime(deviceLocationDto.getStayStatusUpdatetime());
            }

            // 判断是否有数据更新 有数据更新重新设置回缓存
            if (flag) {
                // 同步更新时间
                log.info("报警消费数据===sn:" + deviceLocationCache.getSn() + "Gps时间:" + DateUtil.formatDateTime(deviceLocationCache.getLastGpsTime()) + "基站时间:" + DateUtil.formatDateTime(deviceLocationCache.getLastBsTime()));
                redisUtils.set(setKey(deviceLocationCache.getSn()), deviceLocationCache);
                deviceLocationMap.put(deviceLocationCache.getSn(), deviceLocationCache);
            }
        }

        // 当数据大小为500个 或者 时间间隔3秒 的时候 保存一次
        if (System.currentTimeMillis() >= tempTime + STEP) {
            log.info("开始插入或者更新Gps和基站的数据大小===" + deviceLocationMap.size());
            remoteDeviceLocationService.saveOrUpdateDeviceLocationData(deviceLocationMap.values());
            // 清空缓存map
            deviceLocationMap.clear();
            // 临时时间设置为此次插入数据库的时间
            tempTime = System.currentTimeMillis();
        }
    }

    /**
     * 判断两个时间是否都不为null
     *
     * @param dateOne time
     * @param dateTwo time
     * @return
     */
    private Boolean judgeDate(Date dateOne, Date dateTwo) {
        return !ObjectUtil.isNull(dateOne) && !ObjectUtil.isNull(dateTwo);
    }

    /**
     * 设置key值
     *
     * @param key key
     * @return
     */
    private String setKey(String key) {
        return "gpsbs_" + key;
    }
}