]> git.basschouten.com Git - openhab-addons.git/blob
575c736413762cc637b30ed77ac3459eccffa1c7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.lutron.internal.radiora.handler;
14
15 import java.util.concurrent.atomic.AtomicBoolean;
16 import java.util.concurrent.atomic.AtomicInteger;
17
18 import org.openhab.binding.lutron.internal.LutronBindingConstants;
19 import org.openhab.binding.lutron.internal.radiora.config.DimmerConfig;
20 import org.openhab.binding.lutron.internal.radiora.protocol.LocalZoneChangeFeedback;
21 import org.openhab.binding.lutron.internal.radiora.protocol.RadioRAFeedback;
22 import org.openhab.binding.lutron.internal.radiora.protocol.SetDimmerLevelCommand;
23 import org.openhab.binding.lutron.internal.radiora.protocol.SetSwitchLevelCommand;
24 import org.openhab.binding.lutron.internal.radiora.protocol.ZoneMapFeedback;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.PercentType;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.types.Command;
30
31 /**
32  * Handler for RadioRA dimmers
33  *
34  * @author Jeff Lauterbach - Initial Contribution
35  *
36  */
37 public class DimmerHandler extends LutronHandler {
38
39     /**
40      * Used to internally keep track of dimmer level. This helps us better respond
41      * to external dimmer changes since RadioRA protocol does not send dimmer
42      * levels in their messages.
43      */
44     private AtomicInteger lastKnownIntensity = new AtomicInteger(100);
45
46     private AtomicBoolean switchEnabled = new AtomicBoolean(false);
47
48     public DimmerHandler(Thing thing) {
49         super(thing);
50     }
51
52     @Override
53     public void handleCommand(ChannelUID channelUID, Command command) {
54         DimmerConfig config = getConfigAs(DimmerConfig.class);
55
56         if (LutronBindingConstants.CHANNEL_LIGHTLEVEL.equals(channelUID.getId())) {
57             if (command instanceof PercentType) {
58                 int intensity = ((PercentType) command).intValue();
59
60                 SetDimmerLevelCommand cmd = new SetDimmerLevelCommand(config.getZoneNumber(), intensity);
61                 getRS232Handler().sendCommand(cmd);
62
63                 updateInternalState(intensity);
64             }
65
66             if (command instanceof OnOffType) {
67                 OnOffType onOffCmd = (OnOffType) command;
68
69                 SetSwitchLevelCommand cmd = new SetSwitchLevelCommand(config.getZoneNumber(), onOffCmd);
70                 getRS232Handler().sendCommand(cmd);
71
72                 updateInternalState(onOffCmd);
73
74             }
75         }
76     }
77
78     @Override
79     public void handleFeedback(RadioRAFeedback feedback) {
80         if (feedback instanceof LocalZoneChangeFeedback) {
81             handleLocalZoneChangeFeedback((LocalZoneChangeFeedback) feedback);
82         } else if (feedback instanceof ZoneMapFeedback) {
83             handleZoneMapFeedback((ZoneMapFeedback) feedback);
84         }
85     }
86
87     private void handleZoneMapFeedback(ZoneMapFeedback feedback) {
88         char value = feedback.getZoneValue(getConfigAs(DimmerConfig.class).getZoneNumber());
89         if (value == '1') {
90             turnDimmerOnToLastKnownIntensity();
91         } else if (value == '0') {
92             turnDimmerOff();
93         }
94     }
95
96     private void handleLocalZoneChangeFeedback(LocalZoneChangeFeedback feedback) {
97         if (feedback.getZoneNumber() == getConfigAs(DimmerConfig.class).getZoneNumber()) {
98             if (LocalZoneChangeFeedback.State.ON.equals(feedback.getState())) {
99                 turnDimmerOnToLastKnownIntensity();
100             } else if (LocalZoneChangeFeedback.State.OFF.equals(feedback.getState())) {
101                 turnDimmerOff();
102             }
103
104         }
105     }
106
107     private void turnDimmerOnToLastKnownIntensity() {
108         if (!switchEnabled.get()) {
109             updateState(LutronBindingConstants.CHANNEL_LIGHTLEVEL, new PercentType(lastKnownIntensity.get()));
110         }
111         switchEnabled.set(true);
112     }
113
114     private void turnDimmerOff() {
115         updateState(LutronBindingConstants.CHANNEL_LIGHTLEVEL, PercentType.ZERO);
116         switchEnabled.set(false);
117     }
118
119     private void updateInternalState(int intensity) {
120         if (intensity > 0) {
121             lastKnownIntensity.set(intensity);
122         }
123         switchEnabled.set(intensity > 0);
124     }
125
126     private void updateInternalState(OnOffType type) {
127         switchEnabled.set(OnOffType.ON.equals(type));
128     }
129 }