Search in sources :

Example 16 with SwitchItem

use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.

the class LightwaveRfGenericBindingProviderTest method testProcessBindingConfiguratiLionForOutOnly.

@Test
public void testProcessBindingConfiguratiLionForOutOnly() throws Exception {
    LightwaveRfGenericBindingProvider bingindProvider = new LightwaveRfGenericBindingProvider();
    bingindProvider.processBindingConfiguration(context, new SwitchItem("MySwitch"), ">room=3,device=4,type=SWITCH");
    assertEquals(Arrays.asList("MySwitch"), bingindProvider.getBindingItemsForRoomDevice("3", "4"));
    assertEquals(Arrays.asList("MySwitch"), bingindProvider.getItemNames());
    assertEquals("3", bingindProvider.getRoomId("MySwitch"));
    assertEquals("4", bingindProvider.getDeviceId("MySwitch"));
    assertEquals(LightwaveRfType.SWITCH, bingindProvider.getTypeForItemName("MySwitch"));
    assertEquals(LightwaveRfItemDirection.OUT_ONLY, bingindProvider.getDirection("MySwitch"));
}
Also used : SwitchItem(org.openhab.core.library.items.SwitchItem) Test(org.junit.Test)

Example 17 with SwitchItem

use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.

the class PLCLogoBinding method execute.

@Override
protected void execute() {
    if (!bindingsExist()) {
        logger.debug("There is no existing plclogo binding configuration => refresh cycle aborted!");
        return;
    }
    Iterator<Entry<String, PLCLogoConfig>> entries = controllers.entrySet().iterator();
    while (entries.hasNext()) {
        Entry<String, PLCLogoConfig> thisEntry = entries.next();
        String controller = thisEntry.getKey();
        PLCLogoConfig logoConfig = thisEntry.getValue();
        S7Client LogoS7Client = logoConfig.getS7Client();
        if (LogoS7Client == null) {
            logger.debug("No S7client for {} found", controller);
        } else {
            lock.lock();
            int result = ReadLogoDBArea(LogoS7Client, logoConfig.getMemorySize());
            lock.unlock();
            if (result != 0) {
                logger.warn("Failed to read memory: {}. Reconnecting...", S7Client.ErrorText(result));
                ReconnectLogo(LogoS7Client);
                return;
            }
        // Now have the LOGO! memory (note: not suitable for S7) - more efficient than multiple reads (test
        // shows <14mS to read all)
        // iterate through bindings to see what has changed - this approach assumes a small number (< 100)of
        // bindings
        // otherwise might see what has changed in memory and map to binding
        }
        for (PLCLogoBindingProvider provider : providers) {
            for (String itemName : provider.getItemNames()) {
                PLCLogoBindingConfig config = provider.getBindingConfig(itemName);
                if (config.getController().equals(controller)) {
                    // it is for our currently selected controller
                    PLCLogoMemoryConfig rd = config.getRD();
                    int address = -1;
                    try {
                        address = rd.getAddress(logoConfig.getModel());
                    } catch (BindingConfigParseException exception) {
                        logger.error("Invalid address for block {} on {}", rd.getBlockName(), controller);
                        continue;
                    }
                    int currentValue;
                    if (rd.isDigital()) {
                        int bit = -1;
                        try {
                            bit = rd.getBit(logoConfig.getModel());
                        } catch (BindingConfigParseException exception) {
                            logger.error("Invalid bit for block {} on {}", rd.getBlockName(), controller);
                            continue;
                        }
                        currentValue = S7.GetBitAt(data, address, bit) ? 1 : 0;
                    } else {
                        /*
                             * After the data transfer from a LOGO! Base Module to LOGO!Soft Comfort,
                             * you can view only analog values within the range of -32768 to 32767 on LOGO!Soft Comfort.
                             * If an analog value exceeds the value range,
                             * then only the nearest upper limit (32767) or lower limit (-32768) can be displayed.
                             */
                        currentValue = S7.GetShortAt(data, address);
                    }
                    if (config.isSet()) {
                        if (currentValue == config.getLastValue()) {
                            continue;
                        }
                        int delta = Math.abs(config.getLastValue() - currentValue);
                        if (!rd.isDigital() && (delta < config.getAnalogDelta())) {
                            continue;
                        }
                    }
                    boolean isValid = false;
                    Item item = provider.getItem(itemName);
                    switch(rd.getKind()) {
                        case I:
                        case NI:
                            {
                                isValid = item instanceof ContactItem;
                                break;
                            }
                        case Q:
                        case NQ:
                            {
                                isValid = item instanceof SwitchItem;
                                break;
                            }
                        case M:
                        case VB:
                        case VW:
                            {
                                isValid = item instanceof ContactItem || item instanceof SwitchItem;
                                break;
                            }
                        default:
                            {
                                break;
                            }
                    }
                    if (item instanceof NumberItem || isValid) {
                        eventPublisher.postUpdate(itemName, createState(item, currentValue));
                        config.setLastValue(currentValue);
                    } else {
                        String block = rd.getBlockName();
                        logger.warn("Block {} is incompatible with item {} on {}", block, item.getName(), controller);
                    }
                }
            }
        }
    }
}
Also used : ContactItem(org.openhab.core.library.items.ContactItem) NumberItem(org.openhab.core.library.items.NumberItem) SwitchItem(org.openhab.core.library.items.SwitchItem) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) ContactItem(org.openhab.core.library.items.ContactItem) Entry(java.util.Map.Entry) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) PLCLogoBindingProvider(org.openhab.binding.plclogo.PLCLogoBindingProvider) PLCLogoBindingConfig(org.openhab.binding.plclogo.PLCLogoBindingConfig) SwitchItem(org.openhab.core.library.items.SwitchItem) S7Client(Moka7.S7Client)

