2015-02-08 13:00:44 +00:00
|
|
|
//
|
|
|
|
// tempdb.js - SQLite Backend for storing and retrieving temperatures
|
|
|
|
//
|
2015-01-28 07:21:10 +00:00
|
|
|
|
2015-02-08 13:00:44 +00:00
|
|
|
var sqlite3 = require("sqlite3").verbose();
|
|
|
|
var file = "./temperatures.db"
|
2015-01-28 07:21:10 +00:00
|
|
|
|
|
|
|
exports.getAll = function(req, res) {
|
2015-02-08 13:00:44 +00:00
|
|
|
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) {
|
2015-02-08 13:00:44 +00:00
|
|
|
var db = new sqlite3.Database(file);
|
2015-02-08 20:11:46 +00:00
|
|
|
var data = [];
|
2015-02-08 13:00:44 +00:00
|
|
|
|
|
|
|
db.serialize(function() {
|
2015-02-08 20:11:46 +00:00
|
|
|
db.get("SELECT id, name, lasttemp as temp, lastupd as ts FROM sensors WHERE id = ?;", req.params.id, function(err, row) {
|
2015-02-08 13:00:44 +00:00
|
|
|
if (row !== undefined) {
|
2015-02-08 20:11:46 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-02-08 13:00:44 +00:00
|
|
|
} else {
|
|
|
|
db.close();
|
|
|
|
res.status(400).send({"errid": 3, "errtxt": "Sensor does not exist"});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2015-01-28 07:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.addTemp = function(req, res) {
|
2015-02-08 13:00:44 +00:00
|
|
|
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) {
|
2015-02-08 13:00:44 +00:00
|
|
|
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
|
|
|
}
|
2015-02-08 20:11:46 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|