DayOfDirectives.java 4.36 KB
package com.bsth.data.directive;

import com.alibaba.fastjson.JSONObject;
import com.bsth.data.LineConfigData;
import com.bsth.data.schedule.DayOfSchedule;
import com.bsth.entity.directive.D60;
import com.bsth.entity.directive.D64;
import com.bsth.entity.directive.Directive;
import com.bsth.entity.directive.DirectiveReponse;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.service.directive.DirectiveService;
import com.bsth.websocket.handler.SendUtils;
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.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;

/**
 * 
 * @ClassName: CurrDayDirectives
 * @Description: TODO(当天指令数据)
 * @author PanZhao
 * @date 2016年8月14日 下午5:23:59
 *
 */
@Component
public class DayOfDirectives {

	// 当日60指令缓存
	private static ConcurrentMap<Integer, D60> d60Map;

	// 线路切换指令 64
	public static ConcurrentMap<String, D64> d64Map;

	//等待插入的指令
	public static ConcurrentLinkedQueue<Directive> pstDirectives;
	//等待更新的指令
	public static ConcurrentLinkedQueue<D60> pstD60s;

	@Autowired
	DirectiveService directiveService;
	
	@Autowired
	SendUtils sendUtils;
	
	@Autowired
	LineConfigData lineConfigData;

	@Autowired
	DayOfSchedule dayOfSchedule;
	static Logger logger = LoggerFactory.getLogger(DayOfDirectives.class);

	
	static{
		d60Map = new ConcurrentHashMap<>();
		d64Map = new ConcurrentHashMap<>();
		pstDirectives = new ConcurrentLinkedQueue<>();
		pstD60s = new ConcurrentLinkedQueue<>();
	}
	
	public void put60(D60 d60) {
		d60Map.put(d60.getMsgId(), d60);
		//等待持久化
		pstDirectives.add(d60);
	}

	public void put64(D64 d64) {
		d64Map.put(d64.getKey(), d64);
		//等待持久化
		pstDirectives.add(d64);
	}

	/**
	 * 
	 * @Title: reply @Description: TODO(指令 46,47 响应) @throws
	 */
	public void reply(DirectiveReponse res) {
		Integer msgId = res.getMsgId();
		if (msgId == null) {
			logger.error("reply error , msgId is null.");
			return;
		}

		D60 d60 = d60Map.get(msgId);

		if (null == d60) {
			logger.error("找不到msgId: " + msgId + " 对应的指令数据");
			return;
		}

		switch (res.getStatus()) {
		case 0:
			d60.setReply46((short) -1);// 失败
			break;
		case 1:
			d60.setReply46((short) 0);// 发送成功
			d60.setReply46Time(System.currentTimeMillis());
			break;
		case 2:
			d60.setReply47((short) 0);// 驾驶员阅读
			d60.setReply47Time(System.currentTimeMillis());
			break;
		}

		//更新60数据
		pstD60s.add(d60);

		ScheduleRealInfo sch = d60.getSch();
		if (null == sch)
			return;

		if (d60.isDispatch()) {
			// 更新班次状态
			sch.setDirectiveState(res.getStatus() * 100);
			//持久化
			dayOfSchedule.save(sch);
			// 通知页面
			sendUtils.sendDirectiveToPage(sch);
		}
	}

	/**
	 * 
	 * @Title: reply64 @Description: TODO(64 协议响应) @throws
	 */
	public void reply64(JSONObject json) {
		String key = json.getString("deviceId") + "_" + json.getString("timestamp");

		D64 d64 = d64Map.get(key);

		if (null == d64)
			logger.warn("64响应 -找不到请求源,json: " + json);
		else {
			JSONObject data = json.getJSONObject("data");

			if (null == data)
				logger.warn("64响应 data is null ,json: " + json);
			else {
				logger.info(d64.getDeviceId() + "_" + d64.getData().getLineId() + "响应:" + data.getShort("requestAck"));
				/*d64.setRespAck(data.getShort("requestAck"));
				// 持久化*/
				//64 响应不入库了...
			}
		}
	}

	@Autowired
	DirectivesPstThread directivesPstThread;
	public void clearAll(){
		d60Map = new ConcurrentHashMap<>();
		d64Map = new ConcurrentHashMap<>();
		logger.info("清除指令数据 ,,,");
	}
	
	public Collection<D60> all60(){
		return d60Map.values();
	}
	
	public Collection<D64> all64(){
		return d64Map.values();
	}

	public D60 get(Integer msgId){
		return d60Map.get(msgId);
	}

	public Collection<Directive> all(){
		List<Directive> all = new ArrayList<>();
		all.addAll(d60Map.values());
		all.addAll(d64Map.values());
		
		return all;
	}
	
	public static class DComparator implements Comparator<Directive>{

		@Override
		public int compare(Directive d1, Directive d2) {
			return (int) (d2.getTimestamp() - d1.getTimestamp());
		}
	}
 }