From 41cb337adc9d237f2e77f9cbe10127fb2898d854 Mon Sep 17 00:00:00 2001 From: Mark Herwege Date: Fri, 29 Nov 2024 18:02:47 +0100 Subject: [PATCH] use item methods if last state update/change not in persistence Signed-off-by: Mark Herwege --- .../extensions/PersistenceExtensions.java | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java index d7b5bed0295..b1d186d4c23 100644 --- a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java +++ b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java @@ -66,6 +66,7 @@ * @author Mark Herwege - lastChange and nextChange methods * @author Mark Herwege - handle persisted GroupItem with QuantityType * @author Mark Herwege - add median methods + * @author Mark Herwege - use item lastChange and lastUpdate methods if not in peristence */ @Component(immediate = true) @NonNullByDefault @@ -331,6 +332,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable /** * Query the last historic update time of a given item. The default persistence service is used. + * Note the {@link Item.getLastStateUpdate()} is generally preferred to get the last update time of an item. * * @param item the item for which the last historic update time is to be returned * @return point in time of the last historic update to item, null if there are no @@ -343,6 +345,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable /** * Query for the last historic update time of a given item. + * Note the {@link Item.getLastStateUpdate()} is generally preferred to get the last update time of an item. * * @param item the item for which the last historic update time is to be returned * @param serviceId the name of the {@link PersistenceService} to use @@ -386,6 +389,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable /** * Query the last historic change time of a given item. The default persistence service is used. + * Note the {@link Item.getLastStateChange()} is generally preferred to get the last state change time of an item. * * @param item the item for which the last historic change time is to be returned * @return point in time of the last historic change to item, null if there are no @@ -398,6 +402,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable /** * Query for the last historic change time of a given item. + * Note the {@link Item.getLastStateChange()} is generally preferred to get the last state change time of an item. * * @param item the item for which the last historic change time is to be returned * @param serviceId the name of the {@link PersistenceService} to use @@ -461,26 +466,26 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable filter.setPageNumber(startPage); Iterable items = qService.query(filter); - Iterator itemIterator = items.iterator(); - State state = item.getState(); - if (itemIterator.hasNext()) { - if (!skipEqual) { - HistoricItem historicItem = itemIterator.next(); - if (!forward && !historicItem.getState().equals(state)) { - // Last persisted state value different from current state value, so it must have updated since - // last persist. We do not know when. - return null; - } - return historicItem.getTimestamp(); - } else { - HistoricItem historicItem = itemIterator.next(); - int itemCount = 1; - if (!historicItem.getState().equals(state)) { - // Persisted state value different from current state value, so it must have changed, but we do - // not know when when looking backward. - return forward ? historicItem.getTimestamp() : null; - } - while (items != null) { + while (items != null) { + Iterator itemIterator = items.iterator(); + int itemCount = 0; + State state = item.getState(); + if (itemIterator.hasNext()) { + if (!skipEqual) { + HistoricItem historicItem = itemIterator.next(); + if (!forward && !historicItem.getState().equals(state)) { + // Last persisted state value different from current state value, so it must have updated + // since last persist. We do not know when from persistence, so get it from the item. + return item.getLastStateUpdate(); + } + return historicItem.getTimestamp(); + } else { + HistoricItem historicItem = itemIterator.next(); + if (!historicItem.getState().equals(state)) { + // Persisted state value different from current state value, so it must have changed, but we + // do not know when looking backward in persistence. Get it from the item. + return forward ? historicItem.getTimestamp() : item.getLastStateChange(); + } while (historicItem.getState().equals(state) && itemIterator.hasNext()) { HistoricItem nextHistoricItem = itemIterator.next(); itemCount++; @@ -492,7 +497,6 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable if (itemCount == filter.getPageSize()) { filter.setPageNumber(++startPage); items = qService.query(filter); - itemCount = 0; } else { items = null; } @@ -508,6 +512,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable /** * Returns the previous state of a given item. + * Note the {@link Item.getLastState()} is generally preferred to get the previous state of an item. * * @param item the item to get the previous state value for * @return the previous state or null if no previous state could be found, or if the default @@ -519,6 +524,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable /** * Returns the previous state of a given item. + * Note the {@link Item.getLastState()} is generally preferred to get the previous state of an item. * * @param item the item to get the previous state value for * @param skipEqual if true, skips equal state values and searches the first state not equal the current state @@ -532,6 +538,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable /** * Returns the previous state of a given item. * The {@link PersistenceService} identified by the serviceId is used. + * Note the {@link Item.getLastState()} is generally preferred to get the previous state of an item. * * @param item the item to get the previous state value for * @param serviceId the name of the {@link PersistenceService} to use @@ -545,6 +552,7 @@ private static void internalPersist(Item item, TimeSeries timeSeries, @Nullable /** * Returns the previous state of a given item. * The {@link PersistenceService} identified by the serviceId is used. + * Note the {@link Item.getLastState()} is generally preferred to get the previous state of an item. * * @param item the item to get the previous state value for * @param skipEqual if true, skips equal state values and searches the first state not equal the