]> git.basschouten.com Git - openhab-addons.git/blob
23f8ee899144f702dcddeaeacfb26a1af5460c1b
[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.digitalstrom.internal.lib.structure.devices.deviceparameters.impl;
14
15 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
16 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.DeviceSceneSpec;
17 import org.openhab.binding.digitalstrom.internal.lib.structure.scene.constants.Scene;
18 import org.openhab.binding.digitalstrom.internal.lib.structure.scene.constants.SceneEnum;
19
20 import com.google.gson.JsonObject;
21
22 /**
23  * The {@link JSONDeviceSceneSpecImpl} is the implementation of the {@link DeviceSceneSpec}.
24  *
25  * @author Alexander Betker - Initial contribution
26  * @author Michael Ochel - change from SimpleJSON to GSON, add constructor JSONDeviceSceneSpecImpl(Short sceneID) and
27  *         JSONDeviceSceneSpecImpl(String sceneName)
28  * @author Matthias Siegele - change from SimpleJSON to GSON, add constructor JSONDeviceSceneSpecImpl(Short sceneID) and
29  *         JSONDeviceSceneSpecImpl(String sceneName)
30  */
31 public class JSONDeviceSceneSpecImpl implements DeviceSceneSpec {
32
33     private Scene scene;
34     private boolean dontcare = false;
35     private boolean localPrio = false;
36     private boolean specialMode = false;
37     private boolean flashMode = false;
38
39     /**
40      * Creates a new {@link JSONDeviceSceneSpecImpl} through the digitalSTROM json response as {@link JsonObject}.
41      *
42      * @param jObject must not be null
43      */
44     public JSONDeviceSceneSpecImpl(JsonObject jObject) {
45         if (jObject.get(JSONApiResponseKeysEnum.SCENE_ID.getKey()) != null) {
46             short val = -1;
47             val = jObject.get(JSONApiResponseKeysEnum.SCENE_ID.getKey()).getAsShort();
48
49             if (val > -1) {
50                 this.scene = SceneEnum.getScene(val);
51             }
52         }
53         if (jObject.get(JSONApiResponseKeysEnum.DONT_CARE.getKey()) != null) {
54             this.dontcare = jObject.get(JSONApiResponseKeysEnum.DONT_CARE.getKey()).getAsBoolean();
55         }
56         if (jObject.get(JSONApiResponseKeysEnum.LOCAL_PRIO.getKey()) != null) {
57             this.localPrio = jObject.get(JSONApiResponseKeysEnum.LOCAL_PRIO.getKey()).getAsBoolean();
58         }
59         if (jObject.get(JSONApiResponseKeysEnum.SPECIAL_MODE.getKey()) != null) {
60             this.specialMode = jObject.get(JSONApiResponseKeysEnum.SPECIAL_MODE.getKey()).getAsBoolean();
61         }
62         if (jObject.get(JSONApiResponseKeysEnum.FLASH_MODE.getKey()) != null) {
63             this.flashMode = jObject.get(JSONApiResponseKeysEnum.FLASH_MODE.getKey()).getAsBoolean();
64         }
65     }
66
67     /**
68      * Creates a new {@link JSONDeviceSceneSpecImpl} through the given sceneID.
69      *
70      * @param sceneID must not be null
71      */
72     public JSONDeviceSceneSpecImpl(Short sceneID) {
73         this.scene = SceneEnum.getScene(sceneID);
74     }
75
76     /**
77      * Creates a new {@link JSONDeviceSceneSpecImpl} through the given sceneName.
78      *
79      * @param sceneName must not be null
80      */
81     public JSONDeviceSceneSpecImpl(String sceneName) {
82         try {
83             this.scene = SceneEnum.valueOf(sceneName);
84         } catch (IllegalArgumentException e) {
85             // ignore it
86         }
87     }
88
89     @Override
90     public Scene getScene() {
91         return scene;
92     }
93
94     @Override
95     public boolean isDontCare() {
96         return dontcare;
97     }
98
99     @Override
100     public synchronized void setDontcare(boolean dontcare) {
101         this.dontcare = dontcare;
102     }
103
104     @Override
105     public boolean isLocalPrio() {
106         return localPrio;
107     }
108
109     @Override
110     public synchronized void setLocalPrio(boolean localPrio) {
111         this.localPrio = localPrio;
112     }
113
114     @Override
115     public boolean isSpecialMode() {
116         return specialMode;
117     }
118
119     @Override
120     public synchronized void setSpecialMode(boolean specialMode) {
121         this.specialMode = specialMode;
122     }
123
124     @Override
125     public boolean isFlashMode() {
126         return flashMode;
127     }
128
129     @Override
130     public synchronized void setFlashMode(boolean flashMode) {
131         this.flashMode = flashMode;
132     }
133
134     @Override
135     public String toString() {
136         return "Scene: " + this.getScene() + ", dontcare: " + this.isDontCare() + ", localPrio: " + this.isLocalPrio()
137                 + ", specialMode: " + this.isSpecialMode() + ", flashMode: " + this.isFlashMode();
138     }
139 }