]> git.basschouten.com Git - openhab-addons.git/blob
5b6c49c567fe7160c126cf8041c481b287ad1504
[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
19 import com.google.gson.JsonElement;
20 import com.google.gson.JsonPrimitive;
21
22 /**
23  * The {@link TradfriPlugData} class is a Java wrapper for the raw JSON data about the plug state.
24  *
25  * @author Kai Kreuzer - Initial contribution
26  */
27 @NonNullByDefault
28 public class TradfriPlugData extends TradfriDeviceData {
29
30     public TradfriPlugData() {
31         super(PLUG);
32     }
33
34     public TradfriPlugData(JsonElement json) {
35         super(PLUG, json);
36     }
37
38     public TradfriPlugData setTransitionTime(int seconds) {
39         attributes.add(TRANSITION_TIME, new JsonPrimitive(seconds));
40         return this;
41     }
42
43     public int getTransitionTime() {
44         JsonElement transitionTime = attributes.get(TRANSITION_TIME);
45         if (transitionTime != null) {
46             return transitionTime.getAsInt();
47         } else {
48             return 0;
49         }
50     }
51
52     public TradfriPlugData setOnOffState(boolean on) {
53         attributes.add(ONOFF, new JsonPrimitive(on ? 1 : 0));
54         return this;
55     }
56
57     public boolean getOnOffState() {
58         JsonElement onOff = attributes.get(ONOFF);
59         if (onOff != null) {
60             return onOff.getAsInt() == 1;
61         } else {
62             return false;
63         }
64     }
65
66     public String getJsonString() {
67         return root.toString();
68     }
69 }