]> git.basschouten.com Git - openhab-addons.git/blob
49aef16512c5c4e3790da6b9d834e718ff4cf896
[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.units;
14
15 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.omnilink.internal.handler.UnitHandler;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.ThingStatusDetail;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.RefreshType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.digitaldan.jomnilinkII.MessageTypes.CommandMessage;
32 import com.digitaldan.jomnilinkII.MessageTypes.statuses.ExtendedUnitStatus;
33
34 /**
35  * The {@link UpbRoomHandler} defines some methods that are used to
36  * interface with an OmniLink UPB Room. This by extension also defines the
37  * OmniPro UPB Room thing that openHAB will be able to pick up and interface with.
38  *
39  * @author Craig Hamilton - Initial contribution
40  * @author Ethan Dye - openHAB3 rewrite
41  */
42 @NonNullByDefault
43 public class UpbRoomHandler extends UnitHandler {
44     private final Logger logger = LoggerFactory.getLogger(UpbRoomHandler.class);
45     private final int thingID = getThingNumber();
46     public @Nullable String number;
47
48     public UpbRoomHandler(Thing thing) {
49         super(thing);
50     }
51
52     @Override
53     public void handleCommand(ChannelUID channelUID, Command command) {
54         logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
55
56         if (command instanceof RefreshType) {
57             retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
58                     ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null status update!"));
59             return;
60         }
61
62         switch (channelUID.getId()) {
63             case CHANNEL_ROOM_SCENE_A:
64             case CHANNEL_ROOM_SCENE_B:
65             case CHANNEL_ROOM_SCENE_C:
66             case CHANNEL_ROOM_SCENE_D:
67                 if (command instanceof OnOffType onOffCommand) {
68                     handleRoomScene(channelUID, onOffCommand);
69                 } else {
70                     logger.debug("Invalid command: {}, must be OnOffType", command);
71                 }
72                 break;
73             case CHANNEL_ROOM_SWITCH:
74                 if (command instanceof OnOffType onOffCommand) {
75                     super.handleOnOff(channelUID, onOffCommand);
76                 } else {
77                     logger.debug("Invalid command: {}, must be OnOffType", command);
78                 }
79                 break;
80             case CHANNEL_ROOM_STATE:
81                 if (command instanceof DecimalType decimalCommand) {
82                     handleRoomState(channelUID, decimalCommand);
83                 } else {
84                     logger.debug("Invalid command: {}, must be DecimalType", command);
85                 }
86                 break;
87             default:
88                 logger.debug("Unknown channel for UPB Room thing: {}", channelUID);
89                 super.handleCommand(channelUID, command);
90         }
91     }
92
93     private void handleRoomScene(ChannelUID channelUID, OnOffType command) {
94         logger.debug("handleRoomScene called for channel: {}, command: {}", channelUID, command);
95         int linkNum;
96
97         switch (channelUID.getId()) {
98             case "scene_a":
99                 linkNum = 0;
100                 break;
101             case "scene_b":
102                 linkNum = 1;
103                 break;
104             case "scene_c":
105                 linkNum = 2;
106                 break;
107             case "scene_d":
108                 linkNum = 3;
109                 break;
110             default:
111                 logger.warn("Unexpected UPB Room scene: {}", channelUID);
112                 return;
113         }
114         int roomNum = (thingID + 7) / 8;
115         int param2 = ((roomNum * 6) - 3) + linkNum;
116         sendOmnilinkCommand(OnOffType.ON.equals(command) ? CommandMessage.CMD_UNIT_UPB_LINK_ON
117                 : CommandMessage.CMD_UNIT_UPB_LINK_OFF, 0, param2);
118     }
119
120     private void handleRoomState(ChannelUID channelUID, DecimalType command) {
121         logger.debug("handleRoomState called for channel: {}, command: {}", channelUID, command);
122         final int cmdValue = command.intValue();
123         int cmd;
124         int param2;
125
126         switch (cmdValue) {
127             case 0:
128                 cmd = CommandMessage.CMD_UNIT_OFF;
129                 param2 = thingID;
130                 break;
131             case 1:
132                 cmd = CommandMessage.CMD_UNIT_ON;
133                 param2 = thingID;
134                 break;
135             case 2:
136             case 3:
137             case 4:
138             case 5:
139                 cmd = CommandMessage.CMD_UNIT_UPB_LINK_ON;
140                 /*
141                  * A little magic with the link #'s: 0 and 1 are off and on, respectively.
142                  * So A ends up being 2, but OmniLink Protocol expects an offset of 0. That
143                  * is why we subtract the 2.
144                  */
145                 int roomNum = (thingID + 7) / 8;
146                 param2 = ((roomNum * 6) - 3) + cmdValue - 2;
147                 break;
148             default:
149                 logger.warn("Unexpected UPB Room state: {}", Integer.toString(cmdValue));
150                 return;
151         }
152
153         sendOmnilinkCommand(cmd, 0, param2);
154     }
155
156     @Override
157     public void updateChannels(ExtendedUnitStatus status) {
158         logger.debug("updateChannels called for UPB Room status: {}", status);
159         int unitStatus = status.getStatus();
160
161         updateState(CHANNEL_ROOM_STATE, new DecimalType(unitStatus));
162         updateState(CHANNEL_ROOM_SWITCH, OnOffType.from(unitStatus == 1));
163         updateState(CHANNEL_ROOM_SCENE_A, OnOffType.from(unitStatus == 2));
164         updateState(CHANNEL_ROOM_SCENE_B, OnOffType.from(unitStatus == 3));
165         updateState(CHANNEL_ROOM_SCENE_C, OnOffType.from(unitStatus == 4));
166         updateState(CHANNEL_ROOM_SCENE_D, OnOffType.from(unitStatus == 5));
167     }
168 }