]> git.basschouten.com Git - openhab-addons.git/blob
05188d4e0cf0e1dddcc1bb09d8740ff38f85bf23
[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.DecimalType;
18 import org.openhab.core.thing.Channel;
19 import org.openhab.core.thing.ChannelUID;
20 import org.openhab.core.thing.Thing;
21 import org.openhab.core.thing.ThingStatus;
22 import org.openhab.core.thing.ThingStatusDetail;
23 import org.openhab.core.types.Command;
24 import org.openhab.core.types.RefreshType;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.daveoxley.cbus.CGateException;
29 import com.daveoxley.cbus.Group;
30
31 /**
32  * The {@link CBusTriggerHandler} is responsible for handling commands, which are
33  * sent to one of the channels.
34  *
35  * @author Scott Linton - Initial contribution
36  */
37 @NonNullByDefault
38 public class CBusTriggerHandler extends CBusGroupHandler {
39
40     private final Logger logger = LoggerFactory.getLogger(CBusTriggerHandler.class);
41
42     public CBusTriggerHandler(Thing thing) {
43         super(thing, CBusBindingConstants.CBUS_APPLICATION_TRIGGER);
44     }
45
46     @Override
47     public void handleCommand(ChannelUID channelUID, Command command) {
48         Group group = this.group;
49         if (group == null) {
50             return;
51         }
52         if (command instanceof RefreshType) {
53             /*
54              * Cgate cant provide the current value for a trigger group
55              */
56             logger.debug("Refresh for Trigger group not implemented");
57         } else {
58             if (channelUID.getId().equals(CBusBindingConstants.CHANNEL_VALUE)) {
59                 logger.debug("Channel Value command for {}: {}", channelUID, command);
60                 try {
61                     if (command instanceof DecimalType) {
62                         group.TriggerEvent(((DecimalType) command).intValue());
63                     }
64                 } catch (CGateException e) {
65                     logger.debug("Failed to send trigger command {} to {}", command, group, e);
66                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication Error");
67                 }
68             }
69         }
70     }
71
72     public void updateGroup(int updateApplicationId, int updateGroupId, String value) {
73         if (updateGroupId == groupId && updateApplicationId == applicationId) {
74             Thing thing = getThing();
75             Channel channel = thing.getChannel(CBusBindingConstants.CHANNEL_VALUE);
76             if (channel != null) {
77                 ChannelUID channelUID = channel.getUID();
78                 DecimalType val = new DecimalType(value);
79                 updateState(channelUID, val);
80                 logger.trace("Updating CBus Trigger Group {} with value {}", thing.getUID(), value);
81             }
82         }
83     }
84 }