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;
}
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;
}
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);
}
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;
}
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;
}
Aggregations