Search in sources :

Example 66 with DecimalType

use of org.eclipse.smarthome.core.library.types.DecimalType in project smarthome by eclipse.

the class SitemapResourceTest method whenLongPolling_ShouldObserveItemsFromVisibilityRules.

@Test
public void whenLongPolling_ShouldObserveItemsFromVisibilityRules() {
    new Thread(() -> {
        try {
            // wait for the #getPageData call and listeners to attach to the
            Thread.sleep(STATE_UPDATE_WAIT_TIME);
            // item
            visibilityRuleItem.setState(new DecimalType(BigDecimal.ONE));
        } catch (InterruptedException e) {
        }
    }).start();
    // non-null is sufficient here.
    when(headers.getRequestHeader(HTTP_HEADER_X_ATMOSPHERE_TRANSPORT)).thenReturn(Collections.emptyList());
    Response response = sitemapResource.getPageData(headers, null, SITEMAP_MODEL_NAME, SITEMAP_NAME, null);
    PageDTO pageDTO = (PageDTO) response.getEntity();
    // assert that the item state change did trigger the blocking method to
    assertThat(pageDTO.timeout, is(false));
// return
}
Also used : Response(javax.ws.rs.core.Response) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Test(org.junit.Test)

Example 67 with DecimalType

use of org.eclipse.smarthome.core.library.types.DecimalType in project smarthome by eclipse.

the class WemoCoffeeHandler method updateWemoState.

/**
 * The {@link updateWemoState} polls the actual state of a WeMo CoffeeMaker.
 */
