MessageController.java
4.35 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
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<>();
}
}