2 * Copyright (c) 2010-2020 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.");
63 // mgb: derive the config class from the generic type
64 Class<?> clazz = (Class<?>) (((ParameterizedType) getClass().getGenericSuperclass())
65 .getActualTypeArguments()[1]);
66 C config = (C) getConfigAs(clazz);
71 * Utility method to access the {@link UniFiController} instance associated with this thing.
75 @SuppressWarnings("null")
76 private final @Nullable UniFiController getController() {
77 Bridge bridge = getBridge();
78 if (bridge != null && bridge.getHandler() != null
79 && (bridge.getHandler() instanceof UniFiControllerThingHandler)) {
80 return ((UniFiControllerThingHandler) bridge.getHandler()).getController();
86 public final void handleCommand(ChannelUID channelUID, Command command) {
87 logger.debug("Handling command = {} for channel = {}", command, channelUID);
88 // mgb: only handle commands if we're ONLINE
89 if (getThing().getStatus() == ONLINE) {
90 UniFiController controller = getController();
91 if (controller != null) {
92 E entity = getEntity(controller);
94 if (command == REFRESH) {
95 refreshChannel(entity, channelUID);
98 handleCommand(entity, channelUID, command);
99 } catch (UniFiException e) {
100 logger.warn("Unexpected error handling command = {} for channel = {} : {}", command,
101 channelUID, e.getMessage());
109 protected final void refresh() {
110 // mgb: only refresh if we're ONLINE
111 if (getThing().getStatus() == ONLINE) {
112 UniFiController controller = getController();
113 if (controller != null) {
114 E entity = getEntity(controller);
115 if (entity != null) {
116 for (Channel channel : getThing().getChannels()) {
117 ChannelUID channelUID = channel.getUID();
118 refreshChannel(entity, channelUID);
125 protected abstract void initialize(@NonNull C config);
127 protected abstract @Nullable E getEntity(UniFiController controller);
129 protected abstract void refreshChannel(E entity, ChannelUID channelUID);
131 protected abstract void handleCommand(E entity, ChannelUID channelUID, Command command) throws UniFiException;