Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed some Java code style issues. #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 36 additions & 23 deletions src/main/java/AlexaHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

import com.amazonaws.services.dynamodbv2.document.DynamoDB;
Expand All @@ -32,75 +32,88 @@ public class AlexaHandler {

public static void handler(InputStream inputStream, OutputStream outputStream, Context context) {

String request;
try {
request = getRequest(inputStream);

String request = getRequest(inputStream);

System.out.println("Request:");
System.out.println(request);

JSONObject jsonRequest = new JSONObject(request);
JSONObject directive = (JSONObject) jsonRequest.get("directive");
JSONObject header = (JSONObject) directive.get("header");

AlexaResponse ar;
AlexaResponse alexaResponse;

String namespace = header.optString("namespace", "INVALID");
String correlationToken = header.optString("correlationToken", "INVALID");

switch(namespace) {

case "Alexa.Authorization":

System.out.println("Found Alexa.Authorization Namespace");
ar = new AlexaResponse("Alexa.Authorization","AcceptGrant", "INVALID", "INVALID", correlationToken);
alexaResponse = new AlexaResponse("Alexa.Authorization","AcceptGrant", "INVALID", "INVALID", correlationToken);

break;

case "Alexa.Discovery":

System.out.println("Found Alexa.Discovery Namespace");
ar = new AlexaResponse("Alexa.Discovery", "Discover.Response");
String capabilityAlexa = ar.CreatePayloadEndpointCapability("AlexaInterface", "Alexa", "3", null);
String capabilityAlexaPowerController = ar.CreatePayloadEndpointCapability("AlexaInterface", "Alexa.PowerController", "3", "{\"supported\": [ { \"name\": \"powerState\" } ] }");

alexaResponse = new AlexaResponse("Alexa.Discovery", "Discover.Response");

String capabilityAlexa = alexaResponse.createPayloadEndpointCapability("AlexaInterface", "Alexa", "3", null);
String capabilityAlexaPowerController = alexaResponse.createPayloadEndpointCapability("AlexaInterface", "Alexa.PowerController", "3", "{\"supported\": [ { \"name\": \"powerState\" } ] }");
String capabilities = "[" + capabilityAlexa + ", " + capabilityAlexaPowerController + "]";
ar.AddPayloadEndpoint("Sample Switch", "sample-switch-01", capabilities);

alexaResponse.addPayloadEndpoint("Sample Switch", "sample-switch-01", capabilities);

// For another way to see how to craft an AlexaResponse, have a look at AlexaResponseTest:ResponseDiscovery

break;

case "Alexa.PowerController":

System.out.println("Found Alexa.PowerController Namespace");

String endpointId = directive.getJSONObject("endpoint").optString("endpointId", "INVALID");
String token = directive.getJSONObject("endpoint").getJSONObject("scope").optString("token", "INVALID");
String powerStateValue = directive.getJSONObject("header").optString("name", "TurnOn");
String value = powerStateValue.equals("TurnOn") ? "ON" : "OFF";

// Set the value in the DynamodDB table SampleSmartHome
if(sendDeviceState(endpointId, "powerState", value)) {
ar = new AlexaResponse("Alexa", "Response", endpointId, token, correlationToken);
ar.AddContextProperty("Alexa.PowerController", "powerState", value, 200);
}
else {
ar = new AlexaResponse("Alexa", "ErrorResponse");

alexaResponse = new AlexaResponse("Alexa", "Response", endpointId, token, correlationToken);
alexaResponse.addContextProperty("Alexa.PowerController", "powerState", value, 200);

} else {

alexaResponse = new AlexaResponse("Alexa", "ErrorResponse");
}

break;

default:

System.out.println("INVALID Namespace");
ar = new AlexaResponse();
alexaResponse = new AlexaResponse();

break;
}

System.out.println("Response:");
System.out.println(ar);
System.out.println(alexaResponse);

outputStream.write(ar.toString().getBytes(Charset.forName("UTF-8")));
}
catch (Exception e)
{
outputStream.write(alexaResponse.toString().getBytes(StandardCharsets.UTF_8));

} catch (Exception e) {
e.printStackTrace();
}
}

static boolean sendDeviceState(String endpoint_id, String state, String value) {
static boolean sendDeviceState(String endpointId, String state, String value) {

String attributeValue = state + "Value";

Expand All @@ -111,7 +124,7 @@ static boolean sendDeviceState(String endpoint_id, String state, String value) {

UpdateItemSpec updateItemSpec =
new UpdateItemSpec()
.withPrimaryKey("ItemId", endpoint_id)
.withPrimaryKey("ItemId", endpointId)
.withUpdateExpression("set #v = :val1")
.withNameMap(new NameMap().with("#v", attributeValue))
.withValueMap(new ValueMap().withString(":val1", value))
Expand All @@ -123,7 +136,7 @@ static boolean sendDeviceState(String endpoint_id, String state, String value) {
return true;
}

static String getRequest(java.io.InputStream is) {
static String getRequest(InputStream is) {
Scanner s = new Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
Expand Down
99 changes: 56 additions & 43 deletions src/main/java/AlexaResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,36 @@

public class AlexaResponse {

private JSONObject response = new JSONObject("{}");
private JSONObject event = new JSONObject("{}");
private JSONObject header = new JSONObject("{}");
private JSONObject endpoint = new JSONObject("{}");
private JSONObject payload = new JSONObject("{}");

private String CheckValue(String value, String defaultValue) {

if (value.isEmpty())
return defaultValue;

return value;
}
private final JSONObject response = new JSONObject("{}");
private final JSONObject event = new JSONObject("{}");
private final JSONObject header = new JSONObject("{}");
private final JSONObject endpoint = new JSONObject("{}");
private final JSONObject payload = new JSONObject("{}");

public AlexaResponse() {
this("Alexa", "Response", "INVALID", "INVALID", null);
}

public AlexaResponse(String namespace, String name) { this(namespace, name, "INVALID", "INVALID", null); }
public AlexaResponse(String namespace, String name) {
this(namespace, name, "INVALID", "INVALID", null);
}

public AlexaResponse(String namespace, String name, String endpointId, String token, String correlationToken) {

header.put("namespace", CheckValue(namespace, "Alexa"));
header.put("name", CheckValue(name,"Response"));
header.put("namespace", checkValue(namespace, "Alexa"));
header.put("name", checkValue(name,"Response"));
header.put("messageId", UUID.randomUUID().toString());
header.put("payloadVersion", "3");

if (correlationToken != null) {
header.put("correlationToken", CheckValue(correlationToken, "INVALID"));
}
if (correlationToken != null)
header.put("correlationToken", checkValue(correlationToken, "INVALID"));

JSONObject scope = new JSONObject("{}");
scope.put("type", "BearerToken");
scope.put("token", CheckValue(token, "INVALID"));
scope.put("token", checkValue(token, "INVALID"));

endpoint.put("scope", scope);
endpoint.put("endpointId", CheckValue(endpointId, "INVALID"));
endpoint.put("endpointId", checkValue(endpointId, "INVALID"));

event.put("header", header);
event.put("endpoint", endpoint);
Expand All @@ -65,68 +58,83 @@ public AlexaResponse(String namespace, String name, String endpointId, String to
response.put("event", event);
}

public void AddCookie(String key, String value) {
private static String checkValue(String value, String defaultValue) {

if (value.isEmpty())
return defaultValue;

return value;
}

public void addCookie(String key, String value) {

JSONObject endpointObject = response.getJSONObject("event").getJSONObject("endpoint");
JSONObject cookie;

if (endpointObject.has("cookie")) {

cookie = endpointObject.getJSONObject("cookie");
cookie.put(key, value);

} else {

cookie = new JSONObject();
cookie.put(key, value);
endpointObject.put("cookie", cookie);
}

}

public void AddPayloadEndpoint(String friendlyName, String endpointId, String capabilities) {
public void addPayloadEndpoint(String friendlyName, String endpointId, String capabilities) {

JSONObject payload = response.getJSONObject("event").getJSONObject("payload");

if (payload.has("endpoints"))
{
if (payload.has("endpoints")) {

JSONArray endpoints = payload.getJSONArray("endpoints");
endpoints.put(new JSONObject(CreatePayloadEndpoint(friendlyName, endpointId, capabilities, null)));
}
else
{
endpoints.put(new JSONObject(createPayloadEndpoint(friendlyName, endpointId, capabilities, null)));

} else {

JSONArray endpoints = new JSONArray();
endpoints.put(new JSONObject(CreatePayloadEndpoint(friendlyName, endpointId, capabilities, null)));
endpoints.put(new JSONObject(createPayloadEndpoint(friendlyName, endpointId, capabilities, null)));
payload.put("endpoints", endpoints);
}
}

public void AddContextProperty(String namespace, String name, String value, int uncertaintyInMilliseconds)
{
public void addContextProperty(String namespace, String name, String value, int uncertaintyInMilliseconds) {

JSONObject context;
JSONArray properties;

try {

context = response.getJSONObject("context");
properties = context.getJSONArray("properties");

} catch (JSONException jse) {

context = new JSONObject();
properties = new JSONArray();
context.put("properties", properties);
}

properties.put(new JSONObject(CreateContextProperty(namespace, name, value, uncertaintyInMilliseconds)));
response.put("context", context);
properties.put(new JSONObject(createContextProperty(namespace, name, value, uncertaintyInMilliseconds)));

response.put("context", context);
}

public String CreateContextProperty(String namespace, String name, String value, int uncertaintyInMilliseconds) {
public String createContextProperty(String namespace, String name, String value, int uncertaintyInMilliseconds) {

JSONObject property = new JSONObject();
property.put("namespace", namespace);
property.put("name", name);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss'Z'");
TimeZone tz = TimeZone.getTimeZone("UTC");
sdf.setTimeZone(tz);
String timeOfSample = sdf.format(new Date().getTime());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss'Z'");

TimeZone timeZone = TimeZone.getTimeZone("UTC");
simpleDateFormat.setTimeZone(timeZone);

String timeOfSample = simpleDateFormat.format(new Date().getTime());

property.put("timeOfSample", timeOfSample);
property.put("uncertaintyInMilliseconds", uncertaintyInMilliseconds);
Expand All @@ -141,20 +149,25 @@ public String CreateContextProperty(String namespace, String name, String value,
return property.toString();
}

public String CreatePayloadEndpoint(String friendlyName, String endpointId, String capabilities, String cookie){
public String createPayloadEndpoint(String friendlyName, String endpointId, String capabilities, String cookie) {

JSONObject endpoint = new JSONObject();
endpoint.put("capabilities", new JSONArray(capabilities));
endpoint.put("description", "Sample Endpoint Description");

JSONArray displayCategories = new JSONArray("[\"OTHER\"]");
endpoint.put("displayCategories", displayCategories);

endpoint.put("manufacturerName", "Sample Manufacturer");

if (endpointId == null)
endpointId = "endpoint_" + 100000 + new Random().nextInt(900000);

endpoint.put("endpointId", endpointId);

if (friendlyName == null)
friendlyName = "Sample Endpoint";

endpoint.put("friendlyName", friendlyName);

if (cookie != null)
Expand All @@ -163,7 +176,7 @@ public String CreatePayloadEndpoint(String friendlyName, String endpointId, Stri
return endpoint.toString();
}

public String CreatePayloadEndpointCapability(String type, String interfaceValue, String version, String properties) {
public String createPayloadEndpointCapability(String type, String interfaceValue, String version, String properties) {

JSONObject capability = new JSONObject();
capability.put("type", type);
Expand All @@ -176,7 +189,7 @@ public String CreatePayloadEndpointCapability(String type, String interfaceValue
return capability.toString();
}

public void SetPayload(String payload) {
public void setPayload(String payload) {
response.getJSONObject("event").put("payload", new JSONObject(payload));
}

Expand Down
Loading