Search in sources :

Example 41 with PercentType

use of org.openhab.core.library.types.PercentType 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 42 with PercentType

use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.

the class FatekColorItem method reg2HSB.

private HSBType reg2HSB(RegValue r1, RegValue r2, RegValue r3) {
    int v1 = r1.intValueUnsigned();
    int v2 = r2.intValueUnsigned();
    int v3 = r3.intValueUnsigned();
    if (isColorRGB) {
        v1 = Math.min(v1, 255);
        v2 = Math.min(v2, 255);
        v3 = Math.min(v3, 255);
        return new HSBType(new Color(v1, v2, v3));
    } else {
        v1 = Math.min(v1, 360);
        v2 = Math.min(v2, 100);
        v3 = Math.min(v3, 100);
        return new HSBType(new DecimalType(v1), new PercentType(v2), new PercentType(v3));
    }
}
Also used : Color(java.awt.Color) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) HSBType(org.openhab.core.library.types.HSBType)

Example 43 with PercentType

use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.

the class FatekPercentItem method getState.

@Override
public State getState(Map<Reg, RegValue> response) {
    int v = response.get(reg1).intValueUnsigned();
    if (factor != null) {
        v = new BigDecimal(v).multiply(factor).intValue();
    }
    v = Math.min(v, 100);
    if (v == 0) {
        return PercentType.ZERO;
    }
    if (v == 100) {
        return PercentType.HUNDRED;
    }
    return new PercentType(v);
}
Also used : PercentType(org.openhab.core.library.types.PercentType) BigDecimal(java.math.BigDecimal)

Example 44 with PercentType

use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.

the class KNXCoreTypeMapper method toDPTValue.

/*
     * (non-Javadoc)
     *
     * @see org.openhab.binding.knx.config.KNXTypeMapper#toDPTValue(org.openhab.core.types.Type, java.lang.String)
     */
@Override
public String toDPTValue(Type type, String dptID) {
    DPT dpt;
    int mainNumber = getMainNumber(dptID);
    if (mainNumber == -1) {
        logger.error("toDPTValue couldn't identify mainnumber in dptID: {}", dptID);
        return null;
    }
    try {
        DPTXlator translator = TranslatorTypes.createTranslator(mainNumber, dptID);
        dpt = translator.getType();
    } catch (KNXException e) {
        e.printStackTrace();
        return null;
    }
    // check for HSBType first, because it extends PercentType as well
    if (type instanceof HSBType) {
        Color color = ((HSBType) type).toColor();
        return "r:" + Integer.toString(color.getRed()) + " g:" + Integer.toString(color.getGreen()) + " b:" + Integer.toString(color.getBlue());
    } else if (type instanceof OnOffType) {
        return type.equals(OnOffType.OFF) ? dpt.getLowerValue() : dpt.getUpperValue();
    } else if (type instanceof UpDownType) {
        return type.equals(UpDownType.UP) ? dpt.getLowerValue() : dpt.getUpperValue();
    } else if (type instanceof IncreaseDecreaseType) {
        DPT valueDPT = ((DPTXlator3BitControlled.DPT3BitControlled) dpt).getControlDPT();
        return type.equals(IncreaseDecreaseType.DECREASE) ? valueDPT.getLowerValue() + " 5" : valueDPT.getUpperValue() + " 5";
    } else if (type instanceof OpenClosedType) {
        return type.equals(OpenClosedType.CLOSED) ? dpt.getLowerValue() : dpt.getUpperValue();
    } else if (type instanceof StopMoveType) {
        return type.equals(StopMoveType.STOP) ? dpt.getLowerValue() : dpt.getUpperValue();
    } else if (type instanceof PercentType) {
        return type.toString();
    } else if (type instanceof DecimalType) {
        switch(mainNumber) {
            case 2:
                DPT valueDPT = ((DPTXlator1BitControlled.DPT1BitControlled) dpt).getValueDPT();
                switch(((DecimalType) type).intValue()) {
                    case 0:
                        return "0 " + valueDPT.getLowerValue();
                    case 1:
                        return "0 " + valueDPT.getUpperValue();
                    case 2:
                        return "1 " + valueDPT.getLowerValue();
                    default:
                        return "1 " + valueDPT.getUpperValue();
                }
            case 18:
                int intVal = ((DecimalType) type).intValue();
                if (intVal > 63) {
                    return "learn " + (intVal - 0x80);
                } else {
                    return "activate " + intVal;
                }
            default:
                return type.toString();
        }
    } else if (type instanceof StringType) {
        return type.toString();
    } else if (type instanceof DateTimeType) {
        return formatDateTime((DateTimeType) type, dptID);
    }
    logger.debug("toDPTValue: Couldn't get value for {} dpt id {} (no mapping).", type, dptID);
    return null;
}
Also used : KNXException(tuwien.auto.calimero.exception.KNXException) StringType(org.openhab.core.library.types.StringType) Color(java.awt.Color) DPT(tuwien.auto.calimero.dptxlator.DPT) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) Datapoint(tuwien.auto.calimero.datapoint.Datapoint) StopMoveType(org.openhab.core.library.types.StopMoveType) DateTimeType(org.openhab.core.library.types.DateTimeType) DPTXlator(tuwien.auto.calimero.dptxlator.DPTXlator) OnOffType(org.openhab.core.library.types.OnOffType) OpenClosedType(org.openhab.core.library.types.OpenClosedType) DPTXlator3BitControlled(tuwien.auto.calimero.dptxlator.DPTXlator3BitControlled) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) DPTXlator1BitControlled(tuwien.auto.calimero.dptxlator.DPTXlator1BitControlled) HSBType(org.openhab.core.library.types.HSBType)

Example 45 with PercentType

use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.

the class Ipx800Dimmer method updateStateInternal.

@Override
protected boolean updateStateInternal(Type state) {
    boolean changed = false;
    if (state instanceof PercentType) {
        PercentType commandState = (PercentType) state;
        if (!lastState.equals(commandState)) {
            changed = true;
            lastState = commandState;
        }
    }
    return changed;
}
Also used : PercentType(org.openhab.core.library.types.PercentType)

Aggregations

PercentType (org.openhab.core.library.types.PercentType)105 DecimalType (org.openhab.core.library.types.DecimalType)43 State (org.openhab.core.types.State)29 StringType (org.openhab.core.library.types.StringType)27 DimmerItem (org.openhab.core.library.items.DimmerItem)22 Test (org.junit.Test)21 HSBType (org.openhab.core.library.types.HSBType)19 OnOffType (org.openhab.core.library.types.OnOffType)19 RollershutterItem (org.openhab.core.library.items.RollershutterItem)17 NumberItem (org.openhab.core.library.items.NumberItem)16 BigDecimal (java.math.BigDecimal)14 SwitchItem (org.openhab.core.library.items.SwitchItem)13 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)13 ColorItem (org.openhab.core.library.items.ColorItem)11 DateTimeType (org.openhab.core.library.types.DateTimeType)11 Calendar (java.util.Calendar)9 ContactItem (org.openhab.core.library.items.ContactItem)9 UpDownType (org.openhab.core.library.types.UpDownType)9 StopMoveType (org.openhab.core.library.types.StopMoveType)7 DateTimeItem (org.openhab.core.library.items.DateTimeItem)6