]> git.basschouten.com Git - openhab-addons.git/blob
08270e709311af99e500025ab696cc450b1dff1a
[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.cbus.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.cbus.CBusBindingConstants;
17 import org.openhab.core.library.types.IncreaseDecreaseType;
18 import org.openhab.core.library.types.OnOffType;
19 import org.openhab.core.library.types.PercentType;
20 import org.openhab.core.thing.Channel;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingStatusDetail;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.RefreshType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.daveoxley.cbus.CGateException;
31 import com.daveoxley.cbus.Group;
32
33 /**
34  * The {@link CBusDaliHandler} is responsible for handling commands, which are
35  * sent to one of the channels.
36  *
37  * @author Scott Linton - Initial contribution
38  */
39 @NonNullByDefault
40 public class CBusDaliHandler extends CBusGroupHandler {
41
42     private final Logger logger = LoggerFactory.getLogger(CBusDaliHandler.class);
43
44     public CBusDaliHandler(Thing thing) {
45         super(thing, CBusBindingConstants.CBUS_APPLICATION_DALI);
46     }
47
48     @Override
49     public void handleCommand(ChannelUID channelUID, Command command) {
50         Group group = this.group;
51         if (group == null) {
52             return;
53         }
54         if (command instanceof RefreshType) {
55             try {
56                 int level = group.getLevel();
57                 logger.debug("handle RefreshType Command for Chanell {} Group {} level {}", channelUID, groupId, level);
58                 if (channelUID.getId().equals(CBusBindingConstants.CHANNEL_LEVEL)) {
59                     updateState(channelUID, new PercentType((int) (level * 100 / 255.0)));
60                 }
61             } catch (CGateException e) {
62                 logger.debug("Failed to getLevel for group {}", groupId, e);
63                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication Error");
64             }
65         } else {
66             if (channelUID.getId().equals(CBusBindingConstants.CHANNEL_LEVEL)) {
67                 logger.debug("Channel Level command for {}: {}", channelUID, command);
68                 try {
69                     if (command instanceof OnOffType) {
70                         if (command == OnOffType.ON) {
71                             group.on();
72                         } else if (command == OnOffType.OFF) {
73                             group.off();
74                         }
75                     } else if (command instanceof PercentType percentCommand) {
76                         group.ramp((int) Math.round(percentCommand.doubleValue() / 100 * 255), 0);
77                     } else if (command instanceof IncreaseDecreaseType) {
78                         int level = group.getLevel();
79                         if (command == IncreaseDecreaseType.DECREASE) {
80                             level = Math.max(level - 1, 0);
81                         } else if (command == IncreaseDecreaseType.INCREASE) {
82                             level = Math.min(level + 1, 255);
83                         }
84                         group.ramp(level, 0);
85                         logger.debug("Change group level to {}", level);
86                     }
87                 } catch (CGateException e) {
88                     logger.debug("Cannot send command {} to {}", command, group, e);
89                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication Error");
90                 }
91             }
92         }
93     }
94
95     @Override
96     public void updateGroup(int updateApplicationId, int updateGroupId, String value) {
97         if (updateGroupId == groupId && updateApplicationId == applicationId) {
98             Thing thing = getThing();
99             Channel channel = thing.getChannel(CBusBindingConstants.CHANNEL_LEVEL);
100             if (channel != null) {
101                 ChannelUID channelUID = channel.getUID();
102
103                 if ("on".equalsIgnoreCase(value) || "255".equalsIgnoreCase(value)) {
104                     updateState(channelUID, OnOffType.ON);
105                     updateState(channelUID, new PercentType(100));
106                 } else if ("off".equalsIgnoreCase(value) || "0".equalsIgnoreCase(value)) {
107                     updateState(channelUID, OnOffType.OFF);
108                     updateState(channelUID, new PercentType(0));
109                 } else {
110                     try {
111                         int v = Integer.parseInt(value);
112                         PercentType perc = new PercentType(Math.round(v * 100 / 255));
113                         updateState(channelUID, perc);
114                     } catch (NumberFormatException e) {
115                         logger.warn(
116                                 "Invalid value presented to channel {}. Received {}, expected On/Off or decimal value",
117                                 channelUID, value);
118                     }
119                 }
120                 logger.debug("Updating CBus Lighting Group {} with value {}", thing.getUID(), value);
121             } else {
122                 logger.debug("Failed to Updat CBus Lighting Group {} with value {}: No Channel", thing.getUID(), value);
123             }
124         }
125     }
126 }