add timeseries support (#17334)

Signed-off-by: Hans Böhm <h.boehm@gmx.at>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Hans Böhm 2024-08-26 22:49:29 +02:00 committed by Ciprian Pascu
parent a0292b326a
commit 8589dbdc67

View File

@ -17,12 +17,13 @@ import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.profiles.ProfileCallback;
import org.openhab.core.thing.profiles.ProfileContext;
import org.openhab.core.thing.profiles.ProfileTypeUID;
import org.openhab.core.thing.profiles.StateProfile;
import org.openhab.core.thing.profiles.TimeSeriesProfile;
import org.openhab.core.transform.TransformationException;
import org.openhab.core.transform.TransformationHelper;
import org.openhab.core.transform.TransformationService;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.openhab.core.types.TimeSeries;
import org.openhab.core.types.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -33,7 +34,7 @@ import org.slf4j.LoggerFactory;
* @author Stefan Triller - Initial contribution
*/
@NonNullByDefault
public class MapTransformationProfile implements StateProfile {
public class MapTransformationProfile implements TimeSeriesProfile {
public static final ProfileTypeUID PROFILE_TYPE_UID = new ProfileTypeUID(
TransformationService.TRANSFORM_PROFILE_SCOPE, "MAP");
@ -113,6 +114,21 @@ public class MapTransformationProfile implements StateProfile {
callback.sendUpdate((State) transformState(state));
}
@Override
public void onTimeSeriesFromHandler(TimeSeries timeSeries) {
if (function == null || sourceFormat == null) {
logger.warn(
"Please specify a function and a source format for this Profile in the '{}' and '{}' parameters, e.g \"translation.map\" and \"%s\". Returning the original state now.",
FUNCTION_PARAM, SOURCE_FORMAT_PARAM);
callback.sendTimeSeries(timeSeries);
return;
}
TimeSeries transformedTimeSeries = new TimeSeries(timeSeries.getPolicy());
timeSeries.getStates()
.forEach(entry -> transformedTimeSeries.add(entry.timestamp(), (State) transformState(entry.state())));
callback.sendTimeSeries(transformedTimeSeries);
}
private Type transformState(Type state) {
String result = state.toFullString();
try {