TrafficProcess.java
12.9 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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 com.viontech.utils.VideoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.util.*;
@Component
@Slf4j
public class TrafficProcess implements Process {
@Resource
private Properties manufactureLogoProp;
@Resource
private Properties vehicleColorProp;
@Resource
private Properties illegalTypeProp;
@Resource
private Properties eventProp;
@Resource
private Properties plateTypeProp;
@Resource
private FTPClientHelper ftpClientHelper;
@Value("${xcycle.illegal:71-xcycleinvehicle}")
private String xcycleIllegal;
@Resource
private BehaviorProcess behaviorProcess;
@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;
}
String illegalCode = illegal.getString("code");
//非机动车违法转为事件推送
String[] illegalBehavior = xcycleIllegal.split(",");
Map<String,String> illegalBehaviorMap = new HashMap<>();
for (String s : illegalBehavior) {
String[] split = s.split("-");
illegalBehaviorMap.put(split[0],split[1]);
}
if(illegalBehaviorMap.containsKey(illegalCode)){
jsonObject.put("event_cate","behavior");
jsonObject.put("event_type",illegalBehaviorMap.get(illegalCode));
log.info("非机动车违法数据,转为事件推送");
JSONObject logs = JSONObject.parseObject(jsonObject.toJSONString());
logs.remove("pics");
logs.remove("video");
log.info("推送非机动车事件:"+logs);
return behaviorProcess.process(jsonObject);
}
log.info("收到繁星发来的一条违法记录");
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, 8);
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);
if(model.getIllegalType() <= 0){
return null;
}
// ------------------ 录像 ---------------------
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 filename = item.getString("ofilename");
// unid 应该就是 refid
String refid = item.getString("unid");
if (filename == null || "".equals(filename)) {
return;
}
String filePath;
try {
log.warn("traffic filename:"+filename);
filePath = Process.getFileFTPPath(eventTime, deviceSerialnum, filename);
// 提交推送任务
log.warn("traffic filepath:"+filePath);
VideoUtil.submitTask(refid, filePath, ftpClientHelper);
} catch (Exception ignore) {
return;
}
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);
ftpClientHelper.storeFile(fileFTPPath, pic1Bytes);
} catch (Exception ignore) {
}
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) {
String palteNum = plate.getString("text");
if (StringUtils.isBlank(palteNum) || StringUtils.equals(palteNum, "无牌")) {
palteNum = "11111111";
}
byte[] text = palteNum.getBytes(Charset.forName("GBK"));
System.arraycopy(text, 0, model.getPlateText(), 0, Math.min(text.length, 16));
//车牌类型
String colorCode = plateTypeProp.getProperty(plate.getString("color_code"), "5");
model.setPlateType(Integer.valueOf(colorCode));
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, 8).getTime();
}
String redEndStr = detail.getString("end_dt");
if (redEndStr != null && !"".equals(redEndStr.trim())) {
redEnd = DateUtil.parse(DateUtil.FORMAT_FULL, redEndStr, 8).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());
}
}
}
}