]> git.basschouten.com Git - openhab-addons.git/blob
5a7cdbaf969643be106718c4374ac41e814c0632
[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.nikobus.internal.handler;
14
15 import static org.openhab.binding.nikobus.internal.NikobusBindingConstants.*;
16 import static org.openhab.binding.nikobus.internal.protocol.SwitchModuleGroup.*;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.concurrent.Future;
22 import java.util.concurrent.TimeUnit;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.nikobus.internal.protocol.NikobusCommand;
27 import org.openhab.binding.nikobus.internal.protocol.SwitchModuleGroup;
28 import org.openhab.binding.nikobus.internal.utils.Utils;
29 import org.openhab.core.common.AbstractUID;
30 import org.openhab.core.library.types.OnOffType;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.ThingUID;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.types.Command;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * The {@link NikobusPushButtonHandler} is responsible for handling Nikobus push buttons.
45  *
46  * @author Boris Krivonog - Initial contribution
47  */
48 @NonNullByDefault
49 public class NikobusPushButtonHandler extends NikobusBaseThingHandler {
50     private static class ImpactedModule {
51         private final ThingUID thingUID;
52         private final SwitchModuleGroup group;
53
54         ImpactedModule(ThingUID thingUID, SwitchModuleGroup group) {
55             this.thingUID = thingUID;
56             this.group = group;
57         }
58
59         public ThingUID getThingUID() {
60             return thingUID;
61         }
62
63         public SwitchModuleGroup getGroup() {
64             return group;
65         }
66
67         @Override
68         public String toString() {
69             return "'" + thingUID + "'-" + group;
70         }
71     }
72
73     private static class ImpactedModuleUID extends AbstractUID {
74         ImpactedModuleUID(String uid) {
75             super(uid);
76         }
77
78         String getThingTypeId() {
79             return getSegment(0);
80         }
81
82         String getThingId() {
83             return getSegment(1);
84         }
85
86         SwitchModuleGroup getGroup() {
87             if (getSegment(2).equals("1")) {
88                 return FIRST;
89             }
90             if (getSegment(2).equals("2")) {
91                 return SECOND;
92             }
93             throw new IllegalArgumentException("Unexpected group found " + getSegment(2));
94         }
95
96         @Override
97         protected int getMinimalNumberOfSegments() {
98             return 3;
99         }
100     }
101
102     private static final String END_OF_TRANSMISSION = "\r#E1";
103     private final Logger logger = LoggerFactory.getLogger(NikobusPushButtonHandler.class);
104     private final List<ImpactedModule> impactedModules = Collections.synchronizedList(new ArrayList<>());
105     private @Nullable Future<?> requestUpdateFuture;
106
107     public NikobusPushButtonHandler(Thing thing) {
108         super(thing);
109     }
110
111     @Override
112     public void initialize() {
113         super.initialize();
114
115         if (thing.getStatus() == ThingStatus.OFFLINE) {
116             return;
117         }
118
119         impactedModules.clear();
120
121         try {
122             ThingUID bridgeUID = thing.getBridgeUID();
123             if (bridgeUID == null) {
124                 throw new IllegalArgumentException("Bridge does not exist!");
125             }
126
127             String[] impactedModulesString = getConfig().get(CONFIG_IMPACTED_MODULES).toString().split(",");
128             for (String impactedModuleString : impactedModulesString) {
129                 ImpactedModuleUID impactedModuleUID = new ImpactedModuleUID(impactedModuleString.trim());
130                 ThingTypeUID thingTypeUID = new ThingTypeUID(bridgeUID.getBindingId(),
131                         impactedModuleUID.getThingTypeId());
132                 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, impactedModuleUID.getThingId());
133                 impactedModules.add(new ImpactedModule(thingUID, impactedModuleUID.getGroup()));
134             }
135         } catch (RuntimeException e) {
136             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
137             return;
138         }
139
140         logger.debug("Impacted modules for {} = {}", thing.getUID(), impactedModules);
141
142         NikobusPcLinkHandler pcLink = getPcLink();
143         if (pcLink != null) {
144             pcLink.addListener(getAddress(), this::commandReceived);
145         }
146     }
147
148     @Override
149     public void dispose() {
150         super.dispose();
151
152         Utils.cancel(requestUpdateFuture);
153         requestUpdateFuture = null;
154
155         NikobusPcLinkHandler pcLink = getPcLink();
156         if (pcLink != null) {
157             pcLink.removeListener(getAddress());
158         }
159     }
160
161     @Override
162     public void handleCommand(ChannelUID channelUID, Command command) {
163         logger.debug("handleCommand '{}' '{}'", channelUID, command);
164
165         if (!CHANNEL_BUTTON.equals(channelUID.getId())) {
166             return;
167         }
168
169         // Whenever the button receives an ON command,
170         // we send a simulated button press to the Nikobus.
171         if (command == OnOffType.ON) {
172             NikobusPcLinkHandler pcLink = getPcLink();
173             if (pcLink != null) {
174                 pcLink.sendCommand(new NikobusCommand(getAddress() + END_OF_TRANSMISSION));
175             }
176         }
177     }
178
179     private void commandReceived() {
180         if (thing.getStatus() != ThingStatus.ONLINE) {
181             updateStatus(ThingStatus.ONLINE);
182         }
183
184         updateState(CHANNEL_BUTTON, OnOffType.ON);
185
186         Utils.cancel(requestUpdateFuture);
187         requestUpdateFuture = scheduler.schedule(this::update, 400, TimeUnit.MILLISECONDS);
188     }
189
190     private void update() {
191         for (ImpactedModule module : impactedModules) {
192             NikobusModuleHandler switchModule = getModuleWithId(module.getThingUID());
193             if (switchModule != null) {
194                 switchModule.requestStatus(module.getGroup());
195             }
196         }
197     }
198
199     private @Nullable NikobusModuleHandler getModuleWithId(ThingUID thingUID) {
200         Bridge bridge = getBridge();
201         if (bridge == null) {
202             return null;
203         }
204
205         Thing thing = bridge.getThing(thingUID);
206         if (thing == null) {
207             return null;
208         }
209
210         ThingHandler thingHandler = thing.getHandler();
211         if (thingHandler instanceof NikobusModuleHandler) {
212             return (NikobusModuleHandler) thingHandler;
213         }
214         return null;
215     }
216
217     @Override
218     protected String getAddress() {
219         return "#N" + super.getAddress();
220     }
221 }