]> git.basschouten.com Git - openhab-addons.git/blob
49b8d87533879a8c9367813fa12c024056f09a04
[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.sonyaudio.internal.protocol;
14
15 import java.util.List;
16
17 /**
18  * The {@link SonyAudioMethod} base class for SONY API methods
19  *
20  * @author David Åberg - Initial contribution
21  */
22 public abstract class SonyAudioMethod {
23     protected int id = 1;
24     protected String method;
25     protected String version;
26
27     public SonyAudioMethod(String method, String version) {
28         this.method = method;
29         this.version = version;
30     }
31 }
32
33 /**
34  * The {@link GetPowerStatus} SONY Audio control API method
35  *
36  * @author David Åberg - Initial contribution
37  */
38 class GetPowerStatus extends SonyAudioMethod {
39     public String[] params = new String[] {};
40
41     public GetPowerStatus() {
42         super("getPowerStatus", "1.1");
43     }
44 }
45
46 /**
47  * The {@link GetCurrentExternalTerminalsStatus} SONY Audio control API method
48  *
49  * @author David Åberg - Initial contribution
50  */
51 class GetCurrentExternalTerminalsStatus extends SonyAudioMethod {
52     public String[] params = new String[] {};
53
54     public GetCurrentExternalTerminalsStatus() {
55         super("getCurrentExternalTerminalsStatus", "1.0");
56     }
57 }
58
59 /**
60  * The {@link SetPowerStatus} SONY Audio control API method
61  *
62  * @author David Åberg - Initial contribution
63  */
64 class SetPowerStatus extends SonyAudioMethod {
65     public Param[] params;
66
67     class Param {
68         public String status;
69
70         Param(boolean power) {
71             status = power ? "active" : "off";
72         }
73     }
74
75     SetPowerStatus(boolean power) {
76         super("setPowerStatus", "1.1");
77         Param param = new Param(power);
78         params = new Param[] { param };
79     }
80 }
81
82 /**
83  * The {@link SetActiveTerminal} SONY Audio control API method
84  *
85  * @author David Åberg - Initial contribution
86  */
87 class SetActiveTerminal extends SonyAudioMethod {
88     public Param[] params;
89
90     class Param {
91         public String active;
92         public String uri;
93
94         Param(boolean power, int zone) {
95             active = power ? "active" : "inactive";
96             if (zone > 0) {
97                 uri = "extOutput:zone?zone=" + Integer.toString(zone);
98             }
99         }
100     }
101
102     SetActiveTerminal(boolean power, int zone) {
103         super("setActiveTerminal", "1.0");
104         Param param = new Param(power, zone);
105         params = new Param[] { param };
106     }
107 }
108
109 /**
110  * The {@link GetPlayingContentInfo} SONY Audio control API method
111  *
112  * @author David Åberg - Initial contribution
113  */
114 class GetPlayingContentInfo extends SonyAudioMethod {
115     public Param[] params;
116
117     class Param {
118         String output = "";
119
120         Param() {
121         }
122
123         Param(int zone) {
124             if (zone > 0) {
125                 output = "extOutput:zone?zone=" + Integer.toString(zone);
126             }
127         }
128     }
129
130     GetPlayingContentInfo() {
131         super("getPlayingContentInfo", "1.2");
132         Param param = new Param();
133         params = new Param[] { param };
134     }
135
136     GetPlayingContentInfo(int zone) {
137         super("getPlayingContentInfo", "1.2");
138         Param param = new Param(zone);
139         params = new Param[] { param };
140     }
141 }
142
143 /**
144  * The {@link SetPlayContent} SONY Audio control API method
145  *
146  * @author David Åberg - Initial contribution
147  */
148 class SetPlayContent extends SonyAudioMethod {
149     public Param[] params;
150
151     class Param {
152         String output = "";
153         String uri;
154
155         Param(String input) {
156             uri = input;
157         }
158
159         Param(String input, int zone) {
160             uri = input;
161             if (zone > 0) {
162                 output = "extOutput:zone?zone=" + Integer.toString(zone);
163             }
164         }
165     }
166
167     SetPlayContent(String input) {
168         super("setPlayContent", "1.2");
169         params = new Param[] { new Param(input) };
170     }
171
172     SetPlayContent(String input, int zone) {
173         super("setPlayContent", "1.2");
174         params = new Param[] { new Param(input, zone) };
175     }
176 }
177
178 /**
179  * The {@link GetVolumeInformation} SONY Audio control API method
180  *
181  * @author David Åberg - Initial contribution
182  */
183 class GetVolumeInformation extends SonyAudioMethod {
184     public Param[] params;
185
186     class Param {
187         String output = "";
188
189         Param() {
190         }
191
192         Param(int zone) {
193             if (zone > 0) {
194                 output = "extOutput:zone?zone=" + Integer.toString(zone);
195             }
196         }
197     }
198
199     GetVolumeInformation() {
200         this(0);
201     }
202
203     GetVolumeInformation(int zone) {
204         super("getVolumeInformation", "1.1");
205         params = new Param[] { new Param(zone) };
206     }
207 }
208
209 /**
210  * The {@link SetAudioVolume} SONY Audio control API method
211  *
212  * @author David Åberg - Initial contribution
213  */
214 class SetAudioVolume extends SonyAudioMethod {
215     public Param[] params;
216
217     class Param {
218         String output = "";
219         String volume;
220
221         Param(long new_volume) {
222             volume = Long.toString(new_volume);
223         }
224
225         Param(String volume_change) {
226             volume = volume_change;
227         }
228
229         Param(long new_volume, int zone) {
230             volume = Long.toString(new_volume);
231             if (zone > 0) {
232                 output = "extOutput:zone?zone=" + Integer.toString(zone);
233             }
234         }
235
236         Param(String volume_change, int zone) {
237             volume = volume_change;
238             if (zone > 0) {
239                 output = "extOutput:zone?zone=" + Integer.toString(zone);
240             }
241         }
242     }
243
244     SetAudioVolume(int volume, int min, int max) {
245         super("setAudioVolume", "1.1");
246         long scaled_volume = scaleVolume(volume, min, max);
247         params = new Param[] { new Param(scaled_volume) };
248     }
249
250     SetAudioVolume(int zone, int volume, int min, int max) {
251         super("setAudioVolume", "1.1");
252         long scaled_volume = scaleVolume(volume, min, max);
253         params = new Param[] { new Param(scaled_volume, zone) };
254     }
255
256     SetAudioVolume(String volume_change) {
257         super("setAudioVolume", "1.1");
258         params = new Param[] { new Param(volume_change) };
259     }
260
261     SetAudioVolume(int zone, String volume_change) {
262         super("setAudioVolume", "1.1");
263         params = new Param[] { new Param(volume_change, zone) };
264     }
265
266     long scaleVolume(int volume, int min, int max) {
267         return Math.round(((max - min) * volume / 100.0) + min);
268     }
269 }
270
271 /**
272  * The {@link SetAudioMute} SONY Audio control API method
273  *
274  * @author David Åberg - Initial contribution
275  */
276 class SetAudioMute extends SonyAudioMethod {
277     public Param[] params;
278
279     class Param {
280         String output = "";
281         String mute;
282
283         Param(boolean mute) {
284             this.mute = mute ? "on" : "off";
285         }
286
287         Param(boolean mute, int zone) {
288             this.mute = mute ? "on" : "off";
289             if (zone > 0) {
290                 output = "extOutput:zone?zone=" + Integer.toString(zone);
291             }
292         }
293     }
294
295     SetAudioMute(boolean mute) {
296         super("setAudioMute", "1.1");
297         params = new Param[] { new Param(mute) };
298     }
299
300     SetAudioMute(boolean mute, int zone) {
301         super("setAudioMute", "1.1");
302         params = new Param[] { new Param(mute, zone) };
303     }
304 }
305
306 /**
307  * Helper class
308  *
309  * @author David Åberg - Initial contribution
310  */
311 class GetSoundSettings extends SonyAudioMethod {
312     public Param[] params;
313
314     class Param {
315         String target;
316
317         Param(String target) {
318             this.target = target;
319         }
320     }
321
322     GetSoundSettings() {
323         super("getSoundSettings", "1.1");
324         params = new Param[] { new Param("") };
325     }
326
327     GetSoundSettings(String target) {
328         super("getSoundSettings", "1.1");
329         params = new Param[] { new Param(target) };
330     }
331 }
332
333 /**
334  * Helper class
335  *
336  * @author David Åberg - Initial contribution
337  */
338 class SetSoundSettings extends SonyAudioMethod {
339     public Param[] params;
340
341     class Settings {
342         String value;
343         String target;
344
345         Settings(String target, String value) {
346             this.target = target;
347             this.value = value;
348         }
349     }
350
351     class Param {
352
353         Settings[] settings;
354
355         Param(Settings[] settings) {
356             this.settings = settings;
357         }
358     }
359
360     SetSoundSettings() {
361         super("setSoundSettings", "1.1");
362     }
363
364     SetSoundSettings(String target, String value) {
365         super("setSoundSettings", "1.1");
366         Settings[] settings = { new Settings(target, value) };
367         params = new Param[] { new Param(settings) };
368     }
369 }
370
371 /**
372  * The {@link SetSoundField} SONY Audio control API method
373  *
374  * @author David Åberg - Initial contribution
375  */
376 class SetSoundField extends SetSoundSettings {
377     SetSoundField(String soundField) {
378         super("soundField", soundField);
379     }
380 }
381
382 /**
383  * The {@link SetPureDirect} SONY Audio control API method
384  *
385  * @author David Åberg - Initial contribution
386  */
387 class SetPureDirect extends SetSoundSettings {
388     SetPureDirect(boolean pureDirect) {
389         super("pureDirect", pureDirect ? "on" : "off");
390     }
391 }
392
393 /**
394  * The {@link SetClearAudio} SONY Audio control API method
395  *
396  * @author David Åberg - Initial contribution
397  */
398 class SetClearAudio extends SetSoundSettings {
399     SetClearAudio(boolean clearAudio) {
400         super("clearAudio", clearAudio ? "on" : "off");
401     }
402 }
403
404 /**
405  * The {@link SwitchNotifications} SONY Audio control API method
406  *
407  * @author David Åberg - Initial contribution
408  */
409 class SwitchNotifications extends SonyAudioMethod {
410     public Param[] params;
411
412     class Notification {
413         String name;
414         String version;
415
416         @Override
417         public String toString() {
418             return "Notification{name='" + name + "' version='" + version + "'}";
419         }
420     }
421
422     class Param {
423         List<Notification> enabled;
424         List<Notification> disabled;
425
426         Param(List<Notification> enabled, List<Notification> disabled) {
427             this.enabled = enabled;
428             this.disabled = disabled;
429         }
430     }
431
432     SwitchNotifications(List<Notification> enabled, List<Notification> disabled) {
433         super("switchNotifications", "1.0");
434         params = new Param[] { new Param(!enabled.isEmpty() ? enabled : null, !disabled.isEmpty() ? disabled : null) };
435     }
436 }
437
438 /**
439  * The {@link SeekBroadcastStation} SONY Audio control API method
440  *
441  * @author David Åberg - Initial contribution
442  */
443 class SeekBroadcastStation extends SonyAudioMethod {
444     public Param[] params;
445
446     class Param {
447         String tuning = "auto";
448         String direction;
449
450         Param(boolean forward) {
451             this.direction = forward ? "fwd" : "bwd";
452         }
453     }
454
455     SeekBroadcastStation(boolean forward) {
456         super("seekBroadcastStation", "1.0");
457         params = new Param[] { new Param(forward) };
458     }
459 }