-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
148 lines (130 loc) · 3.96 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Initialise server
const express = require('express');
const app = express();
var fs = require('fs');
var path = require('path');
var mongodb = require('mongodb');
var cors = require('cors');
var multer = require('multer');
var storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads')
},
filename: (req, file, cb) => {
cb(null, "image.jpg")
}
});
var upload = multer({dest: '/uploads/', storage: storage});
var bodyParser = require('body-parser');
const PORT = process.env.PORT || 5000;
// Middleware
app.use(express.static('public'));
app.use(bodyParser({extended: false}));
app.use(bodyParser.json());
app.use(upload.any());
app.use(cors());
var db;
var COLL_SENSOR_DATA = 'sensor_data';
var COLL_SPRAY = 'spray';
var mongodb_url = "mongodb://localhost:27017/sensor_data";
mongodb.MongoClient.connect(process.env.MONGODB_URI || mongodb_url, (err, client) => {
if (err) {
console.error(err);
process.exit(1);
}
db = client.db();
console.log('Database connection ready!');
// Initialise app
var server = app.listen(PORT, () => {
console.log("Example app listening at", server.address().port);
});
})
// Endpoint for main page
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
});
// Endpoint to upload jpg file
app.post('/api/upload', (req, res) => {
var filename = req.files[0].filename;
var file = path.join(__dirname, filename);
fs.readFile(file, (err, data) => {
fs.writeFile(file, data, (err) => {
if (err){
console.log(err);
} else {
response = {
message: "File uploaded successfully",
filename: "image.jpg"
};
}
console.log(response);
res.end(JSON.stringify(response));
});
});
});
// Endpoint to get image file
app.get('/image.jpg', (req, res) => {
res.sendFile(path.resolve('./uploads/image.jpg'))
})
// Endpoint to get CSS file
app.get('/index.css', (req, res) => {
res.sendFile(path.join(__dirname + '/index.css'));
});
// Endpoint to get js file
app.get('/index.js', (req, res) => {
res.sendFile(path.join(__dirname + '/index.js'));
});
// Endpoint to store sensor data
// TODO: Validation of data
app.get('/api/sensor', (req, res) => {
db.collection(COLL_SENSOR_DATA).find().toArray((err, result) => {
if (err) {
console.error(err);
return res.send(500);
}
console.log(result);
res.send(result);
})
});
app.post('/api/sensor', (req, res) => {
var data = req.body;
if (!data.date){
console.error("Invalid JSON");
res.status(400).send({error: "Invalid JSON"});
}
var input_date = data.date;
var input_time = data.time;
var input_sensor_data = data.sensor_data;
db.collection(COLL_SENSOR_DATA).save(data, (err, result) => {
if (err) return console.log(err);
console.log('Data saved to database');
res.end();
});
});
// Endpoint to set if user sprayed
// Sets a variable for 5 seconds, and returns it back to normal
app.post('/api/spray', (req, res) => {
db.collection(COLL_SPRAY).save({"spray": 1}, (err, result) => {
if (err) return console.log(err);
console.log('Plant sprayed!');
setTimeout(()=>{
db.collection(COLL_SPRAY).save({"spray": 0}, (err, result) => {
if (err) return console.log(err);
console.log("Reset spray variable!");
});
}, 7000);
res.end();
});
});
// Endpoint to check if user pressed the spray button
app.get('/api/spray', (req, res) => {
db.collection(COLL_SPRAY).find().toArray((err, result) => {
if (err) {
console.error(err);
return res.send(500);
}
console.log(result);
length_of_array = result.length;
res.send(result[length_of_array-1]);
})
});