Search in sources :

Example 1 with WSResourceValue

use of org.openhab.binding.ihc.ws.datatypes.WSResourceValue in project openhab1-addons by openhab.

the class IhcResourceInteractionService method resourceQuery.

/**
     * Query resource value from controller.
     * 
     * 
     * @param resoureId
     *            Resource Identifier.
     * @return Resource value.
     */
public WSResourceValue resourceQuery(int resoureId) throws IhcExecption {
    final String soapQuery = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soapenv:Body>" + " <ns1:getRuntimeValue1 xmlns:ns1=\"utcs\">%s</ns1:getRuntimeValue1>" + "</soapenv:Body>" + "</soapenv:Envelope>";
    String query = String.format(soapQuery, String.valueOf(resoureId));
    openConnection(url);
    String response = sendQuery(query, timeout);
    closeConnection();
    NodeList nodeList;
    try {
        nodeList = parseList(response, "/SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:getRuntimeValue2");
        if (nodeList.getLength() == 1) {
            WSResourceValue val = parseResourceValue(nodeList.item(0), 2);
            if (val.getResourceID() == resoureId) {
                return val;
            } else {
                throw new IhcExecption("No resource id found");
            }
        } else {
            throw new IhcExecption("No resource value found");
        }
    } catch (XPathExpressionException e) {
        throw new IhcExecption(e);
    } catch (UnsupportedEncodingException e) {
        throw new IhcExecption(e);
    }
}
Also used : WSResourceValue(org.openhab.binding.ihc.ws.datatypes.WSResourceValue) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with WSResourceValue

use of org.openhab.binding.ihc.ws.datatypes.WSResourceValue in project openhab1-addons by openhab.

the class IhcClient method waitResourceValueNotifications.

/**
     * Wait runtime value notifications.
     * 
     * Runtime value notification should firstly be activated by
     * enableRuntimeValueNotifications function.
     * 
     * @param timeoutInSeconds
     *            How many seconds to wait notifications.
     * @return List of received runtime value notifications.
     * @throws SocketTimeoutException
     */
private List<? extends WSResourceValue> waitResourceValueNotifications(int timeoutInSeconds) throws IhcExecption, SocketTimeoutException {
    IhcResourceInteractionService service = new IhcResourceInteractionService(ip, timeout);
    List<? extends WSResourceValue> list = service.waitResourceValueNotifications(timeoutInSeconds);
    for (WSResourceValue val : list) {
        resourceValues.put(val.getResourceID(), val);
    }
    return list;
}
Also used : WSResourceValue(org.openhab.binding.ihc.ws.datatypes.WSResourceValue)

Example 3 with WSResourceValue

use of org.openhab.binding.ihc.ws.datatypes.WSResourceValue 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 4 with WSResourceValue

use of org.openhab.binding.ihc.ws.datatypes.WSResourceValue 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 5 with WSResourceValue

use of org.openhab.binding.ihc.ws.datatypes.WSResourceValue in project openhab1-addons by openhab.

the class IhcResourceInteractionService method waitResourceValueNotifications.

/**
     * Wait runtime value notifications.
     * 
     * Runtime value notification should firstly be activated by
     * enableRuntimeValueNotifications function.
     * 
     * @param timeoutInSeconds
     *            How many seconds to wait notifications.
     * @return List of received runtime value notifications.
     * @throws SocketTimeoutException
     */
public List<? extends WSResourceValue> waitResourceValueNotifications(int timeoutInSeconds) throws IhcExecption, SocketTimeoutException {
    final String soapQuery = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:utcs=\"utcs\">" + "<soapenv:Header/>" + "<soapenv:Body>" + " <utcs:waitForResourceValueChanges1>%s</utcs:waitForResourceValueChanges1>" + "</soapenv:Body>" + "</soapenv:Envelope>";
    String query = String.format(soapQuery, timeoutInSeconds);
    openConnection(url);
    String response = sendQuery(query, timeout + timeoutInSeconds * 1000);
    closeConnection();
    List<WSResourceValue> resourceValueList = new ArrayList<WSResourceValue>();
    NodeList nodeList;
    try {
        nodeList = parseList(response, "/SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:waitForResourceValueChanges2/ns1:arrayItem");
        if (nodeList.getLength() == 1) {
            String resourceId = getValue(nodeList.item(0), "ns1:resourceID");
            if (resourceId == null || resourceId == "") {
                throw new SocketTimeoutException();
            }
        }
        for (int i = 0; i < nodeList.getLength(); i++) {
            int index = i + 2;
            WSResourceValue newVal = parseResourceValue(nodeList.item(i), index);
            resourceValueList.add(newVal);
        }
        return resourceValueList;
    } catch (XPathExpressionException e) {
        throw new IhcExecption(e);
    } catch (UnsupportedEncodingException e) {
        throw new IhcExecption(e);
    }
}
Also used : WSResourceValue(org.openhab.binding.ihc.ws.datatypes.WSResourceValue) SocketTimeoutException(java.net.SocketTimeoutException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

WSResourceValue (org.openhab.binding.ihc.ws.datatypes.WSResourceValue)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 XPathExpressionException (javax.xml.xpath.XPathExpressionException)2 IhcBindingProvider (org.openhab.binding.ihc.IhcBindingProvider)2 IhcExecption (org.openhab.binding.ihc.ws.IhcExecption)2 ConfigurationException (org.osgi.service.cm.ConfigurationException)2 NodeList (org.w3c.dom.NodeList)2 SocketTimeoutException (java.net.SocketTimeoutException)1 ArrayList (java.util.ArrayList)1 ConnectionState (org.openhab.binding.ihc.ws.IhcClient.ConnectionState)1 IhcEnumValue (org.openhab.binding.ihc.ws.IhcEnumValue)1 WSControllerState (org.openhab.binding.ihc.ws.datatypes.WSControllerState)1 WSEnumValue (org.openhab.binding.ihc.ws.datatypes.WSEnumValue)1 Command (org.openhab.core.types.Command)1 State (org.openhab.core.types.State)1