Search in sources :

Example 1 with InsteonHubProxy

use of org.openhab.binding.insteonhub.internal.hardware.InsteonHubProxy in project openhab1-addons by openhab.

the class InsteonHubBinding method updated.

@Override
public synchronized void updated(Dictionary<String, ?> config) throws ConfigurationException {
    logger.debug(BINDING_NAME + " updated");
    try {
        // Process device configuration
        if (config != null) {
            String refreshIntervalString = (String) config.get("refresh");
            if (StringUtils.isNotBlank(refreshIntervalString)) {
                refreshInterval = Long.parseLong(refreshIntervalString);
            }
            // Stop all existing proxy async threads
            for (InsteonHubProxy proxy : proxies.values()) {
                proxy.stop();
            }
            // Clear proxy map. It will be rebuilt.
            proxies.clear();
            // Load new proxies
            Map<String, InsteonHubProxy> newProxies = InsteonHubProxyFactory.createInstances(config);
            proxies.putAll(newProxies);
            for (Map.Entry<String, InsteonHubProxy> entry : proxies.entrySet()) {
                String hubId = entry.getKey();
                InsteonHubProxy proxy = entry.getValue();
                proxy.addListener(new AsyncEventPublisher(hubId));
                // If activated, start proxy now
                if (activated) {
                    proxy.start();
                }
            }
            // Set properly configured
            setProperlyConfigured(true);
        }
    } catch (Throwable t) {
        logger.error("Error configuring " + getName(), t);
        setProperlyConfigured(false);
    }
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) InsteonHubProxy(org.openhab.binding.insteonhub.internal.hardware.InsteonHubProxy)

Example 2 with InsteonHubProxy

use of org.openhab.binding.insteonhub.internal.hardware.InsteonHubProxy in project openhab1-addons by openhab.

the class InsteonHubBinding method activate.

@Override
public synchronized void activate() {
    logger.debug(BINDING_NAME + " activated");
    activated = true;
    dimStopThread.start();
    // start all proxy async threads
    for (InsteonHubProxy proxy : proxies.values()) {
        proxy.start();
    }
}
Also used : InsteonHubProxy(org.openhab.binding.insteonhub.internal.hardware.InsteonHubProxy)

Example 3 with InsteonHubProxy

use of org.openhab.binding.insteonhub.internal.hardware.InsteonHubProxy in project openhab1-addons by openhab.

the class InsteonHubBinding method deactivate.

@Override
public synchronized void deactivate() {
    logger.debug(BINDING_NAME + " deactivated");
    activated = false;
    // stop all proxy async threads
    for (InsteonHubProxy proxy : proxies.values()) {
        proxy.stop();
    }
}
Also used : InsteonHubProxy(org.openhab.binding.insteonhub.internal.hardware.InsteonHubProxy)

Example 4 with InsteonHubProxy

use of org.openhab.binding.insteonhub.internal.hardware.InsteonHubProxy in project openhab1-addons by openhab.

the class InsteonHubBinding method internalReceiveCommand.

