Skip to content
This repository has been archived by the owner on Dec 27, 2021. It is now read-only.

Commit

Permalink
Merge pull request #25 from vmichalak/feature/24-get-room-name
Browse files Browse the repository at this point in the history
Close #24 - getRoomName()
  • Loading branch information
vmichalak authored Feb 16, 2018
2 parents 2c93ab3 + fd0089f commit 74de5ab
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 39 deletions.
30 changes: 15 additions & 15 deletions src/main/java/com/vmichalak/sonoscontroller/CommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
import java.util.Map;

class CommandBuilder {
private final static int SOAP_PORT = 1400;
private final static String TRANSPORT_ENDPOINT = "/MediaRenderer/AVTransport/Control";
private final static String TRANSPORT_SERVICE = "urn:schemas-upnp-org:service:AVTransport:1";
private final static String RENDERING_ENDPOINT = "/MediaRenderer/RenderingControl/Control";
private final static String RENDERING_SERVICE = "urn:schemas-upnp-org:service:RenderingControl:1";
private final static String DEVICE_ENDPOINT = "/DeviceProperties/Control";
private final static String DEVICE_SERVICE = "urn:schemas-upnp-org:service:DeviceProperties:1";
private final static String CONTENT_DIRECTORY_ENDPOINT = "/MediaServer/ContentDirectory/Control";
private final static String CONTENT_DIRECTORY_SERVICE = "urn:schemas-upnp-org:service:ContentDirectory:1";
private final static String ZONE_GROUP_TOPOLOGY_ENDPOINT = "/ZoneGroupTopology/Control";
private final static String ZONE_GROUP_TOPOLOGY_SERVICE = "urn:upnp-org:serviceId:ZoneGroupTopology";

private final static HashMap<Integer, String> ERROR_DESCRIPTION_MAP = new HashMap<Integer, String>();
private static final int SOAP_PORT = 1400;
private static final String TRANSPORT_ENDPOINT = "/MediaRenderer/AVTransport/Control";
private static final String TRANSPORT_SERVICE = "urn:schemas-upnp-org:service:AVTransport:1";
private static final String RENDERING_ENDPOINT = "/MediaRenderer/RenderingControl/Control";
private static final String RENDERING_SERVICE = "urn:schemas-upnp-org:service:RenderingControl:1";
private static final String DEVICE_ENDPOINT = "/DeviceProperties/Control";
private static final String DEVICE_SERVICE = "urn:schemas-upnp-org:service:DeviceProperties:1";
private static final String CONTENT_DIRECTORY_ENDPOINT = "/MediaServer/ContentDirectory/Control";
private static final String CONTENT_DIRECTORY_SERVICE = "urn:schemas-upnp-org:service:ContentDirectory:1";
private static final String ZONE_GROUP_TOPOLOGY_ENDPOINT = "/ZoneGroupTopology/Control";
private static final String ZONE_GROUP_TOPOLOGY_SERVICE = "urn:upnp-org:serviceId:ZoneGroupTopology";

private static final HashMap<Integer, String> ERROR_DESCRIPTION_MAP = new HashMap<Integer, String>();

static {
ERROR_DESCRIPTION_MAP.put(400, "Bad Request");
Expand Down Expand Up @@ -83,8 +83,8 @@ public static CommandBuilder zoneGroupTopology(String action) {
return new CommandBuilder(ZONE_GROUP_TOPOLOGY_ENDPOINT, ZONE_GROUP_TOPOLOGY_SERVICE, action);
}

public static String downloadSpeakerInfo(String ip) throws IOException, SonosControllerException {
String uri = "http://" + ip + ":" + SOAP_PORT + "/status/zp";
public static String download(String ip, String url) throws IOException, SonosControllerException {
String uri = "http://" + ip + ":" + SOAP_PORT + "/" + url;
Request request = new Request.Builder().url(uri).get().build();
String response = getHttpClient().newCall(request).execute().body().string();
handleError(ip, response);
Expand Down
41 changes: 35 additions & 6 deletions src/main/java/com/vmichalak/sonoscontroller/SonosDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

public class SonosDevice {

Expand Down Expand Up @@ -492,12 +493,40 @@ public void switchDialogMode() throws IOException, SonosControllerException {

//<editor-fold desc="DEVICE">

/**
* Get the zone name. (for exemple: "Bedroom + 1", "Living Room", ...)
* @return the zone name.
* @throws IOException
* @throws SonosControllerException
*/
public String getZoneName() throws IOException, SonosControllerException {
return this.getSpeakerInfo().getZoneName();
return this.getZoneGroupState().getName();
}

/**
* Get the room name. (for exemple: "Bedroom", "Living Room", ...)
* @return the room name.
* @throws IOException
* @throws SonosControllerException
*/
public String getRoomName() throws IOException, SonosControllerException {
String r = CommandBuilder.download(this.ip, "xml/device_description.xml");
r = Pattern.compile("<deviceList>.*</deviceList>", Pattern.DOTALL).matcher(r).replaceFirst("");
return ParserHelper.findOne("<roomName>(.*)</roomName>", r);
}

/**
* Get the device name. (for exemple: "Bedroom (L)", "Bedroom (R)", ...)
* @return the device name.
* @throws IOException
* @throws SonosControllerException
*/
public String getDeviceName() throws IOException, SonosControllerException {
return this.getSpeakerInfo().getDeviceName();
}

public void setZoneName(String zoneName) throws IOException, SonosControllerException {
CommandBuilder.device("SetZoneAttributes").put("DesiredZoneName", zoneName).put("DesiredIcon", "")
public void setRoomName(String roomName) throws IOException, SonosControllerException {
CommandBuilder.device("SetZoneAttributes").put("DesiredZoneName", roomName).put("DesiredIcon", "")
.put("DesiredConfiguration", "").executeOn(this.ip);
}

Expand Down Expand Up @@ -569,9 +598,9 @@ public boolean isCoordinator() throws IOException, SonosControllerException {
* @throws SonosControllerException
*/
public SonosSpeakerInfo getSpeakerInfo() throws IOException, SonosControllerException {
String responseString = CommandBuilder.downloadSpeakerInfo(this.ip);
String responseString = CommandBuilder.download(ip, "status/zp");

String zoneName = ParserHelper.findOne("<ZoneName>(.*)</ZoneName>", responseString);
String deviceName = ParserHelper.findOne("<ZoneName>(.*)</ZoneName>", responseString);
String zoneIcon = ParserHelper.findOne("<ZoneIcon>(.*)</ZoneIcon>", responseString);
String configuration = ParserHelper.findOne("<Configuration>(.*)</Configuration>", responseString);
String localUID = ParserHelper.findOne("<LocalUID>(.*)</LocalUID>", responseString);
Expand Down Expand Up @@ -609,7 +638,7 @@ public SonosSpeakerInfo getSpeakerInfo() throws IOException, SonosControllerExce
String regState = ParserHelper.findOne("<RegState>(.*)</RegState>", responseString);
String customerID = ParserHelper.findOne("<CustomerID>(.*)</CustomerID>", responseString);

return new SonosSpeakerInfo(zoneName, zoneIcon, configuration, localUID, serialNumber, softwareVersion,
return new SonosSpeakerInfo(deviceName, zoneIcon, configuration, localUID, serialNumber, softwareVersion,
softwareDate, softwareScm, minCompatibleVersion, legacyCompatibleVersion, hardwareVersion, dspVersion,
hwFlags, hwFeatures, variant, generalFlags, ipAddress, macAddress, copyright, extraInfo, htAudioInCode,
idxTrk, mdp2Ver, mdp3Ver, relBuild, whitelistBuild, prodUnit, fuseCfg, revokeFuse, authFlags,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,13 @@ public static SonosDevice discoverByUID(String uid) throws IOException {
* @throws IOException
*/
public static SonosDevice discoverByName(String name) throws IOException {
name = name.toLowerCase();
List<SonosDevice> sonosDevices = SonosDiscovery.discover();
for(SonosDevice sonosDevice : sonosDevices) {
try {
if(sonosDevice.getZoneName().toLowerCase().equals(name)) {
if(sonosDevice.getZoneName().equalsIgnoreCase(name)) {
return sonosDevice;
}
} catch (SonosControllerException e) { }
} catch (SonosControllerException e) { /* ignored */ }
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.vmichalak.sonoscontroller.model;

public class SonosSpeakerInfo {
private final String zoneName;
private final String deviceName;
private final String zoneIcon;
private final String configuration;
private final String localUID;
Expand Down Expand Up @@ -35,15 +35,15 @@ public class SonosSpeakerInfo {
private final String regState;
private final String customerID;

public SonosSpeakerInfo(String zoneName, String zoneIcon, String configuration, String localUID,
public SonosSpeakerInfo(String deviceName, String zoneIcon, String configuration, String localUID,
String serialNumber, String softwareVersion, String softwareDate, String softwareScm,
String minCompatibleVersion, String legacyCompatibleVersion, String hardwareVersion,
String dspVersion, String hwFlags, String hwFeatures, String variant, String generalFlags,
String ipAddress, String macAddress, String copyright, String extraInfo,
String htAudioInCode, String idxTrk, String mdp2Ver, String mdp3Ver, String relBuild,
String whitelistBuild, String prodUnit, String fuseCfg, String revokeFuse, String authFlags,
String swFeatures, String regState, String customerID) {
this.zoneName = zoneName;
this.deviceName = deviceName;
this.zoneIcon = zoneIcon;
this.configuration = configuration;
this.localUID = localUID;
Expand Down Expand Up @@ -78,8 +78,8 @@ public SonosSpeakerInfo(String zoneName, String zoneIcon, String configuration,
this.customerID = customerID;
}

public String getZoneName() {
return zoneName;
public String getDeviceName() {
return deviceName;
}

public String getZoneIcon() {
Expand Down Expand Up @@ -213,7 +213,7 @@ public String getCustomerID() {
@Override
public String toString() {
return "SonosSpeakerInfo{" +
"zoneName='" + zoneName + '\'' +
"deviceName='" + deviceName + '\'' +
", zoneIcon='" + zoneIcon + '\'' +
", configuration='" + configuration + '\'' +
", localUID='" + localUID + '\'' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public List<SonosDevice> getSonosDevicesInGroup() {
devices.add(device);
}
}
catch (IOException e) { }
catch (IOException e) { /* ignored */ }
}
return devices;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.vmichalak.sonoscontroller.model;

import com.vmichalak.sonoscontroller.ParserHelper;
import org.apache.commons.text.StringEscapeUtils;

public class TrackMetadata {
private final String title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public static CommandBuilder mockCommandBuilder(String output) throws Exception

public static void mockCommandBuilderDownloadSpeakerInfo(String output) throws Exception {
PowerMockito.mockStatic(CommandBuilder.class);
PowerMockito.when(CommandBuilder.downloadSpeakerInfo(anyString())).thenReturn(output);
PowerMockito.when(CommandBuilder.download(anyString(), anyString())).thenReturn(output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public void getSpeakerInfo() throws Exception {
"</ZPInfo></ZPSupportInfo>");

SonosSpeakerInfo speakerInfo = new SonosDevice("127.0.0.1").getSpeakerInfo();
assertEquals("Bedroom", speakerInfo.getZoneName());
assertEquals("Bedroom", speakerInfo.getDeviceName());
assertEquals("x-rincon-roomicon:den", speakerInfo.getZoneIcon());
assertEquals("1", speakerInfo.getConfiguration());
assertEquals("RINCON_99999999AAAAAAAAA", speakerInfo.getLocalUID());
Expand Down Expand Up @@ -286,7 +286,7 @@ public void getSpeakerInfo() throws Exception {
/**
* Issue #1 - Danish Zone Name Parsing Problem
*/
public void getDanishZoneName() throws Exception {
public void getDanishDeviceName() throws Exception {
MockHelper.mockCommandBuilderDownloadSpeakerInfo("<?xml version=\"1.0\" ?>\n" +
"<?xml-stylesheet type=\"text/xsl\" href=\"/xml/review.xsl\"?><ZPSupportInfo><ZPInfo>" +
"<ZoneName>0. Køkken</ZoneName><ZoneIcon>x-rincon-roomicon:kitchen</ZoneIcon>" +
Expand All @@ -303,7 +303,7 @@ public void getDanishZoneName() throws Exception {
"<MDP3Ver>0</MDP3Ver><RegState>3</RegState><CustomerID>XXXX</CustomerID>" +
"</ZPInfo></ZPSupportInfo>");

assertEquals("0. Køkken", new SonosDevice("127.0.0.1").getZoneName());
assertEquals("0. Køkken", new SonosDevice("127.0.0.1").getDeviceName());
}

@Test(expected = UPnPSonosControllerException.class)
Expand Down Expand Up @@ -602,7 +602,7 @@ public void checkCommandBuilderUsage() throws Exception {
sonosDevice.setLoudness(false);
sonosDevice.setTreble(8);
sonosDevice.setNightMode(true);
sonosDevice.setZoneName("test");
sonosDevice.setRoomName("test");
sonosDevice.setLedState(true);
sonosDevice.setLedState(false);
sonosDevice.setDialogMode(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ public void testLoudness() throws IOException, SonosControllerException, Interru
@Test
public void testZoneName() throws IOException, SonosControllerException, InterruptedException {
String initialName = sonosDevice.getZoneName();
sonosDevice.setZoneName("CURRENTLY IN TEST");
sonosDevice.setRoomName("CURRENTLY IN TEST");
Thread.sleep(100);
assertEquals("CURRENTLY IN TEST", sonosDevice.getZoneName());
sonosDevice.setZoneName(initialName);
sonosDevice.setRoomName(initialName);
}

@Test
Expand Down

0 comments on commit 74de5ab

Please sign in to comment.