-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (109 loc) · 3.67 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const http = require('http');
const Wifi = require("Wifi");
const WIFI_NAME = "Erick Wendel";
const WIFI_OPTIONS = { password: "" };
const relayPin = 2;//D7;
// const URL = 'http://192.168.15.72:1880/lightSensor';
// const incomingPassword = "H-random-0.,x";
const TOKEN = '0bb14ac18a2cab7d01d69a3f65e5c7ee1272c';
function promisify(fn, args) {
return new Promise((resolve, reject) => {
const cbFn = (err, res) => err ? reject(err) : resolve(res);
const finalArgs = args.concat(cbFn);
return fn.apply(this, finalArgs);
});
}
function connectWifi(alreadyConnected) {
if (alreadyConnected) return;
return Promise.resolve()
.then(() => promisify(Wifi.connect, [WIFI_NAME, WIFI_OPTIONS]));
}
function postJSON(postURL, data) {
const content = JSON.stringify(data);
const options = url.parse(postURL);
options.method = 'POST';
options.headers = {
"Content-Type": "application/json",
"Content-Length": content.length
};
return new Promise((resolve, reject) => {
const req = http.request(options, function (res) {
let d = "";
res.on('data', (data) => d += data);
res.on('close', resolve);
});
req.on('error', reject);
req.end(content);
});
}
function handleWifi() {
const wifiStatus = Wifi.getStatus();
const alreadyConnected = wifiStatus.station == "connected";
if (wifiStatus.station === 'connecting') {
console.log('trying to connect to Wifi..');
return;
}
return Promise.resolve()
.then(() => connectWifi(alreadyConnected))
.catch(error => {
console.log('Deu Ruim', error);
if (!error) return;
console.log('Deu Ruim', error.stack);
});
}
function timeout(ms) {
return new Promise(r => setTimeout(r, ms));
}
function activateRelay(delay) {
return Promise.resolve()
.then(() => digitalWrite(relayPin, HIGH))
.then(() => console.log('locker on'))
.then(() => timeout(delay))
.then(() => console.log('locker off!'))
.then(() => digitalWrite(relayPin, LOW));
}
function startServer() {
return new Promise((resolve, reject) => {
http.createServer((request, response) => {
console.log('request arrived!', request.url);
if(!request.url.includes(`TOKEN=${TOKEN}`)) {
response.writeHead(401)
response.end("unauthorized!");
return;
}
if (request.url.includes("/locker")) {
response.writeHead(200);
response.write('locker activated!');
response.end();
activateRelay(1000);
return;
}
response.end('Hi!');
return;
})
.listen(80)
.on('error', (msg) => {
console.log('error on initializing server', msg);
reject(msg)
})
setTimeout(resolve, 500);
})
}
function initialize() {
return Promise.resolve()
.then(() => handleWifi())
.then(() => console.log(`I'm running at http://${Wifi.getIP().ip}:80 and http://${Wifi.getHostname()}:80`))
.then(() => startServer())
.catch((error) => {
console.log('error', error);
console.log('error detail', { message: error.message, statck: error.stack });
const timeoutDelay = 1000;
console.log('rebooting...');
setTimeout(() => E.reboot(), timeoutDelay);
});
}
function onInit() {
Wifi.stopAP();
initialize();
}
// http://doorcontroller/locker?TOKEN=0bb14ac18a2cab7d01d69a3f65e5c7ee1272c