Search in sources :

Example 6 with TransformationService

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

the class OpenEnergyMonitorBinding method transformData.

/**
     * Transform received data by Transformation service.
     *
     */
protected org.openhab.core.types.State transformData(String transformationType, String transformationFunction, org.openhab.core.types.State data) {
    if (transformationType != null && transformationFunction != null) {
        String transformedResponse = null;
        try {
            TransformationService transformationService = TransformationHelper.getTransformationService(OpenEnergyMonitorActivator.getContext(), transformationType);
            if (transformationService != null) {
                transformedResponse = transformationService.transform(transformationFunction, String.valueOf(data));
            } else {
                logger.warn("couldn't transform response because transformationService of type '{}' is unavailable", transformationType);
            }
        } catch (TransformationException te) {
            logger.error("transformation throws exception [transformation type=" + transformationType + ", transformation function=" + transformationFunction + ", response=" + data + "]", te);
        }
        logger.debug("transformed response is '{}'", transformedResponse);
        if (transformedResponse != null) {
            return new DecimalType(transformedResponse);
        }
    }
    return data;
}
Also used : TransformationException(org.openhab.core.transform.TransformationException) DecimalType(org.openhab.core.library.types.DecimalType) TransformationService(org.openhab.core.transform.TransformationService)

Example 7 with TransformationService

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

the class TCPBinding method transformResponse.

protected String transformResponse(String transformation, String response) {
    String transformedResponse;
    if (isEmpty(transformation) || transformation.equalsIgnoreCase("default")) {
        transformedResponse = response;
    } else {
        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.error("transformation throws 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 8 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)

Example 9 with TransformationService

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

the class DeviceBindingConfig method transformCommand.

@Override
public String transformCommand(Command command) throws TransformationException {
    // Quickly return null if we don't support commands.
    if (getCommandTransform() == null) {
        return null;
    }
    TransformationService ts = getCommandTransformationService();
    String result;
    String key = command.toString();
    if (ts != null) {
        result = ts.transform(getCommandTransformParam(), key);
        // "_default".
        if (result == null || "".equals(result) || key.equals(result)) {
            result = ts.transform(getCommandTransformParam(), DEFAULT_COMMAND_TRANSFORM);
        }
    } else {
        Map<String, String> map = getCommandMap();
        if (map != null) {
            String value = map.get(key);
            if (value != null) {
                result = value;
            } else {
                // Attempt to provide a default Mapping for it.
                if (map.containsKey(key)) {
                    result = COMMAND_DEFAULTS.get(key);
                } else {
                    result = key;
                }
            }
        } else {
            result = COMMAND_DEFAULTS.get(key);
        }
    }
    return result;
}
Also used : TransformationService(org.openhab.core.transform.TransformationService)

Example 10 with TransformationService

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

the class ExecBinding method transformResponse.

protected String transformResponse(String response, String transformation) {
    String transformedResponse;
    try {
        String[] parts = splitTransformationConfig(transformation);
        String transformationType = parts[0];
        String transformationFunction = parts[1];
        TransformationService transformationService = TransformationHelper.getTransformationService(ExecActivator.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=" + transformation + ", response=" + response + "]", 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)

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