Search in sources :

Example 6 with Item

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

the class ProviderItemIterator method iterate.

/**
     * Iterate through all providers and their items, creates a converter and
     * calls the callback.
     */
public void iterate(HomematicBindingConfig bindingConfig, ProviderItemIteratorCallback callback) {
    for (HomematicBindingProvider provider : context.getProviders()) {
        List<Item> items = provider.getItemsFor(bindingConfig);
        for (Item item : items) {
            HomematicBindingConfig providerBindingConfig = provider.getBindingFor(item.getName());
            Converter<?> converter = context.getConverterFactory().createConverter(item, providerBindingConfig);
            if (converter != null) {
                callback.next(providerBindingConfig, item, converter);
            }
        }
    }
}
Also used : Item(org.openhab.core.items.Item) HomematicBindingProvider(org.openhab.binding.homematic.HomematicBindingProvider) HomematicBindingConfig(org.openhab.binding.homematic.internal.config.binding.HomematicBindingConfig)

Example 7 with Item

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

the class SysteminfoGenericBindingProviderTest method testValidateItemType_nullItem.

@Test(expected = BindingConfigParseException.class)
public void testValidateItemType_nullItem() throws BindingConfigParseException {
    Item s = null;
    provider.validateItemType(s, "?");
}
Also used : GenericItem(org.openhab.core.items.GenericItem) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) Test(org.junit.Test)

Example 8 with Item

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

the class SysteminfoGenericBindingProviderTest method testValidateItemType_itemNotNumOrStr.

@Test(expected = BindingConfigParseException.class)
public void testValidateItemType_itemNotNumOrStr() throws BindingConfigParseException {
    Item newItem = new DummyItem();
    provider.validateItemType(newItem, "?");
}
Also used : GenericItem(org.openhab.core.items.GenericItem) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) Test(org.junit.Test)

Example 9 with Item

use of org.openhab.core.items.Item 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 10 with Item

use of org.openhab.core.items.Item 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)

Aggregations

Item (org.openhab.core.items.Item)59 SwitchItem (org.openhab.core.library.items.SwitchItem)23 NumberItem (org.openhab.core.library.items.NumberItem)20 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)16 State (org.openhab.core.types.State)16 RollershutterItem (org.openhab.core.library.items.RollershutterItem)15 ContactItem (org.openhab.core.library.items.ContactItem)14 DimmerItem (org.openhab.core.library.items.DimmerItem)14 DecimalType (org.openhab.core.library.types.DecimalType)9 HistoricItem (org.openhab.core.persistence.HistoricItem)9 ArrayList (java.util.ArrayList)7 ColorItem (org.openhab.core.library.items.ColorItem)7 StringItem (org.openhab.core.library.items.StringItem)7 HomematicBindingConfig (org.openhab.binding.homematic.internal.config.binding.HomematicBindingConfig)6 DateTimeItem (org.openhab.core.library.items.DateTimeItem)6 Test (org.junit.Test)5 GenericItem (org.openhab.core.items.GenericItem)5 GroupItem (org.openhab.core.items.GroupItem)5 ItemRegistry (org.openhab.core.items.ItemRegistry)5 Calendar (java.util.Calendar)3