-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandle_switch.js
More file actions
78 lines (76 loc) · 3.43 KB
/
Copy pathhandle_switch.js
File metadata and controls
78 lines (76 loc) · 3.43 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
module.exports={
handlePowerControl: function(request, context) {
// get device ID passed in during discovery
const api_url = process.env.API_URL;
var requestMethod = request.directive.header.name;
var powerResult;
var token = request.directive.endpoint.cookie.key1;
var switch_no = request.directive.endpoint.cookie.key2;
const request1 = require('request');
if (requestMethod === "TurnOn") {
// Make the call to your device cloud for control
var url = api_url + token + "/update/"+ switch_no + "?value=1";
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
request1(url, function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body), // Print the HTML for the Google homepage.
response_switch(request, context, powerResult),
context.succeed(response);
});
powerResult = "ON";
}
else if (requestMethod === "TurnOff") {
// Make the call to your device cloud for control
var url_off = api_url + token + "/update/"+ switch_no + "?value=0";
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
// Make the call to your device cloud for control and check for success
request1(url_off, function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body), // Print the HTML for the Google homepage.
response_switch(request, context, powerResult),
context.succeed(response);
});
powerResult = "OFF";
}
function response_switch(request, context, powerResult){
var requestMethod = request.directive.header.name;
var responseHeader = request.directive.header;
var deviceId = request.directive.endpoint.endpointId;
responseHeader.namespace = "Alexa";
responseHeader.name = "Response";
responseHeader.messageId = responseHeader.messageId + "-R";
// get user token pass in request
var requestToken = request.directive.endpoint.scope.token;
var contextResult = {
"properties": [{
"namespace": "Alexa.PowerController",
"name": "powerState",
"value": powerResult,
"timeOfSample": "2017-09-03T16:20:50.52Z", //retrieve from result.
"uncertaintyInMilliseconds": 50
}]
};
var response_json = {
context: contextResult,
event: {
header: responseHeader,
endpoint: {
scope: {
type: "BearerToken",
token: requestToken
},
endpointId: deviceId
},
payload: {}
}
};
function log(message, message1, message2) {
console.log(message + message1 + message2);
}
log("DEBUG", "Alexa.PowerController ", JSON.stringify(response_json));
context.succeed(response_json);
}
}
};