]> git.basschouten.com Git - openhab-addons.git/blob
1ec086889d2d2c62fdf216a35605466ef586c9fc
[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.somneo.internal.model;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.core.library.types.OnOffType;
18 import org.openhab.core.library.types.PercentType;
19 import org.openhab.core.library.types.PlayPauseType;
20 import org.openhab.core.library.types.StringType;
21 import org.openhab.core.types.State;
22 import org.openhab.core.types.UnDefType;
23
24 import com.google.gson.annotations.SerializedName;
25
26 /**
27  * This class represents the audio state from the API.
28  *
29  * @author Michael Myrcik - Initial contribution
30  */
31 @NonNullByDefault
32 public class AudioData {
33
34     private static final String SOURCE_RADIO = "fmr";
35
36     private static final String SOURCE_AUX = "aux";
37
38     private static final String SOURCE_OFF = "off";
39
40     @SerializedName("onoff")
41     private @Nullable Boolean power;
42
43     /**
44      * Must be set to false when the audio is turned on, otherwise a light that is
45      * turned on will be turned off.
46      */
47     @SuppressWarnings("unused")
48     @SerializedName("tempy")
49     private @Nullable Boolean previewLight;
50
51     /**
52      * Volume range from 0 to 25.
53      */
54     @SerializedName("sdvol")
55     private @Nullable Integer volume;
56
57     /**
58      * Current active audio source. Can be radio, aux or off.
59      */
60     @SerializedName("snddv")
61     private @Nullable String source;
62
63     /**
64      * Current active radio preset.
65      */
66     @SerializedName("sndch")
67     private @Nullable String preset;
68
69     public void disableAudio() {
70         power = false;
71         source = SOURCE_OFF;
72     }
73
74     public void enableRadio() {
75         power = true;
76         source = SOURCE_RADIO;
77         previewLight = false;
78     }
79
80     public State getRadioState() {
81         final Boolean power = this.power;
82         if (power == null) {
83             return UnDefType.NULL;
84         }
85         return power && SOURCE_RADIO.equals(source) ? PlayPauseType.PLAY : PlayPauseType.PAUSE;
86     }
87
88     public void enableAux() {
89         power = true;
90         source = SOURCE_AUX;
91         previewLight = false;
92     }
93
94     public State getAuxState() {
95         final Boolean power = this.power;
96         if (power == null) {
97             return UnDefType.NULL;
98         }
99         return OnOffType.from(power && SOURCE_AUX.equals(source));
100     }
101
102     public void setVolume(int percent) {
103         this.volume = percent / 4;
104     }
105
106     public State getVolumeState() {
107         final Integer volume = this.volume;
108         if (volume == null) {
109             return UnDefType.NULL;
110         }
111         return new PercentType(volume * 4);
112     }
113
114     public void setRadioPreset(String preset) {
115         this.preset = preset;
116     }
117
118     public State getPresetState() {
119         final String preset = this.preset;
120         if (preset == null) {
121             return UnDefType.NULL;
122         }
123         return new StringType(preset);
124     }
125 }