diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/defaultscope/ScriptBusEvent.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/defaultscope/ScriptBusEvent.java index bb1bbcd43a9..61bf878e5c3 100644 --- a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/defaultscope/ScriptBusEvent.java +++ b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/defaultscope/ScriptBusEvent.java @@ -12,6 +12,7 @@ */ package org.openhab.core.automation.module.script.defaultscope; +import java.time.ZonedDateTime; import java.util.Map; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -19,6 +20,7 @@ import org.openhab.core.items.Item; import org.openhab.core.types.Command; import org.openhab.core.types.State; +import org.openhab.core.types.TimeSeries; /** * The static methods of this class are made available as functions in the scripts. @@ -98,7 +100,6 @@ public interface ScriptBusEvent { /** * Posts a status update for a specified item to the event bus. - * t * * @param item the item to send the status update for * @param state the new state of the item @@ -106,6 +107,26 @@ public interface ScriptBusEvent { @Nullable Object postUpdate(@Nullable Item item, @Nullable State state); + /** + * Sends a time series to the event bus + * + * @param item the item to send the time series for + * @param timeSeries a {@link TimeSeries} containing policy and values + */ + @Nullable + Object sendTimeSeries(@Nullable Item item, @Nullable TimeSeries timeSeries); + + /** + * Sends a time series to the event bus + * + * @param itemName the name of the item to send the status update for + * @param values a {@link Map} containing the timeseries, composed of pairs of {@link ZonedDateTime} and + * {@link State} + * @param policy either ADD or REPLACE + */ + @Nullable + Object sendTimeSeries(@Nullable String itemName, @Nullable Map values, String policy); + /** * Stores the current states for a list of items in a map. * A group item is not itself put into the map, but instead all its members. diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/defaultscope/ScriptBusEventImpl.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/defaultscope/ScriptBusEventImpl.java index 72490aff17b..8ba2314b79c 100644 --- a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/defaultscope/ScriptBusEventImpl.java +++ b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/defaultscope/ScriptBusEventImpl.java @@ -12,6 +12,7 @@ */ package org.openhab.core.automation.module.script.internal.defaultscope; +import java.time.ZonedDateTime; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; @@ -27,6 +28,7 @@ import org.openhab.core.items.events.ItemEventFactory; import org.openhab.core.types.Command; import org.openhab.core.types.State; +import org.openhab.core.types.TimeSeries; import org.openhab.core.types.TypeParser; import org.slf4j.LoggerFactory; @@ -153,6 +155,31 @@ public void dispose() { return null; } + @Override + public @Nullable Object sendTimeSeries(@Nullable Item item, @Nullable TimeSeries timeSeries) { + EventPublisher eventPublisher1 = this.eventPublisher; + if (eventPublisher1 != null && item != null && timeSeries != null) { + eventPublisher.post(ItemEventFactory.createTimeSeriesEvent(item.getName(), timeSeries, null)); + } + return null; + } + + @Override + public @Nullable Object sendTimeSeries(@Nullable String itemName, @Nullable Map values, + @Nullable String policy) { + EventPublisher eventPublisher1 = this.eventPublisher; + if (eventPublisher1 != null && itemName != null && values != null && policy != null) { + try { + TimeSeries timeSeries = new TimeSeries(TimeSeries.Policy.valueOf(policy)); + values.forEach((key, value) -> timeSeries.add(key.toInstant(), value)); + eventPublisher.post(ItemEventFactory.createTimeSeriesEvent(itemName, timeSeries, null)); + } catch (IllegalArgumentException e) { + LoggerFactory.getLogger(ScriptBusEventImpl.class).warn("Policy '{}' does not exist.", policy); + } + } + return null; + } + @Override public Map storeStates(Item @Nullable... items) { Map statesMap = new HashMap<>();