Search in sources :

Example 6 with StringType

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

the class WemoCoffeeHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    logger.trace("Command '{}' received for channel '{}'", command, channelUID);
    if (command instanceof RefreshType) {
        try {
            updateWemoState();
        } catch (Exception e) {
            logger.debug("Exception during poll : {}", e);
        }
    } else if (channelUID.getId().equals(CHANNEL_STATE)) {
        if (command instanceof OnOffType) {
            if (command.equals(OnOffType.ON)) {
                try {
                    String soapHeader = "\"urn:Belkin:service:deviceevent:1#SetAttributes\"";
                    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:SetAttributes xmlns:u=\"urn:Belkin:service:deviceevent:1\">" + "<attributeList>&lt;attribute&gt;&lt;name&gt;Brewed&lt;/name&gt;&lt;value&gt;NULL&lt;/value&gt;&lt;/attribute&gt;" + "&lt;attribute&gt;&lt;name&gt;LastCleaned&lt;/name&gt;&lt;value&gt;NULL&lt;/value&gt;&lt;/attribute&gt;&lt;attribute&gt;" + "&lt;name&gt;ModeTime&lt;/name&gt;&lt;value&gt;NULL&lt;/value&gt;&lt;/attribute&gt;&lt;attribute&gt;&lt;name&gt;Brewing&lt;/name&gt;" + "&lt;value&gt;NULL&lt;/value&gt;&lt;/attribute&gt;&lt;attribute&gt;&lt;name&gt;TimeRemaining&lt;/name&gt;&lt;value&gt;NULL&lt;/value&gt;" + "&lt;/attribute&gt;&lt;attribute&gt;&lt;name&gt;WaterLevelReached&lt;/name&gt;&lt;value&gt;NULL&lt;/value&gt;&lt;/attribute&gt;&lt;" + "attribute&gt;&lt;name&gt;Mode&lt;/name&gt;&lt;value&gt;4&lt;/value&gt;&lt;/attribute&gt;&lt;attribute&gt;&lt;name&gt;CleanAdvise&lt;/name&gt;" + "&lt;value&gt;NULL&lt;/value&gt;&lt;/attribute&gt;&lt;attribute&gt;&lt;name&gt;FilterAdvise&lt;/name&gt;&lt;value&gt;NULL&lt;/value&gt;&lt;/attribute&gt;" + "&lt;attribute&gt;&lt;name&gt;Cleaning&lt;/name&gt;&lt;value&gt;NULL&lt;/value&gt;&lt;/attribute&gt;</attributeList>" + "</u:SetAttributes>" + "</s:Body>" + "</s:Envelope>";
                    String wemoURL = getWemoURL("deviceevent");
                    if (wemoURL != null) {
                        String wemoCallResponse = WemoHttpCall.executeCall(wemoURL, soapHeader, content);
                        if (wemoCallResponse != null) {
                            updateState(CHANNEL_STATE, OnOffType.ON);
                            State newMode = new StringType("Brewing");
                            updateState(CHANNEL_COFFEEMODE, newMode);
                        }
                    }
                } catch (Exception e) {
                    logger.error("Failed to send command '{}' for device '{}': {}", command, getThing().getUID(), e.getMessage());
                    updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
                }
            }
            // if command.equals(OnOffType.OFF) we do nothing because WeMo Coffee Maker cannot be switched off
            // remotely
            updateStatus(ThingStatus.ONLINE);
        }
    }
}
Also used : OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) StringType(org.eclipse.smarthome.core.library.types.StringType) State(org.eclipse.smarthome.core.types.State) RefreshType(org.eclipse.smarthome.core.types.RefreshType)

Example 7 with StringType

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

the class WeatherUndergroundHandler method updateForecastChannel.

