-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstate_report.js
More file actions
79 lines (74 loc) · 3.1 KB
/
Copy pathstate_report.js
File metadata and controls
79 lines (74 loc) · 3.1 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
79
module.exports={
handleStateReport: function(request, context) {
// get device ID passed in during discovery
const api_url = process.env.API_URL;
var requestMethod = request.directive.header.name;
var token = request.directive.endpoint.cookie.key1;
var switch_no = request.directive.endpoint.cookie.key2;
var powerResult;
const request1 = require('request');
if (requestMethod === "ReportState") {
// Make the call to your device cloud for control
var url_get_state = api_url + token + "/get/"+ switch_no;
console.log(url_get_state);
// Make the call to your device cloud for control and check for success
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
request1(url_get_state, 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.
set_state(request, context, body),
context.succeed(response);
});
}
function set_state(request, context, body){
var regex = /[+-]?\d+(?:\.\d+)?/g;
var present_state = regex.exec(body);
if(present_state[0] === "0"){
powerResult = "OFF";
}
else if(present_state[0] === "1"){
powerResult = "ON";
}
response_state(request, context, powerResult);
}
function response_state(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 = "StateReport";
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);
}
}
};