-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
210 lines (171 loc) · 6.11 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
var express = require('express')
var fs = require('fs')
var formidable = require('formidable')
var SerialPort = require("serialport");
var SerialPort2 = require("serialport");
const Readline = require('@serialport/parser-readline')
const { parse } = require('json2csv');
var sqlite3 = require('sqlite3').verbose();
var app = express()
const { exec } = require("child_process");
var deviceport;
var device;
var db = new sqlite3.Database('seeohtwo.db');
//Config
var data = fs.readFileSync('./seeohtwoconfig.json'),Config;
try {
Config = JSON.parse(data);
}
catch (err) {
console.log('Error parsing config')
console.log(err);
}
//globals
var location_id = 0 //the location we tag the data with. See DB for list of locations
var bLogDataOn = false //don't start logging until web front end says to start
SerialPort.list().then(ports => {
ports.forEach(function(port) {
if(port.manufacturer==Config.manufacturerstring) {
deviceport=port.path;
}
console.log(port.path);
console.log(port.pnpId);
console.log(port.manufacturer);
});
if (deviceport==undefined) {
console.log("No FTDI/ESP32 device found");
process.exit(1);
}
device = new SerialPort2(deviceport, {baudRate: 9600 });
parser = device.pipe(new Readline({ delimiter: '\n' }));
parser.on('data',function (data) {
console.log('Data:', data)
var sensorreading;
sensorreading = parseJSONSafely(data)
if ((bLogDataOn)&&(typeof(sensorreading.co2) != "undefined"))
{
try {
query = "insert into seeohtwo (co2, location_id) values ("+sensorreading.co2+","+location_id+");";
console.log(query);
db.run(query);
}
catch (exception) {
console.log(exception)
}
}
})
});
function parseJSONSafely(str) {
try {
return JSON.parse(str);
}
catch (e) {
console.log(e);
// Return a default object, or null based on use case.
return {}
}
}
var server = app.listen(Config.port, function () {
var host = server.address().address
var port = server.address().port
console.log('seeohtwo App Listening At http://%s:%s', host, port)
});
app.get('/', function(req, res){
strQuery= "select location_id, case when location_id = "+location_id+" then 1 else 0 end \"currentlocation\", description from location;"
db.all(strQuery, function(err,rows) {
res.render('index.ejs', {
bLogDataOn: bLogDataOn,
locations: rows
});
});
});
app.get('/togglebLogDataOn', function(req, res){
if (bLogDataOn) {
bLogDataOn=false;
}
else {
bLogDataOn=true;
}
res.redirect('/');
});
app.get('/shutdown', function(req, res){
exec("shutdown now", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
res.send("Shutting Down - Turn off Pi in 1 min. For shutdown to work, suid bit must be set on shutdown by running as root: chmod 4755 /sbin/shutdown")
});
app.use(express.static('public'))
app.get('/recentstats', function(req, res){
strQuery = "select round(avg(A.co2)) \"last6avg\",min(A.timestamp) \"min6time\", round(avg(B.co2)) \"last60avg\",min(B.timestamp) \"min60time\" FROM (select co2,timestamp from seeohtwo order by seeohtwo_id desc limit 6) A, (select co2,timestamp from seeohtwo order by seeohtwo_id desc limit 60) B";
db.all(strQuery, function(err,rows) {
servertime = new Date().toISOString()
recentstats = {
last6avg: rows[0].last6avg,
min6time: rows[0].min6time,
last60avg: rows[0].last60avg,
min60time: rows[0].min60time,
servertime: servertime }
res.setHeader('Content-disposition', 'attachment; filename=readinglog.csv');
res.set('Content-Type', 'application/json');
res.status(200).send(recentstats);
});
});
//select avg(co2) FROM (select co2 from seeohtwo order by seeohtwo_id desc limit 2) A
app.get('/setlocation', function(req, res){
if (typeof req.query.locationid !== 'undefined') {
templocationid= parseInt(req.query.locationid);
if (Number.isInteger(templocationid)) {
if (templocationid<0) {iStreamSensorMode=0;}
else {location_id=templocationid;}
}
//tell sensor we've changed locations, recalibrate for new humidity/temperature
device.write('h', function(err) {
if (err) {
return console.log('Error on write: ', err.message)
}
console.log('Sent humidity calibration request')
})
}
res.redirect('/');
});
app.get('/getcsv', function(req, res){
fields = ['seeohtwo_id', 'co2', 'description', 'Timestamp'];
opts = {fields}
strQuery = "select seeohtwo_id, co2, description, Timestamp from seeohtwo a, location b where a.location_id = b.location_id";
db.all(strQuery, function(err,rows) {
csv = parse(rows, opts)
res.setHeader('Content-disposition', 'attachment; filename=readinglog.csv');
res.set('Content-Type', 'text/csv');
res.status(200).send(csv);
});
});
app.post('/addlocation', function (req, res){
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
addlocation = fields.addlocation;
strQuery = "INSERT INTO location (\"description\") VALUES (\""+addlocation+"\");";
db.run(strQuery, function(err) {
if (err) {
console.log(err.message)
}
res.redirect('/');
});
});
});
app.get('/purgedb', function (req, res){
strQuery = "delete from seeohtwo; vacuum;";
db.run(strQuery, function(err) {
if (err) {
console.log(err.message)
}
res.redirect('/');
});
});