Java Code Examples for org.eclipse.smarthome.core.types.State#toString()

The following examples show how to use org.eclipse.smarthome.core.types.State#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HistoryLastChangesSkill.java    From org.openhab.ui.habot with Eclipse Public License 1.0 6 votes vote down vote up
private String formatState(Item item, State state) {
    if (item.getStateDescription() != null) {
        StateDescription stateDescription = item.getStateDescription();
        if (stateDescription != null && stateDescription.getPattern() != null) {
            try {
                String transformedState = TransformationHelper.transform(
                        FrameworkUtil.getBundle(HistoryLastChangesSkill.class).getBundleContext(),
                        stateDescription.getPattern(), state.toString());
                if (transformedState != null && transformedState.equals(state.toString())) {
                    return state.format(stateDescription.getPattern());
                } else {
                    return transformedState;
                }
            } catch (NoClassDefFoundError | TransformationException ex) {
                // TransformationHelper is optional dependency, so ignore if class not found
                // return state as it is without transformation
                return state.toString();
            }
        } else {
            return state.toString();
        }
    } else {
        return state.toString();
    }
}
 
Example 2
Source File: CardBuilder.java    From org.openhab.ui.habot with Eclipse Public License 1.0 5 votes vote down vote up
private String formatState(Item item, State state) throws TransformationException {
    if (item.getStateDescription() != null) {
        try {
            StateDescription stateDescription = item.getStateDescription();
            if (stateDescription != null && stateDescription.getPattern() != null) {
                String transformedState = TransformationHelper.transform(
                        FrameworkUtil.getBundle(CardBuilder.class).getBundleContext(),
                        stateDescription.getPattern(), state.toString());
                if (transformedState == null) {
                    return state.toString();
                }
                if (transformedState.equals(state.toString())) {
                    return state.format(stateDescription.getPattern());
                } else {
                    return transformedState;
                }

            } else {
                return state.toString();
            }
        } catch (IllegalFormatConversionException e) {
            return state.toString();
        }
    } else {
        return state.toString();
    }
}
 
Example 3
Source File: VideoRenderer.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Video videoWidget = (Video) w;
    String snippet = null;

    String widgetId = itemUIRegistry.getWidgetId(w);
    String sitemap = w.eResource().getURI().path();

    if (videoWidget.getEncoding() != null && videoWidget.getEncoding().toLowerCase().contains("mjpeg")) {
        // we handle mjpeg streams as an html image as browser can usually handle this
        snippet = getSnippet("image");
        snippet = StringUtils.replace(snippet, "%setrefresh%", "");
        snippet = StringUtils.replace(snippet, "%refresh%", "");
    } else {
        snippet = getSnippet("video");
    }
    String url = "../proxy?sitemap=" + sitemap + "&widgetId=" + widgetId;
    String mediaType = "";
    if (videoWidget.getEncoding() != null && videoWidget.getEncoding().toLowerCase().contains("hls")) {
        // For HTTP Live Stream we don't proxy the URL and we set the appropriate media type
        State state = itemUIRegistry.getState(w);
        url = (state instanceof StringType) ? state.toString() : videoWidget.getUrl();
        mediaType = "type=\"application/vnd.apple.mpegurl\"";
    }
    snippet = StringUtils.replace(snippet, "%url%", url);
    snippet = StringUtils.replace(snippet, "%media_type%", mediaType);
    sb.append(snippet);
    return null;
}
 
Example 4
Source File: AbstractWidgetRenderer.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
protected String getState(Widget w) {
    State state = itemUIRegistry.getState(w);
    if (state != null) {
        return state.toString();
    } else {
        return "NULL";
    }
}
 
Example 5
Source File: DimmerItemTest.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
private static BigDecimal getState(final Item item, Class<? extends State> typeClass) {
    final State state = item.getStateAs(typeClass);
    final String str = state.toString();
    final BigDecimal result = new BigDecimal(str);
    return result;
}
 
Example 6
Source File: SetpointRenderer.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Setpoint sp = (Setpoint) w;

    State state = itemUIRegistry.getState(w);
    String newLowerState = state.toString();
    String newHigherState = state.toString();

    // set defaults for min, max and step
    BigDecimal step = sp.getStep();
    if (step == null) {
        step = BigDecimal.ONE;
    }
    BigDecimal minValue = sp.getMinValue();
    if (minValue == null) {
        minValue = BigDecimal.ZERO;
    }
    BigDecimal maxValue = sp.getMaxValue();
    if (maxValue == null) {
        maxValue = BigDecimal.valueOf(100);
    }

    // if the current state is a valid value, we calculate the up and down step values
    if (state instanceof DecimalType || state instanceof QuantityType) {
        BigDecimal currentState;
        if (state instanceof DecimalType) {
            currentState = ((DecimalType) state).toBigDecimal();
        } else {
            currentState = ((QuantityType<?>) state).toBigDecimal();
        }
        BigDecimal newLower = currentState.subtract(step);
        BigDecimal newHigher = currentState.add(step);
        if (newLower.compareTo(minValue) < 0) {
            newLower = minValue;
        }
        if (newHigher.compareTo(maxValue) > 0) {
            newHigher = maxValue;
        }
        newLowerState = newLower.toString();
        newHigherState = newHigher.toString();

        if (state instanceof QuantityType) {
            newLowerState = newLowerState + " " + ((QuantityType<?>) state).getUnit().toString();
            newHigherState = newHigherState + " " + ((QuantityType<?>) state).getUnit().toString();
        }
    }

    String snippetName = "setpoint";
    String snippet = getSnippet(snippetName);

    snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(w));
    snippet = StringUtils.replace(snippet, "%category%", getCategory(w));
    snippet = StringUtils.replace(snippet, "%item%", w.getItem());
    snippet = StringUtils.replace(snippet, "%state%", getState(w));
    snippet = StringUtils.replace(snippet, "%newlowerstate%", newLowerState);
    snippet = StringUtils.replace(snippet, "%newhigherstate%", newHigherState);
    snippet = StringUtils.replace(snippet, "%label%", getLabel(w));
    snippet = StringUtils.replace(snippet, "%state%", getState(w));
    snippet = StringUtils.replace(snippet, "%format%", getFormat());
    snippet = StringUtils.replace(snippet, "%servletname%", WebAppServlet.SERVLET_NAME);
    snippet = StringUtils.replace(snippet, "%minValue%", minValue.toString());
    snippet = StringUtils.replace(snippet, "%maxValue%", maxValue.toString());
    snippet = StringUtils.replace(snippet, "%step%", step.toString());

    // Process the color tags
    snippet = processColor(w, snippet);

    sb.append(snippet);
    return null;
}
 
