SendUtils.java 2.47 KB
package com.bsth.websocket.handler;

import java.text.SimpleDateFormat;
import java.util.Date;
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.bsth.entity.realcontrol.ScheduleRealInfo;
import com.fasterxml.jackson.databind.ObjectMapper;

@Component
public class SendUtils{

	@Autowired
	private RealControlSocketHandler socketHandler;

	private static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

	private static Logger logger = LoggerFactory.getLogger(SendUtils.class);

	/**
	 * @throws JsonProcessingException
	 *             TODO(推送发车信息)
	 */
	public void sendFcsj(ScheduleRealInfo schedule) {

		Map<String, Object> map = new HashMap<>();
		map.put("fn", "faChe");
		map.put("t", schedule);
		map.put("dataStr", sdf.format(new Date()));

		ObjectMapper mapper = new ObjectMapper();

		try {
			socketHandler.sendMessageToLine(Integer.parseInt(schedule.getXlBm()), mapper.writeValueAsString(map));
		} catch (Exception e) {
			logger.error("", e);
		}
	}
	
	/**
	 * 
	 * @Title: sendFcsjArtificial 
	 * @Description: TODO(要求页面刷新班次) 
	 * @throws
	 */
	public void refreshSch(List<ScheduleRealInfo> list){
		if(null == list || list.size() == 0)
			return;
			
		Map<String, Object> map = new HashMap<>();
		map.put("fn", "refreshSch");
		map.put("ts", list);

		ObjectMapper mapper = new ObjectMapper();

		try {
			socketHandler.sendMessageToLine(Integer.parseInt(list.get(0).getXlBm()), mapper.writeValueAsString(map));
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	/**
	 * @throws JsonProcessingException
	 * 			@throws NumberFormatException @Title: sendFcsj @Description:
	 *             TODO(推送到达终点时间) @param @param schedule 班次 @throws
	 */
	public void sendZdsj(ScheduleRealInfo schedule, ScheduleRealInfo nextSch, int finish) {

		Map<String, Object> map = new HashMap<>();
		map.put("fn", "zhongDian");
		map.put("t", schedule);
		map.put("nt", nextSch);
		map.put("finish", finish);
		map.put("dataStr", sdf.format(new Date()));

		ObjectMapper mapper = new ObjectMapper();

		try {
			socketHandler.sendMessageToLine(Integer.parseInt(schedule.getXlBm()), mapper.writeValueAsString(map));
		} catch (Exception e) {
			logger.error("", e);
		}
	}
}