]> git.basschouten.com Git - openhab-addons.git/blob
aeb1e4845a1fe58efd94a6bd62218d2c908492c7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.pilight.internal.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.pilight.internal.PilightDeviceConfiguration;
18 import org.openhab.binding.pilight.internal.dto.Action;
19 import org.openhab.binding.pilight.internal.dto.Config;
20 import org.openhab.binding.pilight.internal.dto.Device;
21 import org.openhab.binding.pilight.internal.dto.Status;
22 import org.openhab.core.thing.*;
23 import org.openhab.core.thing.binding.BaseThingHandler;
24 import org.openhab.core.thing.binding.BridgeHandler;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.RefreshType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link PilightBaseHandler} is responsible for handling commands, which are
32  * sent to one of the channels.
33  *
34  * @author Stefan Röllin - Initial contribution
35  * @author Niklas Dörfler - Port pilight binding to openHAB 3 + add device discovery
36  */
37 @NonNullByDefault
38 public abstract class PilightBaseHandler extends BaseThingHandler {
39
40     private final Logger logger = LoggerFactory.getLogger(PilightBaseHandler.class);
41
42     private String name = "";
43
44     public PilightBaseHandler(Thing thing) {
45         super(thing);
46     }
47
48     @Override
49     public void handleCommand(ChannelUID channelUID, Command command) {
50         if (command instanceof RefreshType) {
51             refreshConfigAndStatus();
52             return;
53         }
54
55         @Nullable
56         Action action = createUpdateCommand(channelUID, command);
57         if (action != null) {
58             sendAction(action);
59         }
60     }
61
62     @Override
63     public void initialize() {
64         PilightDeviceConfiguration config = getConfigAs(PilightDeviceConfiguration.class);
65         name = config.getName();
66
67         refreshConfigAndStatus();
68     }
69
70     public void updateFromStatusIfMatches(Status status) {
71         if (status.getDevices() != null && !status.getDevices().isEmpty()) {
72             if (name.equals(status.getDevices().get(0))) {
73                 if (!ThingStatus.ONLINE.equals(getThing().getStatus())) {
74                     updateStatus(ThingStatus.ONLINE);
75                 }
76                 updateFromStatus(status);
77             }
78         }
79     }
80
81     public void updateFromConfigIfMatches(Config config) {
82         Device device = config.getDevices().get(getName());
83         if (device != null) {
84             updateFromConfigDevice(device);
85         }
86     }
87
88     abstract void updateFromStatus(Status status);
89
90     abstract void updateFromConfigDevice(Device device);
91
92     abstract @Nullable Action createUpdateCommand(ChannelUID channelUID, Command command);
93
94     protected String getName() {
95         return name;
96     }
97
98     private void sendAction(Action action) {
99         final @Nullable PilightBridgeHandler handler = getPilightBridgeHandler();
100         if (handler != null) {
101             handler.sendAction(action);
102         } else {
103             logger.warn("No pilight bridge handler found to send action.");
104         }
105     }
106
107     private void refreshConfigAndStatus() {
108         final @Nullable PilightBridgeHandler handler = getPilightBridgeHandler();
109         if (handler != null) {
110             handler.refreshConfigAndStatus();
111         } else {
112             logger.warn("No pilight bridge handler found to refresh config and status.");
113         }
114     }
115
116     private @Nullable PilightBridgeHandler getPilightBridgeHandler() {
117         final @Nullable Bridge bridge = getBridge();
118         if (bridge != null) {
119             @Nullable
120             BridgeHandler handler = bridge.getHandler();
121             if (handler instanceof PilightBridgeHandler) {
122                 return (PilightBridgeHandler) handler;
123             }
124         }
125         return null;
126     }
127 }