TempTracker/routes/tempdb.js

150 lines
4.1 KiB
JavaScript

//
// tempdb.js - SQLite Backend for storing and retrieving temperatures
//
var sqlite3 = require("sqlite3").verbose();
var file = "./temperatures.db"
exports.getAll = function(req, res) {
var db = new sqlite3.Database(file);
var data = []
db.serialize(function() {
db.all("SELECT id, name, lasttemp as temp, lastupd as ts FROM sensors ORDER BY id;", function(err, rows) {
if (rows.length > 0) {
getTempRecursive(rows, data, res);
db.close();
} else {
db.close();
res.status(400).send({"errid": 4, "errtxt": "No Sensors in Database"});
return;
}
});
});
}
exports.getTemp = function(req, res) {
var db = new sqlite3.Database(file);
var data = [];
db.serialize(function() {
db.get("SELECT id, name, lasttemp as temp, lastupd as ts FROM sensors WHERE id = ?;", req.params.id, function(err, row) {
if (row !== undefined) {
data = row;
db.get("SELECT avg(value) as avg, max(value) as max, timestamp from temp_" + data.id + ";", function(err, row) {
if (row !== undefined) {
data.avg = row.avg;
data.max = row.max;
data.tsmax = row.timestamp;
db.get("SELECT min(value) as min, timestamp from temp_" + data.id + ";", function(err, row) {
if (row !== undefined) {
data.min = row.min;
data.tsmin = row.timestamp;
db.close();
res.type("application/json");
res.status(200).send(data);
}
});
}
});
} else {
db.close();
res.status(400).send({"errid": 3, "errtxt": "Sensor does not exist"});
}
});
});
}
exports.addTemp = function(req, res) {
var db = new sqlite3.Database(file);
db.serialize(function() {
db.run("CREATE TABLE IF NOT EXISTS sensors (id INTEGER PRIMARY KEY ASC, name TEXT, lasttemp NUMERIC, lastupd INTEGER);");
var name = req.body.name;
if (name == "") {
res.status(400).send({"errid": 1, "errtxt": "Missing parameters"});
return;
}
db.get("SELECT id, name FROM sensors WHERE name = ?", name, function(err, row) {
if (row === undefined) {
db.run("INSERT INTO sensors VALUES (NULL, ?, NULL, NULL);", name);
db.get("SELECT id, name FROM sensors WHERE name = ?", name, function(err, row) {
if (row !== undefined) {
db.run("CREATE TABLE IF NOT EXISTS temp_" + row.id + " (timestamp TIMESTAMP, value NUMERIC);");
db.close();
res.status(200).send(row);
} else {
res.status(400).send({"errid": 4, "errtxt": "Error inserting sensor"});
db.close();
}
});
} else {
res.status(400).send({"errid": 2, "errtxt": "Sensor already exists"});
db.close();
}
});
});
}
exports.updTemp = function(req, res) {
if (req.body.temp === undefined) {
res.status(400).send({"errid": 1, "errtxt": "Missing parameters"});
return;
}
if (req.body.ts === undefined) {
var d = new Date();
var ts = d.getTime();
} else {
var ts = req.body.ts;
}
var db = new sqlite3.Database(file);
db.serialize(function() {
db.get("SELECT id, name FROM sensors WHERE id = ?", req.params.id, function(err, row) {
if (row !== undefined) {
db.run("INSERT INTO temp_" + req.params.id + " VALUES (?, ?)", ts, req.body.temp);
db.run("UPDATE sensors SET lasttemp = ?, lastupd = ? where id = ?;", req.body.temp, ts, req.params.id);
db.close();
res.status(200).end();
} else {
db.close();
res.status(400).send({"errid": 3, "errtxt": "Sensor does not exist"});
}
});
});
}
// Helper Functions
function getTempRecursive(rows, data, res) {
if (rows.length > 0) {
var cur = rows.shift();
var db = new sqlite3.Database(file);
db.get("SELECT avg(value) as avg, max(value) as max, timestamp from temp_" + cur.id + ";", function(err, row) {
if (row !== undefined) {
cur.avg = row.avg;
cur.max = row.max;
cur.tsmax = row.timestamp;
db.get("SELECT min(value) as min, timestamp from temp_" + cur.id + ";", function(err, row) {
if (row !== undefined) {
cur.min = row.min;
cur.tsmin = row.timestamp;
db.close();
data.push(cur);
getTempRecursive(rows, data, res);
}
});
}
});
} else {
res.status(200).send(data);
}
}