]> git.basschouten.com Git - openhab-addons.git/blob
882d97c7e6612329c97188265a1832054d2de67b
[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.dominoswiss.internal;
14
15 import static org.openhab.binding.dominoswiss.internal.DominoswissBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.thing.Bridge;
20 import org.openhab.core.thing.ChannelUID;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.ThingStatus;
23 import org.openhab.core.thing.ThingStatusDetail;
24 import org.openhab.core.thing.binding.BaseThingHandler;
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 BlindHandler} is responsible for handling commands, which are
32  * sent to one of the channels.The class defines common constants, which are
33  * used across the whole binding
34  *
35  * @author Frieso Aeschbacher - Initial contribution
36  */
37 @NonNullByDefault
38 public class BlindHandler extends BaseThingHandler {
39
40     private Logger logger = LoggerFactory.getLogger(BlindHandler.class);
41
42     private @Nullable EGateHandler dominoswissHandler;
43
44     private String id = "";
45
46     public BlindHandler(Thing thing) {
47         super(thing);
48     }
49
50     @Override
51     public void handleCommand(ChannelUID channelUID, Command command) {
52         logger.debug("Blind got command: {} and ChannelUID: {} ", command.toFullString(),
53                 channelUID.getIdWithoutGroup());
54         Bridge bridge = getBridge();
55         EGateHandler localDominoswissHandler = dominoswissHandler;
56         if (bridge != null) {
57             localDominoswissHandler = (EGateHandler) bridge.getHandler();
58         }
59         if (localDominoswissHandler == null) {
60             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, "EGate not available");
61             logger.debug("Blind thing {} has no server configured, ignoring command: {}", getThing().getUID(), command);
62             return;
63         }
64
65         // Some of the code below is not designed to handle REFRESH
66         if (command == RefreshType.REFRESH) {
67             return;
68         }
69         switch (channelUID.getIdWithoutGroup()) {
70             case CHANNEL_PULSEUP:
71                 if (command instanceof Number) {
72                     localDominoswissHandler.pulseUp(id);
73                 }
74                 break;
75             case CHANNEL_PULSEDOWN:
76                 if (command instanceof Number) {
77                     localDominoswissHandler.pulseDown(id);
78                 }
79                 break;
80             case CHANNEL_CONTINOUSUP:
81                 if (command instanceof Number) {
82                     localDominoswissHandler.continuousUp(id);
83                 }
84                 break;
85             case CHANNEL_CONTINOUSDOWN:
86                 if (command instanceof Number) {
87                     localDominoswissHandler.continuousDown(id);
88                 }
89                 break;
90             case CHANNEL_STOP:
91                 if (command instanceof Number) {
92                     localDominoswissHandler.stop(id);
93                 }
94                 break;
95             case UP:
96                 if (command instanceof Number) {
97                     localDominoswissHandler.continuousUp(id);
98                 }
99                 break;
100             case DOWN:
101                 if (command instanceof Number) {
102                     localDominoswissHandler.continuousDown(id);
103                 }
104                 break;
105             case SHUTTER:
106                 if (command.toFullString() == DOWN) {
107                     localDominoswissHandler.continuousDown(id);
108                 } else if (command.toFullString() == UP) {
109                     localDominoswissHandler.continuousUp(id);
110                 } else if (command.toFullString() == CHANNEL_STOP) {
111                     localDominoswissHandler.stop(id);
112                 } else {
113                     logger.debug("Blind got command but nothing executed: {}  and ChannelUID: {}",
114                             command.toFullString(), channelUID.getIdWithoutGroup());
115                 }
116
117             case TILTDOWN:
118                 if (command instanceof Number) {
119                     try {
120                         localDominoswissHandler.tiltDown(id);
121                     } catch (InterruptedException e) {
122                         logger.debug("EGate tiltDown error: {} ", e.toString());
123                     }
124                 }
125                 break;
126
127             case TILTUP:
128                 if (command instanceof Number) {
129                     try {
130                         localDominoswissHandler.tiltUp(id);
131                     } catch (InterruptedException e) {
132                         logger.debug("EGate tiltUP error: {} ", e.toString());
133                     }
134                 }
135                 break;
136
137             case SHUTTERTILT:
138                 if (command.toFullString() == UP) {
139                     localDominoswissHandler.pulseUp(id);
140                 } else if (command.toFullString() == DOWN) {
141                     localDominoswissHandler.pulseDown(id);
142                 } else if (command.toFullString() == CHANNEL_STOP) {
143                     localDominoswissHandler.stop(id);
144                 } else {
145                     logger.debug("Blind got command but nothing executed: {}  and ChannelUID: {}",
146                             command.toFullString(), channelUID.getIdWithoutGroup());
147                 }
148
149             default:
150                 break;
151         }
152     }
153
154     @Override
155     public void initialize() {
156         this.id = getConfig().as(BlindConfig.class).id;
157         Bridge bridge = getBridge();
158         if (bridge != null) {
159             dominoswissHandler = (EGateHandler) bridge.getHandler();
160             EGateHandler localDominoswissHandler = dominoswissHandler;
161             if (localDominoswissHandler != null) {
162                 localDominoswissHandler.registerBlind(this.id, getThing().getUID());
163                 try {
164                     ThingStatus bridgeStatus = bridge.getStatus();
165                     if (bridgeStatus == ThingStatus.ONLINE && getThing().getStatus() != ThingStatus.ONLINE) {
166                         updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
167                         localDominoswissHandler = (EGateHandler) bridge.getHandler();
168                     } else if (bridgeStatus == ThingStatus.OFFLINE) {
169                         updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
170                     }
171                 } catch (Exception e) {
172                     logger.debug("Could not update ThingStatus ", e);
173                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, e.toString());
174
175                 }
176             }
177         }
178     }
179
180     /*
181      * Gets the ID of this Blind
182      */
183     public String getID() {
184         return this.id;
185     }
186 }