BehaviorModel.java 3.97 KB
package com.viontech.model;

import com.fasterxml.jackson.core.util.ByteArrayBuilder;
import com.viontech.Application;
import com.viontech.utils.Utils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import lombok.Getter;
import lombok.Setter;

import java.util.zip.CRC32;

/**
 * .
 *
 * @author 谢明辉
 * @date 2020/8/24
 */
@Getter
@Setter
public class BehaviorModel extends BaseModel {

    private final byte[] licensePlateText = new byte[16];
    private final byte[] recordingPath = new byte[128];
    private final byte[] eventLocation = new byte[32];
    private final byte[] eventSnapshotPath = new byte[128];
    private int serialNum;
    private int recordingStartSecond;
    private int recordingStartMillisecond;
    private int recordingEndSecond;
    private int recordingEndMillisecond;
    private int eventStartSecond;
    private int eventStartMillisecond;
    private int eventEndSecond;
    private int eventEndMillisecond;
    private int eventCode;
    private int laneNo;
    private int pictureSize;
    private int pictureWidth;
    private int pictureHeight;
    private int x;
    private int y;
    private int vehicleType;
    private int color1;
    private int speed;
    private int direction;
    private int color2;
    private int color3;
    private int color1Weights;
    private int color2Weights;
    private int color3Weights;
    private int vehicleCategory;
    private int reservedField;
    private int recordingDuration;
    private int faceWidth;
    private int faceHeight;
    private byte[] reservedField2 = new byte[24];
    private byte[] picture;


    @Override
    public void encodeData() {
        this.length = 16L + 448L + picture.length;
        this.protocol = 0x00040006L;
        this.flag = Application.REAL_TIME;
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        buffer.writeIntLE(serialNum);
        buffer.writeIntLE(recordingStartSecond);
        buffer.writeIntLE(recordingStartMillisecond);
        buffer.writeIntLE(recordingEndSecond);
        buffer.writeIntLE(recordingEndMillisecond);
        buffer.writeIntLE(eventStartSecond);
        buffer.writeIntLE(eventStartMillisecond);
        buffer.writeIntLE(eventEndSecond);
        buffer.writeIntLE(eventEndMillisecond);
        buffer.writeIntLE(eventCode);
        buffer.writeIntLE(laneNo);
        buffer.writeIntLE(pictureSize);
        buffer.writeIntLE(pictureWidth);
        buffer.writeIntLE(pictureHeight);
        buffer.writeIntLE(x);
        buffer.writeIntLE(y);
        buffer.writeBytes(licensePlateText);
        buffer.writeBytes(recordingPath);
        buffer.writeIntLE(vehicleType);
        buffer.writeIntLE(color1);
        buffer.writeIntLE(speed);
        buffer.writeIntLE(direction);
        buffer.writeBytes(eventLocation);
        buffer.writeBytes(eventSnapshotPath);
        buffer.writeIntLE(color2);
        buffer.writeIntLE(color3);
        buffer.writeIntLE(color1Weights);
        buffer.writeIntLE(color2Weights);
        buffer.writeIntLE(color3Weights);
        buffer.writeIntLE(vehicleCategory);
        buffer.writeIntLE(reservedField);
        buffer.writeIntLE(recordingDuration);
        buffer.writeIntLE(faceWidth);
        buffer.writeIntLE(faceHeight);
        buffer.writeBytes(reservedField2);
        buffer.writeBytes(picture);

        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;
        this.data[count++] = (byte) (value >> 8);
        this.data[count++] = (byte) (value >> 16);
        this.data[count] = (byte) (value >> 24);
    }
}