FlowModel.java 2.73 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 = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
    private final int[] traffic = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
    private final int[] avgSpeed = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
    private final int[] maxQueueLen = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
    private final int[] occupy = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
    private final int[] avgDistanceBetweenVehicle = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
    private final int[] trafficSupplement = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
    private int serialNum;
    private int time;
    private int timeDuring;
    private byte[] reservedField = new byte[64];


    @Override
    public void encodeData() {
        this.length = 540L;
        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);
    }
}