[sonyaudio] small cleanup (#15823)

* More SAT fixes

Signed-off-by: Leo Siepel <leosiepel@gmail.com>
This commit is contained in:
lsiepel 2023-10-29 21:57:30 +01:00 committed by GitHub
parent 45760bff62
commit 597f01efe1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 44 deletions

View File

@ -128,19 +128,19 @@ public class SonyAudioDiscoveryParticipant implements UpnpDiscoveryParticipant {
while ((s = bufferedReader.readLine()) != null) { while ((s = bufferedReader.readLine()) != null) {
builder.append(s); builder.append(s);
} }
Pattern ScalarWebAPImatch = Pattern.compile("<av:X_ScalarWebAPI_BaseURL>(.*)</av:X_ScalarWebAPI_BaseURL>"); Pattern scalarWebAPImatch = Pattern.compile("<av:X_ScalarWebAPI_BaseURL>(.*)</av:X_ScalarWebAPI_BaseURL>");
Pattern baseURLmatch = Pattern.compile("http://(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):(\\d+)([^<]*)"); Pattern baseURLmatch = Pattern.compile("http://(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):(\\d+)([^<]*)");
Matcher tagmatch = ScalarWebAPImatch.matcher(builder.toString()); Matcher tagmatch = scalarWebAPImatch.matcher(builder.toString());
if (tagmatch.find()) { if (tagmatch.find()) {
Matcher matcher = baseURLmatch.matcher(tagmatch.group()); Matcher matcher = baseURLmatch.matcher(tagmatch.group());
matcher.find(); matcher.find();
// String scalar_host = matcher.group(0); // String scalar_host = matcher.group(0);
int scalar_port = Integer.parseInt(matcher.group(2)); int scalarPort = Integer.parseInt(matcher.group(2));
String scalar_path = matcher.group(3); String scalarPath = matcher.group(3);
properties.put(SonyAudioBindingConstants.SCALAR_PORT_PARAMETER, scalar_port); properties.put(SonyAudioBindingConstants.SCALAR_PORT_PARAMETER, scalarPort);
properties.put(SonyAudioBindingConstants.SCALAR_PATH_PARAMETER, scalar_path); properties.put(SonyAudioBindingConstants.SCALAR_PATH_PARAMETER, scalarPath);
} }
return properties; return properties;
} }

View File

