Skip to content

Commit

Permalink
[amazonechocontrol] Add support for announcement on echo show and ech…
Browse files Browse the repository at this point in the history
…o spot and some other small features (openhab#5590)

Add apple music and iHeartRadio id

Signed-off-by: Michael Geramb <mail@michael-geramb.at> (github: mgeramb)
  • Loading branch information
Michael Geramb authored and sprehn committed Aug 21, 2019
1 parent 3e6ffd7 commit bb16ba3
Show file tree
Hide file tree
Showing 15 changed files with 499 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,20 @@ public class AccountServlet extends HttpServlet {

private final Logger logger = LoggerFactory.getLogger(AccountServlet.class);

HttpService httpService;
final HttpService httpService;
String servletUrlWithoutRoot;
String servletUrl;
final String servletUrl;
AccountHandler account;
String id;
@Nullable
Connection connectionToInitialize;
Gson gson = new Gson();
final Gson gson;

public AccountServlet(HttpService httpService, String id, AccountHandler account) {
public AccountServlet(HttpService httpService, String id, AccountHandler account, Gson gson) {
this.httpService = httpService;
this.account = account;
this.id = id;
this.gson = gson;
try {
servletUrlWithoutRoot = "amazonechocontrol/" + URLEncoder.encode(id, "UTF8");
} catch (UnsupportedEncodingException e) {
Expand All @@ -112,7 +113,7 @@ private Connection reCreateConnection() {
if (oldConnection == null) {
oldConnection = account.findConnection();
}
return new Connection(oldConnection);
return new Connection(oldConnection, this.gson);
}

public void dispose() {
Expand Down Expand Up @@ -157,7 +158,7 @@ void doVerb(String verb, @Nullable HttpServletRequest req, @Nullable HttpServlet
Map<String, String[]> map = req.getParameterMap();
String domain = map.get("domain")[0];
String loginData = connection.serializeLoginData();
Connection newConnection = new Connection(null);
Connection newConnection = new Connection(null, this.gson);
if (newConnection.tryRestoreLogin(loginData, domain)) {
account.setConnection(newConnection);
}
Expand Down Expand Up @@ -282,7 +283,7 @@ protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResp
}
// handle commands
if (baseUrl.equals("/newdevice") || baseUrl.equals("/newdevice/")) {
this.connectionToInitialize = new Connection(null);
this.connectionToInitialize = new Connection(null, this.gson);
this.account.setConnection(null);
resp.sendRedirect(this.servletUrl);
return;
Expand All @@ -301,9 +302,7 @@ protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResp
Device device = account.findDeviceJson(serialNumber);
if (device != null) {
Thing thing = account.findThingBySerialNumber(device.serialNumber);
if (thing != null) {
handleIds(resp, connection, device, thing);
}
handleIds(resp, connection, device, thing);
return;
}
}
Expand Down Expand Up @@ -331,7 +330,7 @@ protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResp
}

public Map<String, String> getQueryMap(@Nullable String query) {
Map<String, String> map = new HashMap<String, String>();
Map<String, String> map = new HashMap<>();
if (query != null) {
String[] params = query.split("&");
for (String param : params) {
Expand Down Expand Up @@ -374,6 +373,12 @@ private void handleDefaultPageResult(HttpServletResponse resp, String message, C
html.append(" | <a href='" + servletUrl + "/newdevice' >");
html.append(StringEscapeUtils.escapeHtml("Logout and create new device id"));
html.append("</a>");
// customer id
html.append("<br>Customer Id: ");
html.append(StringEscapeUtils.escapeHtml(connection.getCustomerId()));
// customer name
html.append("<br>Customer Name: ");
html.append(StringEscapeUtils.escapeHtml(connection.getCustomerName()));
// device name
html.append("<br>App name: ");
html.append(StringEscapeUtils.escapeHtml(connection.getDeviceName()));
Expand All @@ -393,7 +398,7 @@ private void handleDefaultPageResult(HttpServletResponse resp, String message, C

// device list
html.append(
"<table><tr><th align='left'>Device</th><th align='left'>Serial Number</th><th align='left'>State</th><th align='left'>Thing</th><th align='left'>Family</th><th align='left'>Type</th></tr>");
"<table><tr><th align='left'>Device</th><th align='left'>Serial Number</th><th align='left'>State</th><th align='left'>Thing</th><th align='left'>Family</th><th align='left'>Type</th><th align='left'>Customer Id</th></tr>");
for (Device device : this.account.getLastKnownDevices()) {

html.append("<tr><td>");
Expand All @@ -409,14 +414,18 @@ private void handleDefaultPageResult(HttpServletResponse resp, String message, C
+ URLEncoder.encode(device.serialNumber, "UTF8") + "'>"
+ StringEscapeUtils.escapeHtml(accountHandler.getLabel()) + "</a>");
} else {
html.append("Not defined");
html.append("<a href='" + servletUrl + "/ids/?serialNumber="
+ URLEncoder.encode(device.serialNumber, "UTF8") + "'>"
+ StringEscapeUtils.escapeHtml("Not defined") + "</a>");
}
html.append("</td><td>");
html.append(StringEscapeUtils.escapeHtml(nullReplacement(device.deviceFamily)));
html.append("</td><td>");
html.append(StringEscapeUtils.escapeHtml(nullReplacement(device.deviceType)));
html.append("</td><td>");
html.append("</td></tr>");
html.append(StringEscapeUtils.escapeHtml(nullReplacement(device.deviceOwnerCustomerId)));
html.append("</td>");
html.append("</tr>");
}
html.append("</table>");
createPageEndAndSent(resp, html);
Expand Down Expand Up @@ -467,18 +476,36 @@ private void createPageEndAndSent(HttpServletResponse resp, StringBuilder html)
}
}

private void handleIds(HttpServletResponse resp, Connection connection, Device device, Thing thing)
private void handleIds(HttpServletResponse resp, Connection connection, Device device, @Nullable Thing thing)
throws IOException, URISyntaxException {
StringBuilder html = createPageStart("Channel Options - " + thing.getLabel());

StringBuilder html;
if (thing != null) {
html = createPageStart("Channel Options - " + thing.getLabel());
} else {
html = createPageStart("Device Information - No thing defined");
}
renderBluetoothMacChannel(connection, device, html);
renderAmazonMusicPlaylistIdChannel(connection, device, html);
renderPlayAlarmSoundChannel(connection, device, html);
renderMusicProviderIdChannel(connection, html);

renderCapabilities(connection, device, html);
createPageEndAndSent(resp, html);
}

private void renderCapabilities(Connection connection, Device device, StringBuilder html) {
html.append("<h2>Capabilities</h2>");
html.append("<table><tr><th align='left'>Name</th></tr>");
String[] capabilities = device.capabilities;
if (capabilities != null) {
for (String capability : capabilities) {
html.append("<tr><td>");
html.append(StringEscapeUtils.escapeHtml(capability));
html.append("</td></tr>");
}
}
html.append("</table>");
}

private void renderMusicProviderIdChannel(Connection connection, StringBuilder html) {
html.append("<h2>" + StringEscapeUtils.escapeHtml("Channel " + CHANNEL_MUSIC_PROVIDER_ID) + "</h2>");
html.append("<table><tr><th align='left'>Name</th><th align='left'>Value</th></tr>");
Expand Down Expand Up @@ -599,11 +626,11 @@ void handleProxyRequest(Connection connection, HttpServletResponse resp, String
try {
Map<String, String> headers = null;
if (referer != null) {
headers = new HashMap<String, String>();
headers = new HashMap<>();
headers.put("Referer", referer);
}

urlConnection = connection.makeRequest(verb, url, postData, json, false, headers);
urlConnection = connection.makeRequest(verb, url, postData, json, false, headers, 0);
if (urlConnection.getResponseCode() == 302) {
{
String location = urlConnection.getHeaderField("location");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class AmazonEchoControlBindingConstants {
public static final ThingTypeUID THING_TYPE_FLASH_BRIEFING_PROFILE = new ThingTypeUID(BINDING_ID,
"flashbriefingprofile");

public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = new HashSet<ThingTypeUID>(
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = new HashSet<>(
Arrays.asList(THING_TYPE_ACCOUNT, THING_TYPE_ECHO, THING_TYPE_ECHO_SPOT, THING_TYPE_ECHO_SHOW,
THING_TYPE_ECHO_WHA, THING_TYPE_FLASH_BRIEFING_PROFILE));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.http.HttpService;

import com.google.gson.Gson;

/**
* The {@link AmazonEchoControlHandlerFactory} is responsible for creating things and thing
* handlers.
Expand All @@ -60,7 +62,9 @@ public class AmazonEchoControlHandlerFactory extends BaseThingHandlerFactory {
StorageService storageService;
@Nullable
BindingServlet bindingServlet;

@Nullable
Gson gson;

@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
Expand Down Expand Up @@ -97,11 +101,17 @@ protected void deactivate(ComponentContext componentContext) {
if (storageService == null) {
return null;
}
Gson gson = this.gson;
if (gson == null)
{
gson = new Gson();
this.gson = gson;
}

if (thingTypeUID.equals(THING_TYPE_ACCOUNT)) {
Storage<String> storage = storageService.getStorage(thing.getUID().toString(),
String.class.getClassLoader());
AccountHandler bridgeHandler = new AccountHandler((Bridge) thing, httpService, storage);
AccountHandler bridgeHandler = new AccountHandler((Bridge) thing, httpService, storage, gson);
registerDiscoveryService(bridgeHandler);
BindingServlet bindingServlet = this.bindingServlet;
if (bindingServlet != null) {
Expand All @@ -115,7 +125,7 @@ protected void deactivate(ComponentContext componentContext) {
return new FlashBriefingProfileHandler(thing, storage);
}
if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
return new EchoHandler(thing);
return new EchoHandler(thing, gson);
}
return null;
}
Expand All @@ -124,7 +134,7 @@ private synchronized void registerDiscoveryService(AccountHandler bridgeHandler)
AmazonEchoDiscovery discoveryService = new AmazonEchoDiscovery(bridgeHandler);
discoveryService.activate();
this.discoveryServiceRegistrations.put(bridgeHandler.getThing().getUID(), bundleContext
.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<String, Object>()));
.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class BindingServlet extends HttpServlet {
String servletUrl;
HttpService httpService;

List<Thing> accountHandlers = new ArrayList<Thing>();
List<Thing> accountHandlers = new ArrayList<>();

public BindingServlet(HttpService httpService) {
this.httpService = httpService;
Expand Down Expand Up @@ -126,5 +126,4 @@ protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResp
logger.warn("return html failed with uri syntax error {}", e);
}
}

}
Loading

0 comments on commit bb16ba3

Please sign in to comment.