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

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
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.data.LineConfigData;
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;

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

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

	// 线路切换指令 64
	public static Map<String, D64> d64Map;
	
	//等待C0_A4回复的用户
	//public static Map<K, V>

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

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

	
	static{
		d60Map = new HashMap<>();
		d64Map = new HashMap<>();
	}
	
	public void put60(D60 d60) {
		d60Map.put(d60.getMsgId(), d60);
	}

	public void put64(D64 d64) {
		d64Map.put(d64.getKey(), 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;
		}
		// 入库
		saveD60(d60);

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

		if (d60.isDispatch()) {
			// 更新班次状态
			sch.setDirectiveState(res.getStatus() * 100);
			// 通知页面
			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 {
				d64.setRespAck(data.getShort("requestAck"));
				// 响应入库
				directiveService.save64(d64);
			}
		}
	}

	private void saveD60(D60 d60) {
		// 等47再入库
		if (d60.getReply47() == null)
			return;

		directiveService.save(d60);
	}
	
	public void clear(String device){
		int c60 = 0, c64 = 0;
		//找到该设备的60数据
		Collection<D60> d60s = d60Map.values();
		List<D60> rem60List = new ArrayList<>();
		for(D60 d60 : d60s){
			if(d60.getDeviceId().equals(device))
				rem60List.add(d60);
		}
		//清除60数据
		for(D60 d60 : rem60List){
			if(d60.getReply47() == null)
				directiveService.save(d60);
			if(null != d60Map.remove(d60.getMsgId()))
				c60 ++;
		}
		logger.info("清除60数据 ," + c60);
		
		//找到该设备的64数据
		Collection<D64> d64s = d64Map.values();
		List<D64> rem64List = new ArrayList<>();
		for(D64 d64 : d64s){
			if(d64.getDeviceId().equals(device))
				rem64List.add(d64);
		}
		
		//清除64数据
		for(D64 d64 : rem64List){
			if(d64.getRespAck() == null)
				directiveService.save64(d64);
			
			if(null != d64Map.remove(d64.getKey()))
				c64 ++;
		}
		
		logger.info("清除64数据 ," + c64);
	}
	
	public Collection<D60> all60(){
		return d60Map.values();
	}
	
	public Collection<D64> all64(){
		return d64Map.values();
	}
	
	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());
		}
	}
 }