Search in sources :

Example 6 with BindingProvider

use of org.openhab.core.binding.BindingProvider in project openhab1-addons by openhab.

the class MiosBinding method internalReceiveCommand.

/**
     * {@inheritDoc}
     */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    try {
        logger.debug("internalReceiveCommand: itemName '{}', command '{}'", itemName, command);
        // Lookup the MiOS Unit name and property for this item
        String unitName = getMiosUnitName(itemName);
        MiosUnitConnector connector = getMiosConnector(unitName);
        if (connector == null) {
            logger.warn("Received command ({}) for item '{}' but no connector found for MiOS Unit '{}', ignoring", new Object[] { command.toString(), itemName, unitName });
            return;
        }
        if (!connector.isConnected()) {
            logger.warn("Received command ({}) for item '{}' but the connection to the MiOS Unit '{}' is down, ignoring", new Object[] { command.toString(), itemName, unitName });
            return;
        }
        for (BindingProvider provider : providers) {
            if (provider instanceof MiosBindingProvider) {
                MiosBindingProviderImpl miosProvider = (MiosBindingProviderImpl) provider;
                MiosBindingConfig config = miosProvider.getMiosBindingConfig(itemName);
                if (config != null) {
                    ItemRegistry reg = miosProvider.getItemRegistry();
                    if (reg != null) {
                        Item item = reg.getItem(config.getItemName());
                        State state = item.getState();
                        connector.invokeCommand(config, command, state);
                    } else {
                        logger.warn("internalReceiveCommand: Missing ItemRegistry for item '{}' command '{}'", itemName, command);
                    }
                } else {
                    logger.trace("internalReceiveCommand: Missing BindingConfig for item '{}' command '{}'", itemName, command);
                }
            }
        }
    } catch (Exception e) {
        logger.error("Error handling command", e);
    }
}
Also used : Item(org.openhab.core.items.Item) MiosBindingConfig(org.openhab.binding.mios.internal.config.MiosBindingConfig) MiosBindingProvider(org.openhab.binding.mios.MiosBindingProvider) State(org.openhab.core.types.State) MiosBindingProvider(org.openhab.binding.mios.MiosBindingProvider) BindingProvider(org.openhab.core.binding.BindingProvider) ItemRegistry(org.openhab.core.items.ItemRegistry) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 7 with BindingProvider

use of org.openhab.core.binding.BindingProvider in project openhab1-addons by openhab.

the class MiosBinding method updated.

/**
     * {@inheritDoc}
     */
@Override
public void updated(Dictionary<String, ?> properties) throws ConfigurationException {
    logger.trace(getName() + " updated()");
    // We're called again later, so it'll get established correctly at that point.
    if (properties == null) {
        return;
    }
    Map<String, MiosUnit> units = new HashMap<String, MiosUnit>();
    Enumeration<String> keys = properties.keys();
    while (keys.hasMoreElements()) {
        String key = keys.nextElement();
        if ("service.pid".equals(key)) {
            continue;
        }
        // Only apply this pattern once, as the leading-edge may not be the
        // name of a unit. We support two forms:
        //
        // mios:lounge.host=...
        // mios:host=
        //
        // The latter form refers to the "default" Unit, which can be used
        // in bindings to make things simpler for single-unit owners.
        //
        String unitName = null;
        String value = ((String) properties.get(key)).trim();
        String[] parts = key.split("\\.", 2);
        if (parts.length != 1) {
            unitName = parts[0];
            key = parts[1];
        }
        boolean created = false;
        String hackUnitName = (unitName == null) ? MiosUnit.CONFIG_DEFAULT_UNIT : unitName;
        MiosUnit unit = units.get(hackUnitName);
        if (unit == null) {
            unit = new MiosUnit(hackUnitName);
            created = true;
        }
        if ("host".equals(key)) {
            unit.setHostname(value);
        } else if ("port".equals(key)) {
            unit.setPort(Integer.valueOf(value));
        } else if ("timeout".equals(key)) {
            unit.setTimeout(Integer.valueOf(value));
        } else if ("minimumDelay".equals(key)) {
            unit.setMinimumDelay(Integer.valueOf(value));
        } else if ("refreshCount".equals(key)) {
            unit.setRefreshCount(Integer.valueOf(value));
        } else if ("errorCount".equals(key)) {
            unit.setErrorCount(Integer.valueOf(value));
        } else {
            logger.warn("Unexpected configuration parameter {}", key);
            created = false;
        }
        // it's already there!
        if (created) {
            logger.debug("updated: Created Unit '{}'", hackUnitName);
            units.put(hackUnitName, unit);
        }
    }
    // Close out pre-existing connections
    if (nameUnitMapper != null) {
        MiosUnitConnector connector;
        for (String unitName : nameUnitMapper.keySet()) {
            connector = connectors.get(unitName);
            if (connector != null) {
                try {
                    connector.close();
                } catch (Exception e) {
                // Suppress
                }
            }
        }
    }
    nameUnitMapper = units;
    // Reregister, since the connections will change.
    for (BindingProvider provider : providers) {
        logger.debug("updated: provider '{}'", provider.getClass());
        registerProviderWatch(provider);
    }
}
Also used : HashMap(java.util.HashMap) MiosBindingProvider(org.openhab.binding.mios.MiosBindingProvider) BindingProvider(org.openhab.core.binding.BindingProvider) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 8 with BindingProvider

