Created
January 11, 2017 21:02
-
-
Save johnypony3/b4569d7dfdad79e0a2cdc9c24bb28261 to your computer and use it in GitHub Desktop.
this is a lambda function to be used by an alexa homeskill. this will write to a sns topic, the idea is to have another lambda function subscribe, and act
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This sample demonstrates a simple driver built against the Alexa Lighting Api. | |
* For additional details, please refer to the Alexa Lighting API developer documentation | |
* https://developer.amazon.com/public/binaries/content/assets/html/alexa-lighting-api.html | |
*/ | |
var https = require('https'); | |
var AWS = require('aws-sdk'); | |
var REMOTE_CLOUD_BASE_PATH = '/'; | |
var REMOTE_CLOUD_HOSTNAME = 'www.amazon.com'; | |
/** | |
* Main entry point. | |
* Incoming events from Alexa Lighting APIs are processed via this method. | |
*/ | |
exports.handler = function(event, context) { | |
log('Input', event); | |
switch (event.header.namespace) { | |
/** | |
* The namespace of "Discovery" indicates a request is being made to the lambda for | |
* discovering all appliances associated with the customer's appliance cloud account. | |
* can use the accessToken that is made available as part of the payload to determine | |
* the customer. | |
*/ | |
case 'Alexa.ConnectedHome.Discovery': | |
handleDiscovery(event, context); | |
break; | |
/** | |
* The namespace of "Control" indicates a request is being made to us to turn a | |
* given device on, off or brighten. This message comes with the "appliance" | |
* parameter which indicates the appliance that needs to be acted on. | |
*/ | |
case 'Alexa.ConnectedHome.Control': | |
handleControl(event, context); | |
break; | |
/** | |
* We received an unexpected message | |
*/ | |
default: | |
log('Err', 'No supported namespace: ' + event.header.namespace); | |
context.fail('Something went wrong'); | |
break; | |
} | |
}; | |
/** | |
* This method is invoked when we receive a "Discovery" message from Alexa Smart Home Skill. | |
* We are expected to respond back with a list of appliances that we have discovered for a given | |
* customer. | |
*/ | |
function handleDiscovery(accessToken, context) { | |
/** | |
* Crafting the response header | |
*/ | |
var headers = { | |
namespace: 'Alexa.ConnectedHome.Discovery', | |
name: 'DiscoverAppliancesResponse', | |
payloadVersion: '2' | |
}; | |
/** | |
* Response body will be an array of discovered devices. | |
*/ | |
var appliances = []; | |
var applianceDiscovered = { | |
applianceId: 'Sample-Device-ID', | |
manufacturerName: 'SmartThings', | |
modelName: 'ST01', | |
version: 'VER01', | |
friendlyName: 'kitchen light', | |
friendlyDescription: 'light in kitchen', | |
isReachable: true, | |
actions: [ | |
"TurnOn", | |
"TurnOff" | |
], | |
additionalApplianceDetails: { | |
/** | |
* OPTIONAL: | |
* We can use this to persist any appliance specific metadata. | |
* This information will be returned back to the driver when user requests | |
* action on this appliance. | |
*/ | |
'fullApplianceId': '2cd6b650-c0h0-4062-b31d-7ec2c146c5ea' | |
} | |
}; | |
appliances.push(applianceDiscovered); | |
/** | |
* Craft the final response back to Alexa Smart Home Skill. This will include all the | |
* discoverd appliances. | |
*/ | |
var payloads = { | |
discoveredAppliances: appliances | |
}; | |
var result = { | |
header: headers, | |
payload: payloads | |
}; | |
log('Discovery', result); | |
context.succeed(result); | |
} | |
/** | |
* Control events are processed here. | |
* This is called when Alexa requests an action (IE turn off appliance). | |
*/ | |
function handleControl(event, context) { | |
/** | |
* Fail the invocation if the header is unexpected. This example only demonstrates | |
* turn on / turn off, hence we are filtering on anything that is not SwitchOnOffRequest. | |
*/ | |
if (event.header.namespace != 'Alexa.ConnectedHome.Control' || (event.header.name != 'TurnOnRequest' && event.header.name != 'TurnOffRequest')) { | |
context.fail(generateControlError(event.header.name, 'UNSUPPORTED_OPERATION', 'Unrecognized operation')); | |
} | |
if (event.header.namespace === 'Alexa.ConnectedHome.Control' && (event.header.name === 'TurnOnRequest' || event.header.name === 'TurnOffRequest')) { | |
/** | |
* Retrieve the appliance id and accessToken from the incoming message. | |
*/ | |
var applianceId = event.payload.appliance.applianceId; | |
var accessToken = event.payload.accessToken.trim(); | |
log('applianceId', applianceId); | |
var serverError = function (e) { | |
log('Error', e.message); | |
/** | |
* Craft an error response back to Alexa Smart Home Skill | |
*/ | |
context.fail(generateControlError(event.header.name, 'DEPENDENT_SERVICE_UNAVAILABLE', 'Unable to connect to server')); | |
}; | |
/*var callback = function(response) { | |
var str = ''; | |
response.on('data', function(chunk) { | |
str += chunk.toString('utf-8'); | |
}); | |
response.on('end', function() { | |
}); | |
response.on('error', serverError); | |
};*/ | |
AWS.config.region = 'us-east-1'; | |
var sns = new AWS.SNS(); | |
var message = ""; | |
log('event.header.name', event.header.name); | |
if (event.header.name === 'TurnOnRequest') { | |
message = '{"topic": "0", "action": "true"}'; | |
} else if (event.header.name === 'TurnOffRequest') { | |
message = '{"topic": "0", "action": "false"}'; | |
} | |
sns.publish({ | |
Message: message, | |
TopicArn: '*****FILL IN YOUR TOPIC HERE******' | |
}, function(err, data) { | |
if (err) { | |
log('sns err', err.stack); | |
serverError; | |
} | |
log('sns','push sent'); | |
log('sns',data); | |
log('event.header.name-end', event.header.name); | |
var headers = { | |
namespace: 'Alexa.ConnectedHome.Control', | |
name: event.header.name === 'TurnOnRequest' ? 'TurnOnConfirmation' : 'TurnOffConfirmation', | |
payloadVersion: '2', | |
messageId: event.header.messageId | |
}; | |
var payloads = { | |
success: true | |
}; | |
var result = { | |
header: headers, | |
payload: payloads | |
}; | |
log('Done with result', result); | |
context.succeed(result); | |
}); | |
/** | |
* Make an HTTPS call to remote endpoint. | |
*/ | |
//https.get(options, callback) | |
//.on('error', serverError).end(); | |
} | |
} | |
/** | |
* Utility functions. | |
*/ | |
function log(title, msg) { | |
console.log('*************** ' + title + ' *************'); | |
console.log(msg); | |
console.log('*************** ' + title + ' End*************'); | |
} | |
function generateControlError(name, code, description) { | |
var headers = { | |
namespace: 'Alexa.ConnectedHome.Control', | |
name: name, | |
payloadVersion: '2' | |
}; | |
var payload = { | |
exception: { | |
code: code, | |
description: description | |
} | |
}; | |
var result = { | |
header: headers, | |
payload: payload | |
}; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment