DeviceGpsController.java
4.11 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
package com.bsth.controller;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.bsth.data.gpsdata.GpsEntity;
import com.bsth.data.gpsdata.GpsRealData;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@RestController
@RequestMapping("devicegps")
public class DeviceGpsController {
@Autowired
GpsRealData gpsRealData;
@RequestMapping(value = "/real/line/{lineCode}")
public List<GpsEntity> findByLineCode(@PathVariable("lineCode") String lineCode) {
return gpsRealData.getByLine(lineCode);
}
@RequestMapping(value = "/real/all")
public List<GpsEntity> findByLineCodes() {
return new ArrayList<>(gpsRealData.all());
}
@RequestMapping(value = "/open", method = RequestMethod.POST)
public Map<String, Object> open(@RequestParam(value = "_txt_", required = false) MultipartFile file, HttpServletRequest request) {
Map<String, Object> res = new HashMap<>();
File rf = new File(request.getServletContext().getRealPath("/temp"), System.currentTimeMillis() + "");
File rd = rf.getParentFile();
if (!rd.exists()) {
rd.mkdirs();
}
BufferedReader reader = null;
try {
file.transferTo(rf);
reader = new BufferedReader(new InputStreamReader(new FileInputStream(rf), "GBK"));
String line = null;
List<Map<String, Object>> data = new ArrayList<>();
while ((line = reader.readLine()) != null) {
if (!StringUtils.isEmpty(line)) {
String temp[] = line.split(",");
if (temp.length != 4) {
res.put("errCode", 1);
res.put("msg", "txt文档格式错误,请检查");
return res;
}
Map<String, Object> info = new HashMap<>();
info.put("lineName", temp[0]);
info.put("lineCode", temp[1]);
info.put("inCode", temp[2]);
info.put("deviceId", temp[3]);
info.put("detail", gpsRealData.get(temp[3]));
data.add(info);
}
}
// 删除临时文件
rf.delete();
res.put("errCode", 0);
res.put("data", data);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (reader != null) reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return res;
}
@SuppressWarnings("unchecked")
@RequestMapping(value = "/open/filter", method = RequestMethod.POST)
public Map<String, Object> filter(@RequestParam(value = "json")String json) {
json = StringEscapeUtils.unescapeHtml(json);
Map<String, Object> res = new HashMap<>();
List<Map<String, Object>> data = null;
try {
data = new ObjectMapper().readValue(json, List.class);
for (Map<String, Object> info : data) {
info.put("detail", gpsRealData.findByDeviceId((String)info.get("deviceId")));
}
res.put("errCode", 0);
res.put("data", data);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
}