2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.cbus.handler;
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;
30 import com.daveoxley.cbus.CGateException;
31 import com.daveoxley.cbus.Group;
34 * The {@link CBusLightHandler} is responsible for handling commands, which are
35 * sent to one of the channels.
37 * @author Scott Linton - Initial contribution
40 public class CBusLightHandler extends CBusGroupHandler {
42 private final Logger logger = LoggerFactory.getLogger(CBusLightHandler.class);
44 public CBusLightHandler(Thing thing) {
45 super(thing, CBusBindingConstants.CBUS_APPLICATION_LIGHTING);
49 public void handleCommand(ChannelUID channelUID, Command command) {
50 Group group = this.group;
54 if (command instanceof RefreshType) {
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)));
63 } catch (CGateException e) {
64 logger.debug("Failed to getLevel for group {}", groupId, e);
65 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication Error");
68 if (channelUID.getId().equals(CBusBindingConstants.CHANNEL_STATE)) {
69 logger.debug("Channel State command for {}: {}", channelUID, command);
70 if (command instanceof OnOffType) {
72 if (command == OnOffType.ON) {
74 } else if (command == OnOffType.OFF) {
77 } catch (CGateException e) {
78 logger.debug("Failed to send command {} to {}", command, group, e);
79 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication Error");
82 } else if (channelUID.getId().equals(CBusBindingConstants.CHANNEL_LEVEL)) {
83 logger.debug("Channel Level command for {}: {}", channelUID, command);
85 if (command instanceof OnOffType) {
86 if (command == OnOffType.ON) {
88 } else if (command == OnOffType.OFF) {
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);
100 group.ramp(level, 0);
101 logger.debug("Change group level to {}", level);
103 } catch (CGateException e) {
104 logger.debug("Failed to send command {} to {}", command, group, e);
105 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication Error");
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));
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,
137 logger.debug("Updating CBus Lighting Group {} with value {}", thing.getUID(), value);
139 logger.debug("Failed to Update CBus Lighting Group {} with value {}: No Channel", thing.getUID(),