package ch.hevs.isi.field;

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

public class BooleanRegister extends ModbusRegister{
    private boolean value;
    private BooleanDataPoint bdp;

    /**
     * public constructor of the booleanRegister
     *
     * @param label label of the datapoint
     * @param isOutput  true if it is an output datapoint
     * @param address   modbus address of the datapoint
     */
    public BooleanRegister(String label, boolean isOutput, int address){
        this.bdp = new BooleanDataPoint(label, isOutput);   //create an empty datapoint for the NullPointerException
        updateMapOfRegisters(label, address);       //add the address to the map
    }
    @Override
    /**
     * function to read a boolean datapoint from the modbus register
     */
    public void read() {
        if(bdp.isOutput()){
            return;      //if it is an output datapoint, it is not read
        }
        bdp.setValue(ModbusAccessor.getMySelf().readBoolean(this.getAddress()));    //read the value
    }

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