use of org.openhab.core.library.items.ContactItem in project openhab1-addons by openhab.
the class XplBinding method handleXPLMessage.
@Override
public void handleXPLMessage(xPL_MessageI theMessage) {
for (XplBindingProvider provider : providers) {
List<String> matchingItems = provider.hasMessage(theMessage);
for (String itemName : matchingItems) {
XplBindingConfig config = provider.getConfig(itemName);
if (config == null) {
continue;
}
String current = theMessage.getNamedValue(config.NamedParameter);
Item item = provider.getItem(itemName);
if (item != null) {
if (item instanceof SwitchItem) {
OnOffType status = (current.equalsIgnoreCase("on") || current.equalsIgnoreCase("true") || current.equalsIgnoreCase("1") || current.equalsIgnoreCase("open") || current.equalsIgnoreCase("high")) ? OnOffType.ON : OnOffType.OFF;
synchronized (item) {
if (!item.getState().equals(status)) {
eventPublisher.postUpdate(itemName, status);
((SwitchItem) item).setState(status);
}
}
} else if (item instanceof ContactItem) {
OpenClosedType status = (current.equalsIgnoreCase("on") || current.equalsIgnoreCase("true") || current.equalsIgnoreCase("1") || current.equalsIgnoreCase("open") || current.equalsIgnoreCase("high")) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
synchronized (item) {
if (!item.getState().equals(status)) {
eventPublisher.postUpdate(itemName, status);
((ContactItem) item).setState(status);
}
}
} else if (item instanceof NumberItem) {
DecimalType value = new DecimalType(current);
synchronized (item) {
if (!item.getState().equals(value)) {
eventPublisher.postUpdate(itemName, value);
((NumberItem) item).setState(value);
}
}
} else if (item instanceof StringItem) {
StringType value = new StringType(current);
synchronized (item) {
if (!item.getState().equals(value)) {
eventPublisher.postUpdate(itemName, value);
((StringItem) item).setState(value);
}
}
}
}
}
}
}
use of org.openhab.core.library.items.ContactItem in project openhab1-addons by openhab.
the class OneWireBindingConfigFactory method createOneWireDeviceProperty.
/**
* @param pvItem
* @param pvBindingConfig
* @return a new BindingConfig, corresponding to the given
* <code><pvItem/code> and <code><pvBindingConfig/code>
* @throws BindingConfigParseException
*/
public static OneWireBindingConfig createOneWireDeviceProperty(Item pvItem, String pvBindingConfig) throws BindingConfigParseException {
logger.debug("createOneWireDeviceProperty: {} - bindingConfig:{}", pvItem.getName(), pvBindingConfig);
OneWireBindingConfig lvNewBindingConfig = null;
if (OneWireClearCacheControlBindingConfig.isBindingConfigToCreate(pvItem, pvBindingConfig)) {
lvNewBindingConfig = new OneWireClearCacheControlBindingConfig(pvBindingConfig);
} else if (OneWireDevicePropertyPushButtonBindingConfig.isBindingConfigToCreate(pvItem, pvBindingConfig)) {
lvNewBindingConfig = new OneWireDevicePropertyPushButtonBindingConfig(pvBindingConfig);
} else if (OneWireDevicePropertySwitchMinMaxNumberWarningBindingConfig.isBindingConfigToCreate(pvItem, pvBindingConfig)) {
lvNewBindingConfig = new OneWireDevicePropertySwitchMinMaxNumberWarningBindingConfig(pvBindingConfig);
} else if (pvItem instanceof NumberItem) {
lvNewBindingConfig = new OneWireDevicePropertyNumberBindingConfig(pvBindingConfig);
} else if (pvItem instanceof ContactItem) {
lvNewBindingConfig = new OneWireDevicePropertyContactBindingConfig(pvBindingConfig);
} else if (pvItem instanceof SwitchItem) {
lvNewBindingConfig = new OneWireDevicePropertySwitchBindingConfig(pvBindingConfig);
} else if (pvItem instanceof StringItem) {
lvNewBindingConfig = new OneWireDevicePropertyStringBindingConfig(pvBindingConfig);
} else {
throw new UnsupportedOperationException("the item-type " + pvItem.getClass() + " cannot be a onewire device");
}
logger.debug("created newBindingConfig: {}", lvNewBindingConfig.toString());
return lvNewBindingConfig;
}
use of org.openhab.core.library.items.ContactItem in project openhab1-addons by openhab.
the class GPIOGenericBindingProvider method processBindingConfiguration.
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
/* Not sure when must be called, other bindings seems to call it at the beginning */
super.processBindingConfiguration(context, item, bindingConfig);
GPIOPinBindingConfig config = new GPIOPinBindingConfig();
/* Configuration string should be in the form "pin:NUMBER [debounse:NUMBER] [activelow:yes|no]" */
String[] properties = bindingConfig.split(" ");
if (properties.length > 3) {
logger.error("Wrong number of arguments (" + properties.length + ") in configuration string '" + bindingConfig + "'");
throw new BindingConfigParseException("Wrong number of agruments (" + properties.length + ") in configuration string '" + bindingConfig + "'");
}
for (String property : properties) {
String[] keyValueStructure = property.split(":");
if (keyValueStructure.length != 2) {
logger.error("Incorrect key:value structure (" + property + ") in configuration string '" + bindingConfig + "'");
throw new BindingConfigParseException("Incorrect key:value structure (" + property + ") in configuration string '" + bindingConfig + "'");
}
String key = keyValueStructure[0];
String value = keyValueStructure[1];
if (key.compareToIgnoreCase("pin") == 0) {
try {
config.pinNumber = Integer.parseInt(value);
if (config.pinNumber < 0) {
logger.error("Unsupported, negative value for pin number (" + value + ") in configuration string '" + bindingConfig + "'");
throw new BindingConfigParseException("Unsupported, negative value for pin number (" + value + ") in configuration string '" + bindingConfig + "'");
}
} catch (NumberFormatException e) {
logger.error("Unsupported, not numeric value for pin number (" + value + ") in configuration string '" + bindingConfig + "'");
throw new BindingConfigParseException("Unsupported, not numeric value for pin number (" + value + ") in configuration string '" + bindingConfig + "'");
}
} else if (key.compareToIgnoreCase("force") == 0) {
config.pinForce = false;
if (value.compareToIgnoreCase("yes") == 0 || value.compareToIgnoreCase("true") == 0) {
config.pinForce = true;
} else if (value.compareToIgnoreCase("no") != 0 && value.compareToIgnoreCase("false") != 0) {
logger.error("Unsupported value for force (" + value + ") in configuration string '" + bindingConfig + "'");
throw new BindingConfigParseException("Unsupported value for force (" + value + ") in configuration string '" + bindingConfig + "'");
}
} else if (key.compareToIgnoreCase("debounce") == 0) {
try {
config.debounceInterval = Long.parseLong(value);
if (config.debounceInterval < 0) {
logger.error("Unsupported, negative value for debounce (" + value + ") in configuration string '" + bindingConfig + "'");
throw new BindingConfigParseException("Unsupported, negative value for debounce (" + value + ") in configuration string '" + bindingConfig + "'");
}
} catch (NumberFormatException e) {
logger.error("Unsupported, not numeric value for debounce (" + value + ") in configuration string '" + bindingConfig + "'");
throw new BindingConfigParseException("Unsupported, not numeric value for debounce (" + value + ") in configuration string '" + bindingConfig + "'");
}
} else if (key.compareToIgnoreCase("activelow") == 0) {
if (value.compareToIgnoreCase("yes") == 0) {
config.activeLow = GPIOPin.ACTIVELOW_ENABLED;
} else if (value.compareToIgnoreCase("no") != 0) {
logger.error("Unsupported value for activelow (" + value + ") in configuration string '" + bindingConfig + "'");
throw new BindingConfigParseException("Unsupported value for activelow (" + value + ") in configuration string '" + bindingConfig + "'");
}
} else {
logger.error("Unsupported key (" + key + ") in configuration string '" + bindingConfig + "'");
throw new BindingConfigParseException("Unsupported key (" + key + ") in configuration string '" + bindingConfig + "'");
}
}
/* Pin number wasn't configured */
if (config.pinNumber == GPIOBindingProvider.PINNUMBER_UNDEFINED) {
logger.error("Mandatory parameter (pin) is missing in configuration string '" + bindingConfig + "'");
throw new BindingConfigParseException("Mandatory parameter (pin) is missing in configuration string '" + bindingConfig + "'");
}
if (item instanceof ContactItem) {
config.direction = GPIOPin.DIRECTION_IN;
} else {
/* Item type 'Switch' */
config.direction = GPIOPin.DIRECTION_OUT;
}
addBindingConfig(item, config);
}
use of org.openhab.core.library.items.ContactItem in project openhab1-addons by openhab.
the class IhcDataConverter method convertResourceValueToState.
/**
* Convert IHC data type to openHAB data type.
*
* @param itemType
* OpenHAB data type class
* @param value
* IHC data value
*
* @return openHAB {@link State}
*/
public static State convertResourceValueToState(Class<? extends Item> itemType, WSResourceValue value) throws NumberFormatException {
org.openhab.core.types.State state = UnDefType.UNDEF;
if (itemType == NumberItem.class) {
if (value.getClass() == WSFloatingPointValue.class) {
// state = new
// DecimalType(((WSFloatingPointValue)value).getFloatingPointValue());
// Controller might send floating point value with >10 decimals
// (22.299999237060546875), so round value to have max 2
// decimals
double d = ((WSFloatingPointValue) value).getFloatingPointValue();
BigDecimal bd = new BigDecimal(d).setScale(2, RoundingMode.HALF_EVEN);
state = new DecimalType(bd);
} else if (value.getClass() == WSBooleanValue.class) {
state = new DecimalType(((WSBooleanValue) value).isValue() ? 1 : 0);
} else if (value.getClass() == WSIntegerValue.class) {
state = new DecimalType(((WSIntegerValue) value).getInteger());
} else if (value.getClass() == WSTimerValue.class) {
state = new DecimalType(((WSTimerValue) value).getMilliseconds());
} else if (value.getClass() == WSEnumValue.class) {
state = new DecimalType(((WSEnumValue) value).getEnumValueID());
} else if (value.getClass() == WSWeekdayValue.class) {
state = new DecimalType(((WSWeekdayValue) value).getWeekdayNumber());
} else {
throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to NumberItem");
}
} else if (itemType == DimmerItem.class) {
if (value.getClass() == WSIntegerValue.class) {
state = new PercentType(((WSIntegerValue) value).getInteger());
} else {
throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to NumberItem");
}
} else if (itemType == SwitchItem.class) {
if (value.getClass() == WSBooleanValue.class) {
if (((WSBooleanValue) value).isValue()) {
state = OnOffType.ON;
} else {
state = OnOffType.OFF;
}
} else {
throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to SwitchItem");
}
} else if (itemType == ContactItem.class) {
if (value.getClass() == WSBooleanValue.class) {
if (((WSBooleanValue) value).isValue()) {
state = OpenClosedType.OPEN;
} else {
state = OpenClosedType.CLOSED;
}
} else {
throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to ContactItem");
}
} else if (itemType == DateTimeItem.class) {
if (value.getClass() == WSDateValue.class) {
Calendar cal = WSDateTimeToCalendar((WSDateValue) value, null);
state = new DateTimeType(cal);
} else if (value.getClass() == WSTimeValue.class) {
Calendar cal = WSDateTimeToCalendar(null, (WSTimeValue) value);
state = new DateTimeType(cal);
} else {
throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to DateTimeItem");
}
} else if (itemType == StringItem.class) {
if (value.getClass() == WSEnumValue.class) {
state = new StringType(((WSEnumValue) value).getEnumName());
} else {
throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to StringItem");
}
} else if (itemType == RollershutterItem.class) {
if (value.getClass() == WSIntegerValue.class) {
state = new PercentType(((WSIntegerValue) value).getInteger());
} else {
throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to NumberItem");
}
}
return state;
}
use of org.openhab.core.library.items.ContactItem in project openhab1-addons by openhab.
the class Zone method updateItem.
@Override
public void updateItem(Item item, OmniLinkBindingConfig config, EventPublisher publisher) {
int current = ((properties.getStatus() >> 0) & 0x03);
int latched = ((properties.getStatus() >> 2) & 0x03);
int arming = ((properties.getStatus() >> 4) & 0x03);
String latchedTxt = (latched < LATCHED_TEXT.length ? LATCHED_TEXT[latched] : "?");
String armingTxt = (arming < ARMING_TEXT.length ? ARMING_TEXT[arming] : "?");
String currentTxt = (current < CURRENT_TEXT.length ? CURRENT_TEXT[current] : "?");
int setting = 0;
String str = "";
switch(config.getObjectType()) {
case ZONE_STATUS_ARMING:
setting = arming;
str = armingTxt;
break;
case ZONE_STATUS_CURRENT:
setting = current;
str = currentTxt;
break;
case ZONE_STATUS_LATCHED:
setting = latched;
str = latchedTxt;
break;
case ZONE_STATUS_ALL:
str = currentTxt + " | " + latchedTxt + " | " + armingTxt;
break;
default:
break;
}
if (item instanceof NumberItem) {
publisher.postUpdate(item.getName(), new DecimalType(setting));
} else if (item instanceof StringItem) {
publisher.postUpdate(item.getName(), new StringType(str));
} else if (item instanceof ContactItem) {
publisher.postUpdate(item.getName(), current == 0 ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
}
}
Aggregations