2 * Copyright (c) 2010-2022 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.lgwebos.internal;
16 import java.util.Optional;
17 import java.util.concurrent.ConcurrentHashMap;
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;
28 * An abstract implementation of ChannelHander which serves as a base class for all concrete instances.
30 * @author Sebastian Prehn - initial contribution
33 abstract class BaseChannelHandler<T> implements ChannelHandler {
34 private final Logger logger = LoggerFactory.getLogger(BaseChannelHandler.class);
36 private final ResponseListener<T> defaultResponseListener = createResponseListener();
38 protected <Y> ResponseListener<Y> createResponseListener() {
39 return new ResponseListener<Y>() {
42 public void onError(String error) {
43 logger.debug("{} received error response: {}", BaseChannelHandler.this.getClass().getSimpleName(),
48 public void onSuccess(Y object) {
49 logger.debug("{} received: {}.", BaseChannelHandler.this.getClass().getSimpleName(), object);
54 // IP to Subscriptions map
55 private Map<ThingUID, ServiceSubscription<T>> subscriptions = new ConcurrentHashMap<>();
58 public void onDeviceReady(String channelId, LGWebOSHandler handler) {
63 public void onDeviceRemoved(String channelId, LGWebOSHandler handler) {
68 public final synchronized void refreshSubscription(String channelId, LGWebOSHandler handler) {
69 removeAnySubscription(handler);
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());
79 * Creates a subscription instance for this device if subscription is supported.
81 * @param device device to which state changes to subscribe to
82 * @param channelID channel ID
84 * @return an {@code Optional} containing the ServiceSubscription, or an empty {@code Optional} if subscription is
87 protected Optional<ServiceSubscription<T>> getSubscription(String channelId, LGWebOSHandler handler) {
88 return Optional.empty();
92 public final synchronized void removeAnySubscription(LGWebOSHandler handler) {
93 ServiceSubscription<T> l = subscriptions.remove(handler.getThing().getUID());
95 handler.getSocket().unsubscribe(l);
96 logger.debug("Unsubscribed {} on Thing: {}", this.getClass().getName(), handler.getThing().getUID());
100 protected ResponseListener<T> getDefaultResponseListener() {
101 return defaultResponseListener;