Skip to content

Commit

Permalink
add timeseries support (#17334)
Browse files Browse the repository at this point in the history
Signed-off-by: Hans Böhm <h.boehm@gmx.at>
  • Loading branch information
boehan authored Aug 26, 2024
1 parent f5eb2c6 commit 376b696
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
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;
Expand All @@ -33,7 +34,7 @@
* @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");
Expand Down Expand Up @@ -113,6 +114,21 @@ public void onStateUpdateFromHandler(State state) {
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 {
Expand Down

0 comments on commit 376b696

Please sign in to comment.