]> git.basschouten.com Git - openhab-addons.git/blob
b9c1a5664419c518810945ffa73bc729a119f9c3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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             return;
63         }
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);
68         initialize(config);
69     }
70
71     /**
72      * Utility method to access the {@link UniFiController} instance associated with this thing.
73      *
74      * @return
75      */
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();
82         }
83         return null;
84     }
85
86     @Override
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);
94                 if (entity != null) {
95                     if (command == REFRESH) {
96                         refreshChannel(entity, channelUID);
97                     } else {
98                         try {
99                             handleCommand(entity, channelUID, command);
100                         } catch (UniFiException e) {
101                             logger.warn("Unexpected error handling command = {} for channel = {} : {}", command,
102                                     channelUID, e.getMessage());
103                         }
104                     }
105                 }
106             }
107         }
108     }
109
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);
120                     }
121                 }
122             }
123         }
124     }
125
126     protected abstract void initialize(@NonNull C config);
127
128     protected abstract @Nullable E getEntity(UniFiController controller);
129
130     protected abstract void refreshChannel(E entity, ChannelUID channelUID);
131
132     protected abstract void handleCommand(E entity, ChannelUID channelUID, Command command) throws UniFiException;
133 }