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