Skip to content

Commit

Permalink
scriptbusevent
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <[email protected]>
  • Loading branch information
J-N-K committed May 29, 2023
1 parent de52a25 commit ee25a2f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
*/
package org.openhab.core.automation.module.script.defaultscope;

import java.time.ZonedDateTime;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
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.
Expand Down Expand Up @@ -98,14 +100,33 @@ 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
*/
@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 <code>ADD</code> or <code>REPLACE</code>
*/
@Nullable
Object sendTimeSeries(@Nullable String itemName, @Nullable Map<ZonedDateTime, State> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<ZonedDateTime, State> 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<Item, State> storeStates(Item @Nullable... items) {
Map<Item, State> statesMap = new HashMap<>();
Expand Down

0 comments on commit ee25a2f

Please sign in to comment.