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;
}
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));
}
}
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);
}
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;
}
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;
}
Aggregations