DirectiveBuffer.java 3.62 KB
package com.bsth.vehicle.directive.buffer;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.bsth.vehicle.directive.entity.Directive;
import com.bsth.vehicle.directive.entity.DirectiveReply;
import com.bsth.vehicle.directive.entity.DriverReport;
import com.bsth.vehicle.directive.entity.LineChange;
import com.bsth.vehicle.directive.repository.DriverReportRepository;
import com.bsth.vehicle.directive.repository.LineChangeRepository;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

/**
 * 
 * @ClassName: DirectiveBuffer 
 * @Description: TODO(调度指令缓存) 
 * @author PanZhao
 * @date 2016年6月7日 下午3:24:19 
 *
 */
@Component
public class DirectiveBuffer {
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	DriverReportRepository driverReportRepository;
	
	@Autowired
	LineChangeRepository lineChangeRepository;
	
	/**
	 * 等待入库的调度指令
	 */
	public static LinkedList<Directive> transientList;
	
	/**
	 * 等待确认的线路切换指令
	 */
	public static Map<String, LineChange> changeMap;
	
	/**
	 * 当日调度指令缓存
	 */
	private static Map<Integer, Directive> directiveMap;
	
	/**
	 * 驾驶员上报数据
	 * {K: 线路编码}
	 */
	private static Multimap<Integer, DriverReport> reportMultiMap;
	
	static{
		transientList = new LinkedList<>();
		directiveMap = new HashMap<>();
		reportMultiMap = ArrayListMultimap.create();
		changeMap = new HashMap<>();
	}
	
	public static void put(Directive directive){
		directiveMap.put(directive.getMsgId(), directive);
	}
	
	/**
	 * 
	 * @Title: reply 
	 * @Description: TODO(指令 46,47 响应) 
	 * @throws
	 */
	public void reply(DirectiveReply reply){
		Integer msgId = reply.getMsgId();
		if(msgId == null){
			logger.error("reply error , msgId is null.");
			return;
		}
		
		Directive directive = directiveMap.get(msgId);
		
		if(null == directive){
			//无效的响应
			return;
		}
		
		switch (reply.getStatus()) {
		case 0:
			//失败
			directive.setReply46((short)-1);
			break;

		case 1:
			//发送成功
			directive.setReply46((short)0);
			break;
		case 2:
			//驾驶员阅读
			directive.setReply47((short)0);
			break;
		}
		
		if(directive.isDispatch()){
			directive.getSch().setDirectiveState(reply.getStatus() * 100);
		}
		transientList.add(directive);
	}
	
	/**
	 * 
	 * @Title: reply64 
	 * @Description: TODO(64 协议响应) 
	 * @throws
	 */
	public void reply64(JSONObject json){
		String key = json.getString("deviceId") + "_" + json.getString("timestamp");
		
		LineChange change = changeMap.get(key);
		
		if(null == change)
			logger.warn("64响应 -找不到请求源,json: " + json);
		else{
			JSONObject data = json.getJSONObject("data");
			
			changeMap.remove(key);
			
			if(null == data)
				logger.warn("64响应 data is null ,json: " + json);
			else{
				change.setRespAck(data.getShort("requestAck"));
				//响应入库
				lineChangeRepository.save(change);
			}
		}
	}
	
	/**
	 * 
	 * @Title: jsyReport 
	 * @Description: TODO(80 驾驶员上报) 
	 * @throws
	 */
	public void jsyReport(DriverReport report){
		logger.info("驾驶员上报");
		//实时入库
		driverReportRepository.save(report);
		reportMultiMap.put(report.getData().getLineId(), report);
	}
}