2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.atlona.internal;
16 import java.util.Objects;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.locks.Lock;
19 import java.util.concurrent.locks.ReentrantLock;
21 import org.openhab.core.thing.ThingStatus;
22 import org.openhab.core.thing.ThingStatusDetail;
23 import org.openhab.core.types.State;
26 * Defines an implementation of {@link AtlonaHandlerCallback} that will remember the last state
27 * for a channelId and suppress the callback if the state hasn't changed
29 * @author Tim Roberts - Initial contribution
31 public class StatefulHandlerCallback implements AtlonaHandlerCallback {
33 /** The wrapped callback */
34 private final AtlonaHandlerCallback wrappedCallback;
36 /** The state by channel id */
37 private final Map<String, State> state = new ConcurrentHashMap<>();
39 private final Lock statusLock = new ReentrantLock();
40 private ThingStatus lastThingStatus;
41 private ThingStatusDetail lastThingStatusDetail;
44 * Create the callback from the other {@link AtlonaHandlerCallback}
46 * @param wrappedCallback a non-null {@link AtlonaHandlerCallback}
47 * @throws IllegalArgumentException if wrappedCallback is null
49 public StatefulHandlerCallback(AtlonaHandlerCallback wrappedCallback) {
50 if (wrappedCallback == null) {
51 throw new IllegalArgumentException("wrappedCallback cannot be null");
54 this.wrappedCallback = wrappedCallback;
58 * Overrides the status changed to simply call the {@link #wrappedCallback}
60 * @param status the new status
61 * @param detail the new detail
62 * @param msg the new message
65 public void statusChanged(ThingStatus status, ThingStatusDetail detail, String msg) {
68 // Simply return we match the last status change (prevents loops if changing to the same status)
69 if (status == lastThingStatus && detail == lastThingStatusDetail) {
73 lastThingStatus = status;
74 lastThingStatusDetail = detail;
78 // If we got this far - call the underlying one
79 wrappedCallback.statusChanged(status, detail, msg);
83 * Overrides the state changed to determine if the state is new or changed and then
84 * to call the {@link #wrappedCallback} if it has
86 * @param channelId the channel id that changed
87 * @param state the new state
90 public void stateChanged(String channelId, State state) {
91 if ("".equals(channelId)) {
95 final State oldState = this.state.get(channelId);
97 // If they are equal - nothing changed
98 if (Objects.equals(oldState, state)) {
102 // Something changed - save the new state and call the underlying wrapped
103 this.state.put(channelId, state);
104 wrappedCallback.stateChanged(channelId, state);
108 * Removes the state associated with the channel id. If the channelid
109 * doesn't exist (or is null or is empty), this method will do nothing.
111 * @param channelId the channel id to remove state
113 public void removeState(String channelId) {
114 if (channelId == null || "".equals(channelId)) {
117 state.remove(channelId);
121 * Overrides the set property to simply call the {@link #wrappedCallback}
123 * @param propertyName a non-null, non-empty property name
124 * @param propertyValue a non-null, possibly empty property value
127 public void setProperty(String propertyName, String propertyValue) {
128 wrappedCallback.setProperty(propertyName, propertyValue);
132 * Callback to get the {@link State} for a given property name
134 * @param propertyName a possibly null, possibly empty property name
135 * @return the {@link State} for the propertyName or null if not found
137 public State getState(String propertyName) {
138 return state.get(propertyName);