Example 7
Source File: SetpointRenderer.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Setpoint sp = (Setpoint) w;

    State state = itemUIRegistry.getState(w);
    String newLowerState = state.toString();
    String newHigherState = state.toString();

    // set defaults for min, max and step
    BigDecimal step = sp.getStep();
    if (step == null) {
        step = BigDecimal.ONE;
    }
    BigDecimal minValue = sp.getMinValue();
    if (minValue == null) {
        minValue = BigDecimal.ZERO;
    }
    BigDecimal maxValue = sp.getMaxValue();
    if (maxValue == null) {
        maxValue = BigDecimal.valueOf(100);
    }

    // if the current state is a valid value, we calculate the up and down step values
    if (state instanceof DecimalType) {
        DecimalType actState = (DecimalType) state;
        BigDecimal newLower = actState.toBigDecimal().subtract(step);
        BigDecimal newHigher = actState.toBigDecimal().add(step);
        if (newLower.compareTo(minValue) < 0) {
            newLower = minValue;
        }
        if (newHigher.compareTo(maxValue) > 0) {
            newHigher = maxValue;
        }
        newLowerState = newLower.toString();
        newHigherState = newHigher.toString();
    }

    String unit = getUnitForWidget(w);

    String snippetName = "setpoint";
    String snippet = getSnippet(snippetName);

    snippet = preprocessSnippet(snippet, w);
    snippet = StringUtils.replace(snippet, "%newlowerstate%", newLowerState);
    snippet = StringUtils.replace(snippet, "%newhigherstate%", newHigherState);
    snippet = StringUtils.replace(snippet, "%value%", getValue(w));
    snippet = StringUtils.replace(snippet, "%minValue%", minValue.toString());
    snippet = StringUtils.replace(snippet, "%maxValue%", maxValue.toString());
    snippet = StringUtils.replace(snippet, "%step%", step.toString());
    snippet = StringUtils.replace(snippet, "%unit%", unit);

    // Process the color tags
    snippet = processColor(w, snippet);

    sb.append(snippet);
    return null;
}
 
Example 8
Source File: VideoRenderer.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Video videoWidget = (Video) w;
    String snippet = null;

    String widgetId = itemUIRegistry.getWidgetId(w);
    String sitemap = w.eResource().getURI().path();

    // we handle mjpeg streams as an html image as browser can usually handle this
    String snippetName = (videoWidget.getEncoding() != null
            && videoWidget.getEncoding().toLowerCase().contains("mjpeg")) ? "image" : "video";

    snippet = getSnippet(snippetName);
    snippet = preprocessSnippet(snippet, w);

    State state = itemUIRegistry.getState(w);
    String url;
    if (snippetName.equals("image")) {
        boolean validUrl = isValidURL(videoWidget.getUrl());
        String proxiedUrl = "../proxy?sitemap=" + sitemap + "&amp;widgetId=" + widgetId;
        if (!itemUIRegistry.getVisiblity(w)) {
            url = URL_NONE_ICON;
        } else if ((sitemap != null) && ((state instanceof StringType) || validUrl)) {
            url = proxiedUrl + "&amp;t=" + (new Date()).getTime();
        } else {
            url = URL_NONE_ICON;
        }
        snippet = StringUtils.replace(snippet, "%valid_url%", validUrl ? "true" : "false");
        snippet = StringUtils.replace(snippet, "%proxied_url%", proxiedUrl);
        snippet = StringUtils.replace(snippet, "%update_interval%", "0");
        snippet = StringUtils.replace(snippet, "%ignore_refresh%", "true");
        snippet = StringUtils.replace(snippet, "%url%", url);
    } else {
        String mediaType;
        if (videoWidget.getEncoding() != null && videoWidget.getEncoding().toLowerCase().contains("hls")) {
            // For HTTP Live Stream we don't proxy the URL and we set the appropriate media type
            url = (state instanceof StringType) ? state.toString() : videoWidget.getUrl();
            mediaType = "type=\"application/vnd.apple.mpegurl\"";
        } else {
            url = "../proxy?sitemap=" + sitemap + "&widgetId=" + widgetId;
            mediaType = "";
        }
        snippet = StringUtils.replace(snippet, "%url%", url);
        snippet = StringUtils.replace(snippet, "%media_type%", mediaType);
    }

    sb.append(snippet);
    return null;
}
 
Example 9
Source File: ItemHistoryDTO.java    From smarthome with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Add a new record to the data history.
 * This method returns a double value equal to the state. This may be used for comparison by the caller.
 *
 * @param time the time of the record
 * @param state the state at this time
 */
public void addData(Long time, State state) {
    HistoryDataBean newVal = new HistoryDataBean();
    newVal.time = time;
    newVal.state = state.toString();
    data.add(newVal);
}