TempTracker/routes/tempdb.js

169 lines
4.6 KiB
JavaScript
Raw Normal View History

//
// tempdb.js - SQLite Backend for storing and retrieving temperatures
//
2015-01-28 07:21:10 +00:00
var sqlite3 = require("sqlite3").verbose();
var file = "./temperatures.db"
2015-01-28 07:21:10 +00:00
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;
}
});
});
}
2015-01-28 07:21:10 +00:00
exports.getTemp = function(req, res) {
if (req.query.from === undefined || req.query.to === undefined) {
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"});
}
});
});
} else {
var db = new sqlite3.Database(file);
var data = [];
db.serialize(function() {
db.each("select timestamp as ts, value as temp from temp_" + req.params.id + " where timestamp >= ? and timestamp <= ?;", req.query.from, req.query.to,
function(err, row) {
data.push([row.ts, row.temp]);
},
function(err, num) {
res.status(200).send(data);
}
);
});
}
2015-01-28 07:21:10 +00:00
}
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();
}
});
});
2015-01-28 07:21:10 +00:00
}
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"});
}
});
});
2015-01-28 07:21:10 +00:00
}
// 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);
}
}