-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
123 lines (106 loc) · 3.44 KB
/
server.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 express = require("express");
const bodyParser = require('body-parser');
const redis = require("redis");
if (process.env.NODE_ENV != 'test') {
var Gpio = require('onoff').Gpio;
}
const app = express();
app.use(express.static(__dirname + '/public/'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const port = process.env.PORT || 3000
const client = redis.createClient({
host: 'redis-server',
port: 6379
});
client.set('value', 0);
client.set('toggle', 0);
if (process.env.NODE_ENV != 'test') {
var buzzer = new Gpio(4, 'out');
}
function beepbuzzer() { //function to start blinking
if (buzzer.readSync() === 0) { //check the pin state, if the state is 0 (or off)
buzzer.writeSync(1); //set pin state to 1 (turn buzzer on)
} else {
buzzer.writeSync(0); //set pin state to 0 (turn buzzer off)
}
}
app.post("/update", (req, res) => {
console.log(req.body.value);
client.set('value', req.body.value);
client.get('toggle', (err,value)=>{
if(!err){
if((req.body.value < 30)&&(value == 1)){
var blinkInterval = setInterval(beepbuzzer, 250);
setTimeout(endBlink, 5000);
function endBlink() { //function to stop blinking
clearInterval(blinkInterval); // Stop blink intervals
buzzer.writeSync(0); // Turn buzzer off
// buzzer.unexport(); // Unexport GPIO to free resources
}
}
if((req.body.value > 90)&&(value == 0)){
var blinkInterval = setInterval(beepbuzzer, 250);
setTimeout(endBlink, 5000);
function endBlink() { //function to stop blinking
clearInterval(blinkInterval); // Stop blink intervals
buzzer.writeSync(0); // Turn buzzer off
// buzzer.unexport(); // Unexport GPIO to free resources
}
}
// if((req.body.value > 70)&&(value == 0)){
// client.set('toggle', 1);
// }
}else{
res.statusCode(404);
}
});
res.sendStatus(200);
})
app.get('/', (req, res) => {
res.send("hello");
// var blinkInterval = setInterval(beepbuzzer, 250);
// setTimeout(endBlink, 5000);
// function endBlink() { //function to stop blinking
// clearInterval(blinkInterval); // Stop blink intervals
// buzzer.writeSync(0); // Turn buzzer off
// // buzzer.unexport(); // Unexport GPIO to free resources
// }
})
app.get('/status', (req,res)=>{
client.get('value', (err,value)=>{
if(!err){
var percentage = (((122-value)/122)*100);
res.json({
value: value,
percentage: percentage
});
}else{
res.statusCode(404);
}
})
});
app.get('/turnedoff', (req, res) => {
client.set('toggle', 0);
res.send("turnedoff");
})
app.get('/turnedon', (req, res) => {
client.set('toggle', 1);
res.send("turnedoff");
})
app.get('/togglestatus', (req,res)=>{
client.get('toggle', (err,value)=>{
if(!err){
res.json({
toggle: value
});
}else{
res.statusCode(404);
}
})
});
if (process.env.NODE_ENV === 'test') {
module.exports = app
} else {
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
}