Skip to content

Commit

Permalink
[sonyaudio] small cleanup (openhab#15823)
Browse files Browse the repository at this point in the history
* More SAT fixes

Signed-off-by: Leo Siepel <[email protected]>
  • Loading branch information
lsiepel authored and andrewfg committed Nov 26, 2023
1 parent 0b5e395 commit a7ea27f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,19 @@ private Map<String, Object> getDescription(String host, int port, String path) t
while ((s = bufferedReader.readLine()) != null) {
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+)([^<]*)");

Matcher tagmatch = ScalarWebAPImatch.matcher(builder.toString());
Matcher tagmatch = scalarWebAPImatch.matcher(builder.toString());
if (tagmatch.find()) {
Matcher matcher = baseURLmatch.matcher(tagmatch.group());
matcher.find();
// String scalar_host = matcher.group(0);
int scalar_port = Integer.parseInt(matcher.group(2));
String scalar_path = matcher.group(3);
int scalarPort = Integer.parseInt(matcher.group(2));
String scalarPath = matcher.group(3);

properties.put(SonyAudioBindingConstants.SCALAR_PORT_PARAMETER, scalar_port);
properties.put(SonyAudioBindingConstants.SCALAR_PATH_PARAMETER, scalar_path);
properties.put(SonyAudioBindingConstants.SCALAR_PORT_PARAMETER, scalarPort);
properties.put(SonyAudioBindingConstants.SCALAR_PATH_PARAMETER, scalarPath);
}
return properties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ abstract class SonyAudioHandler extends BaseThingHandler implements SonyAudioEve
private ScheduledFuture<?> refreshJob;

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);

