BehaviorProcess.java
6.32 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
package com.viontech.process;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.viontech.model.BaseModel;
import com.viontech.model.BehaviorModel;
import com.viontech.utils.DateUtil;
import com.viontech.utils.IntUtils;
import org.apache.commons.lang3.StringUtils;
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.Optional;
import java.util.Properties;
@Component
public class BehaviorProcess implements Process {
@Resource
private Properties vehicleColorProp;
@Resource
private Properties illegalTypeProp;
@Resource
private Properties eventProp;
@Override
public BaseModel process(JSONObject jsonObject) throws ParseException {
BehaviorModel model = new BehaviorModel();
model.setSerialNum(IntUtils.next());
JSONObject eventData = jsonObject.getJSONObject("event_data");
JSONArray videos = jsonObject.getJSONArray("video");
JSONArray pics = jsonObject.getJSONArray("pics");
if (videos != null && videos.size() > 0) {
JSONObject video = videos.getJSONObject(0);
String startStr = video.getString("start_dt");
String endStr = video.getString("end_dt");
if (startStr != null && !"".equals(startStr.trim())) {
long start = DateUtil.parse(DateUtil.FORMAT_FULL, startStr,8).getTime();
model.setRecordingStartSecond((int) (start / 1000));
model.setRecordingStartMillisecond((int) (start % 1000));
}
if (endStr != null && !"".equals(endStr.trim())) {
long end = DateUtil.parse(DateUtil.FORMAT_FULL, endStr,8).getTime();
model.setRecordingEndSecond((int) (end / 1000));
model.setRecordingEndMillisecond((int) (end % 1000));
}
}
String eventDtStr = jsonObject.getString("event_dt");
if (eventDtStr != null) {
long time = DateUtil.parse(DateUtil.FORMAT_FULL, eventDtStr,8).getTime();
model.setEventStartSecond((int) (time / 1000));
model.setEventStartMillisecond((int) (time % 1000));
//结束时间暂定和开始时间一致
model.setEventEndSecond((int) (time / 1000));
model.setEventEndMillisecond((int) (time % 1000));
}
String eventType = jsonObject.getString("event_type");
if (eventType != null) {
// eventType
String eventStr = eventProp.getProperty(eventType, "0x00000000");
int event = Integer.parseInt(eventStr, 16);
model.setEventCode(event);
}
if (pics != null && pics.size() > 0) {
JSONObject pic1 = pics.getJSONObject(1);
String picBase64 = pic1.getString("pic_base64");
if (picBase64 != null) {
byte[] picBytes = Base64.getDecoder().decode(picBase64);
model.setPictureSize(picBytes.length);
model.setPicture(picBytes);
}
Integer width = pic1.getInteger("width");
Integer height = pic1.getInteger("hight");
model.setPictureWidth(Optional.ofNullable(width).orElse(0));
model.setPictureHeight(Optional.ofNullable(height).orElse(0));
//todo x,y 待赋值
}
if (eventData != null) {
JSONObject lane = eventData.getJSONObject("lane");
JSONObject vehicle = eventData.getJSONObject("vehicle");
Float speed = eventData.getFloat("speed");
JSONObject location = eventData.getJSONObject("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");
if (locationName != null) {
byte[] locationNameBytes = locationName.getBytes(Charset.forName("GBK"));
System.arraycopy(locationNameBytes, 0, model.getEventLocation(), 0, Math.min(locationNameBytes.length, 32));
}
// todo 录像路径
byte[] videoPathBytes = "testVideoPath".getBytes(Charset.forName("GBK"));
System.arraycopy(videoPathBytes, 0, model.getEventSnapshotPath(), 0, videoPathBytes.length);
}
if (speed != null) {
model.setSpeed(speed.intValue());
}
if (lane != null) {
Integer laneNo = lane.getInteger("number");
model.setLaneNo(laneNo);
}
if (vehicle != null) {
JSONObject plate = vehicle.getJSONObject("plate");
JSONObject body = vehicle.getJSONObject("body");
if (plate != null) {
String text = plate.getString("text");
if(StringUtils.isBlank(text) || StringUtils.equals(text,"无牌")){
text = "11111111";
}
byte[] bytes = text.getBytes(Charset.forName("GBK"));
System.arraycopy(bytes, 0, model.getLicensePlateText(), 0, Math.min(bytes.length, model.getLicensePlateText().length));
}
if (body != null) {
JSONObject type = body.getJSONObject("type");
JSONObject color = body.getJSONObject("color");
if (type != null) {
model.setVehicleType(4);
String categoryCode = type.getString("code");
model.setVehicleCategory(1);
}
if (color != null) {
String colorCode = color.getString("code");
String colorStr = vehicleColorProp.getProperty(colorCode, "0");
int colorParsed = Integer.parseInt(colorStr);
model.setColor1(colorParsed);
}
}
}
}
return model;
}
}