forked from openhab/openhab-alexa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (69 loc) · 2.69 KB
/
index.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
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
var log = require('./log.js');
var utils = require('./utils.js');
var oh2 = require('./oh2.js');
/**
* Main entry point.
* Incoming events from Alexa Lighting APIs are processed via this method.
*/
exports.handler = function (event, context) {
log.debug('Input: ' + JSON.stringify(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':
oh2.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':
case 'Alexa.ConnectedHome.Query':
oh2.handleControl(event, context);
break;
/**
* Requests the availability of the skill adapter. These are periodically sent by
* the Smart Home Skill API to the skill adapter.
*/
case 'Alexa.ConnectedHome.System':
// TODO - handle unhealthy device responses
if (event.header.name === 'HealthCheckRequest') {
var headers = {
messageId: event.header.messageId,
name: event.header.name.replace('Request', 'Response'),
namespace: event.header.namespace,
payloadVersion: event.header.payloadVersion
};
var payloads = {
description: 'The system is currently healthy',
isHealthy: true
};
var result = {
header: headers,
payload: payloads
};
context.succeed(result);
}
break;
/**
* We received an unexpected message
*/
default:
log.error('No supported namespace: ' + event.header.namespace);
context.done(null, utils.generateControlError(event.header.messageId, event.header.name, 'DependentServiceUnavailableError', 'Something went wrong...'));
break;
}
};