TrafficProcess.java 11.3 KB
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 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.Date;
import java.util.Optional;
import java.util.Properties;

@Component
public class TrafficProcess implements Process {

    @Resource
    private Properties manufactureLogoProp;
    @Resource
    private Properties vehicleColorProp;
    @Resource
    private Properties illegalTypeProp;
    @Resource
    private Properties eventProp;
    @Resource
    private FTPClientHelper ftpClientHelper;


    @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;
            }
            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);
            // ------------------ 录像 ---------------------
            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 src_url = item.getString("src_url");
            if (src_url == null || "".equals(src_url)) {
                return;
            }
            // todo 下载录像
            byte[] bytes = Process.downloadFile(src_url);
            String filename = item.getString("ofilename");
            String filePath = "";
            try {
                filePath = Process.getFileFTPPath(eventTime, deviceSerialnum, filename);
            } catch (Exception ignore) {
            }
//            ftpClientHelper.storeFile(filePath, bytes);
            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);
            } catch (Exception ignore) {
            }

            ftpClientHelper.storeFile(fileFTPPath, pic1Bytes);
            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));
                // todo 车牌类型待实现
                model.setPlateType(1);
                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());
            }
        }
    }
}