]> git.basschouten.com Git - openhab-addons.git/blob
9b3f71c4905ce4c113635bcbbc1c096af0e0a440
[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.model;
14
15 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.library.types.PercentType;
20
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonPrimitive;
23
24 /**
25  * The {@link TradfriBlindData} class is a Java wrapper for the raw JSON data about the blinds state.
26  *
27  * @author Manuel Raffel - Initial contribution
28  */
29 @NonNullByDefault
30 public class TradfriBlindData extends TradfriWirelessDeviceData {
31     public TradfriBlindData() {
32         super(BLINDS);
33     }
34
35     public TradfriBlindData(JsonElement json) {
36         super(BLINDS, json);
37     }
38
39     public TradfriBlindData setPosition(PercentType position) {
40         attributes.add(POSITION, new JsonPrimitive(position.intValue()));
41         return this;
42     }
43
44     public TradfriBlindData stop() {
45         attributes.add(STOP_TRIGGER, new JsonPrimitive(0));
46         return this;
47     }
48
49     public @Nullable PercentType getPosition() {
50         PercentType result = null;
51
52         JsonElement position = attributes.get(POSITION);
53         if (position != null) {
54             int percent = position.getAsInt();
55             percent = Math.max(percent, 0);
56             percent = Math.min(100, percent);
57             result = new PercentType(percent);
58         }
59
60         return result;
61     }
62
63     public String getJsonString() {
64         return root.toString();
65     }
66 }