summaryrefslogtreecommitdiffstats
path: root/meta-alexa-led/recipes-demo/alexa-led/files/index.js
blob: 5c5503f3ade12c501be35f21f1a0f34b68727383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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);
            }


          });

        });

    }

  };