]> git.basschouten.com Git - openhab-addons.git/blob
7fdaf5d49c6a0d00bd4b2a002d5304c58b76123f
[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.handler;
14
15 import java.io.IOException;
16 import java.util.Map;
17 import java.util.concurrent.CompletionException;
18
19 import org.eclipse.jetty.websocket.client.WebSocketClient;
20 import org.openhab.core.library.types.StringType;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.types.Command;
24 import org.openhab.core.types.RefreshType;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * The {@link SrsZr5Handler} is responsible for handling commands for SRS-ZR5, which are
30  * sent to one of the channels.
31  *
32  * @author David Ã…berg - Initial contribution
33  */
34 public class SrsZr5Handler extends SonyAudioHandler {
35
36     private final Logger logger = LoggerFactory.getLogger(SrsZr5Handler.class);
37
38     public SrsZr5Handler(Thing thing, WebSocketClient webSocketClient) {
39         super(thing, webSocketClient);
40     }
41
42     @Override
43     public String setInputCommand(Command command) {
44         switch (command.toString().toLowerCase()) {
45             case "btaudio":
46                 return "extInput:btAudio";
47             case "usb":
48                 return "storage:usb1";
49             case "analog":
50                 return "extInput:line?port=1";
51             case "hdmi":
52                 return "extInput:hdmi";
53             case "network":
54                 return "dlna:music";
55             case "cast":
56                 return "cast:audio";
57         }
58         return command.toString();
59     }
60
61     @Override
62     public StringType inputSource(String input) {
63         String in = input.toLowerCase();
64         if (in.contains("extinput:btaudio".toLowerCase())) {
65             return new StringType("btaudio");
66         }
67         if (in.contains("storage:usb1".toLowerCase())) {
68             return new StringType("usb");
69         }
70         if (in.contains("extinput:line?port=1".toLowerCase())) {
71             return new StringType("analog");
72         }
73         if (in.contains("extinput:hdmi".toLowerCase())) {
74             return new StringType("hdmi1");
75         }
76         if (in.contains("dlna:music".toLowerCase())) {
77             return new StringType("network");
78         }
79         if (in.contains("cast:audio".toLowerCase())) {
80             return new StringType("cast");
81         }
82         return new StringType(input);
83     }
84
85     @Override
86     public void handleSoundSettings(Command command, ChannelUID channelUID) throws IOException {
87         if (command instanceof RefreshType) {
88             try {
89                 logger.debug("SrsZr5Handler handleSoundSettings RefreshType");
90                 Map<String, String> result = soundSettingsCache.getValue();
91                 if (result != null) {
92                     logger.debug("SrsZr5Handler Updating sound field to {} {}", result.get("clearAudio"),
93                             result.get("soundField"));
94                     if ("on".equalsIgnoreCase(result.get("clearAudio"))) {
95                         updateState(channelUID, new StringType("clearAudio"));
96                     } else {
97                         updateState(channelUID, new StringType(result.get("soundField")));
98                     }
99                 }
100             } catch (CompletionException ex) {
101                 throw new IOException(ex.getCause());
102             }
103         }
104         if (command instanceof StringType stringCommand) {
105             if ("clearAudio".equalsIgnoreCase(stringCommand.toString())) {
106                 connection.setSoundSettings("clearAudio", "on");
107             } else {
108                 connection.setSoundSettings("soundField", stringCommand.toString());
109             }
110         }
111     }
112 }