]> git.basschouten.com Git - openhab-addons.git/blob
4ec91c0041f885db1dfe30368ae1c3a2700f5ec6
[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.luxom.internal.handler;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.concurrent.atomic.AtomicReference;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.luxom.internal.LuxomBindingConstants;
22 import org.openhab.binding.luxom.internal.handler.config.LuxomThingDimmerConfig;
23 import org.openhab.binding.luxom.internal.handler.util.PercentageConverter;
24 import org.openhab.binding.luxom.internal.protocol.LuxomAction;
25 import org.openhab.binding.luxom.internal.protocol.LuxomCommand;
26 import org.openhab.core.library.types.IncreaseDecreaseType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.PercentType;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingStatusDetail;
34 import org.openhab.core.types.Command;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * The {@link LuxomDimmerHandler} is responsible for handling commands, which are
40  * sent to one of the channels.
41  *
42  * @author Kris Jespers - Initial contribution
43  */
44 @NonNullByDefault
45 public class LuxomDimmerHandler extends LuxomThingHandler {
46     private final Logger logger = LoggerFactory.getLogger(LuxomDimmerHandler.class);
47
48     public LuxomDimmerHandler(Thing thing) {
49         super(thing);
50     }
51
52     private @Nullable LuxomThingDimmerConfig config;
53     private final AtomicReference<Integer> lastLightLevel = new AtomicReference<>(0);
54
55     @Override
56     public void initialize() {
57         super.initialize();
58         config = getConfig().as(LuxomThingDimmerConfig.class);
59
60         logger.debug("Initializing Switch handler for address {}", getAddress());
61
62         initDeviceState();
63     }
64
65     @Override
66     protected void initDeviceState() {
67         logger.debug("Initializing device state for Switch {}", getAddress());
68         @Nullable
69         Bridge bridge = getBridge();
70         if (bridge == null) {
71             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
72         } else if (ThingStatus.ONLINE.equals(bridge.getStatus())) {
73             if (config != null && config.doesNotReply) {
74                 logger.debug("Switch {} will not reply, so always keeping it ONLINE", getAddress());
75                 updateStatus(ThingStatus.ONLINE);
76             } else {
77                 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "@text/status.awaiting-initial-response");
78                 ping(); // handleUpdate() will set thing status to online when response arrives
79             }
80         } else {
81             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
82         }
83     }
84
85     @Override
86     public void handleCommand(ChannelUID channelUID, Command command) {
87         logger.debug("dimmer at address {} received command {} for {}", getAddress(), command.toFullString(),
88                 channelUID);
89         if (LuxomBindingConstants.CHANNEL_SWITCH.equals(channelUID.getId())) {
90             if (OnOffType.ON.equals(command)) {
91                 set();
92             } else if (OnOffType.OFF.equals(command)) {
93                 clear();
94             }
95         } else if (LuxomBindingConstants.CHANNEL_BRIGHTNESS.equals(channelUID.getId()) && config != null) {
96             if (command instanceof Number) {
97                 int level = ((Number) command).intValue();
98                 logger.trace("dimmer at address {} just setting dimmer level", getAddress());
99                 dim(level);
100             } else if (command instanceof IncreaseDecreaseType) {
101                 IncreaseDecreaseType s = (IncreaseDecreaseType) command;
102                 int currentValue = lastLightLevel.get();
103                 int newValue;
104                 if (IncreaseDecreaseType.INCREASE.equals(s)) {
105                     newValue = currentValue + config.stepPercentage;
106                     // round down to step multiple
107                     newValue = newValue - newValue % config.stepPercentage;
108                     logger.trace("dimmer at address {} just increasing dimmer level", getAddress());
109                     dim(newValue);
110                 } else {
111                     newValue = currentValue - config.stepPercentage;
112                     // round up to step multiple
113                     newValue = newValue + newValue % config.stepPercentage;
114                     logger.trace("dimmer at address {} just increasing dimmer level", getAddress());
115                     dim(Math.max(newValue, 0));
116                 }
117             } else if (OnOffType.ON.equals(command)) {
118                 if (config.onToLast) {
119                     dim(lastLightLevel.get());
120                 } else {
121                     dim(config.onLevel.intValue());
122                 }
123             } else if (OnOffType.OFF.equals(command)) {
124                 dim(0);
125             }
126         }
127     }
128
129     @Override
130     public void handleCommandComingFromBridge(LuxomCommand command) {
131         updateStatus(ThingStatus.ONLINE);
132         if (LuxomAction.CLEAR_RESPONSE.equals(command.getAction())) {
133             updateState(LuxomBindingConstants.CHANNEL_SWITCH, OnOffType.OFF);
134         } else if (LuxomAction.SET_RESPONSE.equals(command.getAction())) {
135             updateState(LuxomBindingConstants.CHANNEL_SWITCH, OnOffType.ON);
136         } else if (LuxomAction.DATA_RESPONSE.equals(command.getAction())) {
137             int percentage = PercentageConverter.getPercentage(command.getData());
138
139             lastLightLevel.set(percentage);
140             updateState(LuxomBindingConstants.CHANNEL_BRIGHTNESS, new PercentType(percentage));
141         }
142     }
143
144     @Override
145     public void channelLinked(ChannelUID channelUID) {
146         logger.debug("dimmer at address {} linked to channel {}", getAddress(), channelUID);
147         if (LuxomBindingConstants.CHANNEL_SWITCH.equals(channelUID.getId())
148                 || LuxomBindingConstants.CHANNEL_BRIGHTNESS.equals(channelUID.getId())) {
149             // Refresh state when new item is linked.
150             if (config != null && !config.doesNotReply) {
151                 ping();
152             }
153         }
154     }
155
156     /**
157      * example : *A,0,2,2B;*Z,057;
158      */
159     private void dim(int percentage) {
160         logger.debug("dimming dimmer at address {} to {} %", getAddress(), percentage);
161         List<CommandExecutionSpecification> commands = new ArrayList<>(3);
162         if (percentage == 0) {
163             commands.add(new CommandExecutionSpecification(LuxomAction.CLEAR.getCommand() + ",0," + getAddress()));
164         } else {
165             commands.add(new CommandExecutionSpecification(LuxomAction.SET.getCommand() + ",0," + getAddress()));
166         }
167         commands.add(new CommandExecutionSpecification(LuxomAction.DATA.getCommand() + ",0," + getAddress()));
168         commands.add(new CommandExecutionSpecification(
169                 LuxomAction.DATA_BYTE.getCommand() + ",0" + PercentageConverter.getHexRepresentation(percentage)));
170
171         sendCommands(commands);
172     }
173 }