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