]> git.basschouten.com Git - openhab-addons.git/blob
9c5af8ec1f21e308c1762bb8a6d308670f46b8d3
[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 static org.openhab.core.library.unit.SIUnits.CELSIUS;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.cbus.CBusBindingConstants;
19 import org.openhab.core.library.types.QuantityType;
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 CBusTemperatureHandler} 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 CBusTemperatureHandler extends CBusGroupHandler {
41
42     private final Logger logger = LoggerFactory.getLogger(CBusTemperatureHandler.class);
43
44     public CBusTemperatureHandler(Thing thing) {
45         super(thing, CBusBindingConstants.CBUS_APPLICATION_TEMPERATURE);
46     }
47
48     @Override
49     public void handleCommand(ChannelUID channelUID, Command command) {
50         // Read only thing - no commands to handle
51         if (command instanceof RefreshType) {
52             try {
53                 Group group = this.group;
54                 if (group != null) {
55                     int level = group.getLevel();
56                     logger.debug("handle RefreshType Command for Chanell {} Group {} level {}", channelUID, groupId,
57                             level);
58                     if (channelUID.getId().equals(CBusBindingConstants.CHANNEL_TEMP)) {
59                         updateState(channelUID, new QuantityType<>(level, CELSIUS));
60                     }
61                 }
62             } catch (CGateException e) {
63                 logger.debug("Failed to getLevel for group {}", groupId, e);
64                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Communication Error");
65             }
66         }
67     }
68
69     public void updateGroup(int updateApplicationId, int updateGroupId, String value) {
70         if (updateGroupId == groupId && updateApplicationId == applicationId) {
71             Thing thing = getThing();
72             Channel channel = thing.getChannel(CBusBindingConstants.CHANNEL_TEMP);
73
74             if (channel != null) {
75                 ChannelUID channelUID = channel.getUID();
76                 updateState(channelUID, new QuantityType<>(Double.parseDouble(value), CELSIUS));
77                 logger.trace("Updating CBus Temperature Group {} with value {}", thing.getUID(), value);
78             } else {
79                 logger.trace("Failed to Update CBus Temperature Group {} with value {}: No Channel", thing.getUID(),
80                         value);
81             }
82         }
83     }
84 }