use of org.openhab.core.binding.BindingProvider in project openhab1-addons by openhab.

the class MiosBinding method internalPropertyUpdate.

private void internalPropertyUpdate(String property, State value, boolean incremental) throws Exception {
    int bound = 0;
    if (value == null) {
        logger.trace("internalPropertyUpdate: Value is null for Property '{}', ignored.", property);
        return;
    }
    for (BindingProvider provider : providers) {
        if (provider instanceof MiosBindingProvider) {
            MiosBindingProviderImpl miosProvider = (MiosBindingProviderImpl) provider;
            for (String itemName : miosProvider.getItemNamesForProperty(property)) {
                MiosBindingConfig config = miosProvider.getMiosBindingConfig(itemName);
                if (config != null) {
                    // Transform whatever value we have, based upon the
                    // Transformation Service specified in the Binding
                    // Config.
                    State newValue = config.transformIn(value);
                    if (newValue != value) {
                        logger.trace("internalPropertyUpdate: transformation performed, from '{}' to '{}'", value, newValue);
                    }
                    //
                    if (incremental) {
                        logger.debug("internalPropertyUpdate: BOUND (Incr) Updating '{} {mios=\"{}\"}' to '{}'", itemName, property, newValue);
                        eventPublisher.postUpdate(itemName, newValue);
                    } else {
                        ItemRegistry reg = miosProvider.getItemRegistry();
                        State oldValue = reg.getItem(itemName).getState();
                        if ((oldValue == null && newValue != null) || (UnDefType.UNDEF.equals(oldValue) && !UnDefType.UNDEF.equals(newValue)) || !oldValue.equals(newValue)) {
                            logger.debug("internalPropertyUpdate: BOUND (Full) Updating '{} {mios=\"{}\"}' to '{}', was '{}'", new Object[] { itemName, property, newValue, oldValue });
                            eventPublisher.postUpdate(itemName, newValue);
                        } else {
                            logger.trace("internalPropertyUpdate: BOUND (Full) Ignoring '{} {mios=\"{}\"}' to '{}', was '{}'", new Object[] { itemName, property, newValue, oldValue });
                        }
                    }
                    bound++;
                } else {
                    logger.trace("internalPropertyUpdate: Found null BindingConfig for item '{}' property '{}'", itemName, property);
                }
            }
        }
    }
    if (bound == 0) {
        logger.trace("internalPropertyUpdate: NOT BOUND {mios=\"{}\"}, value={}", property, value);
    } else {
        logger.trace("internalPropertyUpdate: BOUND {mios=\"{}\"}, value={}, bound {} time(s)", new Object[] { property, value, bound });
    }
}
Also used : MiosBindingConfig(org.openhab.binding.mios.internal.config.MiosBindingConfig) MiosBindingProvider(org.openhab.binding.mios.MiosBindingProvider) State(org.openhab.core.types.State) MiosBindingProvider(org.openhab.binding.mios.MiosBindingProvider) BindingProvider(org.openhab.core.binding.BindingProvider) ItemRegistry(org.openhab.core.items.ItemRegistry)

Aggregations

BindingProvider (org.openhab.core.binding.BindingProvider)8 MiosBindingProvider (org.openhab.binding.mios.MiosBindingProvider)5 MiosBindingConfig (org.openhab.binding.mios.internal.config.MiosBindingConfig)4 ConfigurationException (org.osgi.service.cm.ConfigurationException)4 BindingChangeListener (org.openhab.core.binding.BindingChangeListener)3 AbstractGenericBindingProvider (org.openhab.model.item.binding.AbstractGenericBindingProvider)3 ItemRegistry (org.openhab.core.items.ItemRegistry)2 State (org.openhab.core.types.State)2 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)2 HashMap (java.util.HashMap)1 DmxBindingProvider (org.openhab.binding.dmx.DmxBindingProvider)1 DmxStatusUpdateListener (org.openhab.binding.dmx.DmxStatusUpdateListener)1 DmxColorItem (org.openhab.binding.dmx.internal.config.DmxColorItem)1 DmxDimmerItem (org.openhab.binding.dmx.internal.config.DmxDimmerItem)1 DmxItem (org.openhab.binding.dmx.internal.config.DmxItem)1 DmxSwitchItem (org.openhab.binding.dmx.internal.config.DmxSwitchItem)1 MaxCulBindingProvider (org.openhab.binding.maxcul.MaxCulBindingProvider)1 DeviceBindingConfig (org.openhab.binding.mios.internal.config.DeviceBindingConfig)1 SceneBindingConfig (org.openhab.binding.mios.internal.config.SceneBindingConfig)1 MqttBindingProvider (org.openhab.binding.mqtt.MqttBindingProvider)1