Search in sources :

Example 6 with State

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);
    }
}
Also used : Item(org.eclipse.smarthome.core.items.Item) State(org.eclipse.smarthome.core.types.State) ItemNotUniqueException(org.eclipse.smarthome.core.items.ItemNotUniqueException) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 7 with State

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;
    }
}
Also used : OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) State(org.eclipse.smarthome.core.types.State) PercentType(org.eclipse.smarthome.core.library.types.PercentType)

Example 8 with State

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>&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 9 with State

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;
    }
}
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 10 with State

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;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) CallItem(org.eclipse.smarthome.core.library.items.CallItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) DateTimeItem(org.eclipse.smarthome.core.library.items.DateTimeItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) LocationItem(org.eclipse.smarthome.core.library.items.LocationItem) ContactItem(org.eclipse.smarthome.core.library.items.ContactItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) PlayerItem(org.eclipse.smarthome.core.library.items.PlayerItem) ImageItem(org.eclipse.smarthome.core.library.items.ImageItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) State(org.eclipse.smarthome.core.types.State) ColorArray(org.eclipse.smarthome.model.sitemap.ColorArray) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Aggregations

State (org.eclipse.smarthome.core.types.State)130 Test (org.junit.Test)59 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)23 PercentType (org.eclipse.smarthome.core.library.types.PercentType)22 Item (org.eclipse.smarthome.core.items.Item)21 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)19 Temperature (javax.measure.quantity.Temperature)18 StringType (org.eclipse.smarthome.core.library.types.StringType)18 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)17 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)15 HSBType (org.eclipse.smarthome.core.library.types.HSBType)15 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)15 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)13 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)13 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)12 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)11 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)10 RawType (org.eclipse.smarthome.core.library.types.RawType)10 Pressure (javax.measure.quantity.Pressure)9 GroupItem (org.eclipse.smarthome.core.items.GroupItem)8