Created
November 28, 2015 09:35
-
-
Save TheSkorm/6af10346629231425764 to your computer and use it in GitHub Desktop.
This file contains 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 REMOTE_CLOUD_BASE_PATH = '/'; | |
var REMOTE_CLOUD_HOSTNAME = 'www.amazon.com'; | |
var https = require('https'); | |
/** | |
* Main entry point. | |
* Incoming events from Alexa Lighting APIs are processed via this method. | |
*/ | |
exports.handler = function(event, context) { | |
log('Input', event.request.intent); | |
handleDiscovery(event, context) ; | |
}; | |
function handleHealthCheck(event, context){ | |
healthcheck = { | |
"header": { | |
"namespace": "System", | |
"name": "HealthCheckResponse", | |
"payloadVersion": "1" | |
}, | |
"payload": { | |
"isHealthy": true, | |
"description": "The system is currently healthy" | |
} | |
} | |
context.succeed(healthcheck); | |
} | |
/** | |
* This method is invoked when we receive a "Discovery" message from Alexa Connected Home Skill. | |
* We are expected to respond back with a list of appliances that we have discovered for a given | |
* customer. | |
*/ | |
function handleDiscovery(event, context) { | |
var options={ | |
auth: "user:password", | |
hostname: "hostname", | |
method: "GET", | |
path: "/api/devices", | |
port: 443, | |
} | |
appliances = [] | |
https.get(options, function(res){ | |
var body = ''; | |
res.on('data', function(chunk){ | |
body += chunk; | |
}); | |
res.on('end', function(){ | |
var response = JSON.parse(body); | |
var length = response.devices.length | |
var appliances = {} | |
for(x = 0; x<length; x++) | |
{ | |
console.log(response.devices[x]) | |
appliances[response.devices[x].name.toLowerCase()] = response.devices[x].id | |
} | |
handleControl(event.request.intent, context, appliances) | |
}); | |
}).on('error', function(e){ | |
console.log("Got an error: ", e); | |
}); | |
} | |
/** | |
* Control events are processed here. | |
* This is called when Alexa requests an action (IE turn off appliance). | |
*/ | |
function handleControl(event, context, appliances) { | |
var basePath = ''; | |
if (event.slots.States.value === 'on') { | |
basePath = '/api/device/' + appliances[event.slots.Appliance.value.toLowerCase()] + '/turnOn'; | |
} else if (event.slots.States.value === 'off') { | |
basePath = '/api/device/' + appliances[event.slots.Appliance.value.toLowerCase()] + '/turnOff'; | |
} | |
console.log(appliances) | |
console.log(event.slots.Appliance.value.toLowerCase()) | |
var options={ | |
auth: "user:password", | |
hostname: "hostname", | |
method: "GET", | |
path: basePath, | |
port: 443, | |
} | |
var serverError = function (e) { | |
log('Error', e.message); | |
/** | |
* Craft an error response back to Alexa Connected Home Skill | |
*/ | |
context.fail(generateControlError('SwitchOnOffRequest', '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() { | |
/** | |
* Test the response from remote endpoint (not shown) and craft a response message | |
* back to Alexa Connected Home Skill | |
*/ | |
log('done with result'); | |
result = { | |
"version": "1.0", | |
"sessionAttributes": {}, | |
"response": { | |
"outputSpeech": { | |
"type": "PlainText", | |
"text": "Switched" | |
}, | |
"card": { | |
"type": "Simple", | |
"title": "SessionSpeechlet - Switched", | |
"content": "SessionSpeechlet - Switched" | |
}, | |
"reprompt": { | |
"outputSpeech": { | |
"type": "PlainText", | |
"text": "" | |
} | |
}, | |
"shouldEndSession": true | |
} | |
} | |
log('Done with result', result); | |
context.succeed(result); | |
}); | |
response.on('error', serverError); | |
}; | |
/** | |
* 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: 'Control', | |
name: name, | |
payloadVersion: '1' | |
}; | |
var payload = { | |
exception: { | |
code: code, | |
description: description | |
} | |
}; | |
var result = { | |
header: headers, | |
payload: payload | |
}; | |
return result; | |
}console.log("placeholder"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment