use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.
the class UpdateConsoleCommandExtension method execute.
@Override
public void execute(String[] args, Console console) {
if (args.length > 0) {
String itemName = args[0];
try {
Item item = this.itemRegistry.getItemByPattern(itemName);
if (args.length > 1) {
String stateName = args[1];
State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateName);
if (state != null) {
eventPublisher.post(ItemEventFactory.createStateEvent(item.getName(), state));
console.println("Update has been sent successfully.");
} else {
console.println("Error: State '" + stateName + "' is not valid for item '" + itemName + "'");
console.print("Valid data types are: ( ");
for (Class<? extends State> acceptedType : item.getAcceptedDataTypes()) {
console.print(acceptedType.getSimpleName() + " ");
}
console.println(")");
}
} else {
printUsage(console);
}
} catch (ItemNotFoundException e) {
console.println("Error: Item '" + itemName + "' does not exist.");
} catch (ItemNotUniqueException e) {
console.print("Error: Multiple items match this pattern: ");
for (Item item : e.getMatchingItems()) {
console.print(item.getName() + " ");
}
}
} else {
printUsage(console);
}
}
use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.
the class WemoLightHandler method onValueReceived.
@Override
public void onValueReceived(String variable, String value, String service) {
logger.trace("Received pair '{}':'{}' (service '{}') for thing '{}'", new Object[] { variable, value, service, this.getThing().getUID() });
String capabilityId = StringUtils.substringBetween(value, "<CapabilityId>", "</CapabilityId>");
String newValue = StringUtils.substringBetween(value, "<Value>", "</Value>");
switch(capabilityId) {
case "10006":
OnOffType binaryState = null;
binaryState = newValue.equals("0") ? OnOffType.OFF : OnOffType.ON;
if (binaryState != null) {
updateState(CHANNEL_STATE, binaryState);
}
break;
case "10008":
String[] splitValue = newValue.split(":");
if (splitValue[0] != null) {
int newBrightnessValue = Integer.valueOf(splitValue[0]);
int newBrightness = Math.round(newBrightnessValue * 100 / 255);
State newBrightnessState = new PercentType(newBrightness);
updateState(CHANNEL_BRIGHTNESS, newBrightnessState);
currentBrightness = newBrightness;
}
break;
}
}
use of org.eclipse.smarthome.core.types.State 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.types.State 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.types.State in project smarthome by eclipse.
the class ItemUIRegistryImpl method processColorDefinition.
private String processColorDefinition(State state, List<ColorArray> colorList) {
// Sanity check
if (colorList == null) {
return null;
}
if (colorList.size() == 0) {
return null;
}
String colorString = null;
// static colour
if (colorList.size() == 1 && colorList.get(0).getState() == null) {
colorString = colorList.get(0).getArg();
} else {
// with the supplied value
for (ColorArray color : colorList) {
// Use a local state variable in case it gets overridden below
State cmpState = state;
if (color.getState() == null) {
logger.error("Error parsing color");
continue;
}
// If there's an item defined here, get its state
String itemName = color.getItem();
if (itemName != null) {
// Try and find the item to test.
// If it's not found, return visible
Item item;
try {
item = itemRegistry.getItem(itemName);
// Get the item state
cmpState = item.getState();
} catch (ItemNotFoundException e) {
logger.warn("Cannot retrieve color item {} for widget", color.getItem());
}
}
// Handle the sign
String value;
if (color.getSign() != null) {
value = color.getSign() + color.getState();
} else {
value = color.getState();
}
if (matchStateToValue(cmpState, value, color.getCondition()) == true) {
// We have the color for this value - break!
colorString = color.getArg();
break;
}
}
}
// Remove quotes off the colour - if they exist
if (colorString == null) {
return null;
}
if (colorString.startsWith("\"") && colorString.endsWith("\"")) {
colorString = colorString.substring(1, colorString.length() - 1);
}
return colorString;
}
Aggregations