protected void updateWemoState() {
    String action = "GetAttributes";
    String actionService = "deviceevent";
    String soapHeader = "\"urn:Belkin:service:" + actionService + ":1#" + action + "\"";
    String content = "<?xml version=\"1.0\"?>" + "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" + "<s:Body>" + "<u:" + action + " xmlns:u=\"urn:Belkin:service:" + actionService + ":1\">" + "</u:" + action + ">" + "</s:Body>" + "</s:Envelope>";
    try {
        String wemoURL = getWemoURL(actionService);
        if (wemoURL != null) {
            String wemoCallResponse = WemoHttpCall.executeCall(wemoURL, soapHeader, content);
            if (wemoCallResponse != null) {
                try {
                    String stringParser = StringUtils.substringBetween(wemoCallResponse, "<attributeList>", "</attributeList>");
                    // Due to Belkins bad response formatting, we need to run this twice.
                    stringParser = StringEscapeUtils.unescapeXml(stringParser);
                    stringParser = StringEscapeUtils.unescapeXml(stringParser);
                    logger.trace("CoffeeMaker response '{}' for device '{}' received", stringParser, getThing().getUID());
                    stringParser = "<data>" + stringParser + "</data>";
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    InputSource is = new InputSource();
                    is.setCharacterStream(new StringReader(stringParser));
                    Document doc = db.parse(is);
                    NodeList nodes = doc.getElementsByTagName("attribute");
                    // iterate the attributes
                    for (int i = 0; i < nodes.getLength(); i++) {
                        Element element = (Element) nodes.item(i);
                        NodeList deviceIndex = element.getElementsByTagName("name");
                        Element line = (Element) deviceIndex.item(0);
                        String attributeName = getCharacterDataFromElement(line);
                        logger.trace("attributeName: {}", attributeName);
                        NodeList deviceID = element.getElementsByTagName("value");
                        line = (Element) deviceID.item(0);
                        String attributeValue = getCharacterDataFromElement(line);
                        logger.trace("attributeValue: {}", attributeValue);
                        switch(attributeName) {
                            case "Mode":
                                State newMode = new StringType("Brewing");
                                switch(attributeValue) {
                                    case "0":
                                        updateState(CHANNEL_STATE, OnOffType.ON);
                                        newMode = new StringType("Refill");
                                        updateState(CHANNEL_COFFEEMODE, newMode);
                                        break;
                                    case "1":
                                        updateState(CHANNEL_STATE, OnOffType.OFF);
                                        newMode = new StringType("PlaceCarafe");
                                        updateState(CHANNEL_COFFEEMODE, newMode);
                                        break;
                                    case "2":
                                        updateState(CHANNEL_STATE, OnOffType.OFF);
                                        newMode = new StringType("RefillWater");
                                        updateState(CHANNEL_COFFEEMODE, newMode);
                                        break;
                                    case "3":
                                        updateState(CHANNEL_STATE, OnOffType.OFF);
                                        newMode = new StringType("Ready");
                                        updateState(CHANNEL_COFFEEMODE, newMode);
                                        break;
                                    case "4":
                                        updateState(CHANNEL_STATE, OnOffType.ON);
                                        newMode = new StringType("Brewing");
                                        updateState(CHANNEL_COFFEEMODE, newMode);
                                        break;
                                    case "5":
                                        updateState(CHANNEL_STATE, OnOffType.OFF);
                                        newMode = new StringType("Brewed");
                                        updateState(CHANNEL_COFFEEMODE, newMode);
                                        break;
                                    case "6":
                                        updateState(CHANNEL_STATE, OnOffType.OFF);
                                        newMode = new StringType("CleaningBrewing");
                                        updateState(CHANNEL_COFFEEMODE, newMode);
                                        break;
                                    case "7":
                                        updateState(CHANNEL_STATE, OnOffType.OFF);
                                        newMode = new StringType("CleaningSoaking");
                                        updateState(CHANNEL_COFFEEMODE, newMode);
                                        break;
                                    case "8":
                                        updateState(CHANNEL_STATE, OnOffType.OFF);
                                        newMode = new StringType("BrewFailCarafeRemoved");
                                        updateState(CHANNEL_COFFEEMODE, newMode);
                                        break;
                                }
                                break;
                            case "ModeTime":
                                if (attributeValue != null) {
                                    State newAttributeValue = new DecimalType(attributeValue);
                                    updateState(CHANNEL_MODETIME, newAttributeValue);
                                }
                                break;
                            case "TimeRemaining":
                                if (attributeValue != null) {
                                    State newAttributeValue = new DecimalType(attributeValue);
                                    updateState(CHANNEL_TIMEREMAINING, newAttributeValue);
                                }
                                break;
                            case "WaterLevelReached":
                                if (attributeValue != null) {
                                    State newAttributeValue = new DecimalType(attributeValue);
                                    updateState(CHANNEL_WATERLEVELREACHED, newAttributeValue);
                                }
                                break;
                            case "CleanAdvise":
                                if (attributeValue != null) {
                                    State newAttributeValue = attributeValue.equals("0") ? OnOffType.OFF : OnOffType.ON;
                                    updateState(CHANNEL_CLEANADVISE, newAttributeValue);
                                }
                                break;
                            case "FilterAdvise":
                                if (attributeValue != null) {
                                    State newAttributeValue = attributeValue.equals("0") ? OnOffType.OFF : OnOffType.ON;
                                    updateState(CHANNEL_FILTERADVISE, newAttributeValue);
                                }
                                break;
                            case "Brewed":
                                if (attributeValue != null) {
                                    State newAttributeValue = getDateTimeState(attributeValue);
                                    if (newAttributeValue != null) {
                                        updateState(CHANNEL_BREWED, newAttributeValue);
                                    }
                                }
                                break;
                            case "LastCleaned":
                                if (attributeValue != null) {
                                    State newAttributeValue = getDateTimeState(attributeValue);
                                    if (newAttributeValue != null) {
                                        updateState(CHANNEL_LASTCLEANED, newAttributeValue);
                                    }
                                }
                                break;
                        }
                    }
                } catch (Exception e) {
                    logger.error("Failed to parse attributeList for WeMo CoffeMaker '{}'", this.getThing().getUID(), e);
                }
            }
        }
    } catch (Exception e) {
        logger.error("Failed to get attributes for device '{}'", getThing().getUID(), e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StringType(org.eclipse.smarthome.core.library.types.StringType) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) DocumentBuilder(javax.xml.parsers.DocumentBuilder) State(org.eclipse.smarthome.core.types.State) StringReader(java.io.StringReader) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType)

Example 68 with DecimalType

use of org.eclipse.smarthome.core.library.types.DecimalType in project smarthome by eclipse.

the class WemoHandler method onValueReceived.

@Override
public void onValueReceived(String variable, String value, String service) {
    logger.debug("Received pair '{}':'{}' (service '{}') for thing '{}'", new Object[] { variable, value, service, this.getThing().getUID() });
    updateStatus(ThingStatus.ONLINE);
    this.stateMap.put(variable, value);
    if (getThing().getThingTypeUID().getId().equals("insight")) {
        String insightParams = stateMap.get("InsightParams");
        if (insightParams != null) {
            String[] splitInsightParams = insightParams.split("\\|");
            if (splitInsightParams[0] != null) {
                OnOffType binaryState = null;
                binaryState = splitInsightParams[0].equals("0") ? OnOffType.OFF : OnOffType.ON;
                if (binaryState != null) {
                    logger.trace("New InsightParam binaryState '{}' for device '{}' received", binaryState, getThing().getUID());
                    updateState(CHANNEL_STATE, binaryState);
                }
            }
            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], getThing().getUID());
            }
            ZonedDateTime zoned = ZonedDateTime.ofInstant(Instant.ofEpochMilli(lastChangedAt), TimeZone.getDefault().toZoneId());
            State lastChangedAtState = new DateTimeType(zoned);
            if (lastChangedAt != 0) {
                logger.trace("New InsightParam lastChangedAt '{}' for device '{}' received", lastChangedAtState, getThing().getUID());
                updateState(CHANNEL_LASTCHANGEDAT, lastChangedAtState);
            }
            State lastOnFor = DecimalType.valueOf(splitInsightParams[2]);
            if (lastOnFor != null) {
                logger.trace("New InsightParam lastOnFor '{}' for device '{}' received", lastOnFor, getThing().getUID());
                updateState(CHANNEL_LASTONFOR, lastOnFor);
            }
            State onToday = DecimalType.valueOf(splitInsightParams[3]);
            if (onToday != null) {
                logger.trace("New InsightParam onToday '{}' for device '{}' received", onToday, getThing().getUID());
                updateState(CHANNEL_ONTODAY, onToday);
            }
            State onTotal = DecimalType.valueOf(splitInsightParams[4]);
            if (onTotal != null) {
                logger.trace("New InsightParam onTotal '{}' for device '{}' received", onTotal, getThing().getUID());
                updateState(CHANNEL_ONTOTAL, onTotal);
            }
            State timespan = DecimalType.valueOf(splitInsightParams[5]);
            if (timespan != null) {
                logger.trace("New InsightParam timespan '{}' for device '{}' received", timespan, getThing().getUID());
                updateState(CHANNEL_TIMESPAN, timespan);
            }
            // natively given in W
            State averagePower = DecimalType.valueOf(splitInsightParams[6]);
            if (averagePower != null) {
                logger.trace("New InsightParam averagePower '{}' for device '{}' received", averagePower, getThing().getUID());
                updateState(CHANNEL_AVERAGEPOWER, averagePower);
            }
            BigDecimal currentMW = new BigDecimal(splitInsightParams[7]);
            // recalculate
            State currentPower = new DecimalType(currentMW.divide(new BigDecimal(1000), RoundingMode.HALF_UP));
            // mW to W
            if (currentPower != null) {
                logger.trace("New InsightParam currentPower '{}' for device '{}' received", currentPower, getThing().getUID());
                updateState(CHANNEL_CURRENTPOWER, currentPower);
            }
            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, getThing().getUID());
                updateState(CHANNEL_ENERGYTODAY, energyToday);
            }
            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, getThing().getUID());
                updateState(CHANNEL_ENERGYTOTAL, energyTotal);
            }
            BigDecimal standByLimitMW = new BigDecimal(splitInsightParams[10]);
            // recalculate
            State standByLimit = new DecimalType(standByLimitMW.divide(new BigDecimal(1000), RoundingMode.HALF_UP));
            // mW to W
            if (standByLimit != null) {
                logger.trace("New InsightParam standByLimit '{}' for device '{}' received", standByLimit, getThing().getUID());
                updateState(CHANNEL_STANDBYLIMIT, standByLimit);
            }
        }
    } else {
        State state = stateMap.get("BinaryState").equals("0") ? OnOffType.OFF : OnOffType.ON;
        logger.debug("State '{}' for device '{}' received", state, getThing().getUID());
        if (state != null) {
            if (getThing().getThingTypeUID().getId().equals("motion")) {
                updateState(CHANNEL_MOTIONDETECTION, state);
                if (state.equals(OnOffType.ON)) {
                    State lastMotionDetected = new DateTimeType();
                    updateState(CHANNEL_LASTMOTIONDETECTED, lastMotionDetected);
                }
            } else {
                updateState(CHANNEL_STATE, state);
            }
        }
    }
}
Also used : DateTimeType(org.eclipse.smarthome.core.library.types.DateTimeType) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) ZonedDateTime(java.time.ZonedDateTime) State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) BigDecimal(java.math.BigDecimal)

Aggregations

DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)68 Test (org.junit.Test)33 State (org.eclipse.smarthome.core.types.State)17 PercentType (org.eclipse.smarthome.core.library.types.PercentType)16 HistoricItem (org.eclipse.smarthome.core.persistence.HistoricItem)11 StringType (org.eclipse.smarthome.core.library.types.StringType)9 HSBType (org.eclipse.smarthome.core.library.types.HSBType)8 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)7 Type (org.eclipse.smarthome.core.types.Type)7 Item (org.eclipse.smarthome.core.items.Item)6 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 BigDecimal (java.math.BigDecimal)5 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)4 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)4 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)4 Response (javax.ws.rs.core.Response)3 ValueSet (org.eclipse.smarthome.binding.dmx.internal.ValueSet)3 FadeAction (org.eclipse.smarthome.binding.dmx.internal.action.FadeAction)3 DateTimeType (org.eclipse.smarthome.core.library.types.DateTimeType)3