-
Notifications
You must be signed in to change notification settings - Fork 15
/
example.js
230 lines (200 loc) · 7.19 KB
/
example.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
/**
* 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';
/**
* 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 '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 '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 Smart 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: 'SampleDiscovery',
name: 'SampleDiscoverAppliancesResponse',
payloadVersion: '1'
};
/**
* Response body will be an array of discovered devices.
*/
var appliances = [];
var applianceDiscovered = {
applianceId: 'Sample-Device-ID',
manufacturerName: 'SmartThings',
modelName: 'ST01',
version: 'VER01',
friendlyName: 'Sample Name',
friendlyDescription: 'the light in kitchen',
isReachable: true,
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': '2cd6b650-c0h0-4062-b31d-7ec2c146c5ea'
}
};
appliances.push(applianceDiscovered);
/**
* Craft the final response back to Alexa Smart 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) {
/**
* Fail the invocation if the header is unexpected. This example only demonstrates
* turn on / turn off, hence we are filtering on anything that is not SwitchOnOffRequest.
*/
if (event.header.namespace != 'Control' || event.header.name != 'SwitchOnOffRequest') {
context.fail(generateControlError('SwitchOnOffRequest', 'UNSUPPORTED_OPERATION', 'Unrecognized operation'));
}
if (event.header.namespace === 'Control' && event.header.name === 'SwitchOnOffRequest') {
/**
* Retrieve the appliance id and accessToken from the incoming message.
*/
var applianceId = event.payload.appliance.applianceId;
var accessToken = event.payload.accessToken.trim();
log('applianceId', applianceId);
/**
* Make a remote call to execute the action based on accessToken and the applianceId and the switchControlAction
* Some other examples of checks:
* validate the appliance is actually reachable else return TARGET_OFFLINE error
* validate the authentication has not expired else return EXPIRED_ACCESS_TOKEN error
* Please see the technical documentation for detailed list of errors
*/
var basePath = '';
if (event.payload.switchControlAction === 'TURN_ON') {
basePath = REMOTE_CLOUD_BASE_PATH + '/' + applianceId + '/on?access_token=' + accessToken;
} else if (event.payload.switchControlAction === 'TURN_OFF') {
basePath = REMOTE_CLOUD_BASE_PATH + '/' + applianceId + '/of?access_token=' + accessToken;
}
var options = {
hostname: REMOTE_CLOUD_HOSTNAME,
port: 443,
path: REMOTE_CLOUD_BASE_PATH,
headers: {
accept: '*/*'
}
};
var serverError = function (e) {
log('Error', e.message);
/**
* Craft an error response back to Alexa Smart 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 Smart Home Skill
*/
log('done with result');
var headers = {
namespace: 'Control',
name: 'SwitchOnOffResponse',
payloadVersion: '1'
};
var payloads = {
success: true
};
var result = {
header: headers,
payload: payloads
};
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;
}