]> git.basschouten.com Git - openhab-addons.git/blob
d531df1fc7b70d1e2ccaae977c6a268f13da9039
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.dali.internal.handler;
14
15 import static org.openhab.binding.dali.internal.DaliBindingConstants.*;
16
17 import java.math.BigDecimal;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.dali.internal.protocol.DaliAddress;
22 import org.openhab.binding.dali.internal.protocol.DaliDAPCCommand;
23 import org.openhab.binding.dali.internal.protocol.DaliResponse;
24 import org.openhab.binding.dali.internal.protocol.DaliStandardCommand;
25 import org.openhab.core.library.types.IncreaseDecreaseType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.PercentType;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusDetail;
33 import org.openhab.core.thing.binding.BaseThingHandler;
34 import org.openhab.core.thing.binding.BridgeHandler;
35 import org.openhab.core.types.Command;
36 import org.openhab.core.types.RefreshType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * The {@link DaliDeviceHandler} handles commands for things of type Device and Group.
42  *
43  * @author Robert Schmid - Initial contribution
44  */
45 @NonNullByDefault
46 public class DaliDeviceHandler extends BaseThingHandler {
47     private final Logger logger = LoggerFactory.getLogger(DaliDeviceHandler.class);
48     private @Nullable Integer targetId;
49
50     public DaliDeviceHandler(Thing thing) {
51         super(thing);
52     }
53
54     @Override
55     public void initialize() {
56         Bridge bridge = getBridge();
57
58         if (bridge == null) {
59             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
60         } else {
61             updateStatus(ThingStatus.ONLINE);
62         }
63
64         targetId = ((BigDecimal) this.thing.getConfiguration().get(TARGET_ID)).intValueExact();
65     }
66
67     @Override
68     public void handleCommand(ChannelUID channelUID, Command command) {
69         try {
70             if (CHANNEL_DIM_AT_FADE_RATE.equals(channelUID.getId())
71                     || CHANNEL_DIM_IMMEDIATELY.equals(channelUID.getId())) {
72                 DaliAddress address;
73                 if (THING_TYPE_DEVICE.equals(this.thing.getThingTypeUID())) {
74                     address = DaliAddress.createShortAddress(targetId);
75                 } else if (THING_TYPE_GROUP.equals(this.thing.getThingTypeUID())) {
76                     address = DaliAddress.createGroupAddress(targetId);
77                 } else {
78                     throw new DaliException("unknown device type");
79                 }
80
81                 boolean queryDeviceState = false;
82
83                 if (command instanceof PercentType) {
84                     byte dimmValue = (byte) ((((PercentType) command).floatValue() * DALI_SWITCH_100_PERCENT) / 100);
85                     // A dimm value of zero is handled correctly by DALI devices, i.e. they are turned off
86                     getBridgeHandler().sendCommand(new DaliDAPCCommand(address, dimmValue));
87                 } else if (command instanceof OnOffType) {
88                     if ((OnOffType) command == OnOffType.ON) {
89                         getBridgeHandler().sendCommand(new DaliDAPCCommand(address, (byte) DALI_SWITCH_100_PERCENT));
90                     } else {
91                         getBridgeHandler().sendCommand(DaliStandardCommand.createOffCommand(address));
92                     }
93                 } else if (command instanceof IncreaseDecreaseType) {
94                     if (CHANNEL_DIM_AT_FADE_RATE.equals(channelUID.getId())) {
95                         if ((IncreaseDecreaseType) command == IncreaseDecreaseType.INCREASE) {
96                             getBridgeHandler().sendCommand(DaliStandardCommand.createUpCommand(address));
97                         } else {
98                             getBridgeHandler().sendCommand(DaliStandardCommand.createDownCommand(address));
99                         }
100                     } else {
101                         if ((IncreaseDecreaseType) command == IncreaseDecreaseType.INCREASE) {
102                             getBridgeHandler().sendCommand(DaliStandardCommand.createStepUpCommand(address));
103                         } else {
104                             getBridgeHandler().sendCommand(DaliStandardCommand.createStepDownCommand(address));
105                         }
106                     }
107                     queryDeviceState = true;
108                 } else if (command instanceof RefreshType) {
109                     queryDeviceState = true;
110                 }
111
112                 if (queryDeviceState) {
113                     getBridgeHandler()
114                             .sendCommandWithResponse(DaliStandardCommand.createQueryActualLevelCommand(address),
115                                     DaliResponse.NumericMask.class)
116                             .thenAccept(response -> {
117                                 if (response != null && !response.mask) {
118                                     Integer value = response.value != null ? response.value : 0;
119                                     int percentValue = (int) (value.floatValue() * 100 / DALI_SWITCH_100_PERCENT);
120                                     updateState(channelUID, new PercentType(percentValue));
121                                 }
122                             }).exceptionally(e -> {
123                                 logger.warn("Error querying device status: {}", e.getMessage());
124                                 return null;
125                             });
126                 }
127             }
128         } catch (DaliException e) {
129             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
130         }
131     }
132
133     protected DaliserverBridgeHandler getBridgeHandler() throws DaliException {
134         Bridge bridge = this.getBridge();
135         if (bridge == null) {
136             throw new DaliException("No bridge was found");
137         }
138
139         BridgeHandler handler = bridge.getHandler();
140         if (handler == null) {
141             throw new DaliException("No handler was found");
142         }
143
144         return (DaliserverBridgeHandler) handler;
145     }
146 }