Search in sources :

Example 1 with BindingChangeListener

use of org.openhab.core.binding.BindingChangeListener 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 2 with BindingChangeListener

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

the class MaxCulGenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    final String itemName = item.getName();
    logger.debug("Processing item " + itemName);
    final MaxCulBindingConfig config = new MaxCulBindingConfig(bindingConfig);
    addBindingConfig(item, config);
    addBindingChangeListener(new BindingChangeListener() {

        @Override
        public void bindingChanged(BindingProvider provider, String itemName) {
            /* binding changed so update the association map */
            associationsMapBuilt = false;
        // TODO check if config temperatures are set and flag that they
        // should be sent the device because they might have changed?
        }

        @Override
        public void allBindingsChanged(BindingProvider provider) {
            if (!provider.providesBindingFor(itemName)) {
            // TODO get serial number of itemName
            // then check if we still interact with that device, if not
            // then
            // deassociate and send a reset to it
            }
        }
    });
}
Also used : BindingChangeListener(org.openhab.core.binding.BindingChangeListener) AbstractGenericBindingProvider(org.openhab.model.item.binding.AbstractGenericBindingProvider) BindingProvider(org.openhab.core.binding.BindingProvider) MaxCulBindingProvider(org.openhab.binding.maxcul.MaxCulBindingProvider)

Example 3 with BindingChangeListener

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

the class MqttGenericBindingProvider method processBindingConfiguration.

@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    final String itemName = item.getName();
    logger.trace("Starting to load MQTT config for item {}", itemName);
    if (StringUtils.isEmpty(bindingConfig)) {
        throw new BindingConfigParseException("Missing mqtt binding configuration for item " + itemName);
    }
    final MqttItemConfig itemConfig = new MqttItemConfig(item, bindingConfig);
    // register all message consumers
    for (MqttMessageSubscriber subscriber : itemConfig.getMessageSubscribers()) {
        subscriber.setItemName(item.getName());
        mqttService.registerMessageConsumer(subscriber.getBroker(), subscriber);
    }
    // add binding change listener to clean up message consumers on item removal
    addBindingChangeListener(new BindingChangeListener() {

        @Override
        public void bindingChanged(BindingProvider provider, String changedItemName) {
            if (itemName.equals(changedItemName) && !provider.providesBindingFor(itemName)) {
                logger.debug("Removing message consumers for item {}", itemName);
                for (MqttMessageSubscriber subscriber : itemConfig.getMessageSubscribers()) {
                    mqttService.unregisterMessageConsumer(subscriber.getBroker(), subscriber);
                }
                removeBindingChangeListener(this);
            }
        }

        @Override
        public void allBindingsChanged(BindingProvider provider) {
            if (!provider.providesBindingFor(itemName)) {
                logger.debug("Removing message consumers for item {}", itemName);
                for (MqttMessageSubscriber subscriber : itemConfig.getMessageSubscribers()) {
                    mqttService.unregisterMessageConsumer(subscriber.getBroker(), subscriber);
                }
                removeBindingChangeListener(this);
            }
        }
    });
    // register all message producers
    for (MqttMessagePublisher publisher : itemConfig.getMessagePublishers()) {
        publisher.setItemName(item.getName());
        mqttService.registerMessageProducer(publisher.getBroker(), publisher);
    }
    // add binding change listener to clean up message publishers on item
    // removal
    addBindingChangeListener(new BindingChangeListener() {

        @Override
        public void bindingChanged(BindingProvider provider, String changedItemName) {
            if (itemName.equals(changedItemName) && !provider.providesBindingFor(itemName)) {
                logger.debug("Removing message publishers for item {}", itemName);
                for (MqttMessagePublisher publisher : itemConfig.getMessagePublishers()) {
                    mqttService.unregisterMessageProducer(publisher.getBroker(), publisher);
                }
                removeBindingChangeListener(this);
            }
        }

        @Override
        public void allBindingsChanged(BindingProvider provider) {
            if (!provider.providesBindingFor(itemName)) {
                logger.debug("Removing message publishers for item {}", itemName);
                for (MqttMessagePublisher publisher : itemConfig.getMessagePublishers()) {
                    mqttService.unregisterMessageProducer(publisher.getBroker(), publisher);
                }
                removeBindingChangeListener(this);
            }
        }
    });
    addBindingConfig(item, itemConfig);
}
Also used : BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) BindingChangeListener(org.openhab.core.binding.BindingChangeListener) BindingProvider(org.openhab.core.binding.BindingProvider) MqttBindingProvider(org.openhab.binding.mqtt.MqttBindingProvider) AbstractGenericBindingProvider(org.openhab.model.item.binding.AbstractGenericBindingProvider)

Aggregations

BindingChangeListener (org.openhab.core.binding.BindingChangeListener)3 BindingProvider (org.openhab.core.binding.BindingProvider)3 AbstractGenericBindingProvider (org.openhab.model.item.binding.AbstractGenericBindingProvider)3 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)2 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 MqttBindingProvider (org.openhab.binding.mqtt.MqttBindingProvider)1 ColorItem (org.openhab.core.library.items.ColorItem)1 DimmerItem (org.openhab.core.library.items.DimmerItem)1 SwitchItem (org.openhab.core.library.items.SwitchItem)1