forked from openhab/openhab-alexa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (101 loc) · 4.02 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
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
/**
* 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 https = require('https');
var rest = require('./rest.js');
var utils = require('./utils.js');
var oh2 = require('./oh2.js');
var oh1 = require('./oh1.js');
/**
* Main entry point.
* Incoming events from Alexa Lighting APIs are processed via this method.
*/
exports.handler = function (event, context) {
// DEBUG
utils.log('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':
rest.getOpenhabVersion(event.payload.accessToken, function (version) {
// DEBUG
utils.log("Alexa.ConnectedHome.Discovery", "Result " + version);
switch (version) {
// OH1 version not working right now
// case 1:
// oh1.handleDiscovery(event, context);
// break;
case 2:
oh2.handleDiscovery(event, context);
break;
default:
context.done(null, utils.generateControlError(event.header.messageId, event.header.name, 'DependentServiceUnavailableError', 'Could not connect to system, unknown version!'));
break;
}
});
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':
{
switch (event.payload.appliance.additionalApplianceDetails.openhabVersion) {
case '1':
oh1.handleControl(event, context);
break;
case '2':
oh2.handleControl(event, context);
break;
default:
// DEBUG
utils.log('Err', 'No supported version: ' + event.header.namespace);
context.done(null, utils.generateControlError(event.header.messageId, event.header.name, 'DependentServiceUnavailableError', 'Missing or invalid OpenHAB version in the request!'));
break;
}
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:
// DEBUG
utils.log('Err', 'No supported namespace: ' + event.header.namespace);
context.done(null, utils.generateControlError(event.header.messageId, event.header.name, 'DependentServiceUnavailableError', 'Something went wrong...'));
break;
}
};