use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.
the class RFXComLighting3Message method convertFromState.
@Override
public void convertFromState(RFXComValueSelector valueSelector, String id, Object subType, Type type, byte seqNumber) throws RFXComException {
this.subType = ((SubType) subType);
seqNbr = seqNumber;
switch(valueSelector) {
case COMMAND:
if (type instanceof OnOffType) {
command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
dimmingLevel = 0;
} else if (type instanceof DecimalType) {
command = Commands.fromByte(((DecimalType) type).intValue());
dimmingLevel = 0;
} else {
throw new RFXComException("Can't convert " + type + " to Command");
}
break;
case DIMMING_LEVEL:
if (type instanceof OnOffType) {
command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
dimmingLevel = 0;
} else if (type instanceof PercentType) {
command = Commands.DIM;
dimmingLevel = (byte) getDimLevelFromPercentType((PercentType) type);
if (dimmingLevel == 0) {
command = Commands.OFF;
}
} else if (type instanceof IncreaseDecreaseType) {
command = Commands.DIM;
// Evert: I do not know how to get previous object state...
dimmingLevel = 5;
} else {
throw new RFXComException("Can't convert " + type + " to Command");
}
break;
default:
throw new RFXComException("Can't convert " + type + " to " + valueSelector);
}
}
use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.
the class SoulissBinding method receiveCommand.
@Override
public /**
* Get the souliss's typical from the hash table and send a command
*
* @author Tonino Fazio
* @since 1.7.0
*/
void receiveCommand(String itemName, Command command) {
// Get the typical defined in the hash table
SoulissGenericTypical T = SoulissGenericBindingProvider.SoulissTypicalsRecipients.getTypicalFromItem(itemName);
logger.info("receiveCommand - {} = {} - Typical: 0x{}", itemName, command, Integer.toHexString(T.getType()));
switch(T.getType()) {
case Constants.Souliss_T11:
SoulissT11 T11 = (SoulissT11) T;
T11.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), command.toString()));
break;
case Constants.Souliss_T12:
SoulissT12 T12 = (SoulissT12) T;
if (itemName.equals(T12.getsItemNameAutoModeValue())) {
T12.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), Constants.Souliss_T12_Use_Of_Slot_AUTOMODE + "_" + command.toString()));
} else if (itemName.equals(T12.getsItemNameSwitchValue())) {
T12.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), Constants.Souliss_T12_Use_Of_Slot_SWITCH + "_" + command.toString()));
}
break;
case Constants.Souliss_T14:
SoulissT14 T14 = (SoulissT14) T;
T14.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), command.toString()));
break;
case Constants.Souliss_T18:
SoulissT18 T18 = (SoulissT18) T;
T18.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), command.toString()));
break;
case Constants.Souliss_T16:
SoulissT16 T16 = (SoulissT16) T;
String cmd = command.getClass().getSimpleName();
if (cmd.equals(Constants.Openhab_RGB_TYPE)) {
String[] HSB = command.toString().split(",");
short[] RGB = HSBtoRGB(Float.parseFloat(HSB[0]), Float.parseFloat(HSB[1]), Float.parseFloat(HSB[2]));
T16.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), command.getClass().getSimpleName()), RGB[0], RGB[1], RGB[2]);
} else {
T16.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), command.toString()));
}
break;
case Constants.Souliss_T19:
SoulissT19 T19 = (SoulissT19) T;
if (command instanceof PercentType) {
int percentToShort = (((PercentType) command).shortValue() * 254 / 100);
T19.commandSEND(Constants.Souliss_T1n_Set, Short.parseShort(String.valueOf(percentToShort)));
} else if (command instanceof DecimalType) {
int decimalToShort = (((DecimalType) command).shortValue() * 254 / 100);
T19.commandSEND(Constants.Souliss_T1n_Set, Short.parseShort(String.valueOf(decimalToShort)));
} else {
T19.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), command.toString()));
}
break;
case Constants.Souliss_T21:
SoulissT21 T21 = (SoulissT21) T;
T21.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), command.toString()));
break;
case Constants.Souliss_T22:
SoulissT22 T22 = (SoulissT22) T;
T22.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), command.toString()));
break;
case Constants.Souliss_T31:
SoulissT31 T31 = (SoulissT31) T;
// Setpoint
if (itemName.equals(T31.getsItemNameSetpointValue())) {
if (command instanceof DecimalType) {
int uu = HalfFloatUtils.fromFloat(((DecimalType) command).floatValue());
byte B2 = (byte) (uu >> 8);
byte B1 = (byte) uu;
// setpoint command
T31.CommandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), Constants.Souliss_T31_Use_Of_Slot_SETPOINT_COMMAND), B1, B2);
}
} else // Set As Measured
if (itemName.equals(T31.setAsMeasured.getName())) {
T31.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), Constants.Souliss_T31_Use_Of_Slot_SETASMEASURED + "_" + command.toString()));
} else if (itemName.equals(T31.heatingCoolingModeValue.getName())) {
T31.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), Constants.Souliss_T31_Use_Of_Slot_HEATING_COOLING + "_" + command.toString()));
} else if (itemName.equals(T31.fanAutoMode.getName())) {
T31.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), Constants.Souliss_T31_Use_Of_Slot_FANAUTOMODE + "_" + command.toString()));
} else if (itemName.equals(T31.fanOff.getName())) {
T31.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), Constants.Souliss_T31_Use_Of_Slot_FANOFF + "_" + command.toString()));
} else if (itemName.equals(T31.fanLow.getName())) {
T31.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), Constants.Souliss_T31_Use_Of_Slot_FANLOW + "_" + command.toString()));
} else if (itemName.equals(T31.fanMed.getName())) {
T31.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), Constants.Souliss_T31_Use_Of_Slot_FANMED + "_" + command.toString()));
} else if (itemName.equals(T31.fanHigh.getName())) {
T31.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), Constants.Souliss_T31_Use_Of_Slot_FANHIGH + "_" + command.toString()));
} else if (itemName.equals(T31.power.getName())) {
T31.commandSEND(StateTraslator.commandsOHtoSOULISS(T.getType(), Constants.Souliss_T31_Use_Of_Slot_POWER + "_" + command.toString()));
}
break;
default:
logger.debug("Typical Unknown");
}
}
use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.
the class DigitalSTROMBinding method deviceCall.
private void deviceCall(String itemName, Command cm) {
Device device = deviceMap.get(itemName);
if (device != null) {
if (cm instanceof org.openhab.core.library.types.OnOffType) {
if (((org.openhab.core.library.types.OnOffType) cm).equals(OnOffType.ON)) {
boolean transmitted = digitalSTROM.turnDeviceOn(getSessionToken(), device.getDSID(), null);
if (transmitted) {
device.setOutputValue(device.getMaxOutPutValue());
addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.MAXIMUM.getSceneNumber());
}
} else if (((org.openhab.core.library.types.OnOffType) cm).equals(OnOffType.OFF)) {
boolean transmitted = digitalSTROM.turnDeviceOff(getSessionToken(), device.getDSID(), null);
if (transmitted) {
device.setOutputValue(0);
addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.MINIMUM.getSceneNumber());
}
}
} else if (cm instanceof org.openhab.core.library.types.IncreaseDecreaseType) {
if (!device.isDimmable()) {
logger.warn("device is not in dimm mode: " + itemName + " outputMode: " + device.getOutputMode().getMode());
return;
}
if (((org.openhab.core.library.types.IncreaseDecreaseType) cm).equals(IncreaseDecreaseType.INCREASE)) {
boolean transmitted = digitalSTROM.callDeviceScene(getSessionToken(), device.getDSID(), null, ZoneSceneEnum.INCREMENT, false);
if (transmitted) {
addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.INCREMENT.getSceneNumber());
if (device.getOutputValue() == 0) {
initDeviceOutputValue(device, DeviceConstants.DEVICE_SENSOR_OUTPUT);
} else {
device.increase();
}
} else {
logger.error("transmitting increase command FAILED " + itemName);
}
} else if (((org.openhab.core.library.types.IncreaseDecreaseType) cm).equals(IncreaseDecreaseType.DECREASE)) {
boolean transmitted = digitalSTROM.callDeviceScene(getSessionToken(), device.getDSID(), null, ZoneSceneEnum.DECREMENT, false);
if (transmitted) {
addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.DECREMENT.getSceneNumber());
device.decrease();
} else {
logger.error("transmitting decrease command FAILED " + itemName);
}
}
} else if (cm instanceof org.openhab.core.library.types.PercentType) {
int percent = -1;
try {
percent = (int) Float.parseFloat(cm.toString());
} catch (java.lang.NumberFormatException e) {
logger.error("NumberFormatException on a PercentType with command: " + cm.toString());
}
if (percent != -1) {
if (percent > -1 && percent < 101) {
if (device.getOutputMode().equals(OutputModeEnum.SLAT)) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
if (confItem != null) {
if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
int old = device.getSlatPosition();
device.setSlatPosition(fromPercentToValue(percent, device.getMaxSlatPosition()));
boolean transmitted = digitalSTROM.setDeviceOutputValue(getSessionToken(), device.getDSID(), null, DeviceConstants.DEVICE_SENSOR_SLAT_OUTPUT, fromPercentToValue(percent, device.getMaxSlatPosition()));
if (!transmitted) {
device.setSlatPosition(old);
logger.error("could NOT successfully set new value for slats ..." + cm.toString());
}
} else {
int old = device.getOutputValue();
device.setOutputValue(fromPercentToValue(percent, device.getMaxOutPutValue()));
boolean transmitted = digitalSTROM.setDeviceValue(getSessionToken(), device.getDSID(), null, fromPercentToValue(percent, device.getMaxOutPutValue()));
if (!transmitted) {
device.setOutputValue(old);
logger.error("could NOT successfully set new value ..." + cm.toString());
}
}
}
} else {
int old = device.getOutputValue();
device.setOutputValue(fromPercentToValue(percent, device.getMaxOutPutValue()));
boolean transmitted = digitalSTROM.setDeviceValue(getSessionToken(), device.getDSID(), null, fromPercentToValue(percent, device.getMaxOutPutValue()));
if (!transmitted) {
device.setOutputValue(old);
logger.error("could NOT successfully set new value ..." + cm.toString());
}
}
}
}
} else if (cm instanceof org.openhab.core.library.types.StopMoveType) {
if (device.getOutputMode().equals(OutputModeEnum.SLAT)) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
if (confItem != null) {
if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
logger.warn("stop and move command NOT possible for slats, use PercentType command or up and down please");
} else {
handleStopMoveForRollershutter(device, cm);
}
}
} else if (device.getOutputMode().equals(OutputModeEnum.UP_DOWN)) {
handleStopMoveForRollershutter(device, cm);
}
} else if (cm instanceof org.openhab.core.library.types.UpDownType) {
if (device.getOutputMode().equals(OutputModeEnum.SLAT)) {
// 255 is max open, 0 is closed
DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
if (confItem != null) {
if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
if (((org.openhab.core.library.types.UpDownType) cm).equals(UpDownType.UP)) {
int slatPosition = device.getSlatPosition();
int newPosition = slatPosition + DeviceConstants.MOVE_STEP_SLAT;
if (newPosition > device.getMaxSlatPosition()) {
newPosition = device.getMaxSlatPosition();
}
boolean transmitted = digitalSTROM.setDeviceOutputValue(getSessionToken(), device.getDSID(), null, DeviceConstants.DEVICE_SENSOR_SLAT_OUTPUT, newPosition);
if (transmitted) {
device.setSlatPosition(newPosition);
}
} else if (((org.openhab.core.library.types.UpDownType) cm).equals(UpDownType.DOWN)) {
int slatPosition = device.getSlatPosition();
int newPosition = slatPosition - DeviceConstants.MOVE_STEP_SLAT;
if (newPosition < device.getMinSlatPosition()) {
newPosition = device.getMinSlatPosition();
}
boolean transmitted = digitalSTROM.setDeviceOutputValue(getSessionToken(), device.getDSID(), null, DeviceConstants.DEVICE_SENSOR_SLAT_OUTPUT, newPosition);
if (transmitted) {
device.setSlatPosition(newPosition);
}
}
} else {
handleUpDownForRollershutter(device, cm);
}
}
} else if (device.getOutputMode().equals(OutputModeEnum.UP_DOWN)) {
handleUpDownForRollershutter(device, cm);
} else {
logger.warn("Wrong item configuration ... this hardware is not a rollershutter: " + itemName);
}
}
} else {
if (cm instanceof DecimalType) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
if (confItem != null && confItem.context != null) {
if (confItem.context.equals(ContextConfig.apartment)) {
digitalSTROM.callApartmentScene(getSessionToken(), confItem.groupID, null, ApartmentSceneEnum.getApartmentScene(((DecimalType) cm).intValue()), false);
} else if (confItem.context.equals(ContextConfig.zone)) {
digitalSTROM.callZoneScene(getSessionToken(), confItem.zoneID, null, confItem.groupID, null, ZoneSceneEnum.getZoneScene(((DecimalType) cm).intValue()), false);
}
}
} else if (cm instanceof StringType) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
if (confItem != null && confItem.context != null) {
int scene = -1;
try {
scene = Integer.parseInt(cm.toString());
} catch (java.lang.NumberFormatException e) {
logger.error("NumberFormatException by parsing " + cm.toString() + " for " + confItem.itemName);
}
if (scene != -1) {
if (confItem.context.equals(ContextConfig.apartment)) {
digitalSTROM.callApartmentScene(getSessionToken(), confItem.groupID, null, ApartmentSceneEnum.getApartmentScene(scene), false);
} else if (confItem.context.equals(ContextConfig.zone)) {
digitalSTROM.callZoneScene(getSessionToken(), confItem.zoneID, null, confItem.groupID, null, ZoneSceneEnum.getZoneScene(scene), false);
}
}
}
} else {
logger.warn("couldn't find digitalstrom device for " + itemName);
}
}
}
use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.
the class DigitalSTROMBinding method updateItemState.
private void updateItemState(Item item) {
if (item != null) {
Device device = deviceMap.get(item.getName());
if (device != null) {
State state = null;
if (item instanceof DimmerItem) {
state = new PercentType(getPercent(device.getMaxOutPutValue(), device.getOutputValue()));
} else if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
state = device.getOutputValue() > 0 ? OnOffType.ON : OnOffType.OFF;
} else if (item instanceof NumberItem) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(item.getName());
if (confItem != null) {
if (confItem.sensor != null) {
int value = -1;
switch(confItem.sensor) {
case TEMPERATURE_INDOORS:
value = device.getTemperatureSensorValue();
if (value != -1) {
// Celsius
// TODO use resolution
state = new DecimalType((double) (value) / 40 - 43.15);
// from sensor
}
break;
case RELATIVE_HUMIDITY_INDOORS:
value = device.getHumiditySensorValue();
if (value != -1) {
// Percent
// TODO use resolution from
state = new DecimalType((double) (value) / 40);
// sensor
}
break;
case TEMPERATURE_OUTDOORS:
value = device.getTemperatureSensorValue();
if (value != -1) {
// Celsius
// TODO use resolution
state = new DecimalType((double) (value) / 40 - 43.15);
// from sensor
}
break;
case RELATIVE_HUMIDITY_OUTDOORS:
value = device.getHumiditySensorValue();
if (value != -1) {
// Percent
// TODO use resolution from
state = new DecimalType((double) (value) / 40);
// sensor
}
break;
default:
break;
}
} else if (confItem.consumption != null) {
int value = -1;
switch(confItem.consumption) {
case ACTIVE_POWER:
value = device.getPowerConsumption();
break;
case OUTPUT_CURRENT:
value = device.getEnergyMeterValue();
if (value != -1) {
state = new DecimalType(value);
}
break;
default:
break;
}
}
}
} else if (item instanceof RollershutterItem) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(item.getName());
if (confItem != null) {
if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
int output = getPercent(device.getMaxSlatPosition(), device.getSlatPosition());
state = new PercentType(100 - output);
} else if (confItem.context != null && confItem.context.equals(ContextConfig.awning)) {
int output = getPercent(device.getMaxOutPutValue(), device.getOutputValue());
state = new PercentType(output);
} else {
int output = getPercent(device.getMaxOutPutValue(), device.getOutputValue());
state = new PercentType(100 - output);
}
}
} else if (item instanceof StringItem) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(item.getName());
if (confItem != null) {
if (confItem.sensor != null) {
int value = -1;
switch(confItem.sensor) {
case TEMPERATURE_INDOORS:
value = device.getTemperatureSensorValue();
if (value != -1) {
// Celsius
// TODO use resolution
state = new DecimalType((double) (value) / 40 - 43.15);
// from sensor
}
break;
case RELATIVE_HUMIDITY_INDOORS:
value = device.getHumiditySensorValue();
if (value != -1) {
// Percent
// TODO use resolution from
state = new DecimalType((double) (value) / 40);
// sensor
}
break;
case TEMPERATURE_OUTDOORS:
value = device.getTemperatureSensorValue();
if (value != -1) {
// Celsius
// TODO use resolution
state = new DecimalType((double) (value) / 40 - 43.15);
// from sensor
}
break;
case RELATIVE_HUMIDITY_OUTDOORS:
value = device.getHumiditySensorValue();
if (value != -1) {
// Percent
// TODO use resolution from
state = new DecimalType((double) (value) / 40);
// sensor
}
break;
default:
break;
}
} else if (confItem.consumption != null) {
int value = -1;
switch(confItem.consumption) {
case ACTIVE_POWER:
value = device.getPowerConsumption();
if (value != -1) {
state = new DecimalType(value);
}
break;
case OUTPUT_CURRENT:
value = device.getEnergyMeterValue();
if (value != -1) {
state = new DecimalType(value);
}
break;
default:
break;
}
}
}
}
eventPublisher.postUpdate(item.getName(), state);
} else {
logger.error("couldn't update item state, because device is null: " + item.getName());
}
} else {
logger.error("couldn't update item state, because item is null");
}
}
use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.
the class DenonConnector method processUpdate.
/**
* This method tries to parse information received over the telnet connection.
* It's quite unreliable. Some chars go missing or turn into other chars. That's
* why each command is validated using a regex.
*
* @param commandString The received command (one line)
*/
private void processUpdate(String commandString) {
if (COMMAND_PATTERN.matcher(commandString).matches()) {
/*
* This splits the commandString into the command and the parameter. SICD
* for example has SI as the command and CD as the parameter.
*/
String command = commandString.substring(0, 2);
String value = commandString.substring(2, commandString.length()).trim();
// Secondary zone commands with a parameter
if (ZONE_SUBCOMMAND_PATTERN.matcher(commandString).matches()) {
command = commandString.substring(0, 4);
value = commandString.substring(4, commandString.length()).trim();
}
logger.debug("Command: {}, value: {}", command, value);
if (value.equals("ON") || value.equals("OFF")) {
sendUpdate(command, OnOffType.valueOf(value));
} else if (value.equals("STANDBY")) {
sendUpdate(command, OnOffType.OFF);
} else if (StringUtils.isNumeric(value)) {
PercentType percent = new PercentType(fromDenonValue(value));
command = translateVolumeCommand(command);
sendUpdate(command, percent);
} else if (command.equals("SI")) {
sendUpdate(DenonProperty.INPUT.getCode(), new StringType(value));
sendUpdate(commandString, OnOffType.ON);
} else if (command.equals("MS")) {
sendUpdate(DenonProperty.SURROUND_MODE.getCode(), new StringType(value));
} else if (command.equals("NS")) {
processTitleCommand(command, value);
}
} else {
logger.debug("Invalid command: " + commandString);
}
}
Aggregations