FlowProcess.java 4.59 KB
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;


    }
}