ThirdController.java
2.4 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
package com.jkdata.controller;
import com.alibaba.fastjson.JSONObject;
import com.jkdata.service.IThirdService;
import com.jkdata.tool.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/3d")
public class ThirdController {
@Autowired
private IThirdService thirdService;
@PostMapping("/receivePosition")
public Result<Map<String,Object>> save(HttpServletRequest request, HttpServletResponse response) {
Result<Map<String,Object>> result = new Result<Map<String,Object>>();
Map<String,Object> map = new HashMap<>();
String resMsg = getPostData(request);
JSONObject jsonObject = initData(resMsg);
String type = jsonObject.getString("Type");
//判断消息类型
if("location_pos".equals(type)){
map.put("message",resMsg);
thirdService.savePosition(jsonObject);
result.setResult(map);
result.setSuccess(true);
}else{
result.error500("类型错误!");
}
return result;
}
//初始化数据
private JSONObject initData(String resMsg) {
resMsg = "{\"Gateway\":\"10030049\"," +
"\"NodeId\":\"CB8A1DEB\"," +
"\"SystemId\":\"10990145\"," +
"\"Type\":\"location_pos\"," +
"\"group\":\"5\"," +
"\"groupNumber\":1," +
"\"position\":{\"x\":214.44541185285127,\"y\":210.15474327170986}," +
"\"scale\":0}";
System.out.println("***"+System.currentTimeMillis()+"报文***"+resMsg);
JSONObject jsonObject = new JSONObject();
jsonObject = JSONObject.parseObject(resMsg);
return jsonObject;
}
//解析推送报文
private String getPostData(HttpServletRequest request) {
StringBuffer data = new StringBuffer();
String line = null;
BufferedReader reader = null;
try{
reader = request.getReader();
while (null!=(line = reader.readLine())){
data.append(line);
}
}catch (Exception e){
}
return data.toString();
}
}