]> git.basschouten.com Git - openhab-addons.git/blob
745812169072153ba808d8e5afb7606129f9580f
[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 CBusLightHandler} 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 CBusLightHandler extends CBusGroupHandler {
41
42     private final Logger logger = LoggerFactory.getLogger(CBusLightHandler.class);
43
44     public CBusLightHandler(Thing thing) {
45         super(thing, CBusBindingConstants.CBUS_APPLICATION_LIGHTING);
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_STATE)) {
59                     updateState(channelUID, (level > 0) ? OnOffType.ON : OnOffType.OFF);
60                 } else if (channelUID.getId().equals(CBusBindingConstants.CHANNEL_LEVEL)) {
61                     updateState(channelUID, new PercentType((int) (level * 100 / 255.0)));
62                 }
63             } catch (CGateException e) {
64                 logger.debug("Failed to getLevel for group {}", groupId, e);
65                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication Error");
66             }
67         } else {
68             if (channelUID.getId().equals(CBusBindingConstants.CHANNEL_STATE)) {
69                 logger.debug("Channel State command for {}: {}", channelUID, command);
70                 if (command instanceof OnOffType) {
71                     try {
72                         if (command == OnOffType.ON) {
73                             group.on();
74                         } else if (command == OnOffType.OFF) {
75                             group.off();
76                         }
77                     } catch (CGateException e) {
78                         logger.debug("Failed to send command {} to {}", command, group, e);
79                         updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication Error");
80                     }
81                 }
82             } else if (channelUID.getId().equals(CBusBindingConstants.CHANNEL_LEVEL)) {
83                 logger.debug("Channel Level command for {}: {}", channelUID, command);
84                 try {
85                     if (command instanceof OnOffType) {
86                         if (command == OnOffType.ON) {
87                             group.on();
88                         } else if (command == OnOffType.OFF) {
89                             group.off();
90                         }
91                     } else if (command instanceof PercentType percentCommand) {
92                         group.ramp((int) Math.round(percentCommand.doubleValue() / 100 * 255), 0);
93                     } else if (command instanceof IncreaseDecreaseType) {
94                         int level = group.getLevel();
95                         if (command == IncreaseDecreaseType.DECREASE) {
96                             level = Math.max(level - 1, 0);
97                         } else if (command == IncreaseDecreaseType.INCREASE) {
98                             level = Math.min(level + 1, 255);
99                         }
100                         group.ramp(level, 0);
101                         logger.debug("Change group level to {}", level);
102                     }
103                 } catch (CGateException e) {
104                     logger.debug("Failed to send command {} to {}", command, group, e);
105                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication Error");
106                 }
107             }
108         }
109     }
110
111     @Override
112     public void updateGroup(int updateApplicationId, int updateGroupId, String value) {
113         if (updateGroupId == groupId && updateApplicationId == applicationId) {
114             Thing thing = getThing();
115             Channel channel = thing.getChannel(CBusBindingConstants.CHANNEL_STATE);
116             Channel channelLevel = thing.getChannel(CBusBindingConstants.CHANNEL_LEVEL);
117             if (channel != null && channelLevel != null) {
118                 ChannelUID channelUID = channel.getUID();
119                 ChannelUID channelLevelUID = channelLevel.getUID();
120                 logger.debug("channel UID {} level UID {}", channelUID, channelLevelUID);
121                 if ("on".equalsIgnoreCase(value) || "255".equalsIgnoreCase(value)) {
122                     updateState(channelUID, OnOffType.ON);
123                     updateState(channelLevelUID, new PercentType(100));
124                 } else if ("off".equalsIgnoreCase(value) || "0".equalsIgnoreCase(value)) {
125                     updateState(channelUID, OnOffType.OFF);
126                     updateState(channelLevelUID, new PercentType(0));
127                 } else {
128                     try {
129                         int v = Integer.parseInt(value);
130                         updateState(channelUID, v > 0 ? OnOffType.ON : OnOffType.OFF);
131                         updateState(channelLevelUID, new PercentType((int) (v * 100 / 255.0)));
132                     } catch (NumberFormatException e) {
133                         logger.warn("Invalid value presented to channel {}. Received {}, expected On/Off", channelUID,
134                                 value);
135                     }
136                 }
137                 logger.debug("Updating CBus Lighting Group {} with value {}", thing.getUID(), value);
138             } else {
139                 logger.debug("Failed to Update CBus Lighting Group {} with value {}: No Channel", thing.getUID(),
140                         value);
141             }
142         }
143     }
144 }