private State updateForecastChannel(String channelId, WeatherUndergroundJsonForecast forecast) {
    WUQuantity quantity;
    int day = getDay(channelId);
    WeatherUndergroundJsonForecastDay dayForecast = forecast.getSimpleForecast(day);
    String channelTypeId = getChannelTypeId(channelId);
    switch(channelTypeId) {
        case "forecastTime":
            return undefOrState(dayForecast.getForecastTime(), new DateTimeType(dayForecast.getForecastTime()));
        case "conditions":
            return undefOrState(dayForecast.getConditions(), new StringType(dayForecast.getConditions()));
        case "minTemperature":
            quantity = getTemperature(dayForecast.getMinTemperatureC(), dayForecast.getMinTemperatureF());
            return undefOrQuantity(quantity);
        case "maxTemperature":
            quantity = getTemperature(dayForecast.getMaxTemperatureC(), dayForecast.getMaxTemperatureF());
            return undefOrQuantity(quantity);
        case "relativeHumidity":
            return undefOrState(dayForecast.getRelativeHumidity(), new QuantityType<>(dayForecast.getRelativeHumidity(), SmartHomeUnits.PERCENT));
        case "probaPrecipitation":
            return undefOrState(dayForecast.getProbaPrecipitation(), new QuantityType<>(dayForecast.getProbaPrecipitation(), SmartHomeUnits.PERCENT));
        case "precipitationDay":
            quantity = getPrecipitation(dayForecast.getPrecipitationDayMm(), dayForecast.getPrecipitationDayIn());
            return undefOrQuantity(quantity);
        case "snow":
            quantity = getWUQuantity(CENTI(SIUnits.METRE), ImperialUnits.INCH, dayForecast.getSnowCm(), dayForecast.getSnowIn());
            return undefOrQuantity(quantity);
        case "maxWindDirection":
            return undefOrState(dayForecast.getMaxWindDirection(), new StringType(dayForecast.getMaxWindDirection()));
        case "maxWindDirectionDegrees":
            return undefOrState(dayForecast.getMaxWindDirectionDegrees(), new QuantityType<>(dayForecast.getMaxWindDirectionDegrees(), SmartHomeUnits.DEGREE_ANGLE));
        case "maxWindSpeed":
            quantity = getSpeed(dayForecast.getMaxWindSpeedKmh(), dayForecast.getMaxWindSpeedMph());
            return undefOrQuantity(quantity);
        case "averageWindDirection":
            return undefOrState(dayForecast.getAverageWindDirection(), new StringType(dayForecast.getAverageWindDirection()));
        case "averageWindDirectionDegrees":
            return undefOrState(dayForecast.getAverageWindDirectionDegrees(), new QuantityType<>(dayForecast.getAverageWindDirectionDegrees(), SmartHomeUnits.DEGREE_ANGLE));
        case "averageWindSpeed":
            quantity = getSpeed(dayForecast.getAverageWindSpeedKmh(), dayForecast.getAverageWindSpeedMph());
            return undefOrQuantity(quantity);
        case "iconKey":
            return undefOrState(dayForecast.getIconKey(), new StringType(dayForecast.getIconKey()));
        case "icon":
            State icon = HttpUtil.downloadImage(dayForecast.getIcon().toExternalForm());
            if (icon == null) {
                logger.debug("Failed to download the content of URL {}", dayForecast.getIcon().toExternalForm());
                return null;
            }
            return icon;
        default:
            return null;
    }
}
Also used : DateTimeType(org.eclipse.smarthome.core.library.types.DateTimeType) StringType(org.eclipse.smarthome.core.library.types.StringType) State(org.eclipse.smarthome.core.types.State) WeatherUndergroundJsonForecastDay(org.eclipse.smarthome.binding.weatherunderground.internal.json.WeatherUndergroundJsonForecastDay)

Example 8 with StringType

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

the class ProxyServletService method uriFromRequest.

/**
 * Determine which URI to address based on the request contents.
 *
 * @param request the servlet request. New attributes may be added to the request in order to cache the result for
 *            future calls.
 * @return the URI indicated by the request, or <code>null</code> if not possible
 */
