Search in sources :

Example 6 with TransformationException

use of org.openhab.core.transform.TransformationException 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 TransformationException

use of org.openhab.core.transform.TransformationException 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 8 with TransformationException

use of org.openhab.core.transform.TransformationException 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

TransformationException (org.openhab.core.transform.TransformationException)8 TransformationService (org.openhab.core.transform.TransformationService)6 DecimalType (org.openhab.core.library.types.DecimalType)3 State (org.openhab.core.types.State)2 IOException (java.io.IOException)1 Properties (java.util.Properties)1 Matcher (java.util.regex.Matcher)1 HttpBindingProvider (org.openhab.binding.http.HttpBindingProvider)1 SnmpBindingProvider (org.openhab.binding.snmp.SnmpBindingProvider)1 NumberItem (org.openhab.core.library.items.NumberItem)1 StringItem (org.openhab.core.library.items.StringItem)1 SwitchItem (org.openhab.core.library.items.SwitchItem)1 StringType (org.openhab.core.library.types.StringType)1 OID (org.snmp4j.smi.OID)1 OctetString (org.snmp4j.smi.OctetString)1 Variable (org.snmp4j.smi.Variable)1