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

import com.fasterxml.jackson.core.util.ByteArrayBuilder;
import com.sun.xml.internal.stream.util.BufferAllocator;
import com.viontech.Application;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
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.deviceId = 1;
        this.protocol = 0x00020004L;
        this.length = 16L + 452L + picture1.length + picture2.length + 4;
        this.flag = Application.REAL_TIME;
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        buffer.writeIntLE(serialNum);
        buffer.writeIntLE(discernTime);
        buffer.writeIntLE(discernMillisecondTime);
        buffer.writeBytes(plateText);
        buffer.writeIntLE(plateType);
        buffer.writeIntLE(confidence);
        buffer.writeIntLE(lane);
        buffer.writeIntLE(vehicleType);
        buffer.writeIntLE(secondPictureSize);
        buffer.writeIntLE(secondPictureWidth);
        buffer.writeIntLE(secondPictureHeight);
        buffer.writeIntLE(firstPictureSize);
        buffer.writeIntLE(firstPictureWidth);
        buffer.writeIntLE(firstPictureHeight);
        buffer.writeIntLE(patternLeft);
        buffer.writeIntLE(patternTop);
        buffer.writeIntLE(patternRight);
        buffer.writeIntLE(patternBottom);
        buffer.writeIntLE(color);
        buffer.writeIntLE(speed);
        buffer.writeIntLE(direction);
        buffer.writeIntLE(manufacturerLogo);
        buffer.writeBytes(routeLocation);
        buffer.writeBytes(videoPath);
        buffer.writeBytes(picturePath);
        buffer.writeIntLE(reservedField);
        buffer.writeIntLE(reservedField2);
        buffer.writeIntLE(reservedField3);
        buffer.writeIntLE(vehicleCategory);
        buffer.writeIntLE(plateStructure);
        buffer.writeIntLE(illegalType);
        buffer.writeIntLE(reservedField4);
        buffer.writeIntLE(secondPictureTime);
        buffer.writeIntLE(secondPictureMillisecondTime);
        buffer.writeIntLE(redLightBeginTime);
        buffer.writeIntLE(redLightBeginMillisecondTime);
        buffer.writeIntLE(redLightEndTime);
        buffer.writeIntLE(redLightEndMillisecondTime);
        buffer.writeIntLE(speedLimit);
        buffer.writeIntLE(overSpeedThreshold);
        buffer.writeBytes(reservedField5);
        buffer.writeBytes(picture1);
        buffer.writeBytes(picture2);
        byte[] bytes = new byte[buffer.readableBytes()];
        buffer.readBytes(bytes);
        buffer.release();

        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;
    }
}