Search in sources :

Example 1 with FritzboxTr064BindingConfig

use of org.openhab.binding.fritzboxtr064.internal.FritzboxTr064GenericBindingProvider.FritzboxTr064BindingConfig in project openhab1-addons by openhab.

the class FritzboxTr064Binding method execute.

/**
     * @{inheritDoc}
     */
@Override
protected void execute() {
    logger.trace("FritzboxTr064 executing...");
    for (FritzboxTr064BindingProvider provider : providers) {
        for (String itemName : provider.getItemNames()) {
            // check each item relevant for this binding
            // extract itemconfig
            FritzboxTr064BindingConfig conf = provider.getBindingConfigByItemName(itemName);
            // for current item
            if (conf.getConfigString().startsWith("callmonitor")) {
                // check if we need to start call monitor
                if (_callMonitor == null) {
                    // not started yet
                    logger.debug("call monitor is not running. Configured items require call monitor -> Starting call monitor...");
                    if (_pbm == null) {
                        logger.debug("Downloading phonebooks");
                        _pbm = new PhonebookManager(_fboxComm);
                        _pbm.downloadPhonebooks();
                    }
                    _callMonitor = new CallMonitor(_url, eventPublisher, providers, _pbm);
                    _callMonitor.setupReconnectJob();
                    _callMonitor.startThread();
                }
                // make sure, no callmonitor items are processed by tr064
                continue;
            }
            // TR064 protocol usage
            // try to get value for this item
            String tr064result = _fboxComm.getTr064Value(conf.getConfigString());
            // config string from fbox
            if (tr064result == null) {
                // if value cannot be read
                tr064result = "ERR";
            }
            Class<? extends Item> itemType = conf.getItemType();
            if (itemType.isAssignableFrom(StringItem.class)) {
                eventPublisher.postUpdate(itemName, new StringType(tr064result));
            } else if (itemType.isAssignableFrom(ContactItem.class)) {
                State newState = tr064result.equals("1") ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
                eventPublisher.postUpdate(itemName, newState);
            } else if (itemType.isAssignableFrom(SwitchItem.class)) {
                State newState = tr064result.equals("1") ? OnOffType.ON : OnOffType.OFF;
                eventPublisher.postUpdate(itemName, newState);
            } else if (itemType.isAssignableFrom(NumberItem.class)) {
                // number items e.g. TAM messages
                // tr064 retrieves only Strings, trying to parse value returned
                int val = 0;
                try {
                    val = Integer.parseInt(tr064result);
                } catch (NumberFormatException ex) {
                    // indicate error as -1
                    val = -1;
                }
                State newState = new DecimalType(val);
                eventPublisher.postUpdate(itemName, newState);
            }
        }
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) StringType(org.openhab.core.library.types.StringType) FritzboxTr064BindingProvider(org.openhab.binding.fritzboxtr064.FritzboxTr064BindingProvider) State(org.openhab.core.types.State) ContactItem(org.openhab.core.library.items.ContactItem) DecimalType(org.openhab.core.library.types.DecimalType) FritzboxTr064BindingConfig(org.openhab.binding.fritzboxtr064.internal.FritzboxTr064GenericBindingProvider.FritzboxTr064BindingConfig)

Aggregations

FritzboxTr064BindingProvider (org.openhab.binding.fritzboxtr064.FritzboxTr064BindingProvider)1 FritzboxTr064BindingConfig (org.openhab.binding.fritzboxtr064.internal.FritzboxTr064GenericBindingProvider.FritzboxTr064BindingConfig)1 ContactItem (org.openhab.core.library.items.ContactItem)1 NumberItem (org.openhab.core.library.items.NumberItem)1 DecimalType (org.openhab.core.library.types.DecimalType)1 StringType (org.openhab.core.library.types.StringType)1 State (org.openhab.core.types.State)1