Skip to content

Commit

Permalink
Allow sending TimeSeries for items (openhab#3597)
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 authored Nov 8, 2023
1 parent fb6f192 commit cdbca4d
Show file tree
Hide file tree
Showing 47 changed files with 1,622 additions and 267 deletions.
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) {
eventPublisher1.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));
eventPublisher1.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
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ public String getName() {
return "restoreOnStartup";
};
};
public static final Strategy FORECAST = new StrategyImpl() {
@Override
public String getName() {
return "forecast";
};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class PersistenceGlobalScopeProvider extends AbstractGlobalScopeProvider
res.getContents().add(GlobalStrategies.UPDATE);
res.getContents().add(GlobalStrategies.CHANGE);
res.getContents().add(GlobalStrategies.RESTORE);
res.getContents().add(GlobalStrategies.FORECAST);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
*/
package org.openhab.core.model.script.actions;

import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
Expand All @@ -26,6 +28,7 @@
import org.openhab.core.model.script.ScriptServiceUtil;
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 @@ -146,7 +149,7 @@ public static Object postUpdate(Item item, String stateAsString) {
* Posts a status update for a specified item to the event bus.
*
* @param itemName the name of the item to send the status update for
* @param stateAsString the new state of the item
* @param stateString the new state of the item
*/
public static Object postUpdate(String itemName, String stateString) {
ItemRegistry registry = ScriptServiceUtil.getItemRegistry();
Expand All @@ -169,6 +172,43 @@ public static Object postUpdate(String itemName, String stateString) {
return null;
}

/**
* 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
*/
public static Object sendTimeSeries(@Nullable Item item, @Nullable TimeSeries timeSeries) {
EventPublisher eventPublisher1 = ScriptServiceUtil.getEventPublisher();
if (eventPublisher1 != null && item != null && timeSeries != null) {
eventPublisher1.post(ItemEventFactory.createTimeSeriesEvent(item.getName(), timeSeries, null));
}
return null;
}

/**
* 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>
*/
public static Object sendTimeSeries(@Nullable String itemName, @Nullable Map<ZonedDateTime, State> values,
String policy) {
EventPublisher eventPublisher1 = ScriptServiceUtil.getEventPublisher();
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));
eventPublisher1.post(ItemEventFactory.createTimeSeriesEvent(itemName, timeSeries, null));
} catch (IllegalArgumentException e) {
LoggerFactory.getLogger(BusEvent.class).warn("Policy '{}' does not exist.", policy);
}
}
return null;
}

private static <T extends State> List<String> getAcceptedDataTypeNames(Item item) {
return item.getAcceptedDataTypes().stream().map(Class::getSimpleName).toList();
}
Expand Down Expand Up @@ -232,6 +272,4 @@ public static Object restoreStates(Map<Item, State> statesMap) {
}
return null;
}

// static public JobKey timer(AbstractInstant instant, Object)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.time.ZonedDateTime;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.items.Item;
import org.openhab.core.types.State;

Expand Down Expand Up @@ -46,6 +47,25 @@ public interface ModifiablePersistenceService extends QueryablePersistenceServic
*/
void store(Item item, ZonedDateTime date, State state);

/**
* <p>
* Stores the historic item value under a specified alias. This allows the item, time and value to be specified.
*
* <p>
* Adding data with the same time as an existing record should update the current record value rather than adding a
* new record.
*
* <p>
* Implementors should keep in mind that all registered {@link PersistenceService}s are called synchronously. Hence
* long running operations should be processed asynchronously. E.g. <code>store</code> adds things to a queue which
* is processed by some asynchronous workers (Quartz Job, Thread, etc.).
*
* @param item the data to be stored
* @param date the date of the record
* @param state the state to be recorded
*/
void store(Item item, ZonedDateTime date, State state, @Nullable String alias);

/**
* Removes data associated with an item from a persistence service.
* If all data is removed for the specified item, the persistence service should free any resources associated with
Expand Down
Loading

0 comments on commit cdbca4d

Please sign in to comment.