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 CBusDaliHandler} is responsible for handling commands, which are
35 * sent to one of the channels.
37 * @author Scott Linton - Initial contribution
40 public class CBusDaliHandler extends CBusGroupHandler {
42 private final Logger logger = LoggerFactory.getLogger(CBusDaliHandler.class);
44 public CBusDaliHandler(Thing thing) {
45 super(thing, CBusBindingConstants.CBUS_APPLICATION_DALI);
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_LEVEL)) {
59 updateState(channelUID, new PercentType((int) (level * 100 / 255.0)));
61 } catch (CGateException e) {
62 logger.debug("Failed to getLevel for group {}", groupId, e);
63 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication Error");
66 if (channelUID.getId().equals(CBusBindingConstants.CHANNEL_LEVEL)) {
67 logger.debug("Channel Level command for {}: {}", channelUID, command);
69 if (command instanceof OnOffType) {
70 if (command == OnOffType.ON) {
72 } else if (command == OnOffType.OFF) {
75 } else if (command instanceof PercentType) {
76 PercentType value = (PercentType) command;
77 group.ramp((int) Math.round(value.doubleValue() / 100 * 255), 0);
78 } else if (command instanceof IncreaseDecreaseType) {
79 int level = group.getLevel();
80 if (command == IncreaseDecreaseType.DECREASE) {
81 level = Math.max(level - 1, 0);
82 } else if (command == IncreaseDecreaseType.INCREASE) {
83 level = Math.min(level + 1, 255);
86 logger.debug("Change group level to {}", level);
88 } catch (CGateException e) {
89 logger.debug("Cannot send command {} to {}", command, group, e);
90 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication Error");
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();
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));
111 int v = Integer.parseInt(value);
112 PercentType perc = new PercentType(Math.round(v * 100 / 255));
113 updateState(channelUID, perc);
114 } catch (NumberFormatException e) {
116 "Invalid value presented to channel {}. Received {}, expected On/Off or decimal value",
120 logger.debug("Updating CBus Lighting Group {} with value {}", thing.getUID(), value);
122 logger.debug("Failed to Updat CBus Lighting Group {} with value {}: No Channel", thing.getUID(), value);