Example 18 with SwitchItem

use of org.openhab.core.library.items.SwitchItem 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);
                        }
                    }
                }
            }
        }
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) ContactItem(org.openhab.core.library.items.ContactItem) StringItem(org.openhab.core.library.items.StringItem) NumberItem(org.openhab.core.library.items.NumberItem) SwitchItem(org.openhab.core.library.items.SwitchItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) ContactItem(org.openhab.core.library.items.ContactItem) NumberItem(org.openhab.core.library.items.NumberItem) XplBindingConfig(org.openhab.binding.xpl.XplBindingConfig) OnOffType(org.openhab.core.library.types.OnOffType) OpenClosedType(org.openhab.core.library.types.OpenClosedType) DecimalType(org.openhab.core.library.types.DecimalType) SwitchItem(org.openhab.core.library.items.SwitchItem) XplBindingProvider(org.openhab.binding.xpl.XplBindingProvider)

Example 19 with SwitchItem

use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.

the class DmxGenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    if (dmxService == null) {
        logger.error("DMX Service unavailable. Cannot process item configuration for {}", item.getName());
        return;
    }
    super.processBindingConfiguration(context, item, bindingConfig);
    String config = (bindingConfig == null) ? "" : bindingConfig.replaceAll(" ", "").toUpperCase();
    logger.trace("Binding item: {} with configuration {}", item.getName(), config);
    DmxItem itemBinding = null;
    if (item instanceof ColorItem) {
        itemBinding = new DmxColorItem(item.getName(), config, this);
    } else if (item instanceof DimmerItem) {
        itemBinding = new DmxDimmerItem(item.getName(), config, this);
    } else if (item instanceof SwitchItem) {
        itemBinding = new DmxSwitchItem(item.getName(), config, this);
    } else {
        throw new BindingConfigParseException("Unsupported item type " + item.getClass().getSimpleName());
    }
    if (itemBinding.isStatusListener()) {
        final DmxStatusUpdateListener dmxStatusListener = itemBinding;
        final String itemName = item.getName();
        logger.trace("Registering status listener for item {} ", item.getName());
        dmxService.registerStatusListener(dmxStatusListener);
        // add binding change listener to clean up status listeners on item
        // removal
        addBindingChangeListener(new BindingChangeListener() {

            @Override
            public void bindingChanged(BindingProvider provider, String changedItemName) {
                if (itemName.equals(changedItemName) && !provider.providesBindingFor(itemName)) {
                    logger.trace("Removing status listener for item {}", itemName);
                    dmxService.unregisterStatusListener(dmxStatusListener);
                }
            }

            @Override
            public void allBindingsChanged(BindingProvider provider) {
                if (!provider.providesBindingFor(itemName)) {
                    logger.trace("Removing status listener for item {}", itemName);
                    dmxService.unregisterStatusListener(dmxStatusListener);
                }
            }
        });
    }
    addBindingConfig(item, itemBinding);
}
Also used : DmxStatusUpdateListener(org.openhab.binding.dmx.DmxStatusUpdateListener) DmxItem(org.openhab.binding.dmx.internal.config.DmxItem) DmxSwitchItem(org.openhab.binding.dmx.internal.config.DmxSwitchItem) DmxColorItem(org.openhab.binding.dmx.internal.config.DmxColorItem) ColorItem(org.openhab.core.library.items.ColorItem) BindingChangeListener(org.openhab.core.binding.BindingChangeListener) DmxBindingProvider(org.openhab.binding.dmx.DmxBindingProvider) AbstractGenericBindingProvider(org.openhab.model.item.binding.AbstractGenericBindingProvider) BindingProvider(org.openhab.core.binding.BindingProvider) DmxDimmerItem(org.openhab.binding.dmx.internal.config.DmxDimmerItem) DimmerItem(org.openhab.core.library.items.DimmerItem) DmxDimmerItem(org.openhab.binding.dmx.internal.config.DmxDimmerItem) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) DmxColorItem(org.openhab.binding.dmx.internal.config.DmxColorItem) DmxSwitchItem(org.openhab.binding.dmx.internal.config.DmxSwitchItem) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 20 with SwitchItem