@ -64,7 +64,7 @@ abstract class SonyAudioHandler extends BaseThingHandler implements SonyAudioEve
private ScheduledFuture<?> refreshJob; private ScheduledFuture<?> refreshJob;
private int currentRadioStation = 0; private int currentRadioStation = 0;
private final Map<Integer, String> input_zone = new HashMap<>(); private final Map<Integer, String> inputZone = new HashMap<>();
private static final long CACHE_EXPIRY = TimeUnit.SECONDS.toMillis(5); private static final long CACHE_EXPIRY = TimeUnit.SECONDS.toMillis(5);
@ -303,13 +303,13 @@ abstract class SonyAudioHandler extends BaseThingHandler implements SonyAudioEve
SonyAudioConnection.SonyAudioInput result = inputCache[zone].getValue(); SonyAudioConnection.SonyAudioInput result = inputCache[zone].getValue();
if (result != null) { if (result != null) {
if (zone > 0) { if (zone > 0) {
input_zone.put(zone, result.input); inputZone.put(zone, result.input);
} }
updateState(channelUID, inputSource(result.input)); updateState(channelUID, inputSource(result.input));
if (result.radio_freq.isPresent()) { if (result.radioFrequency.isPresent()) {
updateState(SonyAudioBindingConstants.CHANNEL_RADIO_FREQ, updateState(SonyAudioBindingConstants.CHANNEL_RADIO_FREQ,
new DecimalType(result.radio_freq.get() / 1000000.0)); new DecimalType(result.radioFrequency.get() / 1000000.0));
} }
} }
} catch (CompletionException ex) { } catch (CompletionException ex) {
@ -387,7 +387,7 @@ abstract class SonyAudioHandler extends BaseThingHandler implements SonyAudioEve
String radioCommand = "radio:fm?contentId=" + currentRadioStation; String radioCommand = "radio:fm?contentId=" + currentRadioStation;
for (int i = 1; i <= 4; i++) { for (int i = 1; i <= 4; i++) {
String input = input_zone.get(i); String input = inputZone.get(i);
if (input != null && input.startsWith("radio:fm")) { if (input != null && input.startsWith("radio:fm")) {
connection.setInput(radioCommand, i); connection.setInput(radioCommand, i);
} }
@ -418,20 +418,20 @@ abstract class SonyAudioHandler extends BaseThingHandler implements SonyAudioEve
Configuration config = getThing().getConfiguration(); Configuration config = getThing().getConfiguration();
String ipAddress = (String) config.get(SonyAudioBindingConstants.HOST_PARAMETER); String ipAddress = (String) config.get(SonyAudioBindingConstants.HOST_PARAMETER);
String path = (String) config.get(SonyAudioBindingConstants.SCALAR_PATH_PARAMETER); String path = (String) config.get(SonyAudioBindingConstants.SCALAR_PATH_PARAMETER);
Object port_o = config.get(SonyAudioBindingConstants.SCALAR_PORT_PARAMETER); Object portO = config.get(SonyAudioBindingConstants.SCALAR_PORT_PARAMETER);
int port = 10000; int port = 10000;
if (port_o instanceof BigDecimal decimalValue) { if (portO instanceof BigDecimal decimalValue) {
port = decimalValue.intValue(); port = decimalValue.intValue();
} else if (port_o instanceof Integer) { } else if (portO instanceof Integer) {
port = (int) port_o; port = (int) portO;
} }
Object refresh_o = config.get(SonyAudioBindingConstants.REFRESHINTERVAL); Object refreshO = config.get(SonyAudioBindingConstants.REFRESHINTERVAL);
int refresh = 0; int refresh = 0;
if (refresh_o instanceof BigDecimal decimalValue) { if (refreshO instanceof BigDecimal decimalValue) {
refresh = decimalValue.intValue(); refresh = decimalValue.intValue();
} else if (refresh_o instanceof Integer) { } else if (refreshO instanceof Integer) {
refresh = (int) refresh_o; refresh = (int) refreshO;
} }
try { try {
@ -509,9 +509,9 @@ abstract class SonyAudioHandler extends BaseThingHandler implements SonyAudioEve
break; break;
} }
if (input.radio_freq.isPresent()) { if (input.radioFrequency.isPresent()) {
updateState(SonyAudioBindingConstants.CHANNEL_RADIO_FREQ, updateState(SonyAudioBindingConstants.CHANNEL_RADIO_FREQ,
new DecimalType(input.radio_freq.get() / 1000000.0)); new DecimalType(input.radioFrequency.get() / 1000000.0));
} }
} }

View File

@ -49,7 +49,7 @@ public class SonyAudioConnection implements SonyAudioClientSocketEventListener {
private final String host; private final String host;
private final int port; private final int port;
private final String path; private final String path;
private final URI base_uri; private final URI baseUri;
private final WebSocketClient webSocketClient; private final WebSocketClient webSocketClient;
@ -59,8 +59,8 @@ public class SonyAudioConnection implements SonyAudioClientSocketEventListener {
private final SonyAudioEventListener listener; private final SonyAudioEventListener listener;
private int min_volume = 0; private int minVolume = 0;
private int max_volume = 50; private int maxVolume = 50;
private final Gson gson; private final Gson gson;
@ -73,14 +73,14 @@ public class SonyAudioConnection implements SonyAudioClientSocketEventListener {
this.gson = new Gson(); this.gson = new Gson();
this.webSocketClient = webSocketClient; this.webSocketClient = webSocketClient;
base_uri = new URI(String.format("ws://%s:%d/%s", host, port, path)).normalize(); baseUri = new URI(String.format("ws://%s:%d/%s", host, port, path)).normalize();
URI wsAvContentUri = base_uri.resolve(base_uri.getPath() + "/avContent").normalize(); URI wsAvContentUri = baseUri.resolve(baseUri.getPath() + "/avContent").normalize();
avContentSocket = new SonyAudioClientSocket(this, wsAvContentUri, scheduler); avContentSocket = new SonyAudioClientSocket(this, wsAvContentUri, scheduler);
URI wsAudioUri = base_uri.resolve(base_uri.getPath() + "/audio").normalize(); URI wsAudioUri = baseUri.resolve(baseUri.getPath() + "/audio").normalize();
audioSocket = new SonyAudioClientSocket(this, wsAudioUri, scheduler); audioSocket = new SonyAudioClientSocket(this, wsAudioUri, scheduler);
URI wsSystemUri = base_uri.resolve(base_uri.getPath() + "/system").normalize(); URI wsSystemUri = baseUri.resolve(baseUri.getPath() + "/system").normalize();
systemSocket = new SonyAudioClientSocket(this, wsSystemUri, scheduler); systemSocket = new SonyAudioClientSocket(this, wsSystemUri, scheduler);
} }
@ -123,7 +123,7 @@ public class SonyAudioConnection implements SonyAudioClientSocketEventListener {
input.input = param.get("uri").getAsString(); input.input = param.get("uri").getAsString();
if (param.has("broadcastFreq")) { if (param.has("broadcastFreq")) {
int freq = param.get("broadcastFreq").getAsInt(); int freq = param.get("broadcastFreq").getAsInt();
input.radio_freq = Optional.of(freq); input.radioFrequency = Optional.of(freq);
checkRadioPreset(input.input); checkRadioPreset(input.input);
} }
listener.updateInput(zone, input); listener.updateInput(zone, input);
@ -134,7 +134,7 @@ public class SonyAudioConnection implements SonyAudioClientSocketEventListener {
SonyAudioVolume volume = new SonyAudioVolume(); SonyAudioVolume volume = new SonyAudioVolume();
int rawVolume = param.get("volume").getAsInt(); int rawVolume = param.get("volume").getAsInt();
volume.volume = Math.round(100 * (rawVolume - min_volume) / (max_volume - min_volume)); volume.volume = Math.round(100 * (rawVolume - minVolume) / (maxVolume - minVolume));
volume.mute = "on".equalsIgnoreCase(param.get("mute").getAsString()); volume.mute = "on".equalsIgnoreCase(param.get("mute").getAsString());
listener.updateVolume(zone, volume); listener.updateVolume(zone, volume);
@ -274,8 +274,8 @@ public class SonyAudioConnection implements SonyAudioClientSocketEventListener {
} }
public String getConnectionName() { public String getConnectionName() {
if (base_uri != null) { if (baseUri != null) {
return base_uri.toString(); return baseUri.toString();
} }
return String.format("ws://%s:%d/%s", host, port, path); return String.format("ws://%s:%d/%s", host, port, path);
} }
@ -292,8 +292,9 @@ public class SonyAudioConnection implements SonyAudioClientSocketEventListener {
Iterator<JsonElement> terminals = element.getAsJsonArray().get(0).getAsJsonArray().iterator(); Iterator<JsonElement> terminals = element.getAsJsonArray().get(0).getAsJsonArray().iterator();
while (terminals.hasNext()) { while (terminals.hasNext()) {
JsonObject terminal = terminals.next().getAsJsonObject(); JsonObject terminal = terminals.next().getAsJsonObject();
String zoneUri = "extOutput:zone?zone=" + Integer.toString(zone);
String uri = terminal.get("uri").getAsString(); String uri = terminal.get("uri").getAsString();
if (uri.equalsIgnoreCase("extOutput:zone?zone=" + Integer.toString(zone))) { if (uri.equalsIgnoreCase(zoneUri)) {
return "active".equalsIgnoreCase(terminal.get("active").getAsString()) ? true : false; return "active".equalsIgnoreCase(terminal.get("active").getAsString()) ? true : false;
} }
} }
@ -338,7 +339,7 @@ public class SonyAudioConnection implements SonyAudioClientSocketEventListener {
public class SonyAudioInput { public class SonyAudioInput {
public String input = ""; public String input = "";
public Optional<Integer> radio_freq = Optional.empty(); public Optional<Integer> radioFrequency = Optional.empty();
} }
public SonyAudioInput getInput() throws IOException { public SonyAudioInput getInput() throws IOException {
@ -367,7 +368,7 @@ public class SonyAudioConnection implements SonyAudioClientSocketEventListener {
if (result.has("broadcastFreq")) { if (result.has("broadcastFreq")) {
int freq = result.get("broadcastFreq").getAsInt(); int freq = result.get("broadcastFreq").getAsInt();
ret.radio_freq = Optional.of(freq); ret.radioFrequency = Optional.of(freq);
} }
return ret; return ret;
} }
@ -425,9 +426,9 @@ public class SonyAudioConnection implements SonyAudioClientSocketEventListener {
SonyAudioVolume ret = new SonyAudioVolume(); SonyAudioVolume ret = new SonyAudioVolume();
int volume = result.get("volume").getAsInt(); int volume = result.get("volume").getAsInt();
min_volume = result.get("minVolume").getAsInt(); minVolume = result.get("minVolume").getAsInt();
max_volume = result.get("maxVolume").getAsInt(); maxVolume = result.get("maxVolume").getAsInt();
int vol = Math.round(100 * (volume - min_volume) / (max_volume - min_volume)); int vol = Math.round(100 * (volume - minVolume) / (maxVolume - minVolume));
if (vol < 0) { if (vol < 0) {
vol = 0; vol = 0;
} }
@ -445,7 +446,7 @@ public class SonyAudioConnection implements SonyAudioClientSocketEventListener {
if (audioSocket == null) { if (audioSocket == null) {
throw new IOException("Audio Socket not connected"); throw new IOException("Audio Socket not connected");
} }
SetAudioVolume setAudioVolume = new SetAudioVolume(volume, min_volume, max_volume); SetAudioVolume setAudioVolume = new SetAudioVolume(volume, minVolume, maxVolume);
audioSocket.callMethod(setAudioVolume); audioSocket.callMethod(setAudioVolume);
} }
@ -461,7 +462,7 @@ public class SonyAudioConnection implements SonyAudioClientSocketEventListener {
if (audioSocket == null) { if (audioSocket == null) {
throw new IOException("Audio Socket not connected"); throw new IOException("Audio Socket not connected");
} }
SetAudioVolume setAudioVolume = new SetAudioVolume(zone, volume, min_volume, max_volume); SetAudioVolume setAudioVolume = new SetAudioVolume(zone, volume, minVolume, maxVolume);
audioSocket.callMethod(setAudioVolume); audioSocket.callMethod(setAudioVolume);
} }

View File

@ -243,14 +243,14 @@ class SetAudioVolume extends SonyAudioMethod {
SetAudioVolume(int volume, int min, int max) { SetAudioVolume(int volume, int min, int max) {
super("setAudioVolume", "1.1"); super("setAudioVolume", "1.1");
long scaled_volume = scaleVolume(volume, min, max); long scaledVolume = scaleVolume(volume, min, max);
params = new Param[] { new Param(scaled_volume) }; params = new Param[] { new Param(scaledVolume) };
} }
SetAudioVolume(int zone, int volume, int min, int max) { SetAudioVolume(int zone, int volume, int min, int max) {
super("setAudioVolume", "1.1"); super("setAudioVolume", "1.1");
long scaled_volume = scaleVolume(volume, min, max); long scaledVolume = scaleVolume(volume, min, max);
params = new Param[] { new Param(scaled_volume, zone) }; params = new Param[] { new Param(scaledVolume, zone) };
} }
SetAudioVolume(String volume_change) { SetAudioVolume(String volume_change) {