-
Notifications
You must be signed in to change notification settings - Fork 0
/
cabLightsLambda.js
264 lines (218 loc) · 7.33 KB
/
cabLightsLambda.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
var http = require('https');
var kitchenCabinetLEDs = "5fd4da3e-db76-4554-9d5a-978b3be044f9";
var particleServer = "api.particle.io";
var particlePath = "/v1/devices/";
/**
* 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 Connected 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 kitchenLEDLights = {
applianceId: kitchenCabinetLEDs,
manufacturerName: 'srfnmnk',
modelName: 'LEDLights',
version: 'VER01',
friendlyName: 'Kitchen LED Lights',
friendlyDescription: 'Lights underneath Cabinets in Kitchen Raleigh',
isReachable: true,
actions:[
"incrementPercentage",
"decrementPercentage",
"setPercentage",
"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: '5fd4da3e-db76-4554-9d5a-978b3be044f9',
deviceId: "<REDACTED>"
}
};
appliances.push(kitchenLEDLights);
/**
* Craft the final response back to Alexa Connected 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) {
if (event.header.namespace === 'Alexa.ConnectedHome.Control') {
/**
* Retrieve the appliance id and accessToken from the incoming message.
*/
var accessToken = event.payload.accessToken;
var applianceId = event.payload.appliance.applianceId;
var deviceid = event.payload.appliance.additionalApplianceDetails.deviceId;
var message_id = event.header.messageId;
var param = "";
var state = 0;
var confirmation;
var funcName;
log("Access Token: ", accessToken);
log("DeviceID: ", deviceid);
log("Event Header Name: ", event.header.name);
if(event.header.name == "TurnOnRequest"){
state = 1;
confirmation = "TurnOnConfirmation";
funcName = "onoff";
}
else if(event.header.name == "TurnOffRequest"){
state = 0;
confirmation = "TurnOffConfirmation";
funcName = "onoff";
}
else if(event.header.name == "SetPercentageRequest"){
state = event.payload.percentageState.value;
confirmation = "SetPercentageConfirmation";
funcName = "setvalue";
}
else if(event.header.name == "IncrementPercentageRequest"){
var increment = event.payload.deltaPercentage.value;
state = increment;
confirmation = "IncrementPercentageConfirmation";
funcName = "deltachange";
}
else if(event.header.name == "DecrementPercentageRequest"){
var decrement = event.payload.deltaPercentage.value;
state = -decrement;
confirmation = "DecrementPercentageConfirmation";
funcName = "deltachange";
}
log('applianceId', applianceId);
param = state;
var options = {
"method": "POST",
"hostname": "api.particle.io",
"port": null,
"path": "/v1/devices/<REDACTED>/" + funcName,
"headers": {
"content-type": "application/x-www-form-urlencoded",
"authorization": "<REDACTED>",
"cache-control": "no-cache",
"postman-token": "<REDACTED>"
}
};
log(options);
var data = "args=" + param;
log(data);
var serverError = function (e) {
log('Error', e.message);
context.fail(generateControlError('TurnOnRequest', '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() {
log('Return Value');
log(str);
var headers = {
namespace: 'Alexa.ConnectedHome.Control',
name: confirmation,
payloadVersion: '2',
messageId: message_id
};
var payloads = {
};
var result = {
header: headers,
payload: payloads
};
context.succeed(result);
});
response.on('error', serverError);
};
var req = http.request(options, callback);
req.on('error', serverError);
req.write(data);
req.end();
}
}
/**
* Utility functions.
*/
function log(title, msg) {
console.log(title + ": " + msg);
}
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;
}