]> git.basschouten.com Git - openhab-addons.git/blob
d7c33bf0ad2da4c168be5895917f82e4cb4d8e5b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lgwebos.internal;
14
15 import java.util.Map;
16 import java.util.Optional;
17 import java.util.concurrent.ConcurrentHashMap;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.lgwebos.internal.handler.LGWebOSHandler;
21 import org.openhab.binding.lgwebos.internal.handler.command.ServiceSubscription;
22 import org.openhab.binding.lgwebos.internal.handler.core.ResponseListener;
23 import org.openhab.core.thing.ThingUID;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * An abstract implementation of ChannelHander which serves as a base class for all concrete instances.
29  *
30  * @author Sebastian Prehn - initial contribution
31  */
32 @NonNullByDefault
33 abstract class BaseChannelHandler<T> implements ChannelHandler {
34     private final Logger logger = LoggerFactory.getLogger(BaseChannelHandler.class);
35
36     private final ResponseListener<T> defaultResponseListener = createResponseListener();
37
38     protected <Y> ResponseListener<Y> createResponseListener() {
39         return new ResponseListener<Y>() {
40
41             @Override
42             public void onError(String error) {
43                 logger.debug("{} received error response: {}", BaseChannelHandler.this.getClass().getSimpleName(),
44                         error);
45             }
46
47             @Override
48             public void onSuccess(Y object) {
49                 logger.debug("{} received: {}.", BaseChannelHandler.this.getClass().getSimpleName(), object);
50             }
51         };
52     }
53
54     // IP to Subscriptions map
55     private Map<ThingUID, ServiceSubscription<T>> subscriptions = new ConcurrentHashMap<>();
56
57     @Override
58     public void onDeviceReady(String channelId, LGWebOSHandler handler) {
59         // NOP
60     }
61
62     @Override
63     public void onDeviceRemoved(String channelId, LGWebOSHandler handler) {
64         // NOP
65     }
66
67     @Override
68     public final synchronized void refreshSubscription(String channelId, LGWebOSHandler handler) {
69         removeAnySubscription(handler);
70
71         Optional<ServiceSubscription<T>> listener = getSubscription(channelId, handler);
72         if (listener.isPresent()) {
73             logger.debug("Subscribed {} on Thing: {}", this.getClass().getName(), handler.getThing().getUID());
74             subscriptions.put(handler.getThing().getUID(), listener.get());
75         }
76     }
77
78     /**
79      * Creates a subscription instance for this device if subscription is supported.
80      *
81      * @param channelId channel ID
82      * @param handler
83      * @return an {@code Optional} containing the ServiceSubscription, or an empty {@code Optional} if subscription is
84      *         not supported.
85      */
86     protected Optional<ServiceSubscription<T>> getSubscription(String channelId, LGWebOSHandler handler) {
87         return Optional.empty();
88     }
89
90     @Override
91     public final synchronized void removeAnySubscription(LGWebOSHandler handler) {
92         ServiceSubscription<T> l = subscriptions.remove(handler.getThing().getUID());
93         if (l != null) {
94             handler.getSocket().unsubscribe(l);
95             logger.debug("Unsubscribed {} on Thing: {}", this.getClass().getName(), handler.getThing().getUID());
96         }
97     }
98
99     protected ResponseListener<T> getDefaultResponseListener() {
100         return defaultResponseListener;
101     }
102 }