DirectiveServiceImpl.java 13.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
package com.bsth.vehicle.directive.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
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.common.ResponseCode;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.service.impl.BaseServiceImpl;
import com.bsth.service.realcontrol.buffer.ScheduleBuffer;
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.Directive60;
import com.bsth.vehicle.directive.entity.Directive60.DirectiveData;
import com.bsth.vehicle.directive.entity.DirectiveC0.DirectiveC0Data;
import com.bsth.vehicle.directive.entity.Directive80;
import com.bsth.vehicle.directive.entity.DirectiveC0;
import com.bsth.vehicle.directive.entity.Directive64;
import com.bsth.vehicle.directive.entity.Directive64.LineChangeData;
import com.bsth.vehicle.directive.repository.Directive60Repository;
import com.bsth.vehicle.directive.repository.Directive80Repository;
import com.bsth.vehicle.directive.repository.LineChangeRepository;
import com.bsth.vehicle.directive.util.HttpUtils;
import com.bsth.vehicle.gpsdata.buffer.GpsRealDataBuffer;
import com.bsth.vehicle.gpsdata.entity.GpsRealData;
import com.bsth.websocket.handler.RealControlSocketHandler;
import com.google.common.base.Splitter;

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

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	Directive60Repository directiveRepository;

	@Autowired
	GpsRealDataBuffer gpsRealDataBuffer;

	@Autowired
	LineChangeRepository lineChangeRepository;

	@Autowired
	RealControlSocketHandler socketHandler;

	@Autowired
	Directive80Repository d80Repository;

	SimpleDateFormat sdfHHmm = new SimpleDateFormat("HH点mm分"), sdfHHmm2 = new SimpleDateFormat("HH:mm");

	static Long schDiff = 1000 * 60 * 60L;

	// 城市代码
	static final short cityCode = 22;

	@Override
	public int send60Phrase(String nbbm, String text) {
		Directive60 directive = null;
		try {
			directive = create60Data(nbbm, text, (short) 0x00, null);
		} catch (Exception e) {
			logger.error("发送消息短语出现异常", e);
			return -1;
		}

		if (null == directive)
			return -1;

		// 发送指令
		int code = HttpUtils.postJson(JSON.toJSONString(directive));
		directive.setHttpCode(code);
		// 添加到缓存,等待入库
		DirectiveBuffer.put(directive);

		if (code != 0) {
			directive.setErrorText("网关通讯失败, code: " + code);
			DirectiveBuffer.transientList.add(directive);
		}
		return code;
	}

	@Override
	public int send60Dispatch(ScheduleRealInfo sch, int finish) {
		Directive60 directive = null;
		try {
			// 如果发车时间距当前时间较远,则不发送
			if (Math.abs(sch.getFcsjT() - System.currentTimeMillis()) > schDiff) {
				return -2;
			}

			String text = "已完成" + finish + "个班次,下一发车时间" + sdfHHmm.format(new Date(sch.getFcsjT())) + ",由"
					+ sch.getQdzName() + "发往" + sch.getZdzName();

			// 目前使用短语协议下发调度指令
			directive = create60Data(sch.getClZbh(), text, (short) 0x00, sch);
		} catch (Exception e) {
			logger.error("生成调度指令时出现异常", e);
			return -1;
		}

		if (null == directive)
			return -1;

		// 发送指令
		int code = HttpUtils.postJson(JSON.toJSONString(directive));

		sch.setDirectiveState(60);
		// 添加到缓存,等待入库
		directive.setDispatch(true);
		directive.setSch(sch);
		directive.setHttpCode(code);
		DirectiveBuffer.put(directive);

		if (code == 0) {
			// 通知页面,消息已发出
			sendDirectiveToPage(sch);
		} else {
			directive.setErrorText("网关通讯失败, code: " + code);
			DirectiveBuffer.transientList.add(directive);
		}
		return code;
	}

	/**
	 * 
	 * @Title: sendDirectiveState @Description: TODO(向页面推送班次指令状态) @throws
	 */
	@Override
	public void sendDirectiveToPage(ScheduleRealInfo sch) {
		JSONObject json = new JSONObject();
		json.put("fn", "directive");
		json.put("t", sch);
		socketHandler.sendMessageToLine(Integer.parseInt(sch.getXlBm()), json.toJSONString());
	}

	@Override
	public int send60Dispatch(Long id) {
		ScheduleRealInfo sch = ScheduleBuffer.findOne(id);
		// 车辆已完成班次
		int finish = ScheduleBuffer.getFinishSchNo(sch.getClZbh());
		return send60Dispatch(sch, finish);
	}

	@Override
	public int send60Operation(String nbbm, int state, int upDown, ScheduleRealInfo sch) {
		logger.info("切换运营状态, nbbm: " + nbbm + " ,state: " + state + " ,upDown:" + upDown);

		String text = "切换为 " + (upDown == 0 ? "上行" : "下行") + (state == 0 ? "营运" : "未营运");
		Directive60 directive = createDirective60(nbbm, text, (short) 0x03, upDown, state);

		if (null == directive)
			return -1;
		// 发送指令
		int code = HttpUtils.postJson(JSON.toJSONString(directive));
		// 添加到缓存,等待入库
		directive.setHttpCode(code);
		if (null != sch)
			directive.setSch(sch);
		DirectiveBuffer.put(directive);

		if (code != 0) {
			directive.setErrorText("网关通讯失败, code: " + code);
			DirectiveBuffer.transientList.add(directive);
		}
		return code;
	}

	/**
	 * 线路切换
	 */
	@Override
	public int lineChange(String nbbm, Integer lineId) {
		Long t = System.currentTimeMillis();
		String deviceId = CommonMapped.vehicDeviceBiMap.inverse().get(nbbm);

		Directive64 change = new Directive64();
		LineChangeData data = new LineChangeData();
		data.setCityCode(cityCode);
		data.setDeviceId(deviceId);
		data.setLineId("00" + String.valueOf(lineId));

		change.setDeviceId(deviceId);
		change.setOperCode((short) 0X64);
		change.setTimestamp(t);
		change.setData(data);

		int code = HttpUtils.postJson(JSON.toJSONString(change));
		// 入库
		change.setHttpCode(code);
		DirectiveBuffer.changeMap.put(deviceId + '_' + t, change);

		// 通知设备刷新线路文件,忽略结果
		if (code == 0)
			HttpUtils.postJson(createDeviceRefreshData(deviceId, lineId));
		else
			change.setErrorText("网关通讯失败, code: " + code);

		lineChangeRepository.save(change);
		return code;
	}

	public Directive60 create60Data(String nbbm, String text, Short dispatchInstruct, ScheduleRealInfo sch) {

		/*
		 * //向测试设备发送 String deviceId = "ABCDFEGH"; Short company = 5;
		 */

		String deviceId = CommonMapped.vehicDeviceBiMap.inverse().get(nbbm);
		if (null == deviceId) {
			logger.error("没有设备号对照的车辆:" + nbbm);
			return null;
		}
		// 上下行和营运状态
		Integer upDown = null, state = null;
		if (null == sch) {
			GpsRealData gpsData = gpsRealDataBuffer.findOneByDeviceId(deviceId);
			if (null == gpsData) {
				logger.error("没有找到gps对照,无法确认营运状态和上下行:" + nbbm);
				return null;
			}
			upDown = gpsData.getUpDown();
			state = gpsData.getState();
		} else {
			upDown = Integer.parseInt(sch.getXlDir());
			state = 0;
		}

		return createDirective60(nbbm, text, dispatchInstruct, upDown, state);
	}

	public Directive60 createDirective60(String nbbm, String text, Short dispatchInstruct, int upDown, int state) {
		Long timestamp = System.currentTimeMillis();

		Short company = Short.parseShort(CommonMapped.vehicCompanyMap.get(nbbm));
		String deviceId = CommonMapped.vehicDeviceBiMap.inverse().get(nbbm);

		int msgId = MsgIdGenerator.getMsgId();

		Directive60 directive = new Directive60();
		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[upDown][state];
		} catch (IndexOutOfBoundsException e) {
			// 未知营运状态的直接默认为上行非营运
			serviceState = Consts.SERVICE_STATE[0][1];
		}
		data.setServiceState(serviceState);
		data.setTxtContent(text);

		return directive;
	}

	@Override
	public int upDownChange(String nbbm, Integer upDown) {
		/*
		 * Directive60 directive = createDirective60(nbbm, nbbm + "_" + upDown,
		 * (short) 0x03, upDown, 0);
		 * 
		 * if(null == directive) return -1;
		 * 
		 * int code = HttpUtils.postJson(JSON.toJSONString(directive)); if(code
		 * == 0){ //添加到缓存,等待入库 DirectiveBuffer.put(directive); }else{
		 * logger.error("send60 upDownChange error, code: " + code); }
		 */
		return send60Operation(nbbm, 0, upDown, null);
	}

	/**
	 * 
	 * @Title: createDeviceRefreshData @Description:
	 *         TODO(生成设备线路刷新数据包) @param @return 设定文件 @return String 返回类型 @throws
	 */
	public String createDeviceRefreshData(String deviceId, Integer lineId) {
		Long t = System.currentTimeMillis();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("deviceId", deviceId);
		param.put("timestamp", t);
		param.put("operCode", 0Xc0);

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("operCode2", 0xa1);
		data.put("cityCode", cityCode);
		data.put("deviceId", deviceId);
		data.put("timestamp", t);
		data.put("centerId", 1);
		data.put("lineId", lineId);
		data.put("lineVersion", 0);
		data.put("carparkDataVersion", 0);
		param.put("data", data);

		return JSON.toJSONString(param);
	}

	@Override
	public Map<String, List<Directive80>> findNoCofm80(String lineCodes) {
		List<String> lineList = Splitter.on(",").trimResults().splitToList(lineCodes);

		Map<String, List<Directive80>> rs = new HashMap<>();
		for (String code : lineList) {
			rs.put(code, DirectiveBuffer.findNoCofm80(Integer.parseInt(code)));
		}

		return rs;
	}

	@Override
	public Map<String, Object> reply80(int id, int reply) {
		Map<String, Object> rs = new HashMap<>();

		Directive80 d80 = DirectiveBuffer.findById80(id);
		if (null == d80) {
			rs.put("status", ResponseCode.ERROR);
			rs.put("msg", "服务器没有找到对应数据!");
		} else if (d80.isConfirm()) {
			rs.put("status", ResponseCode.ERROR);
			rs.put("msg", "该数据已经被处理了!");
		} else {
			d80.setConfirm(true);
			d80.setConfirmRs(reply);
			// 封装C0数据包并回复设备
			DirectiveC0 c0 = new DirectiveC0();
			c0.setDeviceId(d80.getDeviceId());
			c0.setTimestamp(d80.getTimestamp());
			c0.setOperCode((short) 0xC0);

			DirectiveC0Data data = new DirectiveC0Data();
			data.setOperCode2((short) 0x86);
			data.setRequestAck((short) (reply == 0 ? 0x06 : 0x15));

			c0.setData(data);

			d80.setC0(c0);
			// 入库
			d80Repository.save(d80);

			int code = HttpUtils.postJson(JSON.toJSONString(c0));

			rs.put("status", ResponseCode.SUCCESS);
			if (code != 0)
				rs.put("msg", "发送C0响应指令到车载设备失败,但该操作已经被系统记录!");

			// 通知页面
			Map<String, Object> sockMap = new HashMap<>();
			sockMap.put("fn", "d80Confirm");
			sockMap.put("id", d80.getId());
			socketHandler.sendMessageToLine(d80.getData().getLineId(), JSON.toJSONString(sockMap));
		}

		return rs;
	}

	@Override
	public Map<String, Object> findDirective(String nbbm, int dType, int page, int size) {
		Map<String, Object> rsMap = new HashMap<>();
		List<Directive> list = null;
		
		switch (dType) {
		case -1:
			//所有指令
			list = DirectiveBuffer.findAll();
			break;
		case 0:
			//调度指令
			list = DirectiveBuffer.findDispatch();
			break;
		case 1: 
			//运营指令
			list = DirectiveBuffer.findByDispatchInstruct((short)0x03);
			break;
		case 2:
			//线路切换指令
			list = DirectiveBuffer.findLineChange();
			break;
		case 3:
			//消息短语
			list = DirectiveBuffer.findByDispatchInstruct((short)0x00);
			break;
		}

		// 时间倒序
		Collections.sort(list, new DirectiveBuffer.DComparator());
		if(StringUtils.isNotBlank(nbbm)){
			String deviceId = CommonMapped.vehicDeviceBiMap.inverse().get(nbbm);
			//按车辆过滤
			List<Directive> subList = new ArrayList<>();
			for(Directive d : list){
				if(d.getDeviceId().equals(deviceId)){
					subList.add(d);
				}
			}
			list = subList;
		}
		
		int count = list.size();
		// 分页
		int s = page * size, e = s + size;

		if (e > count)
			e = count;

		List<Directive> rs = list.subList(s, e);

		// 时间格式化,车辆自编号转换
		for (Directive d : rs) {
			if (d.getTimeHHmm() == null)
				d.setTimeHHmm(sdfHHmm2.format(new Date(d.getTimestamp())));
			if (d.getNbbm() == null)
				d.setNbbm(CommonMapped.vehicDeviceBiMap.get(d.getDeviceId()));
		}

		rsMap.put("list", rs);
		rsMap.put("totalPages", count % size == 0 ? count / size : count / size + 1);
		rsMap.put("page", page);
		return rsMap;
	}
}