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