package ch.hevs.isi.field;

import ch.hevs.isi.core.DataPoint;
import ch.hevs.isi.core.FloatDataPoint;

import java.util.HashMap;

public class FloatRegister extends ModbusRegister{
    private Float value;
    private FloatDataPoint fdp;
    private int range;
    private int offset;

    /**
     * public constructor of the Float Register
     *
     * @param label label of the datapoint
     * @param isOutPut true if it is an output datapoint
     * @param address modbus address of the datapoint
     * @param range range of the datapoint value
     * @param offset offset of the datapoint value
     */
    public FloatRegister(String label, boolean isOutPut, int address, int range, int offset) {
        this.fdp = new FloatDataPoint(label, isOutPut);     //create an empty datapoint for the NullPointerException
        updateMapOfRegisters(label,address);    //add the address to the map
        this.range = range;
        this.offset = offset;
    }

    @Override
    /**
     *  function to read a float datapoint from the modbus register
     */
    public void read() {
        if(fdp.isOutput()){
            return;     //if it is an output datapoint, it is not read
        }
        Float value = ModbusAccessor.getMySelf().readFloat(this.getAddress()); //read the value
        value = value * range;      //calculate value
        value = value + offset;     //calculate value
        fdp.setValue(value);
    }

    @Override
    /**
     * function to write a boolean datapoint on the modbus register
     */
    public void write(DataPoint dp) {
        fdp = (FloatDataPoint) dp;      //make the empty datapoint to the desired datapoint, which is to be written on the address
        value = fdp.getValue();         //store the value of the datapoint
        ModbusAccessor.getMySelf().writeFloat(this.getAddress(), fdp.getValue());       //write the desired value on the modbus address
    }
}