Skip to content

Commit

Permalink
[squeezebox] Cleanup / code simplification (openhab#10941)
Browse files Browse the repository at this point in the history
* [squeezebox] Reduce boilerplate by using lambda expressions.
* [squeezebox] Make code dealing with key/value responses more readable.
* Fix off-by-one mistake.
* [squeezebox] Simplify CLI response parsing code.
* [squeezebox] Optimize some redundant code.

Signed-off-by: Danny Baumann <dannybaumann@web.de>
Signed-off-by: dw-8 <davy.wong.on+github@gmail.com>
  • Loading branch information
maniac103 authored and dw-8 committed Jul 25, 2021
1 parent d3f281c commit b401d8b
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 399 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,32 +85,31 @@ public void cancelRequestPlayerJob() {
public void playerAdded(SqueezeBoxPlayer player) {
ThingUID bridgeUID = squeezeBoxServerHandler.getThing().getUID();

ThingUID thingUID = new ThingUID(SQUEEZEBOXPLAYER_THING_TYPE, bridgeUID,
player.getMacAddress().replace(":", ""));
ThingUID thingUID = new ThingUID(SQUEEZEBOXPLAYER_THING_TYPE, bridgeUID, player.macAddress.replace(":", ""));

if (!playerThingExists(thingUID)) {
logger.debug("player added {} : {} ", player.getMacAddress(), player.getName());
logger.debug("player added {} : {} ", player.macAddress, player.name);

Map<String, Object> properties = new HashMap<>(1);
String representationPropertyName = "mac";
properties.put(representationPropertyName, player.getMacAddress());
properties.put(representationPropertyName, player.macAddress);

// Added other properties
properties.put("modelId", player.getModel());
properties.put("name", player.getName());
properties.put("uid", player.getUuid());
properties.put("ip", player.getIpAddr());
properties.put("modelId", player.model);
properties.put("name", player.name);
properties.put("uid", player.uuid);
properties.put("ip", player.ipAddr);

DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
.withRepresentationProperty(representationPropertyName).withBridge(bridgeUID)
.withLabel(player.getName()).build();
.withRepresentationProperty(representationPropertyName).withBridge(bridgeUID).withLabel(player.name)
.build();

thingDiscovered(discoveryResult);
}
}

private boolean playerThingExists(ThingUID newThingUID) {
return squeezeBoxServerHandler.getThing().getThing(newThingUID) != null ? true : false;
return squeezeBoxServerHandler.getThing().getThing(newThingUID) != null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,110 +12,33 @@
*/
package org.openhab.binding.squeezebox.internal.handler;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* Represents a Squeeze Player
*
* @author Dan Cunningham - Initial contribution
*
*/
@NonNullByDefault
public class SqueezeBoxPlayer {
public String macAddress;
public String name;
public String ipAddr;
public String model;
public String uuid;

public SqueezeBoxPlayer() {
super();
}

/**
* UID of player
*
* @return
*/
public String getUuid() {
return uuid;
}

/**
* UID of player
*
* @param uuid
*/
public void setUuid(String uuid) {
this.uuid = uuid;
}

/**
* Mac Address of player
*
* @param macAddress
*/
public String getMacAddress() {
return macAddress;
}

/**
* Mac Address of player
*
* @param macAddress
*/
public void setMacAddress(String macAddress) {
this.macAddress = macAddress;
}

/**
* The name (label) of a player
*
* @return
*/
public String getName() {
return name;
}

/**
* The name (label) of a player
*
* @param name
*/
public void setName(String name) {
public final String macAddress;
@Nullable
public final String name;
@Nullable
public final String ipAddr;
@Nullable
public final String model;
@Nullable
public final String uuid;

public SqueezeBoxPlayer(String mac, @Nullable String name, @Nullable String ip, @Nullable String model,
@Nullable String uuid) {
this.macAddress = mac;
this.name = name;
}

/**
* The ip address of a player
*
* @return
*/
public String getIpAddr() {
return ipAddr;
}

/**
* The ip address of a player
*
* @param ipAddr
*/
public void setIpAddr(String ipAddr) {
this.ipAddr = ipAddr;
}

/**
* The type of player
*
* @return
*/
public String getModel() {
return model;
}

/**
* The type of player
*
* @param model
*/
public void setModel(String model) {
this.ipAddr = ip;
this.model = model;
this.uuid = uuid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -577,83 +577,52 @@ private void updateChannel(String mac, String channelID, State state) {
* @return
*/
int currentVolume() {
if (stateMap.containsKey(CHANNEL_VOLUME)) {
return ((DecimalType) stateMap.get(CHANNEL_VOLUME)).intValue();
} else {
return 0;
}
return cachedStateAsInt(CHANNEL_VOLUME);
}

int currentPlayingTime() {
if (stateMap.containsKey(CHANNEL_CURRENT_PLAYING_TIME)) {
return ((DecimalType) stateMap.get(CHANNEL_CURRENT_PLAYING_TIME)).intValue();
} else {
return 0;
}
return cachedStateAsInt(CHANNEL_CURRENT_PLAYING_TIME);
}

int currentNumberPlaylistTracks() {
if (stateMap.containsKey(CHANNEL_NUMBER_PLAYLIST_TRACKS)) {
return ((DecimalType) stateMap.get(CHANNEL_NUMBER_PLAYLIST_TRACKS)).intValue();
} else {
return 0;
}
return cachedStateAsInt(CHANNEL_NUMBER_PLAYLIST_TRACKS);
}

int currentPlaylistIndex() {
if (stateMap.containsKey(CHANNEL_PLAYLIST_INDEX)) {
return ((DecimalType) stateMap.get(CHANNEL_PLAYLIST_INDEX)).intValue();
} else {
return 0;
}
return cachedStateAsInt(CHANNEL_PLAYLIST_INDEX);
}

boolean currentPower() {
if (stateMap.containsKey(CHANNEL_POWER)) {
return (stateMap.get(CHANNEL_POWER).equals(OnOffType.ON) ? true : false);
} else {
return false;
}
return cachedStateAsBoolean(CHANNEL_POWER, OnOffType.ON);
}

boolean currentStop() {
if (stateMap.containsKey(CHANNEL_STOP)) {
return (stateMap.get(CHANNEL_STOP).equals(OnOffType.ON) ? true : false);
} else {
return false;
}
return cachedStateAsBoolean(CHANNEL_STOP, OnOffType.ON);
}

boolean currentControl() {
if (stateMap.containsKey(CHANNEL_CONTROL)) {
return (stateMap.get(CHANNEL_CONTROL).equals(PlayPauseType.PLAY) ? true : false);
} else {
return false;
}
return cachedStateAsBoolean(CHANNEL_CONTROL, PlayPauseType.PLAY);
}

boolean currentMute() {
if (stateMap.containsKey(CHANNEL_MUTE)) {
return (stateMap.get(CHANNEL_MUTE).equals(OnOffType.ON) ? true : false);
} else {
return false;
}
return cachedStateAsBoolean(CHANNEL_MUTE, OnOffType.ON);
}

int currentShuffle() {
if (stateMap.containsKey(CHANNEL_CURRENT_PLAYLIST_SHUFFLE)) {
return ((DecimalType) stateMap.get(CHANNEL_CURRENT_PLAYLIST_SHUFFLE)).intValue();
} else {
return 0;
}
return cachedStateAsInt(CHANNEL_CURRENT_PLAYLIST_SHUFFLE);
}

int currentRepeat() {
if (stateMap.containsKey(CHANNEL_CURRENT_PLAYLIST_REPEAT)) {
return ((DecimalType) stateMap.get(CHANNEL_CURRENT_PLAYLIST_REPEAT)).intValue();
} else {
return 0;
}
return cachedStateAsInt(CHANNEL_CURRENT_PLAYLIST_REPEAT);
}

private boolean cachedStateAsBoolean(String key, @NonNull State activeState) {
return activeState.equals(stateMap.get(key));
}

private int cachedStateAsInt(String key) {
State state = stateMap.get(key);
return state instanceof DecimalType ? ((DecimalType) state).intValue() : 0;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ boolean isPlaying() {
}

boolean isShuffling() {
return savedShuffle == 0 ? false : true;
return savedShuffle != 0;
}

int getShuffle() {
return savedShuffle;
}

boolean isRepeating() {
return savedRepeat == 0 ? false : true;
return savedRepeat != 0;
}

int getRepeat() {
Expand Down
Loading

0 comments on commit b401d8b

Please sign in to comment.