URI uriFromRequest(HttpServletRequest request) {
    try {
        // Return any URI we've already saved for this request
        URI uri = (URI) request.getAttribute(ATTR_URI);
        if (uri != null) {
            return uri;
        } else {
            ProxyServletException pse = (ProxyServletException) request.getAttribute(ATTR_SERVLET_EXCEPTION);
            if (pse != null) {
                // If we errored on this request before, there is no point continuing
                return null;
            }
        }
        String sitemapName = request.getParameter("sitemap");
        if (sitemapName == null) {
            throw new ProxyServletException(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'sitemap' must be provided!");
        }
        String widgetId = request.getParameter("widgetId");
        if (widgetId == null) {
            throw new ProxyServletException(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'widgetId' must be provided!");
        }
        Sitemap sitemap = (Sitemap) modelRepository.getModel(sitemapName);
        if (sitemap == null) {
            throw new ProxyServletException(HttpServletResponse.SC_NOT_FOUND, String.format("Sitemap '%s' could not be found!", sitemapName));
        }
        Widget widget = itemUIRegistry.getWidget(sitemap, widgetId);
        if (widget == null) {
            throw new ProxyServletException(HttpServletResponse.SC_NOT_FOUND, String.format("Widget '%s' could not be found!", widgetId));
        }
        String uriString = null;
        if (widget instanceof Image) {
            uriString = ((Image) widget).getUrl();
        } else if (widget instanceof Video) {
            uriString = ((Video) widget).getUrl();
        } else {
            throw new ProxyServletException(HttpServletResponse.SC_FORBIDDEN, String.format("Widget type '%s' is not supported!", widget.getClass().getName()));
        }
        String itemName = widget.getItem();
        if (itemName != null) {
            State state = itemUIRegistry.getItemState(itemName);
            if (state != null && state instanceof StringType) {
                try {
                    uri = URI.create(state.toString());
                    request.setAttribute(ATTR_URI, uri);
                    return uri;
                } catch (IllegalArgumentException ex) {
                // fall thru
                }
            }
        }
        try {
            uri = URI.create(uriString);
            request.setAttribute(ATTR_URI, uri);
            return uri;
        } catch (IllegalArgumentException iae) {
            throw new ProxyServletException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, String.format("URI '%s' is not a valid URI.", uriString));
        }
    } catch (ProxyServletException pse) {
        request.setAttribute(ATTR_SERVLET_EXCEPTION, pse);
        return null;
    }
}
Also used : Sitemap(org.eclipse.smarthome.model.sitemap.Sitemap) StringType(org.eclipse.smarthome.core.library.types.StringType) Video(org.eclipse.smarthome.model.sitemap.Video) State(org.eclipse.smarthome.core.types.State) Widget(org.eclipse.smarthome.model.sitemap.Widget) Image(org.eclipse.smarthome.model.sitemap.Image) URI(java.net.URI)

Example 9 with StringType

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

the class ItemUIRegistryImplTest method getLabel_transformationContainingPercentS.

@Test
public void getLabel_transformationContainingPercentS() throws ItemNotFoundException {
    // It doesn't matter that "FOO" doesn't exist - this is to assert it doesn't fail before because of the two "%s"
    String testLabel = "Memory [FOO(echo %s):%s]";
    Widget w = mock(Widget.class);
    Item item = mock(Item.class);
    when(w.getLabel()).thenReturn(testLabel);
    when(w.getItem()).thenReturn("Item");
    when(registry.getItem("Item")).thenReturn(item);
    when(item.getState()).thenReturn(new StringType("State"));
    String label = uiRegistry.getLabel(w);
    assertEquals("Memory [State]", label);
}
Also used : ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) Item(org.eclipse.smarthome.core.items.Item) StringType(org.eclipse.smarthome.core.library.types.StringType) Widget(org.eclipse.smarthome.model.sitemap.Widget) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 10 with StringType

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

the class ItemUIRegistryImplTest method getLabel_labelWithPatternInStateDescription.

@Test
public void getLabel_labelWithPatternInStateDescription() throws ItemNotFoundException {
    String testLabel = "Label";
    StateDescription stateDescription = mock(StateDescription.class);
    when(widget.getLabel()).thenReturn(testLabel);
    when(item.getStateDescription()).thenReturn(stateDescription);
    when(stateDescription.getPattern()).thenReturn("%s");
    when(item.getState()).thenReturn(new StringType("State"));
    String label = uiRegistry.getLabel(widget);
    assertEquals("Label [State]", label);
}
Also used : StringType(org.eclipse.smarthome.core.library.types.StringType) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StateDescription(org.eclipse.smarthome.core.types.StateDescription) Test(org.junit.Test)

Aggregations

StringType (org.eclipse.smarthome.core.library.types.StringType)43 State (org.eclipse.smarthome.core.types.State)13 Test (org.junit.Test)13 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)10 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)9 PercentType (org.eclipse.smarthome.core.library.types.PercentType)8 StateDescription (org.eclipse.smarthome.core.types.StateDescription)6 SonosEntry (org.eclipse.smarthome.binding.sonos.internal.SonosEntry)5 DateTimeType (org.eclipse.smarthome.core.library.types.DateTimeType)5 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)5 Date (java.util.Date)3 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)3 RefreshType (org.eclipse.smarthome.core.types.RefreshType)3 ArrayList (java.util.ArrayList)2 SensorEnum (org.eclipse.smarthome.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.SensorEnum)2 DeviceStateUpdateImpl (org.eclipse.smarthome.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.impl.DeviceStateUpdateImpl)2 Event (org.eclipse.smarthome.core.events.Event)2 Item (org.eclipse.smarthome.core.items.Item)2 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)2 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)2