Minor value cache tweaks (#12493)
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
parent
1918afc3a9
commit
963a73ee74
@ -12,7 +12,8 @@
|
||||
*/
|
||||
package org.openhab.binding.danfossairunit.internal;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ -21,7 +22,7 @@ import org.openhab.core.types.State;
|
||||
|
||||
/**
|
||||
* The {@link ValueCache} is responsible for holding the last value of the channels for a
|
||||
* certain amount of time {@link ValueCache#durationMs} to prevent unnecessary event bus updates if the value didn't
|
||||
* certain amount of time {@link ValueCache#durationMillis} to prevent unnecessary event bus updates if the value didn't
|
||||
* change.
|
||||
*
|
||||
* @author Robert Bach - Initial contribution
|
||||
@ -29,39 +30,38 @@ import org.openhab.core.types.State;
|
||||
@NonNullByDefault
|
||||
public class ValueCache {
|
||||
|
||||
private Map<String, StateWithTimestamp> stateByValue = new HashMap<>();
|
||||
private final Map<String, StateWithTimestamp> stateByValue = new HashMap<>();
|
||||
|
||||
private final long durationMs;
|
||||
private final long durationMillis;
|
||||
|
||||
public ValueCache(long durationMs) {
|
||||
this.durationMs = durationMs;
|
||||
public ValueCache(long durationMillis) {
|
||||
this.durationMillis = durationMillis;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates or inserts the given value into the value cache. Returns true if there was no value in the cache
|
||||
* for the given channelId or if the value has updated to a different value or if the value is older than
|
||||
* the cache duration
|
||||
* the cache duration.
|
||||
*
|
||||
* @param channelId the channel's id
|
||||
* @param state new state
|
||||
*/
|
||||
public boolean updateValue(String channelId, State newState) {
|
||||
long currentTimeMs = Calendar.getInstance().getTimeInMillis();
|
||||
StateWithTimestamp oldState = stateByValue.get(channelId);
|
||||
boolean writeToCache;
|
||||
if (oldState == null) {
|
||||
writeToCache = true;
|
||||
} else {
|
||||
writeToCache = !oldState.state.equals(newState) || oldState.timestamp < (currentTimeMs - durationMs);
|
||||
public boolean updateValue(String channelId, State state) {
|
||||
Instant now = Instant.now();
|
||||
StateWithTimestamp cachedValue = stateByValue.get(channelId);
|
||||
if (cachedValue == null || !state.equals(cachedValue.state)
|
||||
|| cachedValue.timestamp.isBefore(now.minus(durationMillis, ChronoUnit.MILLIS))) {
|
||||
stateByValue.put(channelId, new StateWithTimestamp(state, now));
|
||||
return true;
|
||||
}
|
||||
if (writeToCache) {
|
||||
stateByValue.put(channelId, new StateWithTimestamp(newState, currentTimeMs));
|
||||
}
|
||||
return writeToCache;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static class StateWithTimestamp {
|
||||
State state;
|
||||
long timestamp;
|
||||
Instant timestamp;
|
||||
|
||||
public StateWithTimestamp(State state, long timestamp) {
|
||||
public StateWithTimestamp(State state, Instant timestamp) {
|
||||
this.state = state;
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user