Search in sources :

Example 1 with IhcBindingProvider

use of org.openhab.binding.ihc.IhcBindingProvider in project openhab1-addons by openhab.

the class IhcBinding method enableResourceValueNotifications.

/**
     * Order resource value notifications from IHC controller.
     */
private void enableResourceValueNotifications() throws IhcExecption {
    logger.debug("Subscribe resource runtime value notifications");
    if (ihc != null) {
        if (ihc.getConnectionState() != ConnectionState.CONNECTED) {
            logger.debug("Controller is connecting, abort subscribe");
            return;
        }
        List<Integer> resourceIdList = new ArrayList<Integer>();
        for (IhcBindingProvider provider : providers) {
            for (String itemName : provider.getItemNames()) {
                resourceIdList.add(provider.getResourceIdForInBinding(itemName));
            }
        }
        if (resourceIdList.size() > 0) {
            logger.debug("Enable runtime notfications for {} resources", resourceIdList.size());
            try {
                ihc.enableRuntimeValueNotifications(resourceIdList);
            } catch (IhcExecption e) {
                logger.debug("Reconnection request");
                setReconnectRequest(true);
            }
        }
    } else {
        logger.warn("Controller is not initialized!");
        logger.debug("Reconnection request");
        setReconnectRequest(true);
    }
    setValueNotificationRequest(false);
}
Also used : IhcBindingProvider(org.openhab.binding.ihc.IhcBindingProvider) IhcExecption(org.openhab.binding.ihc.ws.IhcExecption) ArrayList(java.util.ArrayList)

Example 2 with IhcBindingProvider

use of org.openhab.binding.ihc.IhcBindingProvider in project openhab1-addons by openhab.

the class IhcBinding method execute.

/**
     * @{inheritDoc
     */
@Override
public void execute() {
    if (ihc == null || isReconnectRequestActivated()) {
        try {
            if (ihc != null) {
                disconnect();
            }
            connect();
            setReconnectRequest(false);
            enableResourceValueNotifications();
        } catch (IhcExecption e) {
            logger.warn("Can't open connection to controller", e);
            return;
        }
    }
    if (ihc != null) {
        if (isValueNotificationRequestActivated()) {
            try {
                enableResourceValueNotifications();
            } catch (IhcExecption e) {
                logger.warn("Can't enable resource value notifications from controller", e);
            }
        }
        // Poll all requested resources from controller
        for (IhcBindingProvider provider : providers) {
            for (String itemName : provider.getItemNames()) {
                int resourceId = provider.getResourceIdForInBinding(itemName);
                int itemRefreshInterval = provider.getRefreshInterval(itemName) * 1000;
                if (resourceId > 0 && itemRefreshInterval > 0) {
                    Long lastUpdateTimeStamp = lastUpdateMap.get(itemName);
                    if (lastUpdateTimeStamp == null) {
                        lastUpdateTimeStamp = 0L;
                    }
                    long age = System.currentTimeMillis() - lastUpdateTimeStamp;
                    boolean needsUpdate = age >= itemRefreshInterval;
                    if (needsUpdate) {
                        logger.debug("Item '{}' is about to be refreshed now", itemName);
                        try {
                            WSResourceValue resourceValue = null;
                            try {
                                resourceValue = ihc.resourceQuery(resourceId);
                            } catch (IhcExecption e) {
                                logger.warn("Value could not be read from controller - retrying one time.", e);
                                try {
                                    resourceValue = ihc.resourceQuery(resourceId);
                                } catch (IhcExecption ex) {
                                    logger.error("Communication error", ex);
                                    logger.debug("Reconnection request");
                                    setReconnectRequest(true);
                                }
                            }
                            if (resourceValue != null) {
                                Class<? extends Item> itemType = provider.getItemType(itemName);
                                State value = IhcDataConverter.convertResourceValueToState(itemType, resourceValue);
                                eventPublisher.postUpdate(itemName, value);
                            }
                        } catch (Exception e) {
                            logger.error("Error occured during resource query", e);
                        }
                        lastUpdateMap.put(itemName, System.currentTimeMillis());
                    }
                }
            }
        }
    } else {
        logger.warn("Controller is not initialized => refresh cycle aborted!");
    }
}
Also used : IhcExecption(org.openhab.binding.ihc.ws.IhcExecption) IhcBindingProvider(org.openhab.binding.ihc.IhcBindingProvider) WSResourceValue(org.openhab.binding.ihc.ws.datatypes.WSResourceValue) WSControllerState(org.openhab.binding.ihc.ws.datatypes.WSControllerState) ConnectionState(org.openhab.binding.ihc.ws.IhcClient.ConnectionState) State(org.openhab.core.types.State) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 3 with IhcBindingProvider

use of org.openhab.binding.ihc.IhcBindingProvider in project openhab1-addons by openhab.

the class IhcBinding method updateResource.

/**
     * Update resource value to IHC controller.
     */
