summaryrefslogtreecommitdiffstats
path: root/meta-alexa-led/recipes-demo/alexa-led/files/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'meta-alexa-led/recipes-demo/alexa-led/files/index.js')
-rw-r--r--meta-alexa-led/recipes-demo/alexa-led/files/index.js142
1 files changed, 142 insertions, 0 deletions
diff --git a/meta-alexa-led/recipes-demo/alexa-led/files/index.js b/meta-alexa-led/recipes-demo/alexa-led/files/index.js
new file mode 100644
index 0000000..5c5503f
--- /dev/null
+++ b/meta-alexa-led/recipes-demo/alexa-led/files/index.js
@@ -0,0 +1,142 @@
+'use strict';
+var config = {};
+
+var AWS = require('aws-sdk');
+var Alexa = require('alexa-sdk');
+
+//replace with endpoint from AWS IoT Device
+var iotData = new AWS.IotData({endpoint: 'a22iwj71w43omu.iot.us-east-2.amazonaws.com', region: 'us-east-2'});
+var APP_ID = undefined; //OPTIONAL: replace with 'amzn1.echo-sdk-ams.app.[your-unique-value-here]';
+var SKILL_NAME = 'gateway-demo';
+
+exports.handler = function(event, context, callback) {
+ var alexa = Alexa.handler(event, context);
+ alexa.APP_ID = APP_ID;
+ alexa.registerHandlers(handlers);
+ alexa.execute();
+};
+
+var led_state = 0;
+
+var handlers = {
+ 'LaunchRequest': function () {
+
+ var LEDState = [
+ {"state":{ "desired":{"device":"minnowboard_1","led":0}}}
+ ];
+ var speechOutput="Gateway Operational";
+ AWSIoT.sendMessage(this, LEDState,speechOutput);
+
+ },
+ 'DeviceStateIntent': function () {
+
+ var speechOutput;
+ var itemSlot = this.event.request.intent.slots.LEDState;
+ console.log("itemSlot: "+itemSlot);
+ var LED_State="none";
+ if (itemSlot && itemSlot.value) {
+ LED_State = itemSlot.value.toLowerCase();
+ }
+ console.log("LED_State: "+ LED_State);
+ var deviceState=null;
+
+ switch (LED_State) {
+ case "on":
+ led_state = 1;
+ deviceState = [
+ {'state':{ 'desired':{'device':'minnowboard_1','led_state':led_state}}}
+ ];
+ speechOutput="L E D on";
+ console.log('on recieved')
+ break;
+ case "off":
+ led_state = 0;
+ deviceState = [
+ {'state':{ 'desired':{'device':'minnowboard_1','led_state':led_state}}}
+ ];
+ speechOutput="L E D off";
+ break;
+ default:
+ speechOutput="no L E D state";
+ break;
+ }
+ if (deviceState===null) {
+ this.emit(':tell',speechOutput);
+ } else {
+ AWSIoT.sendMessage(this, deviceState, speechOutput);
+ console.log("message sent: ", deviceState )
+ }
+
+
+ },
+ 'AMAZON.HelpIntent': function () {
+ this.attributes['speechOutput'] = 'You can ask questions such as, what\'s the L E D state, or, you can say exit... ' +
+ 'Now, what can I help you with?';
+ this.attributes['repromptSpeech'] = 'You can say things like, what\'s the L E D state, or you can say exit...' +
+ ' Now, what can I help you with?';
+ this.emit(':ask', this.attributes['speechOutput'], this.attributes['repromptSpeech'])
+ },
+ 'AMAZON.RepeatIntent': function () {
+ this.emit(':ask', this.attributes['speechOutput'], this.attributes['repromptSpeech'])
+ },
+ 'AMAZON.StopIntent': function () {
+ this.emit('SessionEndedRequest');
+ },
+ 'AMAZON.CancelIntent': function () {
+ this.emit('SessionEndedRequest');
+ },
+ 'SessionEndedRequest':function () {
+ this.emit(':tell', 'Goodbye!');
+ }
+ };
+
+var AWSIoT = {
+ postSendMessage: function (thisOfParent,speechOutput) {
+ console.log('doot doot, all doot');
+ thisOfParent.emit(':tell', speechOutput);
+ },
+ sendMessage: function (thisOfParent, deviceState, speechOutput) {
+
+ var itemsProcessed = 0;
+
+ deviceState.forEach((item, index, array) => {
+ console.log("item "+index+": "+JSON.stringify(item));
+ console.log("device "+index+": "+item.state.desired.device)
+
+ var thingNameAndPayload= {
+ thingName: 'minnowboard_1',
+ payload: JSON.stringify(item)
+ };
+
+ iotData.updateThingShadow(thingNameAndPayload, function(err, data) {
+ console.log('sent shadow update')
+ if (err) console.log(err, err.stack); // an error occurred
+ else console.log(data); // successful response
+
+
+ if (err){
+ console.log("update error: ");
+ console.log(err);
+ //Handle the error here
+ speechOutput += " - Error updating device state: " + err + " ";
+ speechOutput += thingNameAndPayload.thingName + " ";
+ speechOutput += thingNameAndPayload.payload;
+ }
+
+ else {
+ console.log("update success: ");
+ console.log(data);
+ }
+ itemsProcessed++;
+ if(itemsProcessed === array.length) {
+ AWSIoT.postSendMessage(thisOfParent, speechOutput);
+ }
+
+
+ });
+
+ });
+
+ }
+
+ };