use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.
the class PowerMaxBinding method internalReceiveCommand.
/**
* {@inheritDoc}
*/
@Override
protected void internalReceiveCommand(String itemName, Command command) {
// the code being executed when a command was sent on the openHAB
// event bus goes here. This method is only called if one of the
// BindingProviders provide a binding for the given 'itemName'.
logger.debug("internalReceiveCommand({},{}) is called", itemName, command);
if (!isProperlyConfigured() || !connected) {
logger.warn("PowerMax alarm binding is not properly configured. Command is ignored.");
return;
}
String armMode = null;
String actionPGMX10 = null;
Byte devicePGMX10 = null;
Boolean bypass = null;
byte zoneNr = 0;
String commandStr = null;
PowerMaxPanelSettings settings = PowerMaxPanelSettings.getThePanelSettings();
PowerMaxCommDriver comm = PowerMaxCommDriver.getTheCommDriver();
for (PowerMaxBindingProvider provider : providers) {
PowerMaxBindingConfig config = provider.getConfig(itemName);
if (config == null) {
continue;
}
switch(config.getSelectorType()) {
case PARTITION_ARM_MODE:
if (command instanceof StringType) {
armMode = command.toString();
}
break;
case PARTITION_ARMED:
if (command instanceof OnOffType) {
armMode = command.equals(OnOffType.ON) ? "Armed" : "Disarmed";
}
break;
case PGM_STATUS:
if (command instanceof OnOffType) {
actionPGMX10 = command.toString();
}
break;
case X10_STATUS:
if (command instanceof OnOffType || command instanceof StringType) {
actionPGMX10 = command.toString();
}
try {
devicePGMX10 = Byte.parseByte(config.getSelectorParam());
} catch (NumberFormatException e) {
logger.warn("PowerMax alarm binding: invalid X10 device id: {}", config.getSelectorParam());
actionPGMX10 = null;
}
break;
case ZONE_BYPASSED:
if (command instanceof OnOffType) {
bypass = command.equals(OnOffType.ON) ? Boolean.TRUE : Boolean.FALSE;
}
try {
zoneNr = Byte.parseByte(config.getSelectorParam());
} catch (NumberFormatException e) {
logger.warn("PowerMax alarm binding: invalid zone number: {}", config.getSelectorParam());
bypass = null;
}
break;
case COMMAND:
if (command instanceof StringType) {
commandStr = command.toString();
eventPublisher.postUpdate(itemName, new StringType(commandStr));
}
break;
default:
break;
}
if (armMode != null) {
HashMap<String, Boolean> allowedModes = new HashMap<String, Boolean>();
allowedModes.put("Disarmed", allowDisarming);
allowedModes.put("Stay", allowArming);
allowedModes.put("Armed", allowArming);
allowedModes.put("StayInstant", allowArming);
allowedModes.put("ArmedInstant", allowArming);
allowedModes.put("Night", allowArming);
allowedModes.put("NightInstant", allowArming);
Boolean allowed = allowedModes.get(armMode);
if ((allowed == null) || !allowed) {
logger.warn("PowerMax alarm binding: rejected command {}", armMode);
} else {
comm.requestArmMode(armMode, currentState.isPowerlinkMode() ? PowerMaxPanelSettings.getThePanelSettings().getFirstPinCode() : pinCode);
}
break;
} else if (actionPGMX10 != null) {
comm.sendPGMX10(actionPGMX10, devicePGMX10);
break;
} else if (bypass != null) {
if ((currentState.isPowerlinkMode() == null) || currentState.isPowerlinkMode().equals(Boolean.FALSE)) {
logger.warn("PowerMax alarm binding: Bypass option only supported in Powerlink mode");
} else if (!PowerMaxPanelSettings.getThePanelSettings().isBypassEnabled()) {
logger.warn("PowerMax alarm binding: Bypass option not enabled in panel settings");
} else {
comm.sendZoneBypass(bypass.booleanValue(), zoneNr, PowerMaxPanelSettings.getThePanelSettings().getFirstPinCode());
}
break;
} else if (commandStr != null) {
if (commandStr.equalsIgnoreCase("get_event_log")) {
comm.requestEventLog(currentState.isPowerlinkMode() ? PowerMaxPanelSettings.getThePanelSettings().getFirstPinCode() : pinCode);
} else if (commandStr.equalsIgnoreCase("download_setup")) {
if ((currentState.isPowerlinkMode() == null) || currentState.isPowerlinkMode().equals(Boolean.FALSE)) {
logger.warn("PowerMax alarm binding: download setup only supported in Powerlink mode");
} else {
comm.startDownload();
if (currentState.getLastKeepAlive() != null) {
currentState.setLastKeepAlive(System.currentTimeMillis());
}
}
} else if (commandStr.equalsIgnoreCase("log_setup")) {
settings.log();
} else if (commandStr.equalsIgnoreCase("help_items")) {
settings.helpItems();
} else {
logger.warn("PowerMax alarm binding: rejected command {}", commandStr);
}
break;
}
}
}
use of org.openhab.core.library.types.OnOffType 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.OnOffType in project openhab1-addons by openhab.
the class RFXComThermostat2Message method convertFromState.
@Override
public void convertFromState(RFXComValueSelector valueSelector, String id, Object subType, Type type, byte seqNumber) throws RFXComException {
this.subType = ((SubType) subType);
seqNbr = seqNumber;
String[] ids = id.split("\\.");
unitcode = Byte.parseByte(ids[0]);
switch(valueSelector) {
case COMMAND:
if (type instanceof OnOffType) {
command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
} else if (type instanceof OpenClosedType) {
command = (type == OpenClosedType.CLOSED ? Commands.ON : Commands.OFF);
} 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.OnOffType in project openhab1-addons by openhab.
the class RFXComThermostat3Message method convertFromState.
@Override
public void convertFromState(RFXComValueSelector valueSelector, String id, Object subType, Type type, byte seqNumber) throws RFXComException {
this.subType = ((SubType) subType);
seqNbr = seqNumber;
String[] ids = id.split("\\.");
unitcode = Byte.parseByte(ids[0]);
switch(valueSelector) {
case COMMAND:
if (type instanceof OnOffType) {
command = (type == OnOffType.ON ? Commands.ON : Commands.OFF);
} else if (type instanceof OpenClosedType) {
command = (type == OpenClosedType.CLOSED ? Commands.ON : Commands.OFF);
} 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.OnOffType in project openhab1-addons by openhab.
the class IhcDataConverter method convertCommandToResourceValue.
/**
* Convert openHAB data type to IHC data type.
*
* @param type
* openHAB data type
* @param value
*
* @param enumValues
*
* @return IHC data type
*/
public static WSResourceValue convertCommandToResourceValue(Type type, WSResourceValue value, ArrayList<IhcEnumValue> enumValues) {
if (type instanceof DecimalType) {
if (value instanceof WSFloatingPointValue) {
double newVal = ((DecimalType) type).doubleValue();
double max = ((WSFloatingPointValue) value).getMaximumValue();
double min = ((WSFloatingPointValue) value).getMinimumValue();
if (newVal >= min && newVal <= max) {
((WSFloatingPointValue) value).setFloatingPointValue(newVal);
} else {
throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
}
} else if (value instanceof WSBooleanValue) {
((WSBooleanValue) value).setValue(((DecimalType) type).intValue() > 0 ? true : false);
} else if (value instanceof WSIntegerValue) {
int newVal = ((DecimalType) type).intValue();
int max = ((WSIntegerValue) value).getMaximumValue();
int min = ((WSIntegerValue) value).getMinimumValue();
if (newVal >= min && newVal <= max) {
((WSIntegerValue) value).setInteger(newVal);
} else {
throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
}
} else if (value instanceof WSTimerValue) {
((WSTimerValue) value).setMilliseconds(((DecimalType) type).longValue());
} else if (value instanceof WSWeekdayValue) {
((WSWeekdayValue) value).setWeekdayNumber(((DecimalType) type).intValue());
} else {
throw new NumberFormatException("Can't convert DecimalType to " + value.getClass());
}
} else if (type instanceof OnOffType) {
if (value instanceof WSBooleanValue) {
((WSBooleanValue) value).setValue(type == OnOffType.ON ? true : false);
} else if (value instanceof WSIntegerValue) {
int newVal = type == OnOffType.ON ? 100 : 0;
int max = ((WSIntegerValue) value).getMaximumValue();
int min = ((WSIntegerValue) value).getMinimumValue();
if (newVal >= min && newVal <= max) {
((WSIntegerValue) value).setInteger(newVal);
} else {
throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
}
} else {
throw new NumberFormatException("Can't convert OnOffType to " + value.getClass());
}
} else if (type instanceof OpenClosedType) {
((WSBooleanValue) value).setValue(type == OpenClosedType.OPEN ? true : false);
} else if (type instanceof DateTimeType) {
if (value instanceof WSDateValue) {
Calendar c = ((DateTimeType) type).getCalendar();
short year = (short) c.get(Calendar.YEAR);
byte month = (byte) (c.get(Calendar.MONTH) + 1);
byte day = (byte) c.get(Calendar.DAY_OF_MONTH);
((WSDateValue) value).setYear(year);
((WSDateValue) value).setMonth(month);
((WSDateValue) value).setDay(day);
} else if (value instanceof WSTimeValue) {
Calendar c = ((DateTimeType) type).getCalendar();
int hours = c.get(Calendar.HOUR_OF_DAY);
int minutes = c.get(Calendar.MINUTE);
int seconds = c.get(Calendar.SECOND);
((WSTimeValue) value).setHours(hours);
((WSTimeValue) value).setMinutes(minutes);
((WSTimeValue) value).setSeconds(seconds);
} else {
throw new NumberFormatException("Can't convert DateTimeItem to " + value.getClass());
}
} else if (type instanceof StringType) {
if (value instanceof WSEnumValue) {
boolean found = false;
for (IhcEnumValue item : enumValues) {
if (item.name.equals(type.toString())) {
((WSEnumValue) value).setEnumValueID(item.id);
((WSEnumValue) value).setEnumName(type.toString());
found = true;
break;
}
}
if (found == false) {
throw new NumberFormatException("Can't find enum value for string " + type.toString());
}
} else {
throw new NumberFormatException("Can't convert StringType to " + value.getClass());
}
} else if (type instanceof PercentType) {
if (value instanceof WSIntegerValue) {
int newVal = ((DecimalType) type).intValue();
int max = ((WSIntegerValue) value).getMaximumValue();
int min = ((WSIntegerValue) value).getMinimumValue();
if (newVal >= min && newVal <= max) {
((WSIntegerValue) value).setInteger(newVal);
} else {
throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
}
} else {
throw new NumberFormatException("Can't convert PercentType to " + value.getClass());
}
} else if (type instanceof UpDownType) {
if (value instanceof WSBooleanValue) {
((WSBooleanValue) value).setValue(type == UpDownType.DOWN ? true : false);
} else if (value instanceof WSIntegerValue) {
int newVal = type == UpDownType.DOWN ? 100 : 0;
int max = ((WSIntegerValue) value).getMaximumValue();
int min = ((WSIntegerValue) value).getMinimumValue();
if (newVal >= min && newVal <= max) {
((WSIntegerValue) value).setInteger(newVal);
} else {
throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
}
} else {
throw new NumberFormatException("Can't convert UpDownType to " + value.getClass());
}
} else {
throw new NumberFormatException("Can't convert " + type.getClass().toString());
}
return value;
}
Aggregations