From a5f58f9345e7543fee952d08cd47e10dbcef9eda Mon Sep 17 00:00:00 2001 From: querdenker2k Date: Sat, 30 Sep 2023 10:31:21 +0200 Subject: [PATCH 1/5] add persist --- src/main/history/dependencies.xml | 24 +++++++-------- .../jrule/internal/JRuleFactory.java | 5 +++- .../jrule/internal/engine/JRuleEngine.java | 10 +++++++ .../internal/items/JRuleInternalItem.java | 5 ++++ .../items/JRulePersistenceExtensions.java | 19 +++++++++++- .../automation/jrule/items/JRuleItem.java | 6 ++++ .../integration_test/ITJRulePersistence.java | 17 +++++++++++ .../rules/integration_test/JRuleITBase.java | 2 +- .../jrule/rules/user/TestPersistence.java | 29 ++++++++++++++++++- .../resources/docker/conf/items/default.items | 2 ++ 10 files changed, 103 insertions(+), 16 deletions(-) diff --git a/src/main/history/dependencies.xml b/src/main/history/dependencies.xml index 63b131f5..a4b3ea6c 100644 --- a/src/main/history/dependencies.xml +++ b/src/main/history/dependencies.xml @@ -1,13 +1,13 @@ - - - openhab-runtime-base - wrap - mvn:javax.el/javax.el-api/2.2.4 - mvn:org.freemarker/freemarker/2.3.32 - mvn:org.openhab.addons.bundles/org.openhab.automation.jrule/4.0.0-SNAPSHOT - wrap:mvn:javax.servlet/jsp-api/2.0 - wrap:mvn:javax.servlet/servlet-api/2.4 - wrap:mvn:org.lastnpe.eea/eea-all/2.2.1 - - + + + openhab-runtime-base + wrap + mvn:javax.el/javax.el-api/2.2.4 + mvn:org.freemarker/freemarker/2.3.32 + mvn:org.openhab.addons.bundles/org.openhab.automation.jrule/4.0.0-SNAPSHOT + wrap:mvn:javax.servlet/jsp-api/2.0 + wrap:mvn:javax.servlet/servlet-api/2.4 + wrap:mvn:org.lastnpe.eea/eea-all/2.2.1 + + diff --git a/src/main/java/org/openhab/automation/jrule/internal/JRuleFactory.java b/src/main/java/org/openhab/automation/jrule/internal/JRuleFactory.java index bc178e33..72336e5e 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/JRuleFactory.java +++ b/src/main/java/org/openhab/automation/jrule/internal/JRuleFactory.java @@ -25,6 +25,7 @@ import org.openhab.core.events.EventPublisher; import org.openhab.core.items.ItemRegistry; import org.openhab.core.items.MetadataRegistry; +import org.openhab.core.persistence.PersistenceServiceRegistry; import org.openhab.core.scheduler.CronScheduler; import org.openhab.core.thing.ThingManager; import org.openhab.core.thing.ThingRegistry; @@ -63,10 +64,12 @@ public JRuleFactory(Map properties, final @Reference JRuleEventS final @Reference ThingManager thingManager, final @Reference EventPublisher eventPublisher, final @Reference VoiceManager voiceManager, final ComponentContext componentContext, final @Reference CronScheduler cronScheduler, final @Reference MetadataRegistry metadataRegistry, - @Reference JRuleRuleProvider ruleProvider) { + final @Reference JRuleRuleProvider ruleProvider, + @Reference final PersistenceServiceRegistry persistenceServiceRegistry) { JRuleConfig config = new JRuleConfig(properties); config.initConfig(); jRuleEngine = JRuleEngine.get(); + jRuleEngine.setPersistenceServiceRegistry(persistenceServiceRegistry); jRuleEngine.setConfig(config); jRuleEngine.setItemRegistry(itemRegistry); jRuleEngine.setCronScheduler(cronScheduler); diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java b/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java index 765b4051..6fbe30d9 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java @@ -83,6 +83,7 @@ import org.openhab.core.items.ItemRegistry; import org.openhab.core.items.events.ItemEvent; import org.openhab.core.library.types.QuantityType; +import org.openhab.core.persistence.PersistenceServiceRegistry; import org.openhab.core.scheduler.CronScheduler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -109,6 +110,7 @@ public class JRuleEngine implements PropertyChangeListener { private static volatile JRuleEngine instance; private JRuleRuleProvider ruleProvider; + private PersistenceServiceRegistry persistenceServiceRegistry; public static JRuleEngine get() { if (instance == null) { @@ -557,4 +559,12 @@ private void invokeDelayed(JRuleExecutionContext context, JRuleEvent event, public void setRuleProvider(JRuleRuleProvider ruleProvider) { this.ruleProvider = ruleProvider; } + + public void setPersistenceServiceRegistry(PersistenceServiceRegistry persistenceServiceRegistry) { + this.persistenceServiceRegistry = persistenceServiceRegistry; + } + + public PersistenceServiceRegistry getPersistenceServiceRegistry() { + return persistenceServiceRegistry; + } } diff --git a/src/main/java/org/openhab/automation/jrule/internal/items/JRuleInternalItem.java b/src/main/java/org/openhab/automation/jrule/internal/items/JRuleInternalItem.java index 5f4b7a24..1f87d156 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/items/JRuleInternalItem.java +++ b/src/main/java/org/openhab/automation/jrule/internal/items/JRuleInternalItem.java @@ -112,6 +112,11 @@ public Optional getPreviousState(boolean skipEquals, String persistenceSe return JRulePersistenceExtensions.previousState(name, skipEquals, persistenceServiceId); } + @Override + public void persist(JRuleValue state, ZonedDateTime time, String persistenceServiceId) { + JRulePersistenceExtensions.persist(name, time, state, persistenceServiceId); + } + @Override public boolean equals(Object o) { if (this == o) diff --git a/src/main/java/org/openhab/automation/jrule/internal/items/JRulePersistenceExtensions.java b/src/main/java/org/openhab/automation/jrule/internal/items/JRulePersistenceExtensions.java index c1418330..a3833dd6 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/items/JRulePersistenceExtensions.java +++ b/src/main/java/org/openhab/automation/jrule/internal/items/JRulePersistenceExtensions.java @@ -16,15 +16,18 @@ import java.util.Optional; import org.openhab.automation.jrule.exception.JRuleItemNotFoundException; +import org.openhab.automation.jrule.internal.JRuleLog; +import org.openhab.automation.jrule.internal.engine.JRuleEngine; import org.openhab.automation.jrule.internal.handler.JRuleEventHandler; import org.openhab.automation.jrule.rules.value.JRuleValue; import org.openhab.core.items.Item; import org.openhab.core.items.ItemNotFoundException; import org.openhab.core.items.ItemRegistry; import org.openhab.core.library.types.DecimalType; -import org.openhab.core.persistence.HistoricItem; +import org.openhab.core.persistence.*; import org.openhab.core.persistence.extensions.PersistenceExtensions; import org.openhab.core.types.State; +import org.slf4j.LoggerFactory; /** * The {@link JRulePersistenceExtensions} @@ -32,6 +35,20 @@ * @author Arne Seime - Initial contribution */ class JRulePersistenceExtensions { + public static void persist(String itemName, ZonedDateTime timestamp, JRuleValue state, String serviceId) { + Item item = getItem(itemName); + JRuleEngine jRuleEngine = JRuleEngine.get(); + PersistenceServiceRegistry persistenceServiceRegistry = jRuleEngine.getPersistenceServiceRegistry(); + PersistenceService persistenceService = persistenceServiceRegistry + .get(Optional.ofNullable(serviceId).orElse(persistenceServiceRegistry.getDefaultId())); + if (persistenceService instanceof ModifiablePersistenceService modifiablePersistenceService) { + modifiablePersistenceService.store(item, timestamp, state.toOhState()); + } else { + JRuleLog.warn(LoggerFactory.getLogger(JRulePersistenceExtensions.class), JRuleEngine.class.getSimpleName(), + "cannot persist item state, persistence service not modifiable"); + } + } + public static Optional historicState(String itemName, ZonedDateTime timestamp) { return historicState(itemName, timestamp, null); } diff --git a/src/main/java/org/openhab/automation/jrule/items/JRuleItem.java b/src/main/java/org/openhab/automation/jrule/items/JRuleItem.java index e85c1e77..e4179b5d 100644 --- a/src/main/java/org/openhab/automation/jrule/items/JRuleItem.java +++ b/src/main/java/org/openhab/automation/jrule/items/JRuleItem.java @@ -129,6 +129,12 @@ default Optional getPreviousState(boolean skipEquals) { Optional getPreviousState(boolean skipEquals, String persistenceServiceId); + default void persist(JRuleValue state, ZonedDateTime time) { + persist(state, time, null); + } + + void persist(JRuleValue state, ZonedDateTime time, String persistenceServiceId); + default boolean isGroup() { return false; } diff --git a/src/test/java/org/openhab/automation/jrule/rules/integration_test/ITJRulePersistence.java b/src/test/java/org/openhab/automation/jrule/rules/integration_test/ITJRulePersistence.java index 71d88733..0423aedd 100644 --- a/src/test/java/org/openhab/automation/jrule/rules/integration_test/ITJRulePersistence.java +++ b/src/test/java/org/openhab/automation/jrule/rules/integration_test/ITJRulePersistence.java @@ -114,6 +114,23 @@ public void persistenceAllTypes() throws IOException, InterruptedException { // verifyDimmer(); } + @Test + public void persistFuture() throws IOException, InterruptedException { + sendCommand(TestPersistence.ITEM_TRIGGER_RULE, TestPersistence.COMMMAND_PERISTENCE_IN_FUTURE); + verifyRuleWasExecuted(TestPersistence.NAME_PERSIST_IN_FUTURE); + verifyNoError(); + + Thread.sleep(3000); + + sendCommand(TestPersistence.ITEM_TRIGGER_RULE, TestPersistence.COMMMAND_QUERY_IN_FUTURE); + verifyRuleWasExecuted(TestPersistence.NAME_QUERY_IN_FUTURE); + verifyNoError(); + + verifyLogEntry("now: 10"); + verifyLogEntry("now +1: 20"); + verifyLogEntry("now +2: 30"); + } + private void verifyNumber() { verifyPersistence("historicState", "Number_To_Persist", 7, null, 10); verifyPersistence("historicState", "Number_To_Persist", 5, 20.0, 10); diff --git a/src/test/java/org/openhab/automation/jrule/rules/integration_test/JRuleITBase.java b/src/test/java/org/openhab/automation/jrule/rules/integration_test/JRuleITBase.java index c1ca58cf..9160c5f6 100644 --- a/src/test/java/org/openhab/automation/jrule/rules/integration_test/JRuleITBase.java +++ b/src/test/java/org/openhab/automation/jrule/rules/integration_test/JRuleITBase.java @@ -100,7 +100,7 @@ public abstract class JRuleITBase { "ghcr.io/shopify/toxiproxy:2.5.0").withNetworkAliases("mqtt").withNetwork(network).dependsOn(mqttContainer) .withReuse(true); - private static final GenericContainer influxDbContainer = new GenericContainer<>("influxdb:2.0") + protected static final GenericContainer influxDbContainer = new GenericContainer<>("influxdb:2.0") .withEnv("DOCKER_INFLUXDB_INIT_MODE", "setup").withEnv("DOCKER_INFLUXDB_INIT_USERNAME", "admin") .withEnv("DOCKER_INFLUXDB_INIT_PASSWORD", "influxdb").withEnv("DOCKER_INFLUXDB_INIT_ORG", "openhab") .withEnv("DOCKER_INFLUXDB_INIT_BUCKET", "autogen").withEnv("DOCKER_INFLUXDB_INIT_RETENTION", "1w") diff --git a/src/test/java/org/openhab/automation/jrule/rules/user/TestPersistence.java b/src/test/java/org/openhab/automation/jrule/rules/user/TestPersistence.java index 0c3c7407..7bf3d36d 100755 --- a/src/test/java/org/openhab/automation/jrule/rules/user/TestPersistence.java +++ b/src/test/java/org/openhab/automation/jrule/rules/user/TestPersistence.java @@ -43,8 +43,35 @@ public class TestPersistence extends JRule { public static final String ITEM_LOCATION_TO_PERSIST = "Location_To_Persist"; public static final String ITEM_TRIGGER_RULE = "Trigger_Rule"; public static final String NAME_PERSIST_ALL_TYPES = "persist all types"; - public static final String COMMMAND_PERISTENCE = "peristence"; + public static final String NAME_PERSIST_IN_FUTURE = "persist in future"; + public static final String NAME_QUERY_IN_FUTURE = "query in future"; + public static final String COMMMAND_PERISTENCE = "persistence"; + public static final String COMMMAND_PERISTENCE_IN_FUTURE = "persistence_in_future"; + public static final String COMMMAND_QUERY_IN_FUTURE = "query_in_future"; public static final String PERSISTENCE_SERVICE_ID = "influxdb"; + public static final String ITEM_NUMBER_TO_PERSIST_FUTURE = "Number_To_Persist_Future"; + + private final ZonedDateTime now = ZonedDateTime.now(); + + @JRuleName(NAME_PERSIST_IN_FUTURE) + @JRuleWhenItemReceivedCommand(item = ITEM_TRIGGER_RULE, condition = @JRuleCondition(eq = COMMMAND_PERISTENCE_IN_FUTURE)) + public void persistFuture() { + JRuleNumberItem jRuleNumberItem = JRuleNumberItem.forName(ITEM_NUMBER_TO_PERSIST_FUTURE); + + jRuleNumberItem.persist(new JRuleDecimalValue(10), now, PERSISTENCE_SERVICE_ID); + jRuleNumberItem.persist(new JRuleDecimalValue(20), now.plusHours(1), PERSISTENCE_SERVICE_ID); + jRuleNumberItem.persist(new JRuleDecimalValue(30), now.plusHours(2), PERSISTENCE_SERVICE_ID); + } + + @JRuleName(NAME_QUERY_IN_FUTURE) + @JRuleWhenItemReceivedCommand(item = ITEM_TRIGGER_RULE, condition = @JRuleCondition(eq = COMMMAND_QUERY_IN_FUTURE)) + public void queryFuture() { + JRuleNumberItem jRuleNumberItem = JRuleNumberItem.forName(ITEM_NUMBER_TO_PERSIST_FUTURE); + + logInfo("now: {}", jRuleNumberItem.getHistoricState(now, PERSISTENCE_SERVICE_ID).get()); + logInfo("now +1: {}", jRuleNumberItem.getHistoricState(now.plusHours(1), PERSISTENCE_SERVICE_ID).get()); + logInfo("now +2: {}", jRuleNumberItem.getHistoricState(now.plusHours(2), PERSISTENCE_SERVICE_ID).get()); + } @JRuleName(NAME_PERSIST_ALL_TYPES) @JRuleWhenItemReceivedCommand(item = ITEM_TRIGGER_RULE, condition = @JRuleCondition(eq = COMMMAND_PERISTENCE)) diff --git a/src/test/resources/docker/conf/items/default.items b/src/test/resources/docker/conf/items/default.items index 32b4d62c..3be1dc89 100644 --- a/src/test/resources/docker/conf/items/default.items +++ b/src/test/resources/docker/conf/items/default.items @@ -83,5 +83,7 @@ Color Color_To_Persist (InfluxDbPersist) Location Location_To_Persist (InfluxDbPersist) Number:Power Quantity_To_Persist (InfluxDbPersist) +Number Number_To_Persist_Future (InfluxDbPersist) + Dimmer Dimmer_With_Tags_And_Metadata ["Control", "Light"] { Speech="SetLightState" [ location="Livingroom" ] } From ee9d89e4b14e59f0711eadb38c481f9e093c64eb Mon Sep 17 00:00:00 2001 From: querdenker2k Date: Sat, 30 Sep 2023 17:08:22 +0200 Subject: [PATCH 2/5] add stateAt --- .../internal/items/JRuleInternalItem.java | 5 ++++ .../items/JRulePersistenceExtensions.java | 27 +++++++++++++++++++ .../automation/jrule/items/JRuleItem.java | 6 +++++ .../integration_test/ITJRulePersistence.java | 4 +++ .../jrule/rules/user/TestPersistence.java | 4 +++ 5 files changed, 46 insertions(+) diff --git a/src/main/java/org/openhab/automation/jrule/internal/items/JRuleInternalItem.java b/src/main/java/org/openhab/automation/jrule/internal/items/JRuleInternalItem.java index 1f87d156..d45bfa53 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/items/JRuleInternalItem.java +++ b/src/main/java/org/openhab/automation/jrule/internal/items/JRuleInternalItem.java @@ -107,6 +107,11 @@ public Optional getHistoricState(ZonedDateTime timestamp, String per return JRulePersistenceExtensions.historicState(name, timestamp, persistenceServiceId); } + @Override + public Optional getStateAt(ZonedDateTime timestamp, String persistenceServiceId) { + return JRulePersistenceExtensions.stateAt(name, timestamp, persistenceServiceId); + } + @Override public Optional getPreviousState(boolean skipEquals, String persistenceServiceId) { return JRulePersistenceExtensions.previousState(name, skipEquals, persistenceServiceId); diff --git a/src/main/java/org/openhab/automation/jrule/internal/items/JRulePersistenceExtensions.java b/src/main/java/org/openhab/automation/jrule/internal/items/JRulePersistenceExtensions.java index a3833dd6..0c364c38 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/items/JRulePersistenceExtensions.java +++ b/src/main/java/org/openhab/automation/jrule/internal/items/JRulePersistenceExtensions.java @@ -49,6 +49,33 @@ public static void persist(String itemName, ZonedDateTime timestamp, JRuleValue } } + public static Optional stateAt(String itemName, ZonedDateTime timestamp, String serviceId) { + Item item = getItem(itemName); + JRuleEngine jRuleEngine = JRuleEngine.get(); + PersistenceServiceRegistry persistenceServiceRegistry = jRuleEngine.getPersistenceServiceRegistry(); + PersistenceService persistenceService = persistenceServiceRegistry + .get(Optional.ofNullable(serviceId).orElse(persistenceServiceRegistry.getDefaultId())); + if (persistenceService instanceof QueryablePersistenceService queryablePersistenceService) { + FilterCriteria filter = new FilterCriteria(); + filter.setBeginDate(timestamp.minusSeconds(1)); + filter.setEndDate(timestamp.plusSeconds(1)); + filter.setItemName(item.getName()); + filter.setPageSize(1); + filter.setOrdering(FilterCriteria.Ordering.DESCENDING); + Iterable result = queryablePersistenceService.query(filter); + if (result.iterator().hasNext()) { + return Optional.of(result.iterator().next()).map(HistoricItem::getState) + .map(state -> JRuleEventHandler.get().toValue(state)); + } else { + return Optional.empty(); + } + } else { + JRuleLog.warn(LoggerFactory.getLogger(JRulePersistenceExtensions.class), JRuleEngine.class.getSimpleName(), + "cannot persist item state, persistence service not modifiable"); + return Optional.empty(); + } + } + public static Optional historicState(String itemName, ZonedDateTime timestamp) { return historicState(itemName, timestamp, null); } diff --git a/src/main/java/org/openhab/automation/jrule/items/JRuleItem.java b/src/main/java/org/openhab/automation/jrule/items/JRuleItem.java index e4179b5d..96f5f1b9 100644 --- a/src/main/java/org/openhab/automation/jrule/items/JRuleItem.java +++ b/src/main/java/org/openhab/automation/jrule/items/JRuleItem.java @@ -123,6 +123,12 @@ default Optional getHistoricState(ZonedDateTime timestamp) { Optional getHistoricState(ZonedDateTime timestamp, String persistenceServiceId); + default Optional getStateAt(ZonedDateTime timestamp) { + return getStateAt(timestamp, null); + } + + Optional getStateAt(ZonedDateTime timestamp, String persistenceServiceId); + default Optional getPreviousState(boolean skipEquals) { return getPreviousState(skipEquals, null); } diff --git a/src/test/java/org/openhab/automation/jrule/rules/integration_test/ITJRulePersistence.java b/src/test/java/org/openhab/automation/jrule/rules/integration_test/ITJRulePersistence.java index 0423aedd..dcff8770 100644 --- a/src/test/java/org/openhab/automation/jrule/rules/integration_test/ITJRulePersistence.java +++ b/src/test/java/org/openhab/automation/jrule/rules/integration_test/ITJRulePersistence.java @@ -129,6 +129,10 @@ public void persistFuture() throws IOException, InterruptedException { verifyLogEntry("now: 10"); verifyLogEntry("now +1: 20"); verifyLogEntry("now +2: 30"); + + verifyLogEntry("now stateAt: 10"); + verifyLogEntry("now stateAt +1: 20"); + verifyLogEntry("now stateAt +2: 30"); } private void verifyNumber() { diff --git a/src/test/java/org/openhab/automation/jrule/rules/user/TestPersistence.java b/src/test/java/org/openhab/automation/jrule/rules/user/TestPersistence.java index 7bf3d36d..d2f49073 100755 --- a/src/test/java/org/openhab/automation/jrule/rules/user/TestPersistence.java +++ b/src/test/java/org/openhab/automation/jrule/rules/user/TestPersistence.java @@ -71,6 +71,10 @@ public void queryFuture() { logInfo("now: {}", jRuleNumberItem.getHistoricState(now, PERSISTENCE_SERVICE_ID).get()); logInfo("now +1: {}", jRuleNumberItem.getHistoricState(now.plusHours(1), PERSISTENCE_SERVICE_ID).get()); logInfo("now +2: {}", jRuleNumberItem.getHistoricState(now.plusHours(2), PERSISTENCE_SERVICE_ID).get()); + + logInfo("now stateAt: {}", jRuleNumberItem.getStateAt(now, PERSISTENCE_SERVICE_ID).get()); + logInfo("now stateAt +1: {}", jRuleNumberItem.getStateAt(now.plusHours(1), PERSISTENCE_SERVICE_ID).get()); + logInfo("now stateAt +2: {}", jRuleNumberItem.getStateAt(now.plusHours(2), PERSISTENCE_SERVICE_ID).get()); } @JRuleName(NAME_PERSIST_ALL_TYPES) From 181a1a695066f476de7d17681122845d5d6d165a Mon Sep 17 00:00:00 2001 From: seaside1 Date: Mon, 2 Oct 2023 10:13:50 +0200 Subject: [PATCH 3/5] Remove * import --- .../jrule/internal/items/JRulePersistenceExtensions.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/openhab/automation/jrule/internal/items/JRulePersistenceExtensions.java b/src/main/java/org/openhab/automation/jrule/internal/items/JRulePersistenceExtensions.java index 0c364c38..c544d032 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/items/JRulePersistenceExtensions.java +++ b/src/main/java/org/openhab/automation/jrule/internal/items/JRulePersistenceExtensions.java @@ -24,7 +24,12 @@ import org.openhab.core.items.ItemNotFoundException; import org.openhab.core.items.ItemRegistry; import org.openhab.core.library.types.DecimalType; -import org.openhab.core.persistence.*; +import org.openhab.core.persistence.FilterCriteria; +import org.openhab.core.persistence.HistoricItem; +import org.openhab.core.persistence.ModifiablePersistenceService; +import org.openhab.core.persistence.PersistenceService; +import org.openhab.core.persistence.PersistenceServiceRegistry; +import org.openhab.core.persistence.QueryablePersistenceService; import org.openhab.core.persistence.extensions.PersistenceExtensions; import org.openhab.core.types.State; import org.slf4j.LoggerFactory; From 585182cac161d31ecaabe1b7a3d9de4984569796 Mon Sep 17 00:00:00 2001 From: querdenker2k Date: Tue, 3 Oct 2023 11:41:20 +0200 Subject: [PATCH 4/5] documentation --- README.md | 7 +++++ doc/EXAMPLES.md | 30 +++++++++++++++++++ .../internal/items/JRuleInternalItem.java | 5 ++++ .../items/JRuleInternalNumberGroupItem.java | 2 +- .../items/JRulePersistenceExtensions.java | 12 ++++---- 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index fc067745..d3b0a37b 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,13 @@ Beta, still major changes. - Not supporting OH4 GUI rules +### Database + +- Using persist is just support for `org.openhab.core.persistence.ModifiablePersistenceService`. Currently these are + - Influx + - JDBC + - InMemory + # Getting started 1. Install the addon by either diff --git a/doc/EXAMPLES.md b/doc/EXAMPLES.md index a949fce3..e60f1648 100644 --- a/doc/EXAMPLES.md +++ b/doc/EXAMPLES.md @@ -43,6 +43,7 @@ + [Example 39 - HTTP requests](#example-39---http-requests) + [Example 40 - Delay rule execution](#example-40---delay-rule-execution) + [Example 41 - Get Metadata and Tags](#example-41---get-metadata-and-tags) + + [Example 42 - Persist future data](#example-42---persist-future-data) ### Example 1 - Invoke another item Switch from rule @@ -1064,3 +1065,32 @@ public class DemoRule extends JRule { } } ``` + +## Example 42 - Persist future data + +Use case: Persist future data for e.g. Tibber future prices + +```java +package org.openhab.automation.jrule.rules.user; + +import org.openhab.automation.jrule.rules.JRuleName; +import org.openhab.automation.jrule.rules.JRuleWhenItemReceivedCommand; +import org.openhab.automation.jrule.rules.JRule; +import org.openhab.automation.jrule.rules.JRuleDelayed; + +public class DemoRule extends JRule { + @JRuleName("persist new tibber prices") + @JRuleWhenTimeTrigger(hours = 13, minutes = 30) + public void persistNewPrices() { + var availPrices = getPrices(); + availPrices.forEach(t -> { + ZonedDateTime zonedDateTime = t.getStartsAt().atZone(ZoneId.systemDefault()); + Optional historicState = JRuleItems.Tibber_Hourly_Cost_Future.getStateAt(zonedDateTime, PERSISTENCE); + logDebug("adding available price for: {}, existing: {}", zonedDateTime, historicState); + if (historicState.isEmpty()) { + JRuleItems.Tibber_Hourly_Cost_Future.persist(new JRuleDecimalValue(t.getTotal()), zonedDateTime, PERSISTENCE); + } + }); + } +} +``` diff --git a/src/main/java/org/openhab/automation/jrule/internal/items/JRuleInternalItem.java b/src/main/java/org/openhab/automation/jrule/internal/items/JRuleInternalItem.java index d45bfa53..369eeacf 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/items/JRuleInternalItem.java +++ b/src/main/java/org/openhab/automation/jrule/internal/items/JRuleInternalItem.java @@ -137,4 +137,9 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hash(name, label, type, id); } + + @Override + public String toString() { + return "%s:%s".formatted(name, type); + } } diff --git a/src/main/java/org/openhab/automation/jrule/internal/items/JRuleInternalNumberGroupItem.java b/src/main/java/org/openhab/automation/jrule/internal/items/JRuleInternalNumberGroupItem.java index 97cac574..1d7161cb 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/items/JRuleInternalNumberGroupItem.java +++ b/src/main/java/org/openhab/automation/jrule/internal/items/JRuleInternalNumberGroupItem.java @@ -19,7 +19,7 @@ import org.openhab.automation.jrule.items.metadata.JRuleItemMetadata; /** - * The {@link JRuleInternalColorGroupItem} Items + * The {@link JRuleInternalNumberGroupItem} Items * * @author Arne Seime - Initial contribution */ diff --git a/src/main/java/org/openhab/automation/jrule/internal/items/JRulePersistenceExtensions.java b/src/main/java/org/openhab/automation/jrule/internal/items/JRulePersistenceExtensions.java index c544d032..93b37b62 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/items/JRulePersistenceExtensions.java +++ b/src/main/java/org/openhab/automation/jrule/internal/items/JRulePersistenceExtensions.java @@ -16,7 +16,7 @@ import java.util.Optional; import org.openhab.automation.jrule.exception.JRuleItemNotFoundException; -import org.openhab.automation.jrule.internal.JRuleLog; +import org.openhab.automation.jrule.exception.JRuleRuntimeException; import org.openhab.automation.jrule.internal.engine.JRuleEngine; import org.openhab.automation.jrule.internal.handler.JRuleEventHandler; import org.openhab.automation.jrule.rules.value.JRuleValue; @@ -32,7 +32,6 @@ import org.openhab.core.persistence.QueryablePersistenceService; import org.openhab.core.persistence.extensions.PersistenceExtensions; import org.openhab.core.types.State; -import org.slf4j.LoggerFactory; /** * The {@link JRulePersistenceExtensions} @@ -49,8 +48,8 @@ public static void persist(String itemName, ZonedDateTime timestamp, JRuleValue if (persistenceService instanceof ModifiablePersistenceService modifiablePersistenceService) { modifiablePersistenceService.store(item, timestamp, state.toOhState()); } else { - JRuleLog.warn(LoggerFactory.getLogger(JRulePersistenceExtensions.class), JRuleEngine.class.getSimpleName(), - "cannot persist item state, persistence service not modifiable"); + throw new JRuleRuntimeException("cannot persist item state, persistence service (%s) not modifiable" + .formatted(persistenceService.getId())); } } @@ -75,9 +74,8 @@ public static Optional stateAt(String itemName, ZonedDateTime timest return Optional.empty(); } } else { - JRuleLog.warn(LoggerFactory.getLogger(JRulePersistenceExtensions.class), JRuleEngine.class.getSimpleName(), - "cannot persist item state, persistence service not modifiable"); - return Optional.empty(); + throw new JRuleRuntimeException("cannot get state at for item, persistence service (%s) not queryable" + .formatted(persistenceService.getId())); } } From ef64bfc9dfc5ec41bad9ffa8244fac367d1d2bb8 Mon Sep 17 00:00:00 2001 From: seaside1 Date: Wed, 4 Oct 2023 09:38:45 +0200 Subject: [PATCH 5/5] Update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d3b0a37b..1618416d 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Beta, still major changes. ### Database -- Using persist is just support for `org.openhab.core.persistence.ModifiablePersistenceService`. Currently these are +- JRule persist is a utility for supporting `org.openhab.core.persistence.ModifiablePersistenceService`. Currently these Persistance Services are supported: - Influx - JDBC - InMemory