Expand Down Expand Up @@ -303,13 +303,13 @@ public void handleInputCommand(Command command, ChannelUID channelUID, int zone)
SonyAudioConnection.SonyAudioInput result = inputCache[zone].getValue();
if (result != null) {
if (zone > 0) {
input_zone.put(zone, result.input);
inputZone.put(zone, result.input);
}
updateState(channelUID, inputSource(result.input));

if (result.radio_freq.isPresent()) {
if (result.radioFrequency.isPresent()) {
updateState(SonyAudioBindingConstants.CHANNEL_RADIO_FREQ,
new DecimalType(result.radio_freq.get() / 1000000.0));
new DecimalType(result.radioFrequency.get() / 1000000.0));
}
}
} catch (CompletionException ex) {
Expand Down Expand Up @@ -387,7 +387,7 @@ public void handleRadioStationCommand(Command command, ChannelUID channelUID) th
String radioCommand = "radio:fm?contentId=" + currentRadioStation;

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")) {
connection.setInput(radioCommand, i);
}
Expand Down Expand Up @@ -418,20 +418,20 @@ public void initialize() {
Configuration config = getThing().getConfiguration();
String ipAddress = (String) config.get(SonyAudioBindingConstants.HOST_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;
if (port_o instanceof BigDecimal decimalValue) {
if (portO instanceof BigDecimal decimalValue) {
port = decimalValue.intValue();
} else if (port_o instanceof Integer) {
port = (int) port_o;
} else if (portO instanceof Integer) {
port = (int) portO;
}

Object refresh_o = config.get(SonyAudioBindingConstants.REFRESHINTERVAL);
Object refreshO = config.get(SonyAudioBindingConstants.REFRESHINTERVAL);
int refresh = 0;
if (refresh_o instanceof BigDecimal decimalValue) {
if (refreshO instanceof BigDecimal decimalValue) {
refresh = decimalValue.intValue();
} else if (refresh_o instanceof Integer) {
refresh = (int) refresh_o;
} else if (refreshO instanceof Integer) {
refresh = (int) refreshO;
}

try {
Expand Down Expand Up @@ -509,9 +509,9 @@ public void updateInput(int zone, SonyAudioConnection.SonyAudioInput input) {
break;
}

if (input.radio_freq.isPresent()) {
if (input.radioFrequency.isPresent()) {
updateState(SonyAudioBindingConstants.CHANNEL_RADIO_FREQ,
new DecimalType(input.radio_freq.get() / 1000000.0));
new DecimalType(input.radioFrequency.get() / 1000000.0));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class SonyAudioConnection implements SonyAudioClientSocketEventListener {
private final String host;
private final int port;
private final String path;
private final URI base_uri;
private final URI baseUri;

private final WebSocketClient webSocketClient;

Expand All @@ -59,8 +59,8 @@ public class SonyAudioConnection implements SonyAudioClientSocketEventListener {

private final SonyAudioEventListener listener;

private int min_volume = 0;
private int max_volume = 50;
private int minVolume = 0;
private int maxVolume = 50;

private final Gson gson;

Expand All @@ -73,14 +73,14 @@ public SonyAudioConnection(String host, int port, String path, SonyAudioEventLis
this.gson = new Gson();
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);
URI wsAudioUri = base_uri.resolve(base_uri.getPath() + "/audio").normalize();
URI wsAudioUri = baseUri.resolve(baseUri.getPath() + "/audio").normalize();
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);
}

Expand Down Expand Up @@ -123,7 +123,7 @@ public void handleEvent(JsonObject json) {
input.input = param.get("uri").getAsString();
if (param.has("broadcastFreq")) {
int freq = param.get("broadcastFreq").getAsInt();
input.radio_freq = Optional.of(freq);
input.radioFrequency = Optional.of(freq);
checkRadioPreset(input.input);
}
listener.updateInput(zone, input);
Expand All @@ -134,7 +134,7 @@ public void handleEvent(JsonObject json) {
SonyAudioVolume volume = new SonyAudioVolume();

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());
listener.updateVolume(zone, volume);
Expand Down Expand Up @@ -274,8 +274,8 @@ public boolean checkConnection() {
}

public String getConnectionName() {
if (base_uri != null) {
return base_uri.toString();
if (baseUri != null) {
return baseUri.toString();
}
return String.format("ws://%s:%d/%s", host, port, path);
}
Expand All @@ -292,8 +292,9 @@ public Boolean getPower(int zone) throws IOException {
Iterator<JsonElement> terminals = element.getAsJsonArray().get(0).getAsJsonArray().iterator();
while (terminals.hasNext()) {
JsonObject terminal = terminals.next().getAsJsonObject();
String zoneUri = "extOutput:zone?zone=" + Integer.toString(zone);
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;
}
}
Expand Down Expand Up @@ -338,7 +339,7 @@ public void setPower(boolean power, int zone) throws IOException {

public class SonyAudioInput {
public String input = "";
public Optional<Integer> radio_freq = Optional.empty();
public Optional<Integer> radioFrequency = Optional.empty();
}

public SonyAudioInput getInput() throws IOException {
Expand Down Expand Up @@ -367,7 +368,7 @@ private SonyAudioInput getInput(GetPlayingContentInfo getPlayingContentInfo) thr

if (result.has("broadcastFreq")) {
int freq = result.get("broadcastFreq").getAsInt();
ret.radio_freq = Optional.of(freq);
ret.radioFrequency = Optional.of(freq);
}
return ret;
}
Expand Down Expand Up @@ -425,9 +426,9 @@ public SonyAudioVolume getVolume(int zone) throws IOException {
SonyAudioVolume ret = new SonyAudioVolume();

int volume = result.get("volume").getAsInt();
min_volume = result.get("minVolume").getAsInt();
max_volume = result.get("maxVolume").getAsInt();
int vol = Math.round(100 * (volume - min_volume) / (max_volume - min_volume));
minVolume = result.get("minVolume").getAsInt();
maxVolume = result.get("maxVolume").getAsInt();
int vol = Math.round(100 * (volume - minVolume) / (maxVolume - minVolume));
if (vol < 0) {
vol = 0;
}
Expand All @@ -445,7 +446,7 @@ public void setVolume(int volume) throws IOException {
if (audioSocket == null) {
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);
}

Expand All @@ -461,7 +462,7 @@ public void setVolume(int volume, int zone) throws IOException {
if (audioSocket == null) {
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,14 @@ class Param {

SetAudioVolume(int volume, int min, int max) {
super("setAudioVolume", "1.1");
long scaled_volume = scaleVolume(volume, min, max);
params = new Param[] { new Param(scaled_volume) };
long scaledVolume = scaleVolume(volume, min, max);
params = new Param[] { new Param(scaledVolume) };
}

SetAudioVolume(int zone, int volume, int min, int max) {
super("setAudioVolume", "1.1");
long scaled_volume = scaleVolume(volume, min, max);
params = new Param[] { new Param(scaled_volume, zone) };
long scaledVolume = scaleVolume(volume, min, max);
params = new Param[] { new Param(scaledVolume, zone) };
}

SetAudioVolume(String volume_change) {
Expand Down

0 comments on commit a7ea27f

Please sign in to comment.