package com.yeejoin.amos.iec104.tcp.entity;

import io.netty.buffer.ByteBuf;

import java.io.IOException;

public class IeFloat extends InformationElement {

    private final float value;

    public IeFloat(float value) {
        this.value = value;
    }

    public IeFloat(ByteBuf is) throws IOException {
        value = Float.intBitsToFloat((is.readByte() & 0xff) | ((is.readByte() & 0xff) << 8));
    }

    @Override
    public int encode(byte[] buffer, int i) {

        int tempVal = Float.floatToIntBits(value);
        buffer[i++] = (byte) tempVal;
        buffer[i] = (byte) (tempVal >> 8);

        return 2;
    }

    public float getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "短浮点数值: " + value;
    }
}
