GpsDataServiceImpl.java
6.93 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package com.bsth.vehicle.gpsdata.service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.bsth.util.DateUtils;
import com.bsth.util.TransGPS;
import com.bsth.util.TransGPS.Location;
import com.bsth.util.db.DBUtils_MS;
import com.bsth.vehicle.common.CommonMapped;
@Service
public class GpsDataServiceImpl implements GpsDataService{
/** 历史gps查询最大范围 24小时 */
final static Long GPS_RANGE= 60 * 60 * 24L;
//历史gps查询
@Override
public List<Map<String, Object>> history(String device, Long startTime, Long endTime, int directions) {
Calendar sCal = Calendar.getInstance();
sCal.setTime(new Date(/*startTime*/));
Calendar eCal = Calendar.getInstance();
eCal.setTime(new Date(endTime));
int dayOfYear = sCal.get(Calendar.DAY_OF_YEAR);
/*if(dayOfYear != eCal.get(Calendar.DAY_OF_YEAR)){
System.out.println("暂时不支持跨天查询...");
return null;
}*/
String sql = "select DEVICE_ID,LON,LAT,TS,INOUT_STOP,SERVICE_STATE ,STOP_NO from bsth_c_gps_info where days_year=? and device_id=? and ts > ? and ts < ?";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map = null;
try{
conn = DBUtils_MS.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, dayOfYear);
ps.setString(2, device);
ps.setLong(3, startTime);
ps.setLong(4, endTime);
rs = ps.executeQuery();
Float lon, lat;
Location location;
int upDown;
while (rs.next()) {
upDown = getUpOrDown(rs.getLong("SERVICE_STATE"));
if(upDown != directions)
continue;
//to 百度坐标
lon = rs.getFloat("LON");
lat = rs.getFloat("LAT");
location = TransGPS.LocationMake(lon, lat);
location = TransGPS.bd_encrypt(TransGPS.transformFromWGSToGCJ(location));
map = new HashMap<>();
map.put("device", rs.getString("DEVICE_ID"));
map.put("lon", location.getLng());
map.put("lat", location.getLat());
map.put("ts", rs.getLong("TS"));
map.put("stopNo", rs.getString("STOP_NO"));
map.put("inout_stop", rs.getInt("INOUT_STOP"));
//上下行
map.put("upDown", upDown);
list.add(map);
}
}catch(Exception e){
e.printStackTrace();
}finally {
DBUtils_MS.close(rs, ps, conn);
}
return list;
}
/**王通 2016/6/29 9:23:24
* 获取车辆线路上下行
* @return -1无效 0上行 1下行
*/
public static byte getUpOrDown(long serviceState) {
if ((serviceState & 0x00020000) == 0x00020000 || (serviceState & 0x80000000) == 0x80000000
|| (serviceState & 0x01000000) == 0x01000000
|| (serviceState & 0x08000000) == 0x08000000) return -1;
return (byte)(((serviceState & 0x10000000) == 0x10000000) ? 1 : 0);
}
@Override
public List<Map<String, Object>> history(String[] nbbmArray, Long st, Long et) {
List<Map<String, Object>> list = new ArrayList<>();
//超过最大查询范围,直接忽略
if(et - st > GPS_RANGE)
return list;
//转换设备号
String[] devices = new String[nbbmArray.length];
for(int i = 0; i < nbbmArray.length; i ++){
devices[i] = CommonMapped.vehicDeviceBiMap.inverse().get(nbbmArray[i]);
}
//String device = CommonMapped.vehicDeviceBiMap.inverse().get(nbbm);
//day_of_year
Calendar sCal = Calendar.getInstance();
sCal.setTime(new Date(st * 1000));
int sDayOfYear = /*sCal.get(Calendar.DAY_OF_YEAR)*/200;
Calendar eCal = Calendar.getInstance();
eCal.setTime(new Date(et * 1000));
int eDayOfYear = /*eCal.get(Calendar.DAY_OF_YEAR)*/200;
//如果是同一天
if(sDayOfYear == eDayOfYear){
list = findByTs(sDayOfYear, st, et, devices);
}
else{
//跨天
for(int i = sDayOfYear; i <= eDayOfYear; i ++){
if(i == sDayOfYear)
list.addAll(findByTs(i, st, DateUtils.getTimesnight(sCal), devices));
else if(i == eDayOfYear)
list.addAll(findByTs(i, DateUtils.getTimesmorning(sCal), et, devices));
else
list.addAll(findByTs(i, DateUtils.getTimesmorning(sCal), DateUtils.getTimesnight(sCal), devices));
//加一天
sCal.add(Calendar.DATE, 1);
}
}
//按时间排序
Collections.sort(list, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
return (int) (Long.parseLong(o1.get("ts").toString()) - Long.parseLong(o2.get("ts").toString()));
}
});;
return list;
}
public List<Map<String, Object>> findByTs(int dayOfYear,Long st, Long et, String[] devices){
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map = null;
//setArray 不好用,直接拼 in 语句
String inv = "";
for(String device : devices)
inv += ("'" + device + "',");
inv = inv.substring(0, inv.length() - 1);
String sql = "select DEVICE_ID,LON,LAT,TS,INOUT_STOP,SERVICE_STATE ,STOP_NO from bsth_c_gps_info where days_year=? and device_id in ("+inv+") and ts > ? and ts < ?";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
conn = DBUtils_MS.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, dayOfYear);
/*ps.setArray(2, conn.createArrayOf("VARCHAR", devices));*/
ps.setLong(2, st * 1000);
ps.setLong(3, et * 1000);
rs = ps.executeQuery();
Float lon, lat;
Location bdLoc, gdLoc;
int upDown;
while (rs.next()) {
upDown = getUpOrDown(rs.getLong("SERVICE_STATE"));
map = new HashMap<>();
lon = rs.getFloat("LON");
lat = rs.getFloat("LAT");
//高德坐标
gdLoc = TransGPS.transformFromWGSToGCJ(TransGPS.LocationMake(lon, lat));
map.put("gd_lon", gdLoc.getLng());
map.put("gd_lat", gdLoc.getLat());
//百度坐标
bdLoc = TransGPS.bd_encrypt(gdLoc);
map.put("bd_lon", bdLoc.getLng());
map.put("bd_lat", bdLoc.getLat());
map.put("device", rs.getString("DEVICE_ID"));
map.put("ts", rs.getLong("TS"));
map.put("stopNo", rs.getString("STOP_NO"));
map.put("inout_stop", rs.getInt("INOUT_STOP"));
map.put("nbbm", CommonMapped.vehicDeviceBiMap.get(rs.getString("DEVICE_ID")));
map.put("state", 0);
//上下行
map.put("upDown", upDown);
list.add(map);
}
}catch(Exception e){
e.printStackTrace();
}finally {
DBUtils_MS.close(rs, ps, conn);
}
return list;
}
/*
public static void main(String[] args) {
Calendar eCal = Calendar.getInstance();
eCal.setTime(new Date());
int dayOfYear = eCal.get(Calendar.DAY_OF_YEAR);
System.out.println(dayOfYear);
}*/
}