2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nikobus.internal.handler;
15 import static org.openhab.binding.nikobus.internal.NikobusBindingConstants.*;
16 import static org.openhab.binding.nikobus.internal.protocol.SwitchModuleGroup.*;
18 import java.util.List;
19 import java.util.concurrent.CopyOnWriteArrayList;
20 import java.util.concurrent.Future;
21 import java.util.concurrent.TimeUnit;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.nikobus.internal.protocol.NikobusCommand;
26 import org.openhab.binding.nikobus.internal.protocol.SwitchModuleGroup;
27 import org.openhab.binding.nikobus.internal.utils.Utils;
28 import org.openhab.core.common.AbstractUID;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Channel;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.CommonTriggerEvents;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingStatus;
36 import org.openhab.core.thing.ThingStatusDetail;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.ThingUID;
39 import org.openhab.core.thing.binding.ThingHandler;
40 import org.openhab.core.thing.type.ChannelTypeUID;
41 import org.openhab.core.types.Command;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * The {@link NikobusPushButtonHandler} is responsible for handling Nikobus push buttons.
48 * @author Boris Krivonog - Initial contribution
51 public class NikobusPushButtonHandler extends NikobusBaseThingHandler {
52 private static final String END_OF_TRANSMISSION = "\r#E1";
53 private final Logger logger = LoggerFactory.getLogger(NikobusPushButtonHandler.class);
54 private final List<ImpactedModule> impactedModules = new CopyOnWriteArrayList<>();
55 private final List<TriggerProcessor> triggerProcessors = new CopyOnWriteArrayList<>();
56 private @Nullable Future<?> requestUpdateFuture;
58 public NikobusPushButtonHandler(Thing thing) {
63 public void initialize() {
66 if (thing.getStatus() == ThingStatus.OFFLINE) {
70 impactedModules.clear();
71 triggerProcessors.clear();
73 Object impactedModulesObject = getConfig().get(CONFIG_IMPACTED_MODULES);
74 if (impactedModulesObject != null) {
76 Bridge bridge = getBridge();
78 throw new IllegalArgumentException("Bridge does not exist!");
81 ThingUID bridgeUID = thing.getBridgeUID();
82 if (bridgeUID == null) {
83 throw new IllegalArgumentException("Unable to read BridgeUID!");
86 String[] impactedModulesString = impactedModulesObject.toString().split(",");
87 for (String impactedModuleString : impactedModulesString) {
88 ImpactedModuleUID impactedModuleUID = new ImpactedModuleUID(impactedModuleString.trim());
89 ThingTypeUID thingTypeUID = new ThingTypeUID(bridgeUID.getBindingId(),
90 impactedModuleUID.getThingTypeId());
91 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, impactedModuleUID.getThingId());
93 if (!bridge.getThings().stream().anyMatch(thing -> thing.getUID().equals(thingUID))) {
94 throw new IllegalArgumentException(
95 "Impacted module " + thingUID + " not found for '" + impactedModuleString + "'");
98 impactedModules.add(new ImpactedModule(thingUID, impactedModuleUID.getGroup()));
100 } catch (RuntimeException e) {
101 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
105 logger.debug("Impacted modules for {} = {}", thing.getUID(), impactedModules);
108 for (Channel channel : thing.getChannels()) {
109 TriggerProcessor processor = createTriggerProcessor(channel);
110 if (processor != null) {
111 triggerProcessors.add(processor);
115 logger.debug("Trigger channels for {} = {}", thing.getUID(), triggerProcessors);
117 NikobusPcLinkHandler pcLink = getPcLink();
118 if (pcLink != null) {
119 pcLink.addListener(getAddress(), this::commandReceived);
124 public void dispose() {
127 Utils.cancel(requestUpdateFuture);
128 requestUpdateFuture = null;
130 NikobusPcLinkHandler pcLink = getPcLink();
131 if (pcLink != null) {
132 pcLink.removeListener(getAddress());
137 public void handleCommand(ChannelUID channelUID, Command command) {
138 logger.debug("handleCommand '{}' '{}'", channelUID, command);
140 if (!CHANNEL_BUTTON.equals(channelUID.getId())) {
144 // Whenever the button receives an ON command,
145 // we send a simulated button press to the Nikobus.
146 if (command == OnOffType.ON) {
147 NikobusPcLinkHandler pcLink = getPcLink();
148 if (pcLink != null) {
149 pcLink.sendCommand(new NikobusCommand(getAddress() + END_OF_TRANSMISSION));
154 private void commandReceived() {
155 if (thing.getStatus() != ThingStatus.ONLINE) {
156 updateStatus(ThingStatus.ONLINE);
159 updateState(CHANNEL_BUTTON, OnOffType.ON);
161 if (!triggerProcessors.isEmpty()) {
162 long currentTimeMillis = System.currentTimeMillis();
163 triggerProcessors.forEach(processor -> processor.process(currentTimeMillis));
166 if (!impactedModules.isEmpty()) {
167 Utils.cancel(requestUpdateFuture);
168 requestUpdateFuture = scheduler.schedule(this::update, 400, TimeUnit.MILLISECONDS);
172 private void update() {
173 for (ImpactedModule module : impactedModules) {
174 NikobusModuleHandler switchModule = getModuleWithId(module.getThingUID());
175 if (switchModule != null) {
176 switchModule.requestStatus(module.getGroup());
181 private @Nullable NikobusModuleHandler getModuleWithId(ThingUID thingUID) {
182 Bridge bridge = getBridge();
183 if (bridge == null) {
187 Thing thing = bridge.getThing(thingUID);
192 ThingHandler thingHandler = thing.getHandler();
193 if (thingHandler instanceof NikobusModuleHandler) {
194 return (NikobusModuleHandler) thingHandler;
200 protected String getAddress() {
201 return "#N" + super.getAddress();
204 private @Nullable TriggerProcessor createTriggerProcessor(Channel channel) {
205 ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
206 if (channelTypeUID != null) {
207 switch (channelTypeUID.getId()) {
208 case CHANNEL_TRIGGER_FILTER:
209 return new TriggerFilter(channel);
210 case CHANNEL_TRIGGER_BUTTON:
211 return new TriggerButton(channel);
217 private static class ImpactedModule {
218 private final ThingUID thingUID;
219 private final SwitchModuleGroup group;
221 ImpactedModule(ThingUID thingUID, SwitchModuleGroup group) {
222 this.thingUID = thingUID;
226 public ThingUID getThingUID() {
230 public SwitchModuleGroup getGroup() {
235 public String toString() {
236 return "'" + thingUID + "'-" + group;
240 private static class ImpactedModuleUID extends AbstractUID {
241 ImpactedModuleUID(String uid) {
245 String getThingTypeId() {
246 return getSegment(0);
249 String getThingId() {
250 return getSegment(1);
253 SwitchModuleGroup getGroup() {
254 if (getSegment(2).equals("1")) {
257 if (getSegment(2).equals("2")) {
260 throw new IllegalArgumentException("Unexpected group found " + getSegment(2));
264 protected int getMinimalNumberOfSegments() {
269 private interface TriggerProcessor {
270 void process(long currentTimeMillis);
273 private abstract class AbstractTriggerProcessor<Config> implements TriggerProcessor {
274 private long lastCommandReceivedTimestamp = 0;
275 protected final ChannelUID channelUID;
276 protected final Config config;
278 // Nikobus push button will send a new message on bus every ~50ms so
279 // lets assume if we haven't received a new message in over 150ms that
280 // button was released and pressed again.
281 protected static final long BUTTON_RELEASED_MILIS = 150;
283 protected AbstractTriggerProcessor(Class<Config> configType, Channel channel) {
284 this.channelUID = channel.getUID();
285 this.config = channel.getConfiguration().as(configType);
289 public void process(long currentTimeMillis) {
290 if (Math.abs(currentTimeMillis - lastCommandReceivedTimestamp) > BUTTON_RELEASED_MILIS) {
291 reset(currentTimeMillis);
293 lastCommandReceivedTimestamp = currentTimeMillis;
294 processNext(currentTimeMillis);
297 abstract protected void reset(long currentTimeMillis);
299 abstract protected void processNext(long currentTimeMillis);
302 public static class TriggerButtonConfig {
303 public int threshold = 1000;
306 private class TriggerButton extends AbstractTriggerProcessor<TriggerButtonConfig> {
307 private long nextLongPressTimestamp = 0;
308 private @Nullable Future<?> triggerShortPressFuture;
310 TriggerButton(Channel channel) {
311 super(TriggerButtonConfig.class, channel);
315 protected void reset(long currentTimeMillis) {
316 nextLongPressTimestamp = currentTimeMillis + config.threshold;
320 protected void processNext(long currentTimeMillis) {
321 if (currentTimeMillis < nextLongPressTimestamp) {
322 Utils.cancel(triggerShortPressFuture);
323 triggerShortPressFuture = scheduler.schedule(
324 () -> triggerChannel(channelUID, CommonTriggerEvents.SHORT_PRESSED), BUTTON_RELEASED_MILIS,
325 TimeUnit.MILLISECONDS);
326 } else if (nextLongPressTimestamp != 0) {
327 Utils.cancel(triggerShortPressFuture);
328 nextLongPressTimestamp = 0;
329 triggerChannel(channelUID, CommonTriggerEvents.LONG_PRESSED);
334 public String toString() {
335 return "TriggerButton '" + channelUID + "', config: threshold = " + config.threshold;
339 public static class TriggerFilterConfig {
340 public @Nullable String command;
341 public int delay = 0;
342 public int period = -1;
345 private class TriggerFilter extends AbstractTriggerProcessor<TriggerFilterConfig> {
346 private long nextTriggerTimestamp = 0;
348 TriggerFilter(Channel channel) {
349 super(TriggerFilterConfig.class, channel);
353 protected void reset(long currentTimeMillis) {
354 nextTriggerTimestamp = currentTimeMillis + config.delay;
358 protected void processNext(long currentTimeMillis) {
359 if (currentTimeMillis >= nextTriggerTimestamp) {
360 nextTriggerTimestamp = (config.period < 0) ? Long.MAX_VALUE : currentTimeMillis + config.period;
361 String command = config.command;
362 if (command != null) {
363 triggerChannel(channelUID, command);
365 triggerChannel(channelUID);
371 public String toString() {
372 return "TriggerFilter '" + channelUID + "', config: command = '" + config.command + "', delay = "
373 + config.delay + ", period = " + config.period;