TrafficProcess.java
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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.viontech.process;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.viontech.ftp.FTPClientHelper;
import com.viontech.model.BaseModel;
import com.viontech.model.TrafficModel;
import com.viontech.utils.DateUtil;
import com.viontech.utils.IntUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.util.Base64;
import java.util.Date;
import java.util.Optional;
import java.util.Properties;
@Component
public class TrafficProcess implements Process {
@Resource
private Properties manufactureLogoProp;
@Resource
private Properties vehicleColorProp;
@Resource
private Properties illegalTypeProp;
@Resource
private Properties eventProp;
@Resource
private FTPClientHelper ftpClientHelper;
@Override
public BaseModel process(JSONObject jsonObject) throws Exception {
TrafficModel model = new TrafficModel();
JSONObject eventData = jsonObject.getJSONObject("event_data");
if (eventData != null) {
JSONArray video = jsonObject.getJSONArray("video");
JSONObject vehicle = eventData.getJSONObject("vehicle");
JSONObject lane = eventData.getJSONObject("lane");
JSONObject location = eventData.getJSONObject("location");
JSONObject illegal = eventData.getJSONObject("illegal");
if (illegal != null && illegal.getInteger("state") == 0) {
return null;
}
JSONArray pics = jsonObject.getJSONArray("pics");
model.setSerialNum(IntUtils.next());
String deviceSerialnum = jsonObject.getString("vchan_refid");
// ------------------- 车速 ------------------
Float speedF = eventData.getFloat("speed");
model.setSpeed(Optional.ofNullable(speedF).orElse(0F).intValue());
// ------------------------ 车道 --------------
if (lane != null) {
Integer laneNo = lane.getInteger("number");
model.setLane(laneNo);
}
// ------------------------ 车辆信息 车牌信息 --------------
String eventTimeStr = jsonObject.getString("event_dt");
Date eventTime = DateUtil.parse(DateUtil.FORMAT_FULL, eventTimeStr);
long eventTimeL = eventTime.getTime();
model.setDiscernTime(BaseModel.toInt(eventTimeL / 1000));
model.setDiscernMillisecondTime(BaseModel.toInt(eventTimeL % 1000));
vehicle(model, vehicle);
// ------------------------ 图片 --------------
pic(model, pics, eventTime, deviceSerialnum);
// ---------------- 车速,方向,地点 --------------
location(model, location);
// ------------------ 违章 --------------------
illegal(model, illegal);
// ------------------ 录像 ---------------------
video(model, video, deviceSerialnum, eventTime);
return model;
} else {
return null;
}
}
private void video(TrafficModel model, JSONArray video, String deviceSerialnum, Date eventTime) {
if (video != null && video.size() > 0) {
JSONObject item = video.getJSONObject(0);
String src_url = item.getString("src_url");
if (src_url == null || "".equals(src_url)) {
return;
}
// todo 下载录像
byte[] bytes = Process.downloadFile(src_url);
String filename = item.getString("ofilename");
String filePath = "";
try {
filePath = Process.getFileFTPPath(eventTime, deviceSerialnum, filename);
} catch (Exception ignore) {
}
// ftpClientHelper.storeFile(filePath, bytes);
byte[] videoPathBytes = filePath.getBytes(Charset.forName("GBK"));
System.arraycopy(videoPathBytes, 0, model.getVideoPath(), 0, videoPathBytes.length);
}
}
private void pic(TrafficModel model, JSONArray pics, Date eventTime, String deviceSerialnum) {
if (pics != null && pics.size() > 0) {
// --------------- 图片2 ----------
if (pics.size() > 1) {
JSONObject pic2 = pics.getJSONObject(1);
Integer width = pic2.getInteger("width");
Integer height = pic2.getInteger("hight");
model.setSecondPictureHeight(height);
model.setSecondPictureWidth(width);
model.setSecondPictureTime(model.getDiscernTime());
model.setSecondPictureMillisecondTime(model.getDiscernMillisecondTime());
String picBase64 = pic2.getString("pic_base64");
byte[] pic2Bytes = Base64.getDecoder().decode(picBase64);
model.setPicture2(pic2Bytes);
model.setSecondPictureSize(pic2Bytes.length);
}
// ------------------ 图片1 -------------
JSONObject pic1 = pics.getJSONObject(0);
Integer width = pic1.getInteger("width");
Integer height = pic1.getInteger("hight");
model.setFirstPictureWidth(width);
model.setFirstPictureHeight(height);
String picBase64 = pic1.getString("pic_base64");
byte[] pic1Bytes = Base64.getDecoder().decode(picBase64);
model.setPicture1(pic1Bytes);
model.setFirstPictureSize(pic1Bytes.length);
String filename = pic1.getString("ofilename");
String fileFTPPath = "";
try {
fileFTPPath = Process.getFileFTPPath(eventTime, deviceSerialnum, filename);
} catch (Exception ignore) {
}
ftpClientHelper.storeFile(fileFTPPath, pic1Bytes);
byte[] pic1UrlBytes = fileFTPPath.getBytes();
System.arraycopy(pic1UrlBytes, 0, model.getPicturePath(), 0, Math.min(128, pic1UrlBytes.length));
}
}
private void vehicle(TrafficModel model, JSONObject vehicle) {
if (vehicle != null) {
JSONObject plate = vehicle.getJSONObject("plate");
// ----------------- 车牌 ------------------
if (plate != null) {
byte[] text = plate.getString("text").getBytes(Charset.forName("GBK"));
System.arraycopy(text, 0, model.getPlateText(), 0, Math.min(text.length, 16));
// todo 车牌类型待实现
model.setPlateType(1);
JSONObject rect = plate.getJSONObject("rect");
if (rect != null) {
Float top = rect.getFloat("top");
Float left = rect.getFloat("left");
Float bottom = rect.getFloat("bottom");
Float right = rect.getFloat("right");
model.setPatternTop((int) (top * 10)).setPatternLeft((int) (left * 10))
.setPatternBottom((int) (bottom * 10)).setPatternRight((int) (right * 10));
}
Integer plateScore = plate.getInteger("score");
model.setConfidence(plateScore);
// todo 车牌单双行没有
model.setPlateStructure(1);
}
JSONObject body = vehicle.getJSONObject("body");
// -------------------- 车身 --------------------
if (body != null) {
JSONObject type = body.getJSONObject("type");
if (type != null) {
// todo 大车小车 (vehicleType) 待实现
model.setVehicleType(4);
// todo 车型细分
String categoryCode = type.getString("code");
model.setVehicleCategory(1);
}
JSONObject color = body.getJSONObject("color");
if (color != null) {
// 车身颜色
String colorCode = color.getString("code");
String property = vehicleColorProp.getProperty(colorCode, "0");
int integer = Integer.parseInt(property);
model.setColor(integer);
}
JSONObject logo = body.getJSONObject("logo");
if (logo != null) {
String code = logo.getString("code");
// 车辆品牌
String property = manufactureLogoProp.getProperty(code, "0");
int integer = Integer.parseInt(property);
model.setManufacturerLogo(integer);
}
}
}
}
private void location(TrafficModel model, JSONObject location) {
if (location != null) {
JSONObject driveDirection = location.getJSONObject("drive_direction");
if (driveDirection != null) {
Integer directionCode = driveDirection.getInteger("code");
// 方向不用转换
model.setDirection(directionCode);
}
String locationName = location.getString("name");
byte[] locationNameBytes = locationName.getBytes(Charset.forName("GBK"));
System.arraycopy(locationNameBytes, 0, model.getRouteLocation(), 0, Math.min(locationNameBytes.length, 32));
}
}
private void illegal(TrafficModel model, JSONObject illegal) throws ParseException {
if (illegal != null && illegal.getInteger("state") == 1) {
String illegalCode = illegal.getString("code");
//需要交通违法类型转换 illegalCode --> illegalType
String property = illegalTypeProp.getProperty(illegalCode, "0");
int integer = Integer.parseInt(property);
model.setIllegalType(integer);
JSONObject detail = illegal.getJSONObject("detail");
if (detail != null) {
Float limitSpeed = detail.getFloat("limit_speed");
Float sparkSpeed = detail.getFloat("spark_speed");
String redStartStr = detail.getString("start_dt");
long redStart = 0;
long redEnd = 0;
if (redStartStr != null && !"".equals(redStartStr.trim())) {
redStart = DateUtil.parse(DateUtil.FORMAT_FULL, redStartStr).getTime();
}
String redEndStr = detail.getString("end_dt");
if (redEndStr != null && !"".equals(redEndStr.trim())) {
redEnd = DateUtil.parse(DateUtil.FORMAT_FULL, redEndStr).getTime();
}
model.setRedLightBeginTime((int) (redStart / 1000));
model.setRedLightBeginMillisecondTime((int) (redStart % 1000));
model.setRedLightEndTime((int) (redEnd / 1000));
model.setRedLightEndMillisecondTime((int) (redEnd % 1000));
model.setSpeedLimit(Optional.ofNullable(limitSpeed).orElse(0F).intValue());
model.setOverSpeedThreshold(Optional.ofNullable(sparkSpeed).orElse(0F).intValue());
}
}
}
}