Search in sources :

Example 26 with SwitchItem

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);
                }
            }
        }
    }
}
Also used : WagoBindingConfig(org.openhab.binding.wago.internal.WagoGenericBindingProvider.WagoBindingConfig) WagoBindingProvider(org.openhab.binding.wago.WagoBindingProvider) State(org.openhab.core.types.State) DimmerItem(org.openhab.core.library.items.DimmerItem) PercentType(org.openhab.core.library.types.PercentType) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 27 with SwitchItem

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!");
    }
}
Also used : BindingConfig(org.openhab.core.binding.BindingConfig) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) DimmerItem(org.openhab.core.library.items.DimmerItem) ColorItem(org.openhab.core.library.items.ColorItem) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 28 with SwitchItem

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);
}
Also used : MCP23017Pin(com.pi4j.gpio.extension.mcp.MCP23017Pin) JsonParserFactory(com.json.parsers.JsonParserFactory) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) JSONParser(com.json.parsers.JSONParser) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 29 with SwitchItem

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);
}
Also used : SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 30 with SwitchItem

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);
}
Also used : SimpleDigitalIn(net.wimpi.modbus.procimg.SimpleDigitalIn) SimpleDigitalOut(net.wimpi.modbus.procimg.SimpleDigitalOut) SwitchItem(org.openhab.core.library.items.SwitchItem) Test(org.junit.Test)

Aggregations

SwitchItem (org.openhab.core.library.items.SwitchItem)55 NumberItem (org.openhab.core.library.items.NumberItem)25 DimmerItem (org.openhab.core.library.items.DimmerItem)21 Test (org.junit.Test)18 ContactItem (org.openhab.core.library.items.ContactItem)17 Item (org.openhab.core.items.Item)15 RollershutterItem (org.openhab.core.library.items.RollershutterItem)15 DecimalType (org.openhab.core.library.types.DecimalType)15 ColorItem (org.openhab.core.library.items.ColorItem)12 StringItem (org.openhab.core.library.items.StringItem)12 PercentType (org.openhab.core.library.types.PercentType)11 StringType (org.openhab.core.library.types.StringType)10 State (org.openhab.core.types.State)8 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)8 DateTimeItem (org.openhab.core.library.items.DateTimeItem)7 DateTimeType (org.openhab.core.library.types.DateTimeType)7 Calendar (java.util.Calendar)6 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)5 HSBType (org.openhab.core.library.types.HSBType)5 SappBindingConfigContactItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem)4