This repository has been archived by the owner on Jul 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path'use strict';.js
107 lines (88 loc) · 2.68 KB
/
'use strict';.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
"use strict";
/**
* The following JSON template shows what is sent as the payload:
{
"serialNumber": "GXXXXXXXXXXXXXXXXX",
"batteryVoltage": "xxmV",
"clickType": "SINGLE" | "DOUBLE" | "LONG"
}
*
* A "LONG" clickType is sent if the first press lasts longer than 1.5 seconds.
* "SINGLE" and "DOUBLE" clickType payloads are sent for short clicks.
*
* For more documentation, follow the link below.
* http://docs.aws.amazon.com/iot/latest/developerguide/iot-lambda-rule.html
*/
const http = require("http");
function sendRequest(authToken, serialNum, cookies) {
// need to construct the form data with the auth token and a event obj??
let form_data = {
authenticity_token: authToken,
event: { uuid: serialNum },
};
const options = {
hostname: "13.210.69.76",
port: 80,
path: `/events`,
method: "POST",
headers: {
ContentType: "application/x-www-form-urlencoded",
ContentLength: Object.keys(form_data).length,
Cookie: cookies
},
};
const req = http.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
res.on("data", (d) => {
process.stdout.write(d); //if response sent data back it gets printed
});
});
req.on("error", (error) => {
console.error(error);
});
// req.write(`uuid=${serialNum}`);
req.end();
}
exports.handler = (event, context, callback) => {
console.log("Received event:", event.serialNumber);
// need to make an http request to the server to get the cookie containing the authentication token
const options1 = {
hostname: "13.210.69.76",
port: 80,
path: `/events/new`,
method: "GET",
};
let auth_token;
let cookies;
const req1 = http.request(options1, (res) => {
console.log(`statusCode: ${res.statusCode}`);
cookies = res.headers["set-cookie"]
// res.on("data", (d) => {
// process.stdout.write(d); //if response sent data back it gets printed
// const lines = d.toString().split("\n");
// for (let line of lines) {
// if (line.includes("csrf-token")) {
// auth_token = line.match(/content="(.*)"/)[1];
// }
// }
// });
res.on("data", (d) => {
process.stdout.write(d); //if response sent data back it gets printed
const lines = d.toString().split("\n");
for (let line of lines) {
if (line.includes("csrf-token")) {
auth_token = line.match(/content="(.*)"/)[1];
}
}
});
res.on("end", function () {
console.log("auth_token: ", auth_token);
sendRequest(auth_token, event.serialNumber, cookies);
});
});
req1.end();
// sendRequest(event.serialNumber);
};
// {
// "serialNumber": "P5SJVQ2003H558W6"
// }