GpsRealData.java 6.31 KB
package com.bsth.data.gpsdata_v2;

import com.bsth.data.BasicData;
import com.bsth.data.forecast.ForecastRealServer;
import com.bsth.data.gpsdata_v2.entity.GpsEntity;
import com.bsth.data.schedule.DayOfSchedule;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.message.buffer.CarEnergyBuffer;
import com.bsth.service.realcontrol.impl.ScheduleRealInfoServiceImpl;
import com.google.common.collect.TreeMultimap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author PanZhao
 * @ClassName: GpsRealData
 * @Description: TODO(实时GPS数据集合)
 * @date 2016年8月12日 下午2:04:41
 */
@Component
public class GpsRealData {

    static Logger logger = LoggerFactory.getLogger(GpsRealData.class);

    private static ConcurrentMap<String, GpsEntity> gpsMap;

    //按线路分组设备号
    private static TreeMultimap<String, String> lineCode2Devices;

    @Autowired
    DayOfSchedule dayOfSchedule;

    @Autowired
    ForecastRealServer forecastRealServer;

    /**
     * 构造函数
     */
    public GpsRealData() {
        gpsMap = new ConcurrentHashMap<>();
        lineCode2Devices = TreeMultimap.create();
    }


    public void put(GpsEntity gps) {
        String device = gps.getDeviceId();
        GpsEntity old = gpsMap.get(device);

        try {
            if (!StringUtils.isEmpty(gps.getStopNo())) {
                //站点编码改变
                if (null == old || !gps.getStopNo().equals(old.getStopNo())) {
                    gps.setArrTime(gps.getTimestamp());
                    //预测到达终点时间
                    forecastRealServer.forecast(gps.getNbbm(), gps);
                } else {
                    gps.setArrTime(old.getArrTime());
                    //不预测, 重新计算终点时间
                    gps.setExpectStopTime(forecastRealServer.expectStopTime(gps.getNbbm()));
                }
            }

            //刷新对照
            gpsMap.put(device, gps);
            if (StringUtils.isNotBlank(gps.getLineId())) {
                //站点名称
                gps.setStationName(getStationName(gps));
                lineCode2Devices.put(gps.getLineId(), device);

                if(old != null && !gps.getLineId().equals(old.getLineId()))
                    lineCode2Devices.remove(old.getLineId(), device);
            }

            //车辆换设备了
            String nbbm = gps.getNbbm();
            if(old != null && StringUtils.isNotEmpty(nbbm) && !nbbm.equals(old.getNbbm())){
                List<GpsEntity> list = findByNbbm(nbbm);
                for(GpsEntity g : list){
                    if(!g.getDeviceId().equals(device))
                        gpsMap.remove(g.getDeviceId());
                }
            }

            // 设置车辆的电量
            gps.setEnergy(CarEnergyBuffer.getCarEnergy(gps.getNbbm()));
        } catch (Exception e) {
            logger.error("", e);
        }
    }

    public String getStationName(GpsEntity gps) {
        return BasicData.getStationNameByCode(gps.getStopNo(), gps.getLineId() + "_" + gps.getUpDown() + "_");
    }

    /**
     * @Title: get @Description: TODO(设备号获取GPS)
     */
    public static GpsEntity get(String deviceId) {
        return gpsMap.get(deviceId);
    }

    public List<GpsEntity> findByNbbm(String nbbm){
        Collection<GpsEntity> arr = gpsMap.values();
        List<GpsEntity> rs = new ArrayList<>();
        for(GpsEntity g : arr){
            if(nbbm.equals(g.getNbbm()))
                rs.add(g);
        }
        return rs;
    }

    public GpsEntity getByNbbm(String nbbm){
        String device = BasicData.deviceId2NbbmMap.inverse().get(nbbm);
        if(StringUtils.isNotBlank(device))
            return get(device);
        else
            return null;
    }

    /**
     * @Title: get @Description: TODO(线路编码获取GPS集合) @throws
     */
    public List<GpsEntity> getByLine(String lineCode) {
        NavigableSet<String> set = lineCode2Devices.get(lineCode);//实际车载
        if(null == set)
            set = new TreeSet();
        Set<String> nbbmSet = dayOfSchedule.findCarByLineCode(lineCode);//计划用车

        Map<String, String> nbbm2deviceMap = BasicData.deviceId2NbbmMap.inverse();
        String deviceId;
        for(String nbbm : nbbmSet){
            deviceId = nbbm2deviceMap.get(nbbm);
            if(StringUtils.isNotEmpty(deviceId))
                set.add(deviceId);
        }

        List<GpsEntity> rs = new ArrayList<>();
        GpsEntity gps;
        ScheduleRealInfo sch;
        for (String device : set) {
            gps = gpsMap.get(device);
            //过滤异常GPS数据
            if (gps == null || StringUtils.isBlank(gps.getNbbm()))
                continue;

            sch = dayOfSchedule.execPlanMap().get(gps.getNbbm());
            if (null != sch){
                gps.setSchId(sch.getId());
                if(!sch.getXlBm().equals(lineCode)){
                    //车辆在其他线路营运
                    gps.setRemark("执行 " + sch.getXlName() + " " + sch.getDfsj() + " 班次");
                    gps.setPlanCode(sch.getXlBm());
                }
            }else
                gps.setRemark(null);
            
            gps.setDvrcode(ScheduleRealInfoServiceImpl.DIRMAP.get(BasicData.deviceId2NbbmMap.get(gps.getDeviceId())));;
            rs.add(gps);
        }

        return rs;
    }

    public static Set<String> findDevices(String lineCode){
        return lineCode2Devices.get(lineCode);
    }

    public List<GpsEntity> get(List<String> pArray) {
        List<GpsEntity> list = new ArrayList<>();

        for (String code : pArray)
            list.addAll(getByLine(code));
        return list;
    }

    public Set<String> allDevices() {
        return gpsMap.keySet();
    }

    public Collection<GpsEntity> all() {
        return gpsMap.values();
    }

    public void remove(String device) {
        gpsMap.remove(device);
    }
}