]> git.basschouten.com Git - openhab-addons.git/commitdiff
add timeseries support (#17334)
authorHans Böhm <boehan@users.noreply.github.com>
Mon, 26 Aug 2024 20:49:29 +0000 (22:49 +0200)
committerGitHub <noreply@github.com>
Mon, 26 Aug 2024 20:49:29 +0000 (22:49 +0200)
Signed-off-by: Hans Böhm <h.boehm@gmx.at>
bundles/org.openhab.transform.map/src/main/java/org/openhab/transform/map/internal/profiles/MapTransformationProfile.java

index 31af32e6094d80b71fb166b046a90033bb5f38c4..872e536585427ba52521d722f34ad498c7bf6a57 100644 (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 {