Search in sources :

Example 1 with IhcExecption

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

the class WSBaseDataType method parseValue.

public static String parseValue(String xml, String xpathExpression) throws IhcExecption {
    InputStream is;
    try {
        is = new ByteArrayInputStream(xml.getBytes("UTF8"));
    } catch (UnsupportedEncodingException e) {
        throw new IhcExecption(e);
    }
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource inputSource = new InputSource(is);
    xpath.setNamespaceContext(new NamespaceContext() {

        @Override
        public String getNamespaceURI(String prefix) {
            if (prefix == null) {
                throw new NullPointerException("Null prefix");
            } else if ("SOAP-ENV".equals(prefix)) {
                return "http://schemas.xmlsoap.org/soap/envelope/";
            } else if ("ns1".equals(prefix)) {
                return "utcs";
            } else if ("ns2".equals(prefix)) {
                return "utcs.values";
            }
            return null;
        }

        @Override
        public String getPrefix(String uri) {
            return null;
        }

        @Override
        @SuppressWarnings("rawtypes")
        public Iterator getPrefixes(String uri) {
            throw new UnsupportedOperationException();
        }
    });
    try {
        return (String) xpath.evaluate(xpathExpression, inputSource, XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        throw new IhcExecption(e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) InputSource(org.xml.sax.InputSource) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IhcExecption(org.openhab.binding.ihc.ws.IhcExecption) ByteArrayInputStream(java.io.ByteArrayInputStream) NamespaceContext(javax.xml.namespace.NamespaceContext) Iterator(java.util.Iterator)

Example 2 with IhcExecption

use of org.openhab.binding.ihc.ws.IhcExecption 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 3 with IhcExecption

use of org.openhab.binding.ihc.ws.IhcExecption 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 IhcExecption

use of org.openhab.binding.ihc.ws.IhcExecption 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)

Aggregations

IhcExecption (org.openhab.binding.ihc.ws.IhcExecption)4 IhcBindingProvider (org.openhab.binding.ihc.IhcBindingProvider)3 WSResourceValue (org.openhab.binding.ihc.ws.datatypes.WSResourceValue)2 ConfigurationException (org.osgi.service.cm.ConfigurationException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1 XPath (javax.xml.xpath.XPath)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)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 InputSource (org.xml.sax.InputSource)1