private void updateResource(String itemName, Type type, boolean updateOnlyExclusiveOutBinding) {
    if (itemName != null) {
        Command cmd = null;
        try {
            cmd = (Command) type;
        } catch (Exception e) {
        }
        IhcBindingProvider provider = findFirstMatchingBindingProvider(itemName, cmd);
        if (provider == null) {
            // command not configured, skip
            return;
        }
        if (updateOnlyExclusiveOutBinding && provider.hasInBinding(itemName)) {
            logger.trace("Ignore in binding update for item '{}'", itemName);
            return;
        }
        logger.debug("Received update/command (item='{}', state='{}', class='{}')", new Object[] { itemName, type.toString(), type.getClass().toString() });
        if (ihc == null) {
            logger.warn("Controller is not initialized, abort resource value update for item '{}'!", itemName);
            return;
        }
        if (ihc.getConnectionState() != ConnectionState.CONNECTED) {
            logger.warn("Connection to controller is not ok, abort resource value update for item '{}'!", itemName);
            return;
        }
        try {
            int resourceId = provider.getResourceId(itemName, (Command) type);
            logger.trace("found resourceId {} (item='{}', state='{}', class='{}')", new Object[] { new Integer(resourceId).toString(), itemName, type.toString(), type.getClass().toString() });
            if (resourceId > 0) {
                WSResourceValue value = ihc.getResourceValueInformation(resourceId);
                ArrayList<IhcEnumValue> enumValues = null;
                if (value instanceof WSEnumValue) {
                    enumValues = ihc.getEnumValues(((WSEnumValue) value).getDefinitionTypeID());
                }
                // check if configuration has a custom value defined
                // (0->OFF, 1->ON, >1->trigger)
                // if that is the case, the type will be overridden with a
                // new type
                Integer val = provider.getValue(itemName, (Command) type);
                boolean trigger = false;
                if (val != null) {
                    if (val == 0) {
                        type = OnOffType.OFF;
                    } else if (val == 1) {
                        type = OnOffType.ON;
                    } else {
                        trigger = true;
                    }
                } else {
                // the original type is kept
                }
                if (!trigger) {
                    value = IhcDataConverter.convertCommandToResourceValue(type, value, enumValues);
                    boolean result = updateResource(value);
                    if (result == true) {
                        logger.debug("Item updated '{}' succesfully sent", itemName);
                    } else {
                        logger.error("Item '{}' update failed", itemName);
                    }
                } else {
                    value = IhcDataConverter.convertCommandToResourceValue(OnOffType.ON, value, enumValues);
                    boolean result = updateResource(value);
                    if (result == true) {
                        logger.debug("Item '{}' trigger started", itemName);
                        Thread.sleep(val);
                        value = IhcDataConverter.convertCommandToResourceValue(OnOffType.OFF, value, enumValues);
                        result = updateResource(value);
                        if (result == true) {
                            logger.debug("Item '{}' trigger completed", itemName);
                        } else {
                            logger.error("Item '{}' trigger stop failed", itemName);
                        }
                    } else {
                        logger.error("Item '{}' update failed", itemName);
                    }
                }
            } else {
                logger.error("resourceId invalid");
            }
        } catch (IhcExecption e) {
            logger.error("Can't update Item '{}' value ", itemName, e);
        } catch (Exception e) {
            logger.error("Error occured during item update", e);
        }
    }
}
Also used : IhcEnumValue(org.openhab.binding.ihc.ws.IhcEnumValue) IhcBindingProvider(org.openhab.binding.ihc.IhcBindingProvider) WSResourceValue(org.openhab.binding.ihc.ws.datatypes.WSResourceValue) IhcExecption(org.openhab.binding.ihc.ws.IhcExecption) Command(org.openhab.core.types.Command) WSEnumValue(org.openhab.binding.ihc.ws.datatypes.WSEnumValue) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 4 with IhcBindingProvider

use of org.openhab.binding.ihc.IhcBindingProvider in project openhab1-addons by openhab.

the class IhcBinding method resourceValueUpdateReceived.

@Override
public void resourceValueUpdateReceived(EventObject event, WSResourceValue value) {
    for (IhcBindingProvider provider : providers) {
        for (String itemName : provider.getItemNames()) {
            int resourceId = provider.getResourceIdForInBinding(itemName);
            if (value.getResourceID() == resourceId) {
                if (!provider.hasInBinding(itemName)) {
                    logger.trace("{} has no inbinding...skip update to OpenHAB bus", itemName);
                } else {
                    Class<? extends Item> itemType = provider.getItemType(itemName);
                    State state = IhcDataConverter.convertResourceValueToState(itemType, value);
                    logger.trace("Received resource value update (item='{}', state='{}')", new Object[] { itemName, state });
                    eventPublisher.postUpdate(itemName, state);
                }
            }
        }
    }
}
Also used : IhcBindingProvider(org.openhab.binding.ihc.IhcBindingProvider) WSControllerState(org.openhab.binding.ihc.ws.datatypes.WSControllerState) ConnectionState(org.openhab.binding.ihc.ws.IhcClient.ConnectionState) State(org.openhab.core.types.State)

Aggregations

IhcBindingProvider (org.openhab.binding.ihc.IhcBindingProvider)4 IhcExecption (org.openhab.binding.ihc.ws.IhcExecption)3 ConnectionState (org.openhab.binding.ihc.ws.IhcClient.ConnectionState)2 WSControllerState (org.openhab.binding.ihc.ws.datatypes.WSControllerState)2 WSResourceValue (org.openhab.binding.ihc.ws.datatypes.WSResourceValue)2 State (org.openhab.core.types.State)2 ConfigurationException (org.osgi.service.cm.ConfigurationException)2 ArrayList (java.util.ArrayList)1 IhcEnumValue (org.openhab.binding.ihc.ws.IhcEnumValue)1 WSEnumValue (org.openhab.binding.ihc.ws.datatypes.WSEnumValue)1 Command (org.openhab.core.types.Command)1