Skip to content

Commit

Permalink
[monopriceaudio] Fix compiler warnings and code improvements (openhab…
Browse files Browse the repository at this point in the history
…#16991)

* Fix compiler warnings and code improvements

Signed-off-by: Michael Lobstein <[email protected]>
  • Loading branch information
mlobstein authored and digitaldan committed Aug 29, 2024
1 parent ef42ac5 commit f8b96a0
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 265 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
*/
package org.openhab.binding.monopriceaudio.internal.communication;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.monopriceaudio.internal.configuration.MonopriceAudioThingConfiguration;
Expand Down Expand Up @@ -82,7 +84,7 @@ public List<StateOption> getSourceLabels(MonopriceAudioThingConfiguration config
2, false, List.of("1", "2", "3", "4", "5", "6")) {
@Override
public MonopriceAudioZoneDTO getZoneData(String newZoneData) {
MonopriceAudioZoneDTO zoneData = new MonopriceAudioZoneDTO();
final MonopriceAudioZoneDTO zoneData = new MonopriceAudioZoneDTO();

Matcher matcher = MONOPRICE70_PATTERN.matcher(newZoneData);
if (matcher.find()) {
Expand Down Expand Up @@ -127,8 +129,8 @@ public List<StateOption> getSourceLabels(MonopriceAudioThingConfiguration config
false, List.of("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16")) {
@Override
public MonopriceAudioZoneDTO getZoneData(String newZoneData) {
MonopriceAudioZoneDTO zoneData = new MonopriceAudioZoneDTO();
Matcher matcher = XANTECH_PATTERN.matcher(newZoneData);
final MonopriceAudioZoneDTO zoneData = new MonopriceAudioZoneDTO();
final Matcher matcher = XANTECH_PATTERN.matcher(newZoneData);

if (matcher.find()) {
zoneData.setZone(matcher.group(1));
Expand Down Expand Up @@ -156,8 +158,8 @@ public List<StateOption> getSourceLabels(MonopriceAudioThingConfiguration config

// Used by 10761/DAX66 and DAX88
private static MonopriceAudioZoneDTO getMonopriceZoneData(String newZoneData) {
MonopriceAudioZoneDTO zoneData = new MonopriceAudioZoneDTO();
Matcher matcher = MONOPRICE_PATTERN.matcher(newZoneData);
final MonopriceAudioZoneDTO zoneData = new MonopriceAudioZoneDTO();
final Matcher matcher = MONOPRICE_PATTERN.matcher(newZoneData);

if (matcher.find()) {
zoneData.setZone(matcher.group(1));
Expand Down Expand Up @@ -191,31 +193,31 @@ private static MonopriceAudioZoneDTO getMonopriceZoneData(String newZoneData) {
private static final Pattern XANTECH_PATTERN = Pattern.compile(
"^#(\\d{1,2})ZS PR(\\d{1}) SS(\\d{1}) VO(\\d{1,2}) MU(\\d{1}) TR(\\d{1,2}) BS(\\d{1,2}) BA(\\d{1,2}) LS(\\d{1}) PS(\\d{1})+");

private String cmdPrefix;
private String cmdSuffix;
private String queryPrefix;
private String querySuffix;
private String respPrefix;
private String powerCmd;
private String sourceCmd;
private String volumeCmd;
private String muteCmd;
private String trebleCmd;
private String bassCmd;
private String balanceCmd;
private String dndCmd;
private int maxVol;
private int minTone;
private int maxTone;
private int toneOffset;
private int minBal;
private int maxBal;
private int balOffset;
private int maxZones;
private int numSources;
private boolean padNumbers;
private List<String> zoneIds;
private Map<String, String> zoneIdMap = new HashMap<>();
private final String cmdPrefix;
private final String cmdSuffix;
private final String queryPrefix;
private final String querySuffix;
private final String respPrefix;
private final String powerCmd;
private final String sourceCmd;
private final String volumeCmd;
private final String muteCmd;
private final String trebleCmd;
private final String bassCmd;
private final String balanceCmd;
private final String dndCmd;
private final int maxVol;
private final int minTone;
private final int maxTone;
private final int toneOffset;
private final int minBal;
private final int maxBal;
private final int balOffset;
private final int maxZones;
private final int numSources;
private final boolean padNumbers;
private final List<String> zoneIds;
private final Map<String, String> zoneIdMap;

private static final String ON_STR = "1";
private static final String OFF_STR = "0";
Expand Down Expand Up @@ -256,11 +258,10 @@ private static MonopriceAudioZoneDTO getMonopriceZoneData(String newZoneData) {
this.padNumbers = padNumbers;
this.zoneIds = zoneIds;

int i = 1;
for (String zoneId : zoneIds) {
zoneIdMap.put(zoneId, "zone" + i);
i++;
}
// Build a map between the amp's physical zone IDs and the thing's logical zone names
final Map<String, String> zoneIdMap = new HashMap<>();
IntStream.range(0, zoneIds.size()).forEach(i -> zoneIdMap.put(zoneIds.get(i), "zone" + (i + 1)));
this.zoneIdMap = Collections.unmodifiableMap(zoneIdMap);
}

public abstract MonopriceAudioZoneDTO getZoneData(String newZoneData);
Expand Down Expand Up @@ -378,26 +379,14 @@ public List<String> getZoneIds() {
}

public String getFormattedValue(Integer value) {
if (padNumbers) {
return String.format("%02d", value);
} else {
return value.toString();
}
return padNumbers ? String.format("%02d", value) : value.toString();
}

public String getOnStr() {
if (padNumbers) {
return ON_STR_PAD;
} else {
return ON_STR;
}
return padNumbers ? ON_STR_PAD : ON_STR;
}

public String getOffStr() {
if (padNumbers) {
return OFF_STR_PAD;
} else {
return OFF_STR;
}
return padNumbers ? OFF_STR_PAD : OFF_STR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public abstract class MonopriceAudioConnector {
private boolean connected;
private boolean pingResponseOnly;

private @Nullable AmplifierModel amp;
protected AmplifierModel amp = AmplifierModel.MONOPRICE;

private @Nullable Thread readerThread;

Expand All @@ -77,15 +77,6 @@ protected void setConnected(boolean connected) {
this.pingResponseOnly = false;
}

/**
* Set the AmplifierModel
*
* @param amp the AmplifierModel being used
*/
protected void setAmplifierModel(AmplifierModel amp) {
this.amp = amp;
}

/**
* Set the thread that handles the feedback messages
*
Expand Down Expand Up @@ -177,7 +168,7 @@ protected int readInput(byte[] dataBuffer) throws MonopriceAudioException {
public void sendPing() throws MonopriceAudioException {
pingResponseOnly = true;
// poll zone 1 status only to see if the amp responds
queryZone(amp.getZoneIds().get(0));
queryZone(amp.getZoneIds().iterator().next());
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,16 @@ public MonopriceAudioIpConnector(@Nullable String address, int port, String uid,
this.address = address;
this.port = port;
this.uid = uid;
setAmplifierModel(amp);
super.amp = amp;
}

/**
* Default constructor for temporary connector object that gets replaced during initialize()
*/
public MonopriceAudioIpConnector() {
this.address = "";
this.port = -1;
this.uid = "";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public MonopriceAudioSerialConnector(SerialPortManager serialPortManager, String
this.serialPortManager = serialPortManager;
this.serialPortName = serialPortName;
this.uid = uid;
setAmplifierModel(amp);
super.amp = amp;
}

@Override
Expand Down
Loading

0 comments on commit f8b96a0

Please sign in to comment.