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><attribute><name>Brewed</name><value>NULL</value></attribute>" + "<attribute><name>LastCleaned</name><value>NULL</value></attribute><attribute>" + "<name>ModeTime</name><value>NULL</value></attribute><attribute><name>Brewing</name>" + "<value>NULL</value></attribute><attribute><name>TimeRemaining</name><value>NULL</value>" + "</attribute><attribute><name>WaterLevelReached</name><value>NULL</value></attribute><" + "attribute><name>Mode</name><value>4</value></attribute><attribute><name>CleanAdvise</name>" + "<value>NULL</value></attribute><attribute><name>FilterAdvise</name><value>NULL</value></attribute>" + "<attribute><name>Cleaning</name><value>NULL</value></attribute></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);
}
}
}
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;
}
}
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;
}
}
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);
}
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);
}
Aggregations