2 * Copyright (c) 2010-2020 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.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.concurrent.Future;
22 import java.util.concurrent.TimeUnit;
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;
44 * The {@link NikobusPushButtonHandler} is responsible for handling Nikobus push buttons.
46 * @author Boris Krivonog - Initial contribution
49 public class NikobusPushButtonHandler extends NikobusBaseThingHandler {
50 private static class ImpactedModule {
51 private final ThingUID thingUID;
52 private final SwitchModuleGroup group;
54 ImpactedModule(ThingUID thingUID, SwitchModuleGroup group) {
55 this.thingUID = thingUID;
59 public ThingUID getThingUID() {
63 public SwitchModuleGroup getGroup() {
68 public String toString() {
69 return "'" + thingUID + "'-" + group;
73 private static class ImpactedModuleUID extends AbstractUID {
74 ImpactedModuleUID(String uid) {
78 String getThingTypeId() {
86 SwitchModuleGroup getGroup() {
87 if (getSegment(2).equals("1")) {
90 if (getSegment(2).equals("2")) {
93 throw new IllegalArgumentException("Unexpected group found " + getSegment(2));
97 protected int getMinimalNumberOfSegments() {
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;
107 public NikobusPushButtonHandler(Thing thing) {
112 public void initialize() {
115 if (thing.getStatus() == ThingStatus.OFFLINE) {
119 impactedModules.clear();
122 ThingUID bridgeUID = thing.getBridgeUID();
123 if (bridgeUID == null) {
124 throw new IllegalArgumentException("Bridge does not exist!");
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()));
135 } catch (RuntimeException e) {
136 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
140 logger.debug("Impacted modules for {} = {}", thing.getUID(), impactedModules);
142 NikobusPcLinkHandler pcLink = getPcLink();
143 if (pcLink != null) {
144 pcLink.addListener(getAddress(), this::commandReceived);
149 public void dispose() {
152 Utils.cancel(requestUpdateFuture);
153 requestUpdateFuture = null;
155 NikobusPcLinkHandler pcLink = getPcLink();
156 if (pcLink != null) {
157 pcLink.removeListener(getAddress());
162 public void handleCommand(ChannelUID channelUID, Command command) {
163 logger.debug("handleCommand '{}' '{}'", channelUID, command);
165 if (!CHANNEL_BUTTON.equals(channelUID.getId())) {
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));
179 private void commandReceived() {
180 if (thing.getStatus() != ThingStatus.ONLINE) {
181 updateStatus(ThingStatus.ONLINE);
184 updateState(CHANNEL_BUTTON, OnOffType.ON);
186 Utils.cancel(requestUpdateFuture);
187 requestUpdateFuture = scheduler.schedule(this::update, 400, TimeUnit.MILLISECONDS);
190 private void update() {
191 for (ImpactedModule module : impactedModules) {
192 NikobusModuleHandler switchModule = getModuleWithId(module.getThingUID());
193 if (switchModule != null) {
194 switchModule.requestStatus(module.getGroup());
199 private @Nullable NikobusModuleHandler getModuleWithId(ThingUID thingUID) {
200 Bridge bridge = getBridge();
201 if (bridge == null) {
205 Thing thing = bridge.getThing(thingUID);
210 ThingHandler thingHandler = thing.getHandler();
211 if (thingHandler instanceof NikobusModuleHandler) {
212 return (NikobusModuleHandler) thingHandler;
218 protected String getAddress() {
219 return "#N" + super.getAddress();