]> git.basschouten.com Git - openhab-addons.git/blob
45b3bd70187f271639da31191cdf99b33eb4ae96
[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.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.config.core.Configuration;
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.thing.binding.BaseThingHandler;
35 import org.openhab.core.thing.binding.BridgeHandler;
36 import org.openhab.core.types.Command;
37 import org.openhab.core.types.RefreshType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * The {@link DaliDeviceHandler} handles commands for things of type Device and Group.
43  *
44  * @author Robert Schmid - Initial contribution
45  */
46 @NonNullByDefault
47 public class DaliDeviceHandler extends BaseThingHandler {
48     private final Logger logger = LoggerFactory.getLogger(DaliDeviceHandler.class);
49     protected @Nullable Integer targetId;
50     protected @Nullable Integer readDeviceTargetId;
51
52     public DaliDeviceHandler(Thing thing) {
53         super(thing);
54     }
55
56     @Override
57     public void initialize() {
58         Bridge bridge = getBridge();
59
60         if (bridge == null) {
61             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
62         } else {
63             updateStatus(ThingStatus.ONLINE);
64         }
65
66         final Configuration conf = this.thing.getConfiguration();
67         targetId = ((BigDecimal) conf.get(TARGET_ID)).intValueExact();
68         // Reading from group addresses does not work generally, so if a fallback device id is
69         // defined, use that instead when reading the current state
70         if (conf.get(READ_DEVICE_TARGET_ID) != null) {
71             readDeviceTargetId = ((BigDecimal) this.thing.getConfiguration().get(READ_DEVICE_TARGET_ID))
72                     .intValueExact();
73         } else {
74             readDeviceTargetId = null;
75         }
76     }
77
78     @Override
79     public void handleCommand(ChannelUID channelUID, Command command) {
80         try {
81             if (CHANNEL_DIM_AT_FADE_RATE.equals(channelUID.getId())
82                     || CHANNEL_DIM_IMMEDIATELY.equals(channelUID.getId())) {
83                 DaliAddress address;
84                 if (THING_TYPE_DEVICE.equals(this.thing.getThingTypeUID())
85                         || THING_TYPE_DEVICE_DT8.equals(this.thing.getThingTypeUID())) {
86                     address = DaliAddress.createShortAddress(targetId);
87                 } else if (THING_TYPE_GROUP.equals(this.thing.getThingTypeUID())
88                         || THING_TYPE_GROUP_DT8.equals(this.thing.getThingTypeUID())) {
89                     address = DaliAddress.createGroupAddress(targetId);
90                 } else {
91                     throw new DaliException("unknown device type");
92                 }
93
94                 boolean queryDeviceState = false;
95
96                 if (command instanceof PercentType percentCommand) {
97                     byte dimmValue = (byte) ((percentCommand.floatValue() * DALI_SWITCH_100_PERCENT) / 100);
98                     // A dimm value of zero is handled correctly by DALI devices, i.e. they are turned off
99                     getBridgeHandler().sendCommand(new DaliDAPCCommand(address, dimmValue));
100                 } else if (command instanceof OnOffType onOffCommand) {
101                     if (onOffCommand == OnOffType.ON) {
102                         getBridgeHandler().sendCommand(new DaliDAPCCommand(address, (byte) DALI_SWITCH_100_PERCENT));
103                     } else {
104                         getBridgeHandler().sendCommand(DaliStandardCommand.createOffCommand(address));
105                     }
106                 } else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
107                     if (CHANNEL_DIM_AT_FADE_RATE.equals(channelUID.getId())) {
108                         if (increaseDecreaseCommand == IncreaseDecreaseType.INCREASE) {
109                             getBridgeHandler().sendCommand(DaliStandardCommand.createUpCommand(address));
110                         } else {
111                             getBridgeHandler().sendCommand(DaliStandardCommand.createDownCommand(address));
112                         }
113                     } else {
114                         if (increaseDecreaseCommand == IncreaseDecreaseType.INCREASE) {
115                             getBridgeHandler().sendCommand(DaliStandardCommand.createStepUpCommand(address));
116                         } else {
117                             getBridgeHandler().sendCommand(DaliStandardCommand.createStepDownCommand(address));
118                         }
119                     }
120                     queryDeviceState = true;
121                 } else if (command instanceof RefreshType) {
122                     queryDeviceState = true;
123                 }
124
125                 if (queryDeviceState) {
126                     DaliAddress readAddress = address;
127                     if (readDeviceTargetId != null) {
128                         readAddress = DaliAddress.createShortAddress(readDeviceTargetId);
129                     }
130                     getBridgeHandler()
131                             .sendCommandWithResponse(DaliStandardCommand.createQueryActualLevelCommand(readAddress),
132                                     DaliResponse.NumericMask.class)
133                             .thenAccept(response -> {
134                                 if (response != null && !response.mask) {
135                                     Integer value = response.value != null ? response.value : 0;
136                                     int percentValue = (int) (value.floatValue() * 100 / DALI_SWITCH_100_PERCENT);
137                                     updateState(channelUID, new PercentType(percentValue));
138                                 }
139                             }).exceptionally(e -> {
140                                 logger.warn("Error querying device status: {}", e.getMessage());
141                                 return null;
142                             });
143                 }
144             }
145         } catch (DaliException e) {
146             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
147         }
148     }
149
150     protected DaliserverBridgeHandler getBridgeHandler() throws DaliException {
151         Bridge bridge = this.getBridge();
152         if (bridge == null) {
153             throw new DaliException("No bridge was found");
154         }
155
156         BridgeHandler handler = bridge.getHandler();
157         if (handler == null) {
158             throw new DaliException("No handler was found");
159         }
160
161         return (DaliserverBridgeHandler) handler;
162     }
163 }