FlowProcess.java
4.59 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
package com.viontech.process;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.viontech.model.BaseModel;
import com.viontech.model.FlowModel;
import com.viontech.utils.DateUtil;
import com.viontech.utils.IntUtils;
import org.springframework.stereotype.Component;
import java.util.HashMap;
/**
* .
*
* @author 谢明辉
* @date 2020/9/1
*/
@Component
public class FlowProcess implements Process {
@Override
public BaseModel process(JSONObject jsonObject) throws Exception {
FlowModel model = new FlowModel();
model.setTimeDuring(120);
String eventDtStr = jsonObject.getString("event_dt");
if (eventDtStr != null) {
long time = DateUtil.parse(DateUtil.FORMAT_FULL, eventDtStr).getTime();
model.setTime((int) (time / 1000));
}
JSONObject eventData = jsonObject.getJSONObject("event_data");
// 序列号
model.setSerialNum(IntUtils.next());
// 机动车大车
JSONArray bigVehicle = eventData.getJSONArray("big_vehicle");
// 机动车小车
JSONArray smallVehicle = eventData.getJSONArray("small_vehicle");
// 非机动车
JSONArray xcycle = eventData.getJSONArray("xcycle");
// 行人
JSONArray pedestrian = eventData.getJSONArray("pedestrian");
HashMap<Integer, Integer> lane_index_map = new HashMap<>();
int count = cal(model, bigVehicle, lane_index_map, 0, 0);
count = cal(model, smallVehicle, lane_index_map, count, 1);
count = cal(model, xcycle, lane_index_map, count, 2);
cal(model, pedestrian, lane_index_map, count, 3);
return model;
}
/**
* @param data 数据
* @param lane_index_map 车道和index的map
* @param count maximum is 16
* @param type 0->机动车大车 1->机动车小车 2->非机动车 3->行人
*
* @return 下一个插入的index位置
*/
private int cal(FlowModel model, JSONArray data, HashMap<Integer, Integer> lane_index_map, int count, int type) {
if (data == null) {
return count;
}
for (int i = 0; i < data.size(); i++) {
if (count >= 16) {
break;
}
JSONObject item = data.getJSONObject(i);
Integer road = item.getInteger("road");
Integer index = lane_index_map.get(road);
if (index == null) {
int laneNoAndType = type == 0 || type == 1 ? (road << 16) | 0x00000001 : (road << 16) | 0x00000002;
model.getLaneNoAndType()[count] = laneNoAndType;
Integer traffic = item.getInteger("sample_num");
model.getTraffic()[count] = traffic & 0x0000FFFF;
if (type == 0) {
model.getTraffic()[count] = (traffic << 16) | model.getTraffic()[count];
} else if (type == 1 || type == 3) {
model.getTrafficSupplement()[count] = traffic & 0x0000FFFF;
} else if (type == 2) {
model.getTrafficSupplement()[count] = traffic << 16;
}
Float speed = item.getFloat("velocity");
model.getAvgSpeed()[count] = speed.intValue();
Integer queueLength = item.getInteger("queue_length");
model.getMaxQueueLen()[count] = queueLength;
Float occupy = item.getFloat("occupy");
model.getOccupy()[count] = (int) (occupy * 100);
Float distance = item.getFloat("distance");
model.getAvgDistanceBetweenVehicle()[count] = distance.intValue();
count++;
} else {
int laneType = model.getLaneNoAndType()[index] & 0x0000FFFF;
Integer traffic = item.getInteger("sample_num");
if (type == 1 || type == 3) {
model.getTrafficSupplement()[index] = model.getTrafficSupplement()[index] + (traffic & 0x0000FFFF);
model.getTraffic()[index] = model.getTraffic()[index] + (traffic & 0x0000FFFF);
} else if (laneType == 0x00000001 && type == 0) {
model.getTraffic()[index] = model.getTraffic()[index] + (traffic << 16) + (traffic & 0x0000FFFF);
} else if (laneType == 0x00000002 && type == 2) {
model.getTrafficSupplement()[index] = model.getTrafficSupplement()[index] + (traffic << 16);
model.getTraffic()[index] = model.getTraffic()[index] + (traffic & 0x0000FFFF);
}
}
}
return count;
}
}