2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.fsinternetradio.internal.radio;
15 import static org.openhab.binding.fsinternetradio.internal.radio.FrontierSiliconRadioConstants.*;
17 import java.io.IOException;
19 import org.eclipse.jetty.client.HttpClient;
22 * Class representing an internet radio based on the frontier silicon chipset. Tested with "hama IR110" and Medion
23 * MD87180" internet radios.
25 * @author Rainer Ostendorf
26 * @author Patrick Koenemann
27 * @author Mihaela Memova - removed duplicated check for the percent value range
29 public class FrontierSiliconRadio {
31 /** The http connection/session used for controlling the radio. */
32 private final FrontierSiliconRadioConnection conn;
34 /** the volume of the radio. we cache it for fast increase/decrease. */
35 private int currentVolume = 0;
38 * Constructor for the Radio class
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
45 * @author Rainer Ostendorf
47 public FrontierSiliconRadio(String hostname, int port, String pin, HttpClient httpClient) {
48 this.conn = new FrontierSiliconRadioConnection(hostname, port, pin, httpClient);
51 public boolean isLoggedIn() {
52 return conn.isLoggedIn();
56 * Perform login to the radio and establish new session
58 * @author Rainer Ostendorf
59 * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
61 public boolean login() throws IOException {
62 return conn.doLogin();
66 * get the radios power state
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.
71 public boolean getPower() throws IOException {
72 final FrontierSiliconRadioApiResult result = conn.doRequest(REQUEST_GET_POWER);
73 return result.getValueU8AsBoolean();
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.
83 public void setPower(boolean powerOn) throws IOException {
84 final String params = "value=" + (powerOn ? "1" : "0");
85 conn.doRequest(REQUEST_SET_POWER, params);
89 * read the volume (as absolute value, 0-32)
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.
94 public int getVolumeAbsolute() throws IOException {
95 FrontierSiliconRadioApiResult result = conn.doRequest(REQUEST_GET_VOLUME);
96 currentVolume = result.getValueU8AsInt();
101 * read the volume (as percent value, 0-100)
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.
106 public int getVolumePercent() throws IOException {
107 FrontierSiliconRadioApiResult result = conn.doRequest(REQUEST_GET_VOLUME);
108 currentVolume = result.getValueU8AsInt();
109 return (currentVolume * 100) / 32;
113 * Set the radios 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.
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;
127 * Set the radios volume in percent
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.
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;
141 * Increase radio volume by 1 step, max is 32.
143 * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
145 public void increaseVolumeAbsolute() throws IOException {
146 if (currentVolume < 32) {
147 setVolumeAbsolute(currentVolume + 1);
152 * Decrease radio volume by 1 step.
154 * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
156 public void decreaseVolumeAbsolute() throws IOException {
157 if (currentVolume > 0) {
158 setVolumeAbsolute(currentVolume - 1);
163 * Read the radios operating mode
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.
168 public int getMode() throws IOException {
169 FrontierSiliconRadioApiResult result = conn.doRequest(REQUEST_GET_MODE);
170 return result.getValueU32AsInt();
174 * Set the radio operating 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.
180 public void setMode(int mode) throws IOException {
181 final String params = "value=" + mode;
182 conn.doRequest(REQUEST_SET_MODE, params);
186 * Read the Station info name, e.g. "WDR2"
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.
191 public String getPlayInfoName() throws IOException {
192 FrontierSiliconRadioApiResult result = conn.doRequest(REQUEST_GET_PLAY_INFO_NAME);
193 return result.getValueC8ArrayAsString();
197 * read the stations radio text like the song name currently playing
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.
202 public String getPlayInfoText() throws IOException {
203 FrontierSiliconRadioApiResult result = conn.doRequest(REQUEST_GET_PLAY_INFO_TEXT);
204 return result.getValueC8ArrayAsString();
208 * set a station preset. Tunes the radio to a preselected station.
211 * @throws IOException if communication with the radio failed, e.g. because the device is not reachable.
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");
220 * read the muted state
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.
225 public boolean getMuted() throws IOException {
226 FrontierSiliconRadioApiResult result = conn.doRequest(REQUEST_GET_MUTE);
227 return result.getValueU8AsBoolean();
231 * mute the radio volume
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.
237 public void setMuted(boolean muted) throws IOException {
238 final String params = "value=" + (muted ? "1" : "0");
239 conn.doRequest(REQUEST_SET_MUTE, params);