Search in sources :

Example 1 with TransformationService

use of org.openhab.core.transform.TransformationService in project openhab1-addons by openhab.

the class SmarthomaticBinding method processTransformation.

private String processTransformation(String transformation, String response) {
    String transformedResponse = response;
    if (transformation == null) {
        return transformedResponse;
    }
    try {
        String[] parts = splitTransformationConfig(transformation);
        String transformationType = parts[0];
        String transformationFunction = parts[1];
        TransformationService transformationService = TransformationHelper.getTransformationService(SmarthomaticActivator.getContext(), transformationType);
        if (transformationService != null) {
            transformedResponse = transformationService.transform(transformationFunction, response);
        } else {
            transformedResponse = response;
            logger.warn("couldn't transform response because transformationService of type '{}' is unavailable", transformationType);
        }
    } catch (TransformationException te) {
        logger.error("transformation throws exception [transformation= {}, response= {}]", transformation, response);
        logger.error("received transformation exception", te);
        // in case of an error we return the response without any
        // transformation
        transformedResponse = response;
    }
    logger.debug("transformed response is '{}'", transformedResponse);
    return transformedResponse;
}
Also used : TransformationException(org.openhab.core.transform.TransformationException) TransformationService(org.openhab.core.transform.TransformationService)

Example 2 with TransformationService

use of org.openhab.core.transform.TransformationService in project openhab1-addons by openhab.

the class LcnGenericBindingProvider method resolveMappings.

/**
     * Resolves LCN commands (with mappings) to plain commands.
     * 
     * @param lcnTarget the target or a mapping
     * @param openHABcmd the command send by openHAB
     * @return the resolved result (can be null)
     */
private static String resolveMappings(String lcnTarget, String openHABcmd) {
    String result = null;
    Matcher matcher = PATTERN_MAPPING.matcher(lcnTarget);
    if (!matcher.matches()) {
        result = lcnTarget;
    } else {
        matcher.reset();
        matcher.find();
        String s1 = matcher.group(1);
        String s2 = matcher.group(2);
        TransformationService transformationService = TransformationHelper.getTransformationService(LcnBindingActivator.getContext(), s1);
        if (transformationService != null) {
            try {
                result = transformationService.transform(s2, openHABcmd);
            } catch (TransformationException e) {
                result = lcnTarget;
            }
        } else {
            result = lcnTarget;
        }
    }
    return result;
}
Also used : TransformationException(org.openhab.core.transform.TransformationException) Matcher(java.util.regex.Matcher) TransformationService(org.openhab.core.transform.TransformationService)

Example 3 with TransformationService

use of org.openhab.core.transform.TransformationService in project openhab1-addons by openhab.

the class MiosBindingConfig method transformIn.

/**
     * Transform data from a MiOS Unit into a form suitable for use in an openHAB Item.
     * <p>
     * 
     * Data received from a MiOS unit is in a number of different formats (String, Boolean, DataTime, etc). These values
     * may need to be transformed from their original format prior to being pushed into the corresponding openHAB Item.
     * <p>
     * This method is used internally within the MiOS Binding to perform that transformation.
     * <p>
     * This method is responsible for transforming the inbound value from the MiOS Unit, into the form required by the
     * openHAB Item.
     * <p>
     * metadata supplied by the user, via the <code>in:</code> parameter, in the Binding Configuration is used to define
     * the transformation that must be performed.
     * <p>
     * If the <code>in:</code> parameter is missing, then no transformation will occur, and the source-value will be
     * returned (as a <code>StringType</code>).
     * <p>
     * If the <code>in:</code> parameter is present, then its value is used to determine which openHAB
     * TransformationService should be used to transform the value.
     * 
     * @return the transformed value, or the input (<code>value</code>) if no transformation has been specified in the
     *         Binding Configuration.
     * 
     * @throws TransformationException
     *             if the underlying Transformation fails in any manner.
     */
public State transformIn(State value) throws TransformationException {
    TransformationService ts = getInTransformationService();
    if (ts != null) {
        return createState(ts.transform(getInTransformParam(), value.toString()));
    } else {
        if (value instanceof StringType) {
            value = createState(value.toString());
            logger.trace("transformIn: Converted value '{}' from StringType to more scoped type '{}'", value, value.getClass());
        }
        return value;
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) TransformationService(org.openhab.core.transform.TransformationService)

Example 4 with TransformationService

use of org.openhab.core.transform.TransformationService in project openhab1-addons by openhab.

the class UDPBinding method transformResponse.

protected String transformResponse(String transformation, String response) {
    String transformedResponse;
    if (isEmpty(transformation) || transformation.equalsIgnoreCase("default")) {
        logger.debug("transformed response is '{}'", response);
        return response;
    }
    Matcher matcher = EXTRACT_FUNCTION_PATTERN.matcher(transformation);
    if (matcher.matches()) {
        matcher.reset();
        matcher.find();
        String transformationServiceName = matcher.group(1);
        String transformationServiceParam = matcher.group(2);
        try {
            TransformationService transformationService = TransformationHelper.getTransformationService(TCPActivator.getContext(), transformationServiceName);
            if (transformationService != null) {
                transformedResponse = transformationService.transform(transformationServiceParam, response);
            } else {
                transformedResponse = response;
                logger.warn("couldn't transform response because transformationService of type '{}' is unavailable", transformationServiceName);
            }
        } catch (Exception te) {
            logger.warn("Transformation threw an exception. [transformation={}, response={}]", transformation, response, te);
            // in case of an error we return the response without any
            // transformation
            transformedResponse = response;
        }
    } else {
        transformedResponse = transformation;
    }
    logger.debug("transformed response is '{}'", transformedResponse);
    return transformedResponse;
}
Also used : Matcher(java.util.regex.Matcher) TransformationService(org.openhab.core.transform.TransformationService) ConfigurationException(org.osgi.service.cm.ConfigurationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with TransformationService

use of org.openhab.core.transform.TransformationService in project openhab1-addons by openhab.

the class FatekNumberItem method getStateWithTransformation.

private State getStateWithTransformation(BigDecimal val) {
    TransformationService transformationService = TransformationHelper.getTransformationService(FatekPLCActivator.getContext(), transType);
    String strVal = String.valueOf(val);
    String transOut;
    if (transformationService != null) {
        try {
            transOut = transformationService.transform(transFunc, strVal);
            logger.debug("transOut={}", transOut);
        } catch (TransformationException e) {
            transOut = null;
            logger.warn("Transformation error: {}", e.getMessage());
        }
    } else {
        transOut = null;
        logger.warn("No transformation service for: {}", transType);
    }
    if (transOut != null && !"null".equals(transOut)) {
        strVal = transOut;
    }
    return new DecimalType(strVal);
}
Also used : TransformationException(org.openhab.core.transform.TransformationException) DecimalType(org.openhab.core.library.types.DecimalType) TransformationService(org.openhab.core.transform.TransformationService)

Aggregations

TransformationService (org.openhab.core.transform.TransformationService)10 TransformationException (org.openhab.core.transform.TransformationException)6 Matcher (java.util.regex.Matcher)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 DecimalType (org.openhab.core.library.types.DecimalType)2 ConfigurationException (org.osgi.service.cm.ConfigurationException)2 Properties (java.util.Properties)1 HttpBindingProvider (org.openhab.binding.http.HttpBindingProvider)1 StringType (org.openhab.core.library.types.StringType)1 State (org.openhab.core.types.State)1