]> git.basschouten.com Git - openhab-addons.git/blob
4c66dbf616014179f6ce29b27347fc3f229694de
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.fsinternetradio.internal.radio;
14
15 import static org.openhab.binding.fsinternetradio.internal.radio.FrontierSiliconRadioConstants.*;
16
17 import java.io.IOException;
18
19 import org.eclipse.jetty.client.HttpClient;
20
21 /**
22  * Class representing an internet radio based on the frontier silicon chipset. Tested with "hama IR110" and Medion
23  * MD87180" internet radios.
24  *
25  * @author Rainer Ostendorf
26  * @author Patrick Koenemann
27  * @author Mihaela Memova - removed duplicated check for the percent value range
28  */
29 public class FrontierSiliconRadio {
30
31     /** The http connection/session used for controlling the radio. */
32     private final FrontierSiliconRadioConnection conn;
33
34     /** the volume of the radio. we cache it for fast increase/decrease. */
35     private int currentVolume = 0;
36
37     /**
38      * Constructor for the Radio class
39      *
40      * @param hostname Host name of the Radio addressed, e.g. "192.168.0.100"
41      * @param port Port number, default: 80 (http)
42      * @param pin Access PIN number of the radio. Must be 4 digits, e.g. "1234"
43      * @param httpClient http client instance to use
44      *
45      * @author Rainer Ostendorf
46      */
47     public FrontierSiliconRadio(String hostname, int port, String pin, HttpClient httpClient) {
48         this.conn = new FrontierSiliconRadioConnection(hostname, port, pin, httpClient);
49     }
50
51     public boolean isLoggedIn() {
52         return conn.isLoggedIn();
53     }
54
55     /**
56      * Perform login to the radio and establish new session
57      *
58      * @author Rainer Ostendorf
59      * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
60      */
61     public boolean login() throws IOException {
62         return conn.doLogin();
63     }
64
65     /**
66      * get the radios power state
67      *
68      * @return true when radio is on, false when radio is off
69      * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
70      */
71     public boolean getPower() throws IOException {
72         final FrontierSiliconRadioApiResult result = conn.doRequest(REQUEST_GET_POWER);
73         return result.getValueU8AsBoolean();
74     }
75
76     /**
77      * Turn radio on/off
78      *
79      * @param powerOn
80      *            true turns on the radio, false turns it off
81      * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
82      */
83     public void setPower(boolean powerOn) throws IOException {
84         final String params = "value=" + (powerOn ? "1" : "0");
85         conn.doRequest(REQUEST_SET_POWER, params);
86     }
87
88     /**
89      * read the volume (as absolute value, 0-32)
90      *
91      * @return volume: 0=muted, 32=max. volume
92      * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
93      */
94     public int getVolumeAbsolute() throws IOException {
95         FrontierSiliconRadioApiResult result = conn.doRequest(REQUEST_GET_VOLUME);
96         currentVolume = result.getValueU8AsInt();
97         return currentVolume;
98     }
99
100     /**
101      * read the volume (as percent value, 0-100)
102      *
103      * @return volume: 0=muted, 100=max. volume (100 corresponds 32 absolute value)
104      * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
105      */
106     public int getVolumePercent() throws IOException {
107         FrontierSiliconRadioApiResult result = conn.doRequest(REQUEST_GET_VOLUME);
108         currentVolume = result.getValueU8AsInt();
109         return (currentVolume * 100) / 32;
110     }
111
112     /**
113      * Set the radios volume
114      *
115      * @param volume
116      *            Radio volume: 0=mute, 32=max. volume
117      * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
118      */
119     public void setVolumeAbsolute(int volume) throws IOException {
120         final int newVolume = volume < 0 ? 0 : volume > 32 ? 32 : volume;
121         final String params = "value=" + newVolume;
122         conn.doRequest(REQUEST_SET_VOLUME, params);
123         currentVolume = volume;
124     }
125
126     /**
127      * Set the radios volume in percent
128      *
129      * @param volume
130      *            Radio volume: 0=muted, 100=max. volume (100 corresponds 32 absolute value)
131      * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
132      */
133     public void setVolumePercent(int volume) throws IOException {
134         final int newVolumeAbsolute = (volume * 32) / 100;
135         final String params = "value=" + newVolumeAbsolute;
136         conn.doRequest(REQUEST_SET_VOLUME, params);
137         currentVolume = volume;
138     }
139
140     /**
141      * Increase radio volume by 1 step, max is 32.
142      *
143      * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
144      */
145     public void increaseVolumeAbsolute() throws IOException {
146         if (currentVolume < 32) {
147             setVolumeAbsolute(currentVolume + 1);
148         }
149     }
150
151     /**
152      * Decrease radio volume by 1 step.
153      *
154      * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
155      */
156     public void decreaseVolumeAbsolute() throws IOException {
157         if (currentVolume > 0) {
158             setVolumeAbsolute(currentVolume - 1);
159         }
160     }
161
162     /**
163      * Read the radios operating mode
164      *
165      * @return operating mode. On hama radio: 0="Internet Radio", 1=Spotify, 2=Player, 3="AUX IN"
166      * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
167      */
168     public int getMode() throws IOException {
169         FrontierSiliconRadioApiResult result = conn.doRequest(REQUEST_GET_MODE);
170         return result.getValueU32AsInt();
171     }
172
173     /**
174      * Set the radio operating mode
175      *
176      * @param mode
177      *            On hama radio: 0="Internet Radio", 1=Spotify, 2=Player, 3="AUX IN"
178      * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
179      */
180     public void setMode(int mode) throws IOException {
181         final String params = "value=" + mode;
182         conn.doRequest(REQUEST_SET_MODE, params);
183     }
184
185     /**
186      * Read the Station info name, e.g. "WDR2"
187      *
188      * @return the station name, e.g. "WDR2"
189      * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
190      */
191     public String getPlayInfoName() throws IOException {
192         FrontierSiliconRadioApiResult result = conn.doRequest(REQUEST_GET_PLAY_INFO_NAME);
193         return result.getValueC8ArrayAsString();
194     }
195
196     /**
197      * read the stations radio text like the song name currently playing
198      *
199      * @return the radio info text, e.g. music title
200      * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
201      */
202     public String getPlayInfoText() throws IOException {
203         FrontierSiliconRadioApiResult result = conn.doRequest(REQUEST_GET_PLAY_INFO_TEXT);
204         return result.getValueC8ArrayAsString();
205     }
206
207     /**
208      * set a station preset. Tunes the radio to a preselected station.
209      *
210      * @param presetId
211      * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
212      */
213     public void setPreset(Integer presetId) throws IOException {
214         conn.doRequest(REQUEST_SET_PRESET, "value=1");
215         conn.doRequest(REQUEST_SET_PRESET_ACTION, "value=" + presetId.toString());
216         conn.doRequest(REQUEST_SET_PRESET, "value=0");
217     }
218
219     /**
220      * read the muted state
221      *
222      * @return true: radio is muted, false: radio is not muted
223      * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
224      */
225     public boolean getMuted() throws IOException {
226         FrontierSiliconRadioApiResult result = conn.doRequest(REQUEST_GET_MUTE);
227         return result.getValueU8AsBoolean();
228     }
229
230     /**
231      * mute the radio volume
232      *
233      * @param muted
234      *            true: mute the radio, false: unmute the radio
235      * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
236      */
237     public void setMuted(boolean muted) throws IOException {
238         final String params = "value=" + (muted ? "1" : "0");
239         conn.doRequest(REQUEST_SET_MUTE, params);
240     }
241 }