BehaviorProcess.java 6.31 KB
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).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).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).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;
    }
}