DirectiveServiceImpl.java 5.43 KB
package com.bsth.vehicle.directive.service;

import java.io.IOException;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsth.service.impl.BaseServiceImpl;
import com.bsth.vehicle.common.CommonMapped;
import com.bsth.vehicle.directive.Consts;
import com.bsth.vehicle.directive.MsgIdGenerator;
import com.bsth.vehicle.directive.buffer.DirectiveBuffer;
import com.bsth.vehicle.directive.entity.Directive;
import com.bsth.vehicle.directive.entity.Directive.DirectiveData;
import com.bsth.vehicle.directive.entity.DispatchInstruct;
import com.bsth.vehicle.directive.entity.LineChange;
import com.bsth.vehicle.directive.entity.LineChange.LineChangeData;
import com.bsth.vehicle.directive.repository.DirectiveRepository;
import com.bsth.vehicle.directive.repository.LineChangeRepository;
import com.bsth.vehicle.gpsdata.buffer.GpsRealDataBuffer;
import com.bsth.vehicle.gpsdata.entity.GpsRealData;

@Service
public class DirectiveServiceImpl extends BaseServiceImpl<Directive, Integer> implements DirectiveService{

	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	DirectiveRepository directiveRepository;
	
	@Autowired
	GpsRealDataBuffer gpsRealDataBuffer;
	
	@Autowired
	LineChangeRepository lineChangeRepository;
	
	@Override
	public int send60Phrase(String nbbm, String text) {
		Directive directive = create60Data(nbbm, text, DispatchInstruct.PHRASE);
		
		//发送指令
		int code = postJson(JSON.toJSONString(directive));
		
		if(code == 0){
			//添加到缓存,等待入库
			DirectiveBuffer.put(directive);
		}else{
			logger.error("send60Phrase error, code: " + code);
		}
		return code;
	}
	
	@Override
	public int send60Dispatch(String nbbm, String text) {
		return 0;
	}

	@Override
	public int send60Operation(String nbbm, int state, int upDown) {
		return 0;
	}

	/**
	 * 线路切换
	 */
	@Override
	public int lineChange(String nbbm, Integer lineId) {
		Long t = System.currentTimeMillis();
		String deviceId = CommonMapped.vehicDeviceBiMap.inverse().get(nbbm);
		
		LineChange change = new LineChange();
		LineChangeData data = new LineChangeData();
		//暂时写死,没什么用
		data.setCityCode((short) 22);
		data.setDeviceId(deviceId);
		data.setLineId(String.valueOf(lineId));
		
		change.setDeviceId(deviceId);
		change.setOperCode((short) 0X64);
		change.setTimestamp(t);
		change.setData(data);
		
		int code = 0;//postJson(JSON.toJSONString(change));
		//if(code == 0){
			//入库
			lineChangeRepository.save(change);
			DirectiveBuffer.changeMap.put(deviceId + '_'  + t , change);
		//}else{
		//	logger.error("send60Phrase error, code: " + code);
		//}
		return code;
	}
	
	
	public Directive create60Data(String nbbm, String text, DispatchInstruct dispatchInstruct){
		Long timestamp = System.currentTimeMillis();
		
		String deviceId = CommonMapped.vehicDeviceBiMap.inverse().get(nbbm);
		Short company = Short.parseShort(CommonMapped.vehicCompanyMap.get(nbbm));
		if(null == deviceId){
			logger.error("没有设备号对照的车辆:" + nbbm);
			return null;
		}
		
		GpsRealData gpsData = gpsRealDataBuffer.findOneByDeviceId(deviceId);
		if(null == gpsData){
			logger.error("没有找到gps对照,无法确认营运状态和上下行:" + nbbm);
			return null;
		}
		
		int msgId = MsgIdGenerator.getMsgId();
		
		Directive directive = new Directive();
		DirectiveData data = new DirectiveData();
		//一级协议
		directive.setOperCode((short) 0x60);
		//设备号
		directive.setDeviceId(deviceId);
		//时间戳
		directive.setTimestamp(timestamp);
		directive.setMsgId(msgId);
		//构造数据
		data.setDeviceId(deviceId);
		data.setDispatchInstruct(dispatchInstruct);
		data.setTimestamp(timestamp);
		data.setCompanyCode(company);
		data.setMsgId(msgId);
		directive.setData(data);
		long serviceState;
		try{
			serviceState = Consts.SERVICE_STATE[gpsData.getUpDown()][gpsData.getState()];
		}catch(IndexOutOfBoundsException e){
			//未知营运状态的直接默认为上行非营运
			serviceState = Consts.SERVICE_STATE[0][1];
		}
		data.setServiceState(serviceState);
		data.setTxtContent(text);
		
		return directive;
	}
	
	public int postJson(String jsonStr){
		CloseableHttpClient httpClient = null;
		int code = -1;
		try{
			httpClient = HttpClients.createDefault();
			HttpPost post = new HttpPost(Consts.SEND_DIRECTIVE_URL);
			
			post.setEntity(new StringEntity(jsonStr, "utf-8"));
		
			CloseableHttpResponse response = httpClient.execute(post);
			
			JSONObject json = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
			if(null != json && json.getInteger("errCode") == 0)
				code = 0;
			else
				logger.error("和网关http通讯失败,rs: " + json);
		}catch(Exception e){
			logger.error("", e);
		}finally {
			try {
				if(httpClient != null)
					httpClient.close();
			} catch (IOException e) {
				logger.error("", e);
			}
		}
		return code;
	}
}