]> git.basschouten.com Git - openhab-addons.git/blob
a288fa00a88f4b6c9c04d3014bd55e6ad4b9c68e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.omnilink.internal.handler;
14
15 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.*;
16
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequest;
23 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequests;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.RefreshType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.digitaldan.jomnilinkII.MessageTypes.properties.AreaProperties;
35 import com.digitaldan.jomnilinkII.MessageTypes.properties.ButtonProperties;
36
37 /**
38  * The {@link ButtonHandler} defines some methods that are used to
39  * interface with an OmniLink Button. This by extension also defines the
40  * Button thing that openHAB will be able to pick up and interface with.
41  *
42  * @author Craig Hamilton - Initial contribution
43  * @author Ethan Dye - openHAB3 rewrite
44  */
45 @NonNullByDefault
46 public class ButtonHandler extends AbstractOmnilinkHandler {
47     private final Logger logger = LoggerFactory.getLogger(ButtonHandler.class);
48     private final int thingID = getThingNumber();
49     public @Nullable String number;
50
51     public ButtonHandler(Thing thing) {
52         super(thing);
53     }
54
55     @Override
56     public void initialize() {
57         final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
58         if (bridgeHandler != null) {
59             updateStatus(ThingStatus.ONLINE);
60             updateChannels();
61             updateButtonProperties(bridgeHandler);
62         } else {
63             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
64                     "Received null bridge while initializing Button!");
65         }
66     }
67
68     private void updateButtonProperties(OmnilinkBridgeHandler bridgeHandler) {
69         final List<AreaProperties> areas = getAreaProperties();
70         if (areas != null) {
71             for (AreaProperties areaProperties : areas) {
72                 int areaFilter = bitFilterForArea(areaProperties);
73
74                 ObjectPropertyRequest<ButtonProperties> objectPropertyRequest = ObjectPropertyRequest
75                         .builder(bridgeHandler, ObjectPropertyRequests.BUTTONS, thingID, 0).selectNamed()
76                         .areaFilter(areaFilter).build();
77
78                 for (ButtonProperties buttonProperties : objectPropertyRequest) {
79                     Map<String, String> properties = editProperties();
80                     properties.put(THING_PROPERTIES_NAME, buttonProperties.getName());
81                     properties.put(THING_PROPERTIES_AREA, Integer.toString(areaProperties.getNumber()));
82                     updateProperties(properties);
83                 }
84             }
85         }
86     }
87
88     @Override
89     public void handleCommand(ChannelUID channelUID, Command command) {
90         logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
91
92         if (command instanceof RefreshType) {
93             updateChannels();
94             return;
95         }
96
97         switch (channelUID.getId()) {
98             case CHANNEL_BUTTON_PRESS:
99                 if (command instanceof OnOffType) {
100                     sendOmnilinkCommand(OmniLinkCmd.CMD_BUTTON.getNumber(), 0, thingID);
101                     updateChannels();
102                 } else {
103                     logger.debug("Invalid command: {}, must be OnOffType", command);
104                 }
105                 break;
106             default:
107                 logger.warn("Unknown channel for Button thing: {}", channelUID);
108         }
109     }
110
111     public void buttonActivated() {
112         ChannelUID activateChannel = new ChannelUID(getThing().getUID(), TRIGGER_CHANNEL_BUTTON_ACTIVATED_EVENT);
113         triggerChannel(activateChannel);
114     }
115
116     public void updateChannels() {
117         updateState(CHANNEL_BUTTON_PRESS, OnOffType.OFF);
118     }
119 }