use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.
the class WagoBinding method updateItemPWM.
public void updateItemPWM(String itemName, String couplerName, int module, int[] values) {
for (WagoBindingProvider provider : providers) {
if (provider.providesBindingFor(itemName)) {
WagoBindingConfig conf = provider.getConfig(itemName);
if (conf.couplerName.equals(couplerName) && conf.module == module) {
State currentState = conf.getItemState();
State newState;
if (conf.getItem() instanceof DimmerItem) {
newState = new PercentType((int) ((float) values[conf.channel] / 1023 * 100));
} else if (conf.getItem() instanceof SwitchItem) {
if (values[conf.channel] == 0) {
newState = OnOffType.OFF;
} else {
newState = OnOffType.ON;
}
} else {
logger.debug("Unsupported Itemtype");
return;
}
if (!newState.equals(currentState)) {
eventPublisher.postUpdate(itemName, newState);
}
}
}
}
}
use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.
the class MilightGenericBindingProvider method processBindingConfiguration.
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
super.processBindingConfiguration(context, item, bindingConfig);
try {
if (bindingConfig != null) {
String[] configParts = bindingConfig.split(";");
if (configParts.length > 4) {
throw new BindingConfigParseException("milight binding configuration must not have more than four parts");
}
if (item instanceof ColorItem) {
BindingConfig milightBindingConfig = new MilightBindingConfig(configParts[0], configParts[1], BindingType.rgb.name(), null);
addBindingConfig(item, milightBindingConfig);
} else if (item instanceof DimmerItem || item instanceof SwitchItem) {
BindingConfig milightBindingConfig = new MilightBindingConfig(configParts[0], configParts[1], configParts.length < 3 ? null : configParts[2], configParts.length < 4 ? null : configParts[3]);
addBindingConfig(item, milightBindingConfig);
}
} else {
logger.warn("bindingConfig is NULL (item=" + item + ") -> processing bindingConfig aborted!");
}
} catch (ArrayIndexOutOfBoundsException e) {
logger.warn("bindingConfig is invalid (item=" + item + ") -> processing bindingConfig aborted!");
}
}
use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.
the class MCP23017GenericBindingProvider method processBindingConfiguration.
/**
* {@inheritDoc}
*/
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
super.processBindingConfiguration(context, item, bindingConfig);
MCP23017BindingConfig config = new MCP23017BindingConfig();
// parse bindingConfig here ...
/*
* Configuration string should be a json in the form:
* Contact Test1 "Test 1" (Tests) { mcp23017="{ address:20, pin:'A0', mode:'DIGITAL_INPUT'}" }
* Switch Test2 "Test 2" (Tests) { mcp23017="{ address:20, pin:'B0', mode:'DIGITAL_OUTPUT', defaultState:'LOW'}"
* }
*/
JsonParserFactory factory = JsonParserFactory.getInstance();
JSONParser parser = factory.newJsonParser();
@SuppressWarnings("unchecked") Map<String, Object> jsonData = parser.parseJson(bindingConfig);
try {
logger.debug("Process binding configuration in context {}", context);
config.setBusAddress(Integer.parseInt((String) jsonData.get("address"), 16));
config.setPin((Pin) MCP23017Pin.class.getField("GPIO_" + (String) jsonData.get("pin")).get(null));
config.setPinMode(PinMode.valueOf((String) jsonData.get("mode")));
if (item instanceof SwitchItem) {
config.setDefaultState(PinState.valueOf((String) jsonData.get("defaultState")));
}
} catch (IllegalArgumentException exception) {
final String message = "Illegal argument exception in configuration string ";
logger.error("{} '{}': {}", message, bindingConfig, exception.getMessage());
throw new BindingConfigParseException(message + "'" + bindingConfig + "'");
} catch (IllegalAccessException exception) {
final String message = "Illegal access exception in configuration string ";
logger.error("{} '{}': {}", message, bindingConfig, exception.getMessage());
throw new BindingConfigParseException(message + "'" + bindingConfig + "'");
} catch (NoSuchFieldException exception) {
final String message = "No such field exception in configuration string ";
logger.error("{} '{}': {}", message, bindingConfig, exception.getMessage());
throw new BindingConfigParseException(message + "'" + bindingConfig + "'");
} catch (SecurityException exception) {
final String message = "Security exception in configuration string ";
logger.error("{} '{}': {}", message, bindingConfig, exception.getMessage());
throw new BindingConfigParseException(message + "'" + bindingConfig + "'");
}
addBindingConfig(item, config);
}
use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.
the class TestCaseSupport method configureSwitchItemBinding.
protected void configureSwitchItemBinding(int items, String slaveName, int itemOffset, String itemPrefix, State initialState) throws BindingConfigParseException {
Assert.assertEquals(REFRESH_INTERVAL, binding.getRefreshInterval());
final ModbusGenericBindingProvider provider = new ModbusGenericBindingProvider();
for (int itemIndex = itemOffset; itemIndex < items + itemOffset; itemIndex++) {
SwitchItem item = new SwitchItem(String.format("%sItem%d", itemPrefix, itemIndex + 1));
if (initialState != null) {
item.setState(initialState);
}
provider.processBindingConfiguration("test.items", item, String.format("%s:%d", slaveName, itemIndex));
}
binding.setEventPublisher(eventPublisher);
binding.addBindingProvider(provider);
}
use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.
the class ErroringQueriesTestCase method testReadingTooMuchTwoSlaves.
/**
* Test case for situation where we try to poll too much data.
*
* In this test server has
* - single coil
* - two discrete inputs
*
* Binding is configured to read
* - two coils
* - two discrete inputs
*
* Items are follows
* - first (index=0) coil (Item1) -> no output since coil query should fail
* - index=1 discrete (Item2) should be OK
* - index=2 discrete (Item3) no event transmitted, item readIndex out of bounds. WARN logged
*/
@Test
public void testReadingTooMuchTwoSlaves() throws UnknownHostException, ConfigurationException, BindingConfigParseException {
spi.addDigitalOut(new SimpleDigitalOut(true));
spi.addDigitalIn(new SimpleDigitalIn(true));
spi.addDigitalIn(new SimpleDigitalIn(true));
spi.addDigitalIn(new SimpleDigitalIn(true));
binding = new ModbusBinding();
Dictionary<String, Object> config = newLongPollBindingConfig();
addSlave(config, SLAVE_NAME, ModbusBindingProvider.TYPE_COIL, null, 0, 2);
addSlave(config, SLAVE2_NAME, ModbusBindingProvider.TYPE_DISCRETE, null, 0, 2);
binding.updated(config);
// Configure items
final ModbusGenericBindingProvider provider = new ModbusGenericBindingProvider();
provider.processBindingConfiguration("test.items", new SwitchItem("Item1"), String.format("%s:%d", SLAVE_NAME, 0));
provider.processBindingConfiguration("test.items", new SwitchItem("Item2"), String.format("%s:%d", SLAVE2_NAME, 1));
provider.processBindingConfiguration("test.items", new SwitchItem("Item3"), String.format("%s:%d", SLAVE2_NAME, 2));
binding.setEventPublisher(eventPublisher);
binding.addBindingProvider(provider);
binding.execute();
// Give the system some time to make the expected connections & requests
waitForConnectionsReceived(2);
waitForRequests(2);
verify(eventPublisher, never()).postCommand(null, null);
verify(eventPublisher, never()).sendCommand(null, null);
verify(eventPublisher).postUpdate("Item2", OnOffType.ON);
verifyNoMoreInteractions(eventPublisher);
}
Aggregations