GpsManager.java
4.73 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
package cn.com.glsx.entmicro.manager;
import cn.com.glsx.entmicro.constants.Constants;
import cn.com.glsx.entmicro.model.collision.GopBaseResponse;
import cn.com.glsx.entmicro.model.collision.GpsRecordsResponse;
import cn.com.glsx.entmicro.util.GopUtils;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.framework.core.util.DateUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Consts;
import org.oreframework.util.http.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
/**
* gps 定位服务
*/
@Slf4j
@Component
public class GpsManager {
@Autowired
private Environment environment;
private final static String APPKEY = "48e5e13229b82c1b4e6e8c96151f0637";
private static final String VERSION = "1.0.0";
private static final String FORMAT = "json";
private static final String SECRET = "c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b";
public List<GpsRecordsResponse> getGpsRecords(String sn, Date collisionTime) {
try {
Calendar beforeTime=Calendar.getInstance();
beforeTime.setTime(collisionTime);
beforeTime.add(Calendar.MINUTE, -3);
Calendar afterTime=Calendar.getInstance();
afterTime.setTime(collisionTime);
afterTime.add(Calendar.MINUTE, 3);
MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
form.add("sn", sn);
form.add("startTime", DateUtils.formatDate(beforeTime.getTime(), DateUtils.DATE_FORMAT_DATE_TIME));
form.add("endTime", DateUtils.formatDate(afterTime.getTime(), DateUtils.DATE_FORMAT_DATE_TIME));
form.add("method", Constants.GPS_RECORDES);
form.add("app_key", APPKEY);
form.add("v", VERSION);
form.add("format", FORMAT);
form.add("source", "scrm-ent");
form.add("timestamp", String.valueOf(System.currentTimeMillis()));
String sign = GopUtils.sign(form.toSingleValueMap(), SECRET);
form.add("sign", sign);
String url = environment.getProperty("gop.open.url.prefix");
url = url + gainJointGetParams(form);
log.info("请求gps服务参数[{}]", url);
String result = HttpUtil.get(url,null);
if(result != null) {
log.info("请求gps服务,返回信息[{}]", result);
result = result.replaceAll(":null", ":\"\"");
//去掉商户名称尾缀
String merchantNameAffix = environment.getProperty("replace.merchant.name.affix");
if(StringUtils.isNotBlank(merchantNameAffix)) {
String[] affixs = merchantNameAffix.split(",");
for(String affix:affixs){
String reg = "(\"merchantName\":\"([^\"]*?))" + affix + "(\")";
result = result.replaceAll(reg, "$1$3");
}
}
GopBaseResponse response = JSONObject.parseObject(result, GopBaseResponse.class);
if(response != null && response.isSuccess()) {
return response.getList(GpsRecordsResponse.class);
} else {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "获取设备轨迹数据失败");
}
} else {
log.info("请求gps返回数据为空!");
return null;
}
} catch (Exception e) {
log.error("请求gps服务异常", e);
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
}
}
private static String gainJointGetParams (MultiValueMap<String, String> form) {
Iterator<String> iter = form.keySet().iterator();
StringBuilder param = new StringBuilder("");
while (iter.hasNext()) {
Object key = iter.next();
String val = form.get(key).toString();
val = val.substring(1, val.length() - 1);
try {
param.append(key).append("=").append(URLEncoder.encode(val, Consts.UTF_8.name())).append("&");
} catch (Exception e) {
log.info(e.getMessage(), e);
}
}
return param.substring(0, param.length() - 1);
}
}