]> git.basschouten.com Git - openhab-addons.git/blob
6b3bdf35ace73906511a5be08aeda67ebfb5312d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.tradfri.internal.handler;
14
15 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.tradfri.internal.model.TradfriBlindData;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.types.PercentType;
22 import org.openhab.core.library.types.StopMoveType;
23 import org.openhab.core.library.types.UpDownType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.RefreshType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.google.gson.JsonElement;
33
34 /**
35  * The {@link TradfriBlindHandler} is responsible for handling commands for individual blinds.
36  *
37  * @author Manuel Raffel - Initial contribution
38  */
39 @NonNullByDefault
40 public class TradfriBlindHandler extends TradfriThingHandler {
41
42     private final Logger logger = LoggerFactory.getLogger(TradfriBlindHandler.class);
43
44     public TradfriBlindHandler(Thing thing) {
45         super(thing);
46     }
47
48     @Override
49     public void onUpdate(JsonElement data) {
50         if (active && !(data.isJsonNull())) {
51             TradfriBlindData state = new TradfriBlindData(data);
52             updateStatus(state.getReachabilityStatus() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
53
54             PercentType position = state.getPosition();
55             if (position != null) {
56                 updateState(CHANNEL_POSITION, position);
57             }
58
59             DecimalType batteryLevel = state.getBatteryLevel();
60             if (batteryLevel != null) {
61                 updateState(CHANNEL_BATTERY_LEVEL, batteryLevel);
62             }
63
64             OnOffType batteryLow = state.getBatteryLow();
65             if (batteryLow != null) {
66                 updateState(CHANNEL_BATTERY_LOW, batteryLow);
67             }
68
69             updateDeviceProperties(state);
70
71             logger.debug(
72                     "Updating thing for blindId {} to state {position: {}, firmwareVersion: {}, modelId: {}, vendor: {}}",
73                     state.getDeviceId(), position, state.getFirmwareVersion(), state.getModelId(), state.getVendor());
74         }
75     }
76
77     private void setPosition(PercentType percent) {
78         set(new TradfriBlindData().setPosition(percent).getJsonString());
79     }
80
81     private void triggerStop() {
82         set(new TradfriBlindData().stop().getJsonString());
83     }
84
85     @Override
86     public void handleCommand(ChannelUID channelUID, Command command) {
87         if (active) {
88             if (command instanceof RefreshType) {
89                 logger.debug("Refreshing channel {}", channelUID);
90                 coapClient.asyncGet(this);
91                 return;
92             }
93
94             switch (channelUID.getId()) {
95                 case CHANNEL_POSITION:
96                     handlePositionCommand(command);
97                     break;
98                 default:
99                     logger.error("Unknown channel UID {}", channelUID);
100             }
101         }
102     }
103
104     private void handlePositionCommand(Command command) {
105         if (command instanceof PercentType) {
106             setPosition((PercentType) command);
107         } else if (command instanceof StopMoveType) {
108             if (StopMoveType.STOP.equals(command)) {
109                 triggerStop();
110             } else {
111                 logger.debug("Cannot handle command '{}' for channel '{}'", command, CHANNEL_POSITION);
112             }
113         } else if (command instanceof UpDownType) {
114             if (UpDownType.UP.equals(command)) {
115                 setPosition(PercentType.ZERO);
116             } else {
117                 setPosition(PercentType.HUNDRED);
118             }
119         } else {
120             logger.debug("Cannot handle command '{}' for channel '{}'", command, CHANNEL_POSITION);
121         }
122     }
123 }