]> git.basschouten.com Git - openhab-addons.git/blob
c6609498ce76eab6913f2f02f2fcaa1366f8a8ab
[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.CHANNEL_UNIT_LEVEL;
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.IncreaseDecreaseType;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.library.types.PercentType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.types.Command;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import com.digitaldan.jomnilinkII.MessageTypes.CommandMessage;
30
31 /**
32  * The {@link DimmableUnitHandler} defines some methods that are used to
33  * interface with an OmniLink Dimmable Unit. This by extension also defines the
34  * Dimmable Unit things that openHAB will be able to pick up and interface with.
35  *
36  * @author Brian O'Connell - Initial contribution
37  * @author Ethan Dye - openHAB3 rewrite
38  */
39 @NonNullByDefault
40 public class DimmableUnitHandler extends UnitHandler {
41     private final Logger logger = LoggerFactory.getLogger(DimmableUnitHandler.class);
42     private final int thingID = getThingNumber();
43     public @Nullable String number;
44
45     public DimmableUnitHandler(Thing thing) {
46         super(thing);
47     }
48
49     @Override
50     public void handleCommand(ChannelUID channelUID, Command command) {
51         logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
52         switch (channelUID.getId()) {
53             case CHANNEL_UNIT_LEVEL:
54                 handleUnitLevel(channelUID, command);
55                 break;
56             default:
57                 logger.debug("Unknown channel for Dimmable Unit thing: {}", channelUID);
58                 super.handleCommand(channelUID, command);
59         }
60     }
61
62     private void handleUnitLevel(ChannelUID channelUID, Command command) {
63         logger.debug("handleUnitLevel called for channel: {}, command: {}", channelUID, command);
64         if (command instanceof PercentType) {
65             handlePercent(channelUID, (PercentType) command);
66         } else if (command instanceof IncreaseDecreaseType) {
67             handleIncreaseDecrease(channelUID, (IncreaseDecreaseType) command);
68         } else {
69             // Only handle percent or increase/decrease.
70             super.handleCommand(channelUID, command);
71         }
72     }
73
74     private void handlePercent(ChannelUID channelUID, PercentType command) {
75         logger.debug("handlePercent called for channel: {}, command: {}", channelUID, command);
76         int lightLevel = command.intValue();
77
78         if (lightLevel == 0) {
79             super.handleOnOff(channelUID, OnOffType.OFF);
80         } else if (lightLevel == 100) {
81             super.handleOnOff(channelUID, OnOffType.ON);
82         } else {
83             sendOmnilinkCommand(CommandMessage.CMD_UNIT_PERCENT, lightLevel, thingID);
84         }
85     }
86
87     private void handleIncreaseDecrease(ChannelUID channelUID, IncreaseDecreaseType command) {
88         logger.debug("handleIncreaseDecrease called for channel: {}, command: {}", channelUID, command);
89         sendOmnilinkCommand(IncreaseDecreaseType.INCREASE.equals(command) ? CommandMessage.CMD_UNIT_UPB_BRIGHTEN_STEP_1
90                 : CommandMessage.CMD_UNIT_UPB_DIM_STEP_1, 0, thingID);
91     }
92 }