TrafficModel.java 5.47 KB
package com.viontech.model;

import com.fasterxml.jackson.core.util.ByteArrayBuilder;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.zip.CRC32;

/**
 * 发送车牌记录,包含电警记录
 *
 * @author 谢明辉
 * @date 2020/8/21
 */
@Getter
@Setter
@Accessors(chain = true)
public class TrafficModel extends BaseModel {
    /** 车牌文字 */
    private final byte[] plateText = new byte[16];
    /** 经过地点 */
    private final byte[] routeLocation = new byte[32];
    /** 录像路径 */
    private final byte[] videoPath = new byte[128];
    /** 第一张照片路径 */
    private final byte[] picturePath = new byte[128];
    /** 序列号 */
    private int serialNum;
    /** 识别车牌时间(秒) */
    private int discernTime;
    /** 识别车牌时间(毫秒) */
    private int discernMillisecondTime;
    /** 车牌类型 */
    private int plateType;
    /** 车牌识别可靠度 */
    private int confidence;
    /** 车道 */
    private int lane;
    /** 车辆类型 */
    private int vehicleType;
    /** 图片大小宽高 */
    private int secondPictureSize;
    private int secondPictureWidth;
    private int secondPictureHeight;
    private int firstPictureSize;
    private int firstPictureWidth;
    private int firstPictureHeight;
    /** 车牌在第一张图片两种 左上右下 位置 */
    private int patternLeft;
    private int patternTop;
    private int patternRight;
    private int patternBottom;
    /** 车身颜色 */
    private int color;
    /** 车速 */
    private int speed;
    /** 方向 */
    private int direction;
    /** 厂商标志 */
    private int manufacturerLogo;
    /** 三个预留位 */
    private int reservedField;
    private int reservedField2;
    private int reservedField3;
    /** 车型细分 */
    private int vehicleCategory;
    /** 车牌结构 1单行2双行3其他 */
    private int plateStructure;
    /** 违章类型 */
    private int illegalType;
    /** 预留位 */
    private int reservedField4;
    /** 第二张图片抓拍时间 */
    private int secondPictureTime;
    private int secondPictureMillisecondTime;
    /** 红灯开始时间 */
    private int redLightBeginTime;
    private int redLightBeginMillisecondTime;
    /** 红灯结束时间 */
    private int redLightEndTime;
    private int redLightEndMillisecondTime;
    /** 限速值 */
    private int speedLimit;
    /** 超速抓拍值 */
    private int overSpeedThreshold;
    /** 预留字段 */
    private byte[] reservedField5 = new byte[4];
    /** picture1 */
    private byte[] picture1 = new byte[0];
    /** picture2 */
    private byte[] picture2 = new byte[0];

    @Override

    public void encodeData() {
        this.protocol = 0x00020004L;
        this.length = 16L + 452L + picture1.length + picture2.length + 4;
        ByteArrayBuilder builder = new ByteArrayBuilder(Math.toIntExact(this.length - 16 - 4));
        builder.appendFourBytes(serialNum);
        builder.appendFourBytes(discernTime);
        builder.appendFourBytes(discernMillisecondTime);
        builder.write(plateText);
        builder.appendFourBytes(plateType);
        builder.appendFourBytes(confidence);
        builder.appendFourBytes(lane);
        builder.appendFourBytes(vehicleType);
        builder.appendFourBytes(secondPictureSize);
        builder.appendFourBytes(secondPictureWidth);
        builder.appendFourBytes(secondPictureHeight);
        builder.appendFourBytes(firstPictureSize);
        builder.appendFourBytes(firstPictureWidth);
        builder.appendFourBytes(firstPictureHeight);
        builder.appendFourBytes(patternLeft);
        builder.appendFourBytes(patternTop);
        builder.appendFourBytes(patternRight);
        builder.appendFourBytes(patternBottom);

        builder.appendFourBytes(color);
        builder.appendFourBytes(speed);
        builder.appendFourBytes(direction);
        builder.appendFourBytes(manufacturerLogo);
        builder.write(routeLocation);
        builder.write(videoPath);
        builder.write(picturePath);
        builder.appendFourBytes(reservedField);
        builder.appendFourBytes(reservedField2);
        builder.appendFourBytes(reservedField3);
        builder.appendFourBytes(vehicleCategory);
        builder.appendFourBytes(plateStructure);
        builder.appendFourBytes(illegalType);
        builder.appendFourBytes(reservedField4);
        builder.appendFourBytes(secondPictureTime);
        builder.appendFourBytes(secondPictureMillisecondTime);
        builder.appendFourBytes(redLightBeginTime);
        builder.appendFourBytes(redLightBeginMillisecondTime);
        builder.appendFourBytes(redLightEndTime);
        builder.appendFourBytes(redLightEndMillisecondTime);
        builder.appendFourBytes(speedLimit);
        builder.appendFourBytes(overSpeedThreshold);
        builder.write(reservedField5);
        builder.write(picture1);
        builder.write(picture2);
        byte[] bytes = builder.resetAndGetFirstSegment();
        int count = bytes.length;
        // crc32
        CRC32 crc32 = new CRC32();
        crc32.update(bytes);
        int value = (int) crc32.getValue();
        for (int i = 0; i < count; i++) {
            bytes[i] = (byte) (bytes[i] ^ 0x94);
        }

        this.data = new byte[count + 4];
        System.arraycopy(bytes, 0, data, 0, count);
        this.data[count++] = (byte) (value >> 24);
        this.data[count++] = (byte) (value >> 16);
        this.data[count++] = (byte) (value >> 8);
        this.data[count] = (byte) value;
    }
}