]> git.basschouten.com Git - openhab-addons.git/blob
cc8c408c07e10b65f6ab85ddd4e3c0dbc2d8fa61
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.unifi.internal.handler;
14
15 import static org.openhab.core.thing.ThingStatus.*;
16 import static org.openhab.core.types.RefreshType.REFRESH;
17
18 import java.lang.reflect.ParameterizedType;
19
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;
34
35 /**
36  * @author Matthew Bowman - Initial contribution
37  *
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
40  */
41 @NonNullByDefault
42 public abstract class UniFiBaseThingHandler<E, C> extends BaseThingHandler {
43
44     private final Logger logger = LoggerFactory.getLogger(UniFiBaseThingHandler.class);
45
46     public UniFiBaseThingHandler(Thing thing) {
47         super(thing);
48     }
49
50     @Override
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.");
58             return;
59         }
60         if (bridge.getStatus() == OFFLINE) {
61             updateStatus(OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, "The UniFi Controller is currently offline.");
62         }
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);
67         initialize(config);
68     }
69
70     /**
71      * Utility method to access the {@link UniFiController} instance associated with this thing.
72      *
73      * @return
74      */
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();
81         }
82         return null;
83     }
84
85     @Override
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);
93                 if (entity != null) {
94                     if (command == REFRESH) {
95                         refreshChannel(entity, channelUID);
96                     } else {
97                         try {
98                             handleCommand(entity, channelUID, command);
99                         } catch (UniFiException e) {
100                             logger.warn("Unexpected error handling command = {} for channel = {} : {}", command,
101                                     channelUID, e.getMessage());
102                         }
103                     }
104                 }
105             }
106         }
107     }
108
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);
119                     }
120                 }
121             }
122         }
123     }
124
125     protected abstract void initialize(@NonNull C config);
126
127     protected abstract @Nullable E getEntity(UniFiController controller);
128
129     protected abstract void refreshChannel(E entity, ChannelUID channelUID);
130
131     protected abstract void handleCommand(E entity, ChannelUID channelUID, Command command) throws UniFiException;
132 }