FlowModel.java 2.37 KB
package com.viontech.model;

import com.viontech.Application;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import lombok.Getter;
import lombok.Setter;

import java.util.zip.CRC32;

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

    private final int[] laneNoAndType = new int[16];
    private final int[] traffic = new int[16];
    private final int[] avgSpeed = new int[16];
    private final int[] maxQueueLen = new int[16];
    private final int[] occupy = new int[16];
    private final int[] avgDistanceBetweenVehicle = new int[16];
    private final int[] trafficSupplement = new int[16];
    private int serialNum;
    private int time;
    private int timeDuring;
    private byte[] reservedField = new byte[64];


    @Override
    public void encodeData() {
        this.length = 544L;
        this.protocol = 0x00040005L;
        this.flag = Application.REAL_TIME;


        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        buffer.writeIntLE(serialNum);
        buffer.writeIntLE(time);
        buffer.writeIntLE(timeDuring);
        for (int i : laneNoAndType) {
            buffer.writeIntLE(i);
        }
        for (int i : traffic) {
            buffer.writeIntLE(i);
        }
        for (int i : avgSpeed) {
            buffer.writeIntLE(i);
        }
        for (int i : maxQueueLen) {
            buffer.writeIntLE(i);
        }
        for (int i : occupy) {
            buffer.writeIntLE(i);
        }
        for (int i : avgDistanceBetweenVehicle) {
            buffer.writeIntLE(i);
        }
        for (int i : trafficSupplement) {
            buffer.writeIntLE(i);
        }
        buffer.writeBytes(reservedField);
        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);
    }
}