This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webHookServer.js
124 lines (102 loc) · 2.43 KB
/
webHookServer.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
const exec = require('child_process').exec;
var express = require('express')
var app = express()
require('dotenv').config()
var secretAccessKey = process.env.ACCESS_KEY;
var port = process.env.PORT;
/////////////////////////////KAKU////////////////////////////////
app.get('/lampOn', function (req, res) {
if(req.query.secret == secretAccessKey) {
exec('./kaku ' + req.query.lamp + ' A on', function(error, stdout, stderr) {
if (!error) {
// print the output
// sys.puts(stdout);
} else {
// handle error
}
});
res.send("OK");
}else{
res.status(403).send("NO. :(");
}
})
app.get('/lampOff', function (req, res) {
if(req.query.secret == secretAccessKey) {
exec('./kaku ' + req.query.lamp +' A off', function(error, stdout, stderr) {
if (!error) {
// print the output
// sys.puts(stdout);
} else {
// handle error
}
});
res.send("OK");
}else{
res.status(403).send("NO. :(");
}
})
app.get('/coffee', function(req, res){
if(trackerConnected){
var date = new Date(latestBrew);
res.jsonp({coffeeLeft: coffeeLeft, latestBrew: date.toString() });
}else{
res.send("TRACKER NOT CONNECTED");
}
});
app.listen(port, function () {
console.log('Example app listening on port '+port+'!')
})
////////////////COFFEE TRACKER 9000 ////////////////////
var datetime = require('node-datetime');
var time = datetime.create();
var coffeeLeft = 0;
var newBrew = false;
var trackerConnected = false;
var latestBrew = time.now();
var serialport = require("serialport");
const parsers = serialport.parsers;
const parser = new parsers.Readline({
delimiter: '\r\n'
});
var ports = [];
var idx = 0;
var arduinoId = -1;
serialport.list(function (err, p) {
p.forEach(function(p) {
var name = p.manufacturer;
if(typeof name != 'undefined'){
if(name.includes("Arduino")){
arduinoId = idx;
}
}
console.log(' [' + idx + '] ' + p.comName);
ports.push(p.comName);
idx++;
});
if(arduinoId != -1){
var port = new serialport(ports[arduinoId], {
baudRate: 9600
});
port.pipe(parser);
port.on('open', function(){
console.log('Serial Port Opend');
trackerConnected = true;
parser.on('data',function(data){
//console.log(data);
if(data == "W"){
newBrew = true;
}
if(data == "B" && newBrew){
latestBrew = time.now();
newBrew = false;
}
if(data == "E"){
coffeeLeft = 0;
}
if(Number(parseInt(data))){
coffeeLeft = data;
}
});
});
}
});