use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.

the class ErroringQueriesTestCase method testConnectionTimeout.

@Test
public void testConnectionTimeout() throws UnknownHostException, ConfigurationException, BindingConfigParseException, InterruptedException {
    /**
         * Test that connection timeout is handled properly
         *
         * In the test we have non-responding slave (see http://stackoverflow.com/a/904609), and we use connection
         * timeout of 300ms
         *
         * We assert that after 100ms the binding still have not given up on the connection (no UNDEF posted to the
         * event bus)
         * but after 400ms from the connection, the UNDEF is there.
         */
    binding = new ModbusBinding();
    Dictionary<String, Object> config = newLongPollBindingConfig();
    addSlave(config, ServerType.TCP, "10.255.255.1:9999:0:0:0:1:300", SLAVE_NAME, ModbusBindingProvider.TYPE_DISCRETE, null, 0, 0, 2);
    putSlaveConfigParameter(config, serverType, SLAVE_NAME, "postundefinedonreaderror", "true");
    binding.updated(config);
    // Configure items
    final ModbusGenericBindingProvider provider = new ModbusGenericBindingProvider();
    provider.processBindingConfiguration("test.items", new SwitchItem("Item1"), String.format("%s:%d", SLAVE_NAME, 0));
    binding.setEventPublisher(eventPublisher);
    binding.addBindingProvider(provider);
    final CountDownLatch lock = new CountDownLatch(1);
    Thread thread = new Thread() {

        @Override
        public void run() {
            binding.execute();
            lock.countDown();
        }

        ;
    };
    try {
        thread.start();
        lock.await(100, TimeUnit.MILLISECONDS);
        // After 100ms the binding has not yet given up, i.e. no UNDEF posted to event bus
        verifyNoMoreInteractions(eventPublisher);
        // After 100ms+300ms the timeout of 300ms has passed and UNDEF should have been posted
        lock.await(300, TimeUnit.MILLISECONDS);
        verify(eventPublisher).postUpdate("Item1", UnDefType.UNDEF);
        verifyNoMoreInteractions(eventPublisher);
    } finally {
        thread.interrupt();
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) 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