MessageController.java 4.35 KB
package com.bsth.web;

import com.bsth.constant.Constant;
import com.bsth.socket.manager.MessageSender;
import com.bsth.socket.protocol.Message80Cmd;
import com.bsth.socket.protocol.Message;
import com.bsth.service.MessageService;
import com.bsth.socket.protocol.Message81Cmd;
import com.bsth.util.ConvertUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/message/*")
public class MessageController {
	
	private final static Logger log = LoggerFactory.getLogger(MessageController.class);
	@Autowired
	private MessageService messageService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> down(HttpServletRequest request) {
		Map<String, Object> res = new HashMap<String, Object>();
		InputStream in = null;
		Map<String, Object> param = null;
		try {
			/*in = request.getInputStream();
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			IOUtils.copy(in, bout);
			param = new ObjectMapper().readValue(bout.toByteArray(), Map.class);
			log.info(param == null ? "null" : param.toString());
			log.info(new String(bout.toByteArray(), "GBK"));*/
			Message message = new Message();
			Message80Cmd traffic80Cmd = new Message80Cmd();
			message.setCommand((byte) 0x80);
			message.setAckFlag(Constant.ACK_FLAG_COMMAND);
			message.setVin("12345678901234567");
			message.setEncryption((byte) 1);
			message.setMessageBody(traffic80Cmd);
			traffic80Cmd.setTimestamp(System.currentTimeMillis());
			traffic80Cmd.setParamNum((byte)16);
			byte[] paramIds = new byte[16];
			for (int i = 0;i < paramIds.length;i++) {
				paramIds[i] = (byte) (i + 1);
			}
			traffic80Cmd.setParamIds(paramIds);
			System.out.println("0x80cmd: " + ConvertUtil.toHexString(message.write()));
			MessageSender.getInstance().send(message);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error(param == null ? "null" : param.toString());
			log.error("消息下发异常", e);
			res.put("errCode", 1);
			res.put("errMsg", "服务端错误");
		} finally {
			try {
				if (in != null) in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				log.error("", e);
			}
		}
		log.info(res.toString());
		return res;
	}

	@RequestMapping(value = "/81", method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> down81(HttpServletRequest request) {
		Map<String, Object> res = new HashMap<String, Object>();
		InputStream in = null;
		Map<String, Object> param = null;
		try {
			Message message = new Message();
			Message81Cmd traffic81Cmd = new Message81Cmd();
			message.setCommand((byte) 0x81);
			message.setAckFlag(Constant.ACK_FLAG_COMMAND);
			message.setVin("12345678901234567");
			message.setEncryption((byte) 1);
			message.setMessageBody(traffic81Cmd);
			traffic81Cmd.setTimestamp(System.currentTimeMillis());
			traffic81Cmd.getParams().put((byte) 1, (short) 500);
			traffic81Cmd.getParams().put((byte) 2, (short) 20);
			traffic81Cmd.getParams().put((byte) 3, (short) 10000);
			//traffic81Cmd.getParams().put((byte) 5, "11111111111111111111");
			traffic81Cmd.getParams().put((byte) 9, (byte) 30);
			traffic81Cmd.setParamNum((byte) traffic81Cmd.getParams().size());
			System.out.println("0x81cmd: " + ConvertUtil.toHexString(message.write()));
			MessageSender.getInstance().send(message);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error(param == null ? "null" : param.toString());
			log.error("消息下发异常", e);
			res.put("errCode", 1);
			res.put("errMsg", "服务端错误");
		} finally {
			try {
				if (in != null) in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				log.error("", e);
			}
		}
		log.info(res.toString());
		return res;
	}
	
	@RequestMapping(value = "/up", method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> up(HttpServletRequest request) {
		messageService.up(null);
		return new HashMap<>();
	}
}