-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.js
More file actions
148 lines (126 loc) · 4.64 KB
/
lambda.js
File metadata and controls
148 lines (126 loc) · 4.64 KB
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
143
144
145
146
147
148
"use strict";
const parseString = require('xml2js').parseString;
const http = require('http');
const base_url = 'http://geninhofloripa.ddns.net:82';
const Alexa = require("alexa-sdk");
const replies = {
launch1: "switchboard listening!",
launch2: "Hello, this is the switchboard. This is deployed on a aws lambda function. All systems are go!",
whoIs : "Hey Marcelo, that is easy! Eugenio is the most special man on earth! You guys make a wonderful couple together",
worklightsOn : "this one?",
worklightsOff: "ok",
deskOn : "desklights are on",
deskOff: "desklights are off",
bathLightsOff : "is that it?",
bathLightsOn: "done",
gateStateClose: "the sensor shows gate is closed",
gateStateOpen: "the sensor shows gate is open",
gateOpening: "gate should be opening now",
gateClosing: "gate should be closing now",
gateAlreadyClosed: "sorry gate seems to be already closed",
gateAlreadyOpen: "sorry gate seems to be already open",
};
const pins = {
"WORKLIGHTS" :62,
"PORTCH" :26,
"VERDE FUNDOS" :30,
"FRED LOW" :32,
"FIGUEIRA" :34,
"DECK MAIN" :36,
"HIDROMASSAGEM" :29,
"RAMPA" :33,
"DECK HI" :37,
"LUZ HIDRO" :38,
"FILL" :40,
"HOLLY+DECK" :42,
"SHELVES" :46,
"DESK" :48,
"LAVANDERIA" :66,
"PORTAO" :24,
};
function callArduino(pinNum, action, reply) {
const url = base_url + '/PIN' + pinNum + '=' + action;
return function () {
http.get(url, (error, response, body) => {
this.response.speak(reply);
this.emit(':responseReady');
});
};
}
function isGateClosed(callback) {
http.get(base_url, (res) => {
res.setEncoding('utf8');
let xml = '';
res.on('data', (chunk) => { xml += chunk; });
res.on('end', () => {
parseString(xml, function (err, result) {
const error = err;
const gateState = result.estadoDomotica.Pin.filter(pin => pin.digitalPin[0] === '14')[0].Estado[0].namePin[0];
callback(gateState == 'portão fechado');
});
});
});
}
var handlers = {
// <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 //
"WhoIsTheOne": function () {
this.response.speak(replies["WHOIS"] );
this.emit(':responseReady');
}, // :-) :-) :-) :-) :-) :-) :-) :-) :-) //
"LaunchRequest": function () {
this.response.speak(replies["LAUNCH1"]);
this.emit(':responseReady');
},
"CeilingLightsOnIntent" : callArduino.call(this, pins["WORKLIGHTS"], "ON", replies.worklightsOn ),
"CeilingLightsOffIntent": callArduino.call(this, pins["WORKLIGHTS"], "OFF", replies.worklightsOff ),
"DeskLightsOnIntent" : callArduino.call(this, pins["DESK"], "ON", replies.deskOn),
"DeskLightsOffIntent": callArduino.call(this, pins["DESK"], "OFF", replies.deskOff ),
"bathroomLightsOnIntent" : callArduino.call(this, pins["LAVANDERIA"], "ON", replies.bathLightsOn),
"bathroomLightsOffIntent" : callArduino.call(this, pins["LAVANDERIA"], "OFF", replies.bathLightsOff),
"gateOpenIntent": function () {
isGateClosed(isClosed => {
if (isClosed) {
callArduino.call(this, pins["PORTAO"], "ON", replies.gateOpening).call(this);
} else {
this.response.speak(replies.gateAlreadyOpen);
this.emit(':responseReady');
}
});
},
"gateCloseIntent": function () {
isGateClosed(isClosed => {
if (isClosed) {
this.response.speak(replies.gateAlreadyClosed);
this.emit(':responseReady');
} else {
callArduino.call(this, pins["PORTAO"], "ON", replies.gateOpening).call(this);
}
});
},
"gateStateIntent": function () {
isGateClosed(isClosed => {
this.response.speak(isClosed ? replies.gateStateClose : replies.gateStateOpen);
this.emit(':responseReady');
});
},
/* TODO: Create intent for items below:
gardenLightsOnIntent
gardenLightsOffIntent
bathroomLightsOnIntent
bathroomLightsOnIntent
shelvesLightOnIntent
shelvesLightOffIntent
jacuzziFillIntentOn
jacuzziFillIntentOff
jacuzziMassageOnIntent
jacuzziMassageOffIntent
jacuzziLightsOnIntent
jacuzziLightsOffIntent
*/
};
// registra handlers e executa Lambda.
exports.handler = function(event, context, callback) {
var alexa = Alexa.handler(event, context);
alexa.registerHandlers(handlers);
alexa.execute();
};