Search in sources :

Example 1 with WemoBindingProvider

use of org.openhab.binding.wemo.WemoBindingProvider in project openhab1-addons by openhab.

the class WemoBinding method execute.

/**
     * @{inheritDoc}
     */
@Override
protected void execute() {
    logger.debug("execute() method is called!");
    for (WemoBindingProvider provider : providers) {
        for (String itemName : provider.getItemNames()) {
            logger.debug("Wemo item '{}' state will be updated", itemName);
            try {
                if (provider.getUDN(itemName).toLowerCase().contains("insight")) {
                    String insightParams = getInsightParams(itemName);
                    if (insightParams != null) {
                        String[] splitInsightParams = insightParams.split("\\|");
                        if (splitInsightParams[0] != null) {
                            if (provider.getChannelType(itemName).equals(WemoChannelType.state)) {
                                OnOffType binaryState = null;
                                binaryState = splitInsightParams[0].equals("0") ? OnOffType.OFF : OnOffType.ON;
                                if (binaryState != null) {
                                    logger.trace("New InsightParam binaryState '{}' for device '{}' received", binaryState, itemName);
                                    eventPublisher.postUpdate(itemName, binaryState);
                                }
                            }
                            if (provider.getChannelType(itemName).equals(WemoChannelType.lastChangedAt)) {
                                long lastChangedAt = 0;
                                try {
                                    // convert s to ms
                                    lastChangedAt = Long.parseLong(splitInsightParams[1]) * 1000;
                                } catch (NumberFormatException e) {
                                    logger.error("Unable to parse lastChangedAt value '{}' for device '{}'; expected long", splitInsightParams[1], itemName);
                                }
                                GregorianCalendar cal = new GregorianCalendar();
                                cal.setTimeInMillis(lastChangedAt);
                                State lastChangedAtState = new DateTimeType(cal);
                                if (lastChangedAt != 0) {
                                    logger.trace("New InsightParam lastChangedAt '{}' for device '{}' received", lastChangedAtState, itemName);
                                    eventPublisher.postUpdate(itemName, lastChangedAtState);
                                }
                            }
                            if (provider.getChannelType(itemName).equals(WemoChannelType.lastOnFor)) {
                                State lastOnFor = DecimalType.valueOf(splitInsightParams[2]);
                                if (lastOnFor != null) {
                                    logger.trace("New InsightParam lastOnFor '{}' for device '{}' received", lastOnFor, itemName);
                                    eventPublisher.postUpdate(itemName, lastOnFor);
                                }
                            }
                            if (provider.getChannelType(itemName).equals(WemoChannelType.onToday)) {
                                State onToday = DecimalType.valueOf(splitInsightParams[3]);
                                if (onToday != null) {
                                    logger.trace("New InsightParam onToday '{}' for device '{}' received", onToday, itemName);
                                    eventPublisher.postUpdate(itemName, onToday);
                                }
                            }
                            if (provider.getChannelType(itemName).equals(WemoChannelType.onTotal)) {
                                State onTotal = DecimalType.valueOf(splitInsightParams[4]);
                                if (onTotal != null) {
                                    logger.trace("New InsightParam onTotal '{}' for device '{}' received", onTotal, itemName);
                                    eventPublisher.postUpdate(itemName, onTotal);
                                }
                            }
                            if (provider.getChannelType(itemName).equals(WemoChannelType.timespan)) {
                                State timespan = DecimalType.valueOf(splitInsightParams[5]);
                                if (timespan != null) {
                                    logger.trace("New InsightParam timespan '{}' for device '{}' received", timespan, itemName);
                                    eventPublisher.postUpdate(itemName, timespan);
                                }
                            }
                            if (provider.getChannelType(itemName).equals(WemoChannelType.averagePower)) {
                                // natively given
                                State averagePower = DecimalType.valueOf(splitInsightParams[6]);
                                // in W
                                if (averagePower != null) {
                                    logger.trace("New InsightParam averagePower '{}' for device '{}' received", averagePower, itemName);
                                    eventPublisher.postUpdate(itemName, averagePower);
                                }
                            }
                            if (provider.getChannelType(itemName).equals(WemoChannelType.currentPower)) {
                                BigDecimal currentMW = new BigDecimal(splitInsightParams[7]);
                                State currentPower = new DecimalType(// recalculate
                                currentMW.divide(new BigDecimal(1000), RoundingMode.HALF_UP));
                                // mW to W
                                if (currentPower != null) {
                                    logger.trace("New InsightParam currentPower '{}' for device '{}' received", currentPower, itemName);
                                    eventPublisher.postUpdate(itemName, currentPower);
                                }
                            }
                            if (provider.getChannelType(itemName).equals(WemoChannelType.energyToday)) {
                                BigDecimal energyTodayMWMin = new BigDecimal(splitInsightParams[8]);
                                // recalculate mW-mins to Wh
                                State energyToday = new DecimalType(energyTodayMWMin.divide(new BigDecimal(60000), RoundingMode.HALF_UP));
                                if (energyToday != null) {
                                    logger.trace("New InsightParam energyToday '{}' for device '{}' received", energyToday, itemName);
                                    eventPublisher.postUpdate(itemName, energyToday);
                                }
                            }
                            if (provider.getChannelType(itemName).equals(WemoChannelType.energyTotal)) {
                                BigDecimal energyTotalMWMin = new BigDecimal(splitInsightParams[9]);
                                // recalculate mW-mins to Wh
                                State energyTotal = new DecimalType(energyTotalMWMin.divide(new BigDecimal(60000), RoundingMode.HALF_UP));
                                if (energyTotal != null) {
                                    logger.trace("New InsightParam energyTotal '{}' for device '{}' received", energyTotal, itemName);
                                    eventPublisher.postUpdate(itemName, energyTotal);
                                }
                            }
                            if (provider.getChannelType(itemName).equals(WemoChannelType.standbyLimit)) {
                                BigDecimal standbyLimitMW = new BigDecimal(splitInsightParams[10]);
                                // recalculate mW to W
                                State standbyLimit = new DecimalType(standbyLimitMW.divide(new BigDecimal(1000), RoundingMode.HALF_UP));
                                if (standbyLimit != null) {
                                    logger.trace("New InsightParam standbyLimit '{}' for device '{}' received", standbyLimit, itemName);
                                    eventPublisher.postUpdate(itemName, standbyLimit);
                                }
                            }
                        }
                    }
                } else {
                    String state = getWemoState(itemName);
                    if (state != null) {
                        if (provider.getUDN(itemName).toLowerCase().contains("motion")) {
                            State newState = state.equals("0") ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
                            eventPublisher.postUpdate(itemName, newState);
                        } else {
                            State itemState = state.equals("0") ? OnOffType.OFF : OnOffType.ON;
                            eventPublisher.postUpdate(itemName, itemState);
                        }
                    }
                }
            } catch (Exception e) {
                logger.error("Error in execute method: " + e.getMessage(), e);
            }
        }
    }
}
Also used : DateTimeType(org.openhab.core.library.types.DateTimeType) WemoBindingProvider(org.openhab.binding.wemo.WemoBindingProvider) OnOffType(org.openhab.core.library.types.OnOffType) State(org.openhab.core.types.State) GregorianCalendar(java.util.GregorianCalendar) DecimalType(org.openhab.core.library.types.DecimalType) BigDecimal(java.math.BigDecimal) ConfigurationException(org.osgi.service.cm.ConfigurationException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Example 2 with WemoBindingProvider

use of org.openhab.binding.wemo.WemoBindingProvider in project openhab1-addons by openhab.

the class WemoBinding method wemoCall.

private String wemoCall(String itemName, String soapMethod, String content) {
    try {
        for (WemoBindingProvider provider : providers) {
            String soapHeader = "SOAPACTION: \"" + soapMethod + "\"";
            String contentHeader = "Content-Type: text/xml; charset=\"utf-8\"";
            String endpoint = "/upnp/control/basicevent1";
            if (soapMethod.contains("insight")) {
                endpoint = "/upnp/control/insight1";
            }
            String wemoUDN = provider.getUDN(itemName);
            if (wemoUDN == null) {
                return null;
            }
            logger.trace("Calling WeMo item '{}' with configuration :", itemName);
            logger.trace("        UDN = '{}'", provider.getUDN(itemName));
            logger.trace("ChannelType = '{}'", provider.getChannelType(itemName));
            String wemoLocation = wemoConfigMap.get(wemoUDN);
            if (wemoLocation != null) {
                logger.trace("    Location = '{}'", wemoLocation);
                logger.trace("    EndPoint = '{}'", endpoint);
                String wemoURL = wemoLocation + endpoint;
                Properties wemoHeaders = new Properties();
                wemoHeaders.setProperty(soapHeader, contentHeader);
                InputStream wemoContent = new ByteArrayInputStream(content.getBytes(Charset.forName("UTF-8")));
                String wemoCallResponse = HttpUtil.executeUrl("POST", wemoURL, wemoHeaders, wemoContent, "text/xml", 2000);
                logger.trace("wemoresp: {}", wemoCallResponse);
                return wemoCallResponse;
            } else {
                logger.debug("No Location found for item '{}', start new discovery ", itemName);
                wemoDiscovery();
                String wemoCallResponse = "";
                return wemoCallResponse;
            }
        }
    } catch (Exception e) {
        wemoDiscovery();
        throw new RuntimeException("Could not call Wemo, did rediscovery", e);
    }
    return null;
}
Also used : WemoBindingProvider(org.openhab.binding.wemo.WemoBindingProvider) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Properties(java.util.Properties) ConfigurationException(org.osgi.service.cm.ConfigurationException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Example 3 with WemoBindingProvider

use of org.openhab.binding.wemo.WemoBindingProvider in project openhab1-addons by openhab.

the class WemoBinding method internalReceiveCommand.

/**
     * @{inheritDoc}
     */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    logger.debug("internalReceiveCommand() is called!");
    for (WemoBindingProvider provider : providers) {
        try {
            String udn = provider.getUDN(itemName);
            logger.trace("item '{}' has UDN '{}'", itemName, udn);
            logger.trace("Command '{}' is about to be sent to item '{}'", command, itemName);
            sendCommand(itemName, command);
        } catch (Exception e) {
            logger.error("Failed to send {} command", command, e);
        }
    }
}
Also used : WemoBindingProvider(org.openhab.binding.wemo.WemoBindingProvider) ConfigurationException(org.osgi.service.cm.ConfigurationException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)3 SocketTimeoutException (java.net.SocketTimeoutException)3 WemoBindingProvider (org.openhab.binding.wemo.WemoBindingProvider)3 ConfigurationException (org.osgi.service.cm.ConfigurationException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 BigDecimal (java.math.BigDecimal)1 GregorianCalendar (java.util.GregorianCalendar)1 Properties (java.util.Properties)1 DateTimeType (org.openhab.core.library.types.DateTimeType)1 DecimalType (org.openhab.core.library.types.DecimalType)1 OnOffType (org.openhab.core.library.types.OnOffType)1 State (org.openhab.core.types.State)1