@Override
protected void internalReceiveCommand(String itemName, Command command) {
    // get configuration for this item
    InsteonHubBindingConfig config = InsteonHubBindingConfigUtil.getConfigForItem(providers, itemName);
    if (config == null) {
        logger.error(BINDING_NAME + " received command for unknown item '" + itemName + "'");
        return;
    }
    // parse info from config
    BindingType type = config.getBindingType();
    String hubId = config.getDeviceInfo().getHubId();
    String deviceId = config.getDeviceInfo().getDeviceId();
    // lookup proxy from this configuration
    InsteonHubProxy proxy = proxies.get(hubId);
    if (proxy == null) {
        logger.error(BINDING_NAME + " received command for unknown hub id '" + hubId + "'");
        return;
    }
    if (logger.isDebugEnabled()) {
        logger.debug(BINDING_NAME + " processing command '" + command + "' of type '" + command.getClass().getSimpleName() + "' for item '" + itemName + "'");
    }
    try {
        // process according to type
        if (type == BindingType.SWITCH) {
            // set value on or off
            if (command instanceof OnOffType) {
                proxy.setDevicePower(deviceId, command == OnOffType.ON);
            }
        } else if (type == BindingType.DIMMER) {
            // INSTEON Dimmer supports Dimmer and RollerShutter types
            if (command instanceof OnOffType) {
                // ON or OFF => Set level to 255 or 0
                int level = command == OnOffType.ON ? 255 : 0;
                proxy.setDeviceLevel(deviceId, level);
            } else if (command instanceof IncreaseDecreaseType) {
                // Increase/Decrease => Incremental Brighten/Dim
                InsteonHubAdjustmentType adjustmentType;
                if (command == IncreaseDecreaseType.INCREASE) {
                    adjustmentType = InsteonHubAdjustmentType.BRIGHTEN;
                } else {
                    adjustmentType = InsteonHubAdjustmentType.DIM;
                }
                if (setDimTimeout(itemName)) {
                    proxy.startDeviceAdjustment(deviceId, adjustmentType);
                }
            } else if (command instanceof UpDownType) {
                // Up/Down => Start Brighten/Dim
                InsteonHubAdjustmentType adjustmentType;
                if (command == UpDownType.UP) {
                    adjustmentType = InsteonHubAdjustmentType.BRIGHTEN;
                } else {
                    adjustmentType = InsteonHubAdjustmentType.DIM;
                }
                proxy.startDeviceAdjustment(deviceId, adjustmentType);
            } else if (command instanceof StopMoveType) {
                // Stop => Stop Brighten/Dim
                if (command == StopMoveType.STOP) {
                    proxy.stopDeviceAdjustment(deviceId);
                }
            } else {
                // set level from 0 to 100 percent value
                byte percentByte = Byte.parseByte(command.toString());
                float percent = percentByte * .01f;
                int level = (int) (255 * percent);
                proxy.setDeviceLevel(deviceId, level);
            }
        }
    } catch (Throwable t) {
        logger.error("Error processing command '" + command + "' for item '" + itemName + "'", t);
    }
}
Also used : UpDownType(org.openhab.core.library.types.UpDownType) StopMoveType(org.openhab.core.library.types.StopMoveType) InsteonHubProxy(org.openhab.binding.insteonhub.internal.hardware.InsteonHubProxy) OnOffType(org.openhab.core.library.types.OnOffType) BindingType(org.openhab.binding.insteonhub.internal.InsteonHubBindingConfig.BindingType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) InsteonHubAdjustmentType(org.openhab.binding.insteonhub.internal.hardware.InsteonHubAdjustmentType)

Example 5 with InsteonHubProxy

use of org.openhab.binding.insteonhub.internal.hardware.InsteonHubProxy in project openhab1-addons by openhab.

the class InsteonHubProxyFactory method createInstances.

public static Map<String, InsteonHubProxy> createInstances(Dictionary<String, ?> config) {
    Map<String, InsteonHubProxy> proxies = new HashMap<String, InsteonHubProxy>();
    // parse all configured receivers
    // ( insteonhub:<hubid>.host=10.0.0.2 )
    Enumeration<String> keys = config.keys();
    while (keys.hasMoreElements()) {
        String key = keys.nextElement();
        if (key.endsWith(CONFIG_KEY_HOST)) {
            // parse host
            String host = (String) config.get(key);
            int separatorIdx = key.indexOf('.');
            String hubId, keyPrefix;
            if (separatorIdx == -1) {
                // no prefix/hubid => one hub => use default hub ID
                hubId = InsteonHubBinding.DEFAULT_HUB_ID;
                keyPrefix = "";
            } else {
                // prefix => use it as the hub ID
                hubId = key.substring(0, separatorIdx);
                keyPrefix = hubId + ".";
            }
            String portStr = (String) config.get(keyPrefix + CONFIG_KEY_PORT);
            int port = StringUtils.isBlank(portStr) ? InsteonHubSerialProxy.DEFAULT_PORT : Integer.parseInt(config.get(keyPrefix + CONFIG_KEY_PORT).toString());
            // Create proxy, and add it to map
            InsteonHubProxy proxy = new InsteonHubSerialProxy(host, port);
            proxies.put(hubId, proxy);
        }
    }
    return proxies;
}
Also used : HashMap(java.util.HashMap) InsteonHubSerialProxy(org.openhab.binding.insteonhub.internal.hardware.api.serial.InsteonHubSerialProxy) InsteonHubProxy(org.openhab.binding.insteonhub.internal.hardware.InsteonHubProxy)

Aggregations

InsteonHubProxy (org.openhab.binding.insteonhub.internal.hardware.InsteonHubProxy)6 HashMap (java.util.HashMap)2 Map (java.util.Map)1 BindingType (org.openhab.binding.insteonhub.internal.InsteonHubBindingConfig.BindingType)1 InsteonHubAdjustmentType (org.openhab.binding.insteonhub.internal.hardware.InsteonHubAdjustmentType)1 InsteonHubSerialProxy (org.openhab.binding.insteonhub.internal.hardware.api.serial.InsteonHubSerialProxy)1 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)1 OnOffType (org.openhab.core.library.types.OnOffType)1 StopMoveType (org.openhab.core.library.types.StopMoveType)1 UpDownType (org.openhab.core.library.types.UpDownType)1