Search in sources :

Example 1 with FatekReadMixDataCmd

use of org.simplify4u.jfatek.FatekReadMixDataCmd 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 FatekReadMixDataCmd

use of org.simplify4u.jfatek.FatekReadMixDataCmd 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 FatekReadMixDataCmd

use of org.simplify4u.jfatek.FatekReadMixDataCmd 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)

Aggregations

FatekReadMixDataCmd (org.simplify4u.jfatek.FatekReadMixDataCmd)3 Reg (org.simplify4u.jfatek.registers.Reg)3 RegValue (org.simplify4u.jfatek.registers.RegValue)3 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 FatekException (org.simplify4u.jfatek.FatekException)1 FatekIOException (org.simplify4u.jfatek.io.FatekIOException)1