Skip to content

Commit

Permalink
[modbus] update time-of-last-update (openhab#4802)
Browse files Browse the repository at this point in the history
Fixes sloppy modification based on [code review
comment](https://github.com/openhab/openhab2-addons/pull/4701#discussion_r251032820)
(which was valid on its own) from a maintainer,
which lead to [situation that "time of last update"
was not kept up-to-date](openhab/openhab2-addons@e13e893).

Signed-off-by: Sami Salonen <[email protected]>
Signed-off-by: Pshatsillo <[email protected]>
  • Loading branch information
ssalonen authored and Pshatsillo committed Jun 19, 2019
1 parent 8ab0825 commit 9ba2ac0
Showing 1 changed file with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -231,7 +230,7 @@ public synchronized void handleCommand(ChannelUID channelUID, Command command) {
* In case of JSON as transformation output, the output processed using {@link processJsonTransform}.
*
* @param channelUID channel UID corresponding to received command
* @param command command to be transformed
* @param command command to be transformed
* @return transformed command. Null is returned with JSON transformation outputs and configuration errors
*
* @see processJsonTransform
Expand Down Expand Up @@ -792,7 +791,7 @@ public synchronized void onWriteResponse(ModbusWriteRequestBlueprint request, Mo
* Update linked channels
*
* @param numericState numeric state corresponding to polled data
* @param boolValue boolean value corresponding to polled data
* @param boolValue boolean value corresponding to polled data
* @return updated channel data
*/
private Map<ChannelUID, State> processUpdatedValue(DecimalType numericState, boolean boolValue) {
Expand Down Expand Up @@ -862,11 +861,23 @@ private void updateExpiredChannels(Map<ChannelUID, State> states) {
updateStatusIfChanged(ThingStatus.ONLINE);
long now = System.currentTimeMillis();
// Update channels that have not been updated in a while, or when their values has changed
states.forEach((uid, state) -> whenExpired(now, uid, state, this::tryUpdateState));
states.forEach((uid, state) -> updateExpiredChannel(now, uid, state));
channelLastState = states;
}
}

private void updateExpiredChannel(long now, ChannelUID uid, State state) {
@Nullable
State lastState = channelLastState.get(uid);
long lastUpdatedMillis = channelLastUpdated.getOrDefault(uid, 0L);
long millisSinceLastUpdate = now - lastUpdatedMillis;
if (lastUpdatedMillis <= 0L || lastState == null || updateUnchangedValuesEveryMillis <= 0L
|| millisSinceLastUpdate > updateUnchangedValuesEveryMillis || !lastState.equals(state)) {
tryUpdateState(uid, state);
channelLastUpdated.put(uid, now);
}
}

private void tryUpdateState(ChannelUID uid, State state) {
try {
updateState(uid, state);
Expand All @@ -877,17 +888,6 @@ private void tryUpdateState(ChannelUID uid, State state) {
}
}

private void whenExpired(long now, ChannelUID uid, State state, BiConsumer<ChannelUID, State> action) {
@Nullable
State lastState = channelLastState.get(uid);
long lastUpdatedMillis = channelLastUpdated.getOrDefault(uid, 0L);
long millisSinceLastUpdate = now - lastUpdatedMillis;
if (lastUpdatedMillis <= 0L || lastState == null || updateUnchangedValuesEveryMillis <= 0L
|| millisSinceLastUpdate > updateUnchangedValuesEveryMillis || !lastState.equals(state)) {
action.accept(uid, state);
}
}

private ChannelUID getChannelUID(String channelID) {
return channelCache.computeIfAbsent(channelID, id -> new ChannelUID(getThing().getUID(), id));
}
Expand Down

0 comments on commit 9ba2ac0

Please sign in to comment.