Search in sources :

Example 1 with Reg

use of org.simplify4u.jfatek.registers.Reg in project openhab1-addons by openhab.

the class FatekColorItem method valueForIncreaseDecrease.

private HSBType valueForIncreaseDecrease(FatekPLC fatekPLC, Command command) throws CommandException {
    HSBType val = null;
    try {
        // first read current state
        Map<Reg, RegValue> regVal = new FatekReadMixDataCmd(fatekPLC, reg1, reg2, reg3).send();
        HSBType currentVal = reg2HSB(regVal.get(reg1), regVal.get(reg1), regVal.get(reg3));
        int b = currentVal.getBrightness().intValue();
        if (IncreaseDecreaseType.INCREASE.equals(command)) {
            b = Math.min(b + step, 100);
        } else if (IncreaseDecreaseType.DECREASE.equals(command)) {
            b = Math.max(b - step, 0);
        } else {
            throw new CommandException(this, command, "Unknown IncreaseDecrease type");
        }
        if (b != currentVal.getBrightness().intValue()) {
            val = new HSBType(currentVal.getHue(), currentVal.getSaturation(), new PercentType(b));
        }
    } catch (FatekIOException | FatekException e) {
        throw new CommandException(this, command, e);
    }
    return val;
}
Also used : FatekIOException(org.simplify4u.jfatek.io.FatekIOException) FatekReadMixDataCmd(org.simplify4u.jfatek.FatekReadMixDataCmd) Reg(org.simplify4u.jfatek.registers.Reg) FatekException(org.simplify4u.jfatek.FatekException) PercentType(org.openhab.core.library.types.PercentType) HSBType(org.openhab.core.library.types.HSBType) RegValue(org.simplify4u.jfatek.registers.RegValue)

Example 2 with Reg

use of org.simplify4u.jfatek.registers.Reg in project openhab1-addons by openhab.

the class FatekDimmerItem method valueForIncreaseDecrease.

private int valueForIncreaseDecrease(FatekPLC fatekPLC, Command command) throws FatekIOException, FatekException {
    // read current value from PLC
    Map<Reg, RegValue> currentPlcValues = new FatekReadMixDataCmd(fatekPLC, reg1).send();
    RegValue currRegValue = currentPlcValues.get(reg1);
    long plcVal = currRegValue.longValueUnsigned();
    if (factor != null) {
        plcVal = new BigDecimal(plcVal).multiply(factor).longValue();
    }
    if (IncreaseDecreaseType.INCREASE.equals(command)) {
        plcVal += step;
    }
    if (IncreaseDecreaseType.DECREASE.equals(command)) {
        plcVal -= step;
    }
    plcVal = Math.max(plcVal, 0);
    plcVal = Math.min(plcVal, 100);
    RegValue val = RegValue.getForReg(reg1, plcVal);
    if (currRegValue.equals(val)) {
        return -1;
    }
    return (int) plcVal;
}
Also used : FatekReadMixDataCmd(org.simplify4u.jfatek.FatekReadMixDataCmd) Reg(org.simplify4u.jfatek.registers.Reg) BigDecimal(java.math.BigDecimal) RegValue(org.simplify4u.jfatek.registers.RegValue)

Example 3 with Reg

use of org.simplify4u.jfatek.registers.Reg in project openhab1-addons by openhab.

the class FatekPLCSlave method updateItems.

/**
	 * Read current data from Fatek PLC and publish new state on OpenHab bus.
	 *
	 * @param items items to updates
	 * @throws FatekIOException
	 * @throws FatekException
	 */
public void updateItems(List<FatekPLCItem> items) throws FatekIOException, FatekException {
    // collect unique register to read from PLC
    Set<Reg> regsToUpdate = new HashSet<>();
    for (FatekPLCItem item : items) {
        regsToUpdate.addAll(item.getRegs());
    }
    Map<Reg, RegValue> response;
    synchronized (fatekPLC) {
        // read register
        response = new FatekReadMixDataCmd(fatekPLC, regsToUpdate).send();
    }
    // distribute read data
    for (FatekPLCItem fatekItem : items) {
        State newState = fatekItem.getState(response);
        if (stateCache.isStateChange(fatekItem, newState)) {
            eventPublisher.postUpdate(fatekItem.getItemName(), newState);
        }
    }
}
Also used : Reg(org.simplify4u.jfatek.registers.Reg) FatekReadMixDataCmd(org.simplify4u.jfatek.FatekReadMixDataCmd) State(org.openhab.core.types.State) FatekPLCItem(org.openhab.binding.fatekplc.items.FatekPLCItem) HashSet(java.util.HashSet) RegValue(org.simplify4u.jfatek.registers.RegValue)

Example 4 with Reg

use of org.simplify4u.jfatek.registers.Reg in project openhab1-addons by openhab.

the class FatekSwitchItem method command.

@Override
public void command(FatekPLC fatekPLC, Command command) throws CommandException {
    boolean value;
    if (OnOffType.ON.equals(command)) {
        value = true;
    } else if (OnOffType.OFF.equals(command)) {
        value = false;
    } else {
        throw new UnsupportedCommandException(this, command);
    }
    Reg reg;
    if (reg2 == null) {
        if (negate1) {
            value = !value;
        }
        reg = reg1;
    } else {
        value = !negate2;
        reg = reg2;
    }
    try {
        new FatekWriteMixDataCmd(fatekPLC).addReg(reg, value).send();
    } catch (FatekIOException | FatekException e) {
        throw new CommandException(this, command, e);
    }
}
Also used : FatekWriteMixDataCmd(org.simplify4u.jfatek.FatekWriteMixDataCmd) FatekIOException(org.simplify4u.jfatek.io.FatekIOException) Reg(org.simplify4u.jfatek.registers.Reg) FatekException(org.simplify4u.jfatek.FatekException)

Aggregations

Reg (org.simplify4u.jfatek.registers.Reg)4 FatekReadMixDataCmd (org.simplify4u.jfatek.FatekReadMixDataCmd)3 RegValue (org.simplify4u.jfatek.registers.RegValue)3 FatekException (org.simplify4u.jfatek.FatekException)2 FatekIOException (org.simplify4u.jfatek.io.FatekIOException)2 BigDecimal (java.math.BigDecimal)1 HashSet (java.util.HashSet)1 FatekPLCItem (org.openhab.binding.fatekplc.items.FatekPLCItem)1 HSBType (org.openhab.core.library.types.HSBType)1 PercentType (org.openhab.core.library.types.PercentType)1 State (org.openhab.core.types.State)1 FatekWriteMixDataCmd (org.simplify4u.jfatek.FatekWriteMixDataCmd)1