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.unifi.internal.handler;
15 import static org.openhab.core.thing.ThingStatus.*;
16 import static org.openhab.core.types.RefreshType.REFRESH;
18 import java.lang.reflect.ParameterizedType;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.unifi.internal.api.UniFiException;
24 import org.openhab.binding.unifi.internal.api.model.UniFiController;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.Channel;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.thing.binding.BaseThingHandler;
31 import org.openhab.core.types.Command;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * @author Matthew Bowman - Initial contribution
38 * @param <E> entity - the UniFi entity class used by this thing handler
39 * @param <C> config - the UniFi config class used by this thing handler
42 public abstract class UniFiBaseThingHandler<E, C> extends BaseThingHandler {
44 private final Logger logger = LoggerFactory.getLogger(UniFiBaseThingHandler.class);
46 public UniFiBaseThingHandler(Thing thing) {
51 @SuppressWarnings("unchecked")
52 public final void initialize() {
53 Bridge bridge = getBridge();
54 if (bridge == null || bridge.getHandler() == null
55 || !(bridge.getHandler() instanceof UniFiControllerThingHandler)) {
56 updateStatus(OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
57 "You must choose a UniFi Controller for this thing.");
60 if (bridge.getStatus() == OFFLINE) {
61 updateStatus(OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, "The UniFi Controller is currently offline.");
64 // mgb: derive the config class from the generic type
65 Class<?> clazz = (Class<?>) (((ParameterizedType) getClass().getGenericSuperclass())
66 .getActualTypeArguments()[1]);
67 C config = (C) getConfigAs(clazz);
72 * Utility method to access the {@link UniFiController} instance associated with this thing.
76 @SuppressWarnings("null")
77 private final @Nullable UniFiController getController() {
78 Bridge bridge = getBridge();
79 if (bridge != null && bridge.getHandler() != null
80 && (bridge.getHandler() instanceof UniFiControllerThingHandler)) {
81 return ((UniFiControllerThingHandler) bridge.getHandler()).getController();
87 public final void handleCommand(ChannelUID channelUID, Command command) {
88 logger.debug("Handling command = {} for channel = {}", command, channelUID);
89 // mgb: only handle commands if we're ONLINE
90 if (getThing().getStatus() == ONLINE) {
91 UniFiController controller = getController();
92 if (controller != null) {
93 E entity = getEntity(controller);
95 if (command == REFRESH) {
96 refreshChannel(entity, channelUID);
99 handleCommand(entity, channelUID, command);
100 } catch (UniFiException e) {
101 logger.warn("Unexpected error handling command = {} for channel = {} : {}", command,
102 channelUID, e.getMessage());
110 protected final void refresh() {
111 // mgb: only refresh if we're ONLINE
112 if (getThing().getStatus() == ONLINE) {
113 UniFiController controller = getController();
114 if (controller != null) {
115 E entity = getEntity(controller);
116 if (entity != null) {
117 for (Channel channel : getThing().getChannels()) {
118 ChannelUID channelUID = channel.getUID();
119 refreshChannel(entity, channelUID);
126 protected abstract void initialize(@NonNull C config);
128 protected abstract @Nullable E getEntity(UniFiController controller);
130 protected abstract void refreshChannel(E entity, ChannelUID channelUID);
132 protected abstract void handleCommand(E entity, ChannelUID channelUID, Command command) throws UniFiException;