Search in sources :

Example 1 with AutelisBindingProvider

use of org.openhab.binding.autelis.AutelisBindingProvider in project openhab1-addons by openhab.

the class AutelisBinding method internalReceiveCommand.

/**
     * @{inheritDoc}
     */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    logger.trace("internalReceiveCommand({},{}) is called!", itemName, command);
    for (AutelisBindingProvider provider : providers) {
        Item item = provider.getItem(itemName);
        String config = provider.getAutelisBindingConfigString(itemName);
        Matcher m = commandPattern.matcher(config);
        if (m.find() && m.groupCount() > 1) {
            String type = m.group(1);
            String name = m.group(2);
            if (type.equals(AUTELIS_TYPES_EQUIP)) {
                String cmd = AUTELIS_CMD_VALUE;
                int value;
                if (command == OnOffType.OFF) {
                    value = 0;
                } else if (command == OnOffType.ON) {
                    value = 1;
                } else if (command instanceof DecimalType) {
                    value = ((DecimalType) item.getStateAs(DecimalType.class)).intValue();
                    if (value >= 3) {
                        // this is a dim type. not sure what 2 does
                        cmd = AUTELIS_CMD_DIM;
                    }
                } else {
                    logger.error("Equipment commands must be of Decimal type not {}", command);
                    break;
                }
                String response = HttpUtil.executeUrl("GET", baseURL + "/set.cgi?name=" + name + "&" + cmd + "=" + value, TIMEOUT);
                logger.trace("equipment set {} {} {} : result {}", name, cmd, value, response);
            } else if (type.equals(AUTELIS_TYPES_TEMP)) {
                String value;
                if (command == IncreaseDecreaseType.INCREASE) {
                    value = AUTELIS_CMD_UP;
                } else if (command == IncreaseDecreaseType.DECREASE) {
                    value = AUTELIS_CMD_DOWN;
                } else {
                    value = command.toString();
                }
                String cmd;
                // name ending in sp are setpoints, ht are heat types?
                if (name.endsWith(AUTELIS_SETPOINT)) {
                    cmd = AUTELIS_TYPES_TEMP;
                } else if (name.endsWith(AUTELIS_HEATTYPE)) {
                    cmd = AUTELIS_CMD_HEAT;
                } else {
                    logger.error("Unknown temp type {}", name);
                    break;
                }
                String response = HttpUtil.executeUrl("GET", baseURL + "/set.cgi?wait=1&name=" + name + "&" + cmd + "=" + value, TIMEOUT);
                logger.trace("temp set {} {} : result {}", cmd, value, response);
            }
        } else if (config.equals(AUTELIS_TYPES_LIGHTS)) {
            /*
                 * lighting command
                 * possible values, but we will let anything through.
                 * alloff, allon, csync, cset, cswim, party, romance, caribbean, american,
                 * sunset, royalty, blue, green, red, white, magenta, hold, recall
                 */
            String response = HttpUtil.executeUrl("GET", baseURL + "lights.cgi?val=" + command.toString(), TIMEOUT);
            logger.trace("lights set {} : result {}", command.toString(), response);
        } else {
            logger.error("Unsupported set config {}", config);
        }
    }
    scheduleClearTime(UPDATE_CLEARTIME);
}
Also used : AutelisBindingProvider(org.openhab.binding.autelis.AutelisBindingProvider) SwitchItem(org.openhab.core.library.items.SwitchItem) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) Matcher(java.util.regex.Matcher) DecimalType(org.openhab.core.library.types.DecimalType)

Example 2 with AutelisBindingProvider

use of org.openhab.binding.autelis.AutelisBindingProvider in project openhab1-addons by openhab.

the class AutelisBinding method execute.

/**
     * @{inheritDoc}
     */
@Override
protected void execute() {
    logger.trace("Connecting to {}" + baseURL);
    clearState();
    String xmlDoc = fetchStateFromController();
    if (xmlDoc == null) {
        return;
    }
    for (AutelisBindingProvider provider : providers) {
        for (String itemName : provider.getItemNames()) {
            Item item = provider.getItem(itemName);
            String config = provider.getAutelisBindingConfigString(itemName);
            XPathFactory xpathFactory = XPathFactory.newInstance();
            XPath xpath = xpathFactory.newXPath();
            try {
                InputSource is = new InputSource(new StringReader(xmlDoc));
                String value = xpath.evaluate("response/" + config.replace('.', '/'), is);
                State state = toState(item.getClass(), value);
                State oldState = stateMap.put(itemName, state);
                if (!state.equals(oldState)) {
                    logger.debug("updating item {} with state {}", itemName, state);
                    eventPublisher.postUpdate(itemName, state);
                }
            } catch (XPathExpressionException e) {
                logger.warn("could not parse xml", e);
            }
        }
    }
}
Also used : AutelisBindingProvider(org.openhab.binding.autelis.AutelisBindingProvider) XPath(javax.xml.xpath.XPath) SwitchItem(org.openhab.core.library.items.SwitchItem) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) XPathFactory(javax.xml.xpath.XPathFactory) InputSource(org.xml.sax.InputSource) State(org.openhab.core.types.State) XPathExpressionException(javax.xml.xpath.XPathExpressionException) StringReader(java.io.StringReader)

Aggregations

AutelisBindingProvider (org.openhab.binding.autelis.AutelisBindingProvider)2 Item (org.openhab.core.items.Item)2 NumberItem (org.openhab.core.library.items.NumberItem)2 SwitchItem (org.openhab.core.library.items.SwitchItem)2 StringReader (java.io.StringReader)1 Matcher (java.util.regex.Matcher)1 XPath (javax.xml.xpath.XPath)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 XPathFactory (javax.xml.xpath.XPathFactory)1 DecimalType (org.openhab.core.library.types.DecimalType)1 State (org.openhab.core.types.State)1 InputSource (org.xml.sax.InputSource)1