SafeDrivCenter.java 2.14 KB
package com.bsth.data.safe_driv;

import com.bsth.websocket.handler.SendUtils;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 安全驾驶
 * Created by panzhao on 2017/4/6.
 */
@Component
public class SafeDrivCenter implements ApplicationContextAware {

    private static Set<SafeDriv> data;

    @Autowired
    SafeDrivDataLoadThread safeDrivDataLoadThread;

    static SendUtils sendUtils;

    /**
     * 车辆自编号 和 最新一条数据对照
     */
    private static Map<String, SafeDriv> safeMap;

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

    static {
        data = new HashSet<>();
        safeMap = new HashMap<>();
    }

    private static DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
    public static void put(SafeDriv sd){
        sd.setTs(fmt.parseMillis(sd.getStartime()));

        if(sd.getYczltype().indexOf("A") == -1)
            sd.setYczltype("A" + sd.getYczltype());

        //SafeDriv old = safeMap.get(sd.getClzbh());
        if(!data.contains(sd)){
            //通知客户端
            sendUtils.sendSafeDriv(sd);
            data.add(sd);
            safeMap.put(sd.getClzbh(), sd);
        }
    }

    public static Set<SafeDriv> findAll(){
        return data;
    }

    public static void clear(){
        data = new HashSet<>();
        safeMap = new HashMap<>();
        logger.info("清除安全驾驶数据,,,");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        sendUtils = applicationContext.getBean(SendUtils.class);
    }
}