Temperature-DB Working

* GET all, PUT and POST working
* JQuery Mobile page to show temperatures
* Send HTML for index
This commit is contained in:
seiichiro 2015-02-08 14:00:44 +01:00
parent a22bc75dbd
commit 8f51247037
15 changed files with 540 additions and 11 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
node_modules
temperatures.db

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

0
html/inc/images/none.gif Normal file
View file

2
html/inc/jquery-1.8.2.min.js vendored Normal file

File diff suppressed because one or more lines are too long

2
html/inc/jquery.mobile-1.3.0.min.css vendored Normal file

File diff suppressed because one or more lines are too long

2
html/inc/jquery.mobile-1.3.0.min.js vendored Normal file

File diff suppressed because one or more lines are too long

368
html/index.html Normal file
View file

@ -0,0 +1,368 @@
<!DOCTYPE html>
<!--
file: index.html
author: Stefan Brand (seiichiro<at>seiichiro0185.org)
date: 17.08.2013
desc: This is the main file for the web-based RaspiControl.
It countains the jquerymobile UI code and the javascript to
fetch temperature data and control wireless switches
-->
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Temperaturen</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<link rel="stylesheet" href="inc/jquery.mobile-1.3.0.min.css" />
<script src="inc/jquery-1.8.2.min.js"></script>
<script src="inc/jquery.mobile-1.3.0.min.js"></script>
<script>
var updateInterval = 0;
// Add a leading Zero if number is < 10
function twoDigits (num) {
if (num < 10) {
return "0" + num;
} else {
return num;
}
}
// Format the data for the detail pages
// Parameters:
// * line: CSV-like Line with the data
// * num: Number of the detail page
function formatDetail (line, num) {
var value = line.split(";");
$("#t"+num+"min").html(Number(value[1]).toFixed(2) + " &deg;C");
//var tmin = Date(value[2] * 1000);
$("#t"+num+"mindat").html(value[2]);
$("#t"+num+"max").html(Number(value[3]).toFixed(2) + " &deg;C");
//var tmax = Date(value[4] * 1000);
$("#t"+num+"maxdat").html(value[4]);
$("#t"+num+"avg").html(Number(value[5]).toFixed(2) + " &deg;C");
}
// Load Temperature data using ajax and update the page
function loadData()
{
$.mobile.loading( "show" );
$.getJSON("/temp", function(data) {
$.each(data, function(i, item) {
$("#t" + item.id).html(Number(item.temp).toFixed(2) + " &deg;C");
$("#t" + item.id + "name").html(item.name);
$("#t" + item.id + "min").html(Number(item.min).toFixed(2) + " &deg;C")
$("#t" + item.id + "max").html(Number(item.max).toFixed(2) + " &deg;C")
$("#t" + item.id + "avg").html(Number(item.avg).toFixed(2) + " &deg;C")
var ts = new Date(item.tsmin);
var month = ts.getMonth() + 1;
$("#t" + item.id + "mindat").html(twoDigits(ts.getDate()) + "." + twoDigits(month) + "." + ts.getFullYear() + " " + twoDigits(ts.getHours()) + ":" + twoDigits(ts.getMinutes()));
ts = new Date(item.tsmax);
month = ts.getMonth() + 1;
$("#t" + item.id + "maxdat").html(twoDigits(ts.getDate()) + "." + twoDigits(month) + "." + twoDigits(ts.getFullYear()) + " " + twoDigits(ts.getHours()) + ":" + twoDigits(ts.getMinutes()));
});
$.mobile.loading( "hide" );
});
}
// Send command to the rfbb backend for power switches
// Parameters:
// * chan: Wireless channel of the switch (1-4)
// * id: Wireless ID of the switch (1-4)
// * cmd: Command to send (1 = on, 0 = off)
function pswitch(chan, id, cmd)
{
$.ajax({url: "rfbb?chan=" + chan + "&id=" + id + "&cmd=" + cmd});
}
/*$(document).bind("pagebeforeshow", function(event,pdata) {
var parsedUrl = $.mobile.path.parseUrl( location.href );
switch ( parsedUrl.hash ) {
case "#t1detail":
$("#t1img").attr("src", "tgraph?id=1&from=now-24h&to=now&step=1&w=800&h=240&title=Außen&r="+Math.random());
break;
case "#t2detail":
$("#t2img").attr("src", "tgraph?id=2&from=now-24h&to=now&step=1&w=800&h=240&title=Wohnzimmer&r="+Math.random());
break;
case "#t3detail":
$("#t3img").attr("src", "tgraph?id=3&from=now-24h&to=now&step=1&w=800&h=240&title=Schlafzimmer&r="+Math.random());
break;
case "#t4detail":
$("#t4img").attr("src", "tgraph?id=4&from=now-24h&to=now&step=1&w=800&h=240&title=Netzwerkschrank&r="+Math.random());
break;
}
});*/
$(document).ready(function() {loadData(); updateInterval = setInterval("loadData()", 600000);})
</script>
</head>
<body>
<!-- Main Page -->
<div data-role="page" id="main">
<header data-role="header">
<h1>RasPiControl</h1>
</header>
<article data-role="content">
<ul data-role="listview">
<li data-icon="info">
<a href="#temp">
<h1>Temperaturen</h1>
</a>
</li>
<li data-icon="info">
<a href="#switch">
<h1>Schalter</h1>
</a>
</li>
</ul>
</article>
<footer data-role="footer" data-position="fixed">
<p style="padding-left: 15px; font-size:0.7em;"><span></span></p>
</footer>
</div>
<!-- Temperature Overview -->
<div data-role="page" id="temp">
<header data-role="header">
<a href="#main" data-icon="bars" data-iconpos="notext">Übersicht</a>
<h1>RasPiControl: Temperaturen</h1>
<a href="javascript:loadData()" data-icon="refresh" data-iconpos="notext" data-transition="slidedown">Neu laden</a>
</header>
<article data-role="content">
<ul data-role="listview">
<li data-icon="info">
<a href="#t1detail">
<h1 id="t1name">Keine Daten</h1>
<p id="t1">Keine Daten</p>
</a>
</li>
<li data-icon="info">
<a href="#t2detail">
<h1 id="t2name">Keine Daten</h1>
<p id="t2">Keine Daten</p>
</a>
</li>
<li data-icon="info">
<a href="#t3detail">
<h1 id="t3name">Keine Daten</h1>
<p id="t3">Keine Daten</p>
</a>
</li>
<li data-icon="info">
<a href="#t4detail">
<h1 id="t4name">Keine Daten</h1>
<p id="t4">Keine Daten</p>
</a>
</li>
</ul>
</article>
<footer data-role="footer" data-position="fixed">
<p style="padding-left: 15px; font-size:0.7em;"><span>Daten vom </span><span id="timestamp"></span></p>
</footer>
</div>
<!-- Temperature 1 Details -->
<div data-role="page" id="t1detail" data-cache="false">
<header data-role="header">
<h1>RasPiControl: Au&szlig;en</h1>
<a href="#temp" data-icon="grid" data-iconpos="notext">&Uuml;bersicht</a>
</header>
<article data-role="content">
<div class="ui-grid-b">
<div class="ui-block-a">
<div class="ui-bar ui-bar-c">
<h3>&dArr;</h3>
<p id="t1min">Keine Daten</p>
<p id="t1mindat" style="font-size:0.7em;">Keine Daten</p>
</div>
</div>
<div class="ui-block-b">
<div class="ui-bar ui-bar-c">
<h3>&uArr;</h3>
<p id="t1max">Keine Daten</p>
<p id="t1maxdat" style="font-size:0.7em;">Keine Daten</p>
</div>
</div>
<div class="ui-block-c">
<div class="ui-bar ui-bar-c">
<h3>&Oslash;</h3>
<p id="t1avg">Keine Daten</p>
<p style="font-size:0.7em;">Letzte 24h</p>
</div>
</div>
</div>
<!--div class="ui-grid-solo">
<div class="ui-grid-a" style="text-align: center;">
<img src="" id="t1img" style="width: 100%;"/>
</div>
</div-->
</article>
<footer data-role="footer" data-position="fixed">
<p style="padding-left: 15px; font-size:0.7em;">Basierend auf Daten der letzten 24h<p>
</footer>
</div>
<!-- Temperature 2 Details -->
<div data-role="page" id="t2detail">
<header data-role="header">
<h1>RasPiControl: Wohnzimmer</h1>
<a href="#temp" data-icon="grid" data-iconpos="notext">&Uuml;bersicht</a>
</header>
<article data-role="content">
<div class="ui-grid-b">
<div class="ui-block-a">
<div class="ui-bar ui-bar-c">
<h3>&dArr;</h3>
<p id="t2min">Keine Daten</p>
<p id="t2mindat" style="font-size:0.7em;">Keine Daten</p>
</div>
</div>
<div class="ui-block-b">
<div class="ui-bar ui-bar-c">
<h3>&uArr;</h3>
<p id="t2max">Keine Daten</p>
<p id="t2maxdat" style="font-size:0.7em;">Keine Daten</p>
</div>
</div>
<div class="ui-block-c">
<div class="ui-bar ui-bar-c">
<h3>&Oslash;</h3>
<p id="t2avg">Keine Daten</p>
<p style="font-size:0.7em;">Letzte 24h</p>
</div>
</div>
</div>
<!--div class="ui-grid-solo" style="text-align: center;">
<div class="ui-grid-a">
<img src="" id="t2img" style="width: 100%;"/>
</div>
</div-->
</article>
<footer data-role="footer" data-position="fixed">
<p style="padding-left: 15px; font-size:0.7em;">Basierend auf Daten der letzten 24h<p>
</footer>
</div>
<!-- Temperature 3 Details -->
<div data-role="page" id="t3detail">
<header data-role="header">
<h1>RasPiControl: Schlafzimmer</h1>
<a href="#temp" data-icon="grid" data-iconpos="notext">&Uuml;bersicht</a>
</header>
<article data-role="content">
<div class="ui-grid-b">
<div class="ui-block-a">
<div class="ui-bar ui-bar-c">
<h3>&dArr;</h3>
<p id="t3min">Keine Daten</p>
<p id="t3mindat" style="font-size:0.7em;">Keine Daten</p>
</div>
</div>
<div class="ui-block-b">
<div class="ui-bar ui-bar-c">
<h3>&uArr;</h3>
<p id="t3max">Keine Daten</p>
<p id="t3maxdat" style="font-size:0.7em;">Keine Daten</p>
</div>
</div>
<div class="ui-block-c">
<div class="ui-bar ui-bar-c">
<h3>&Oslash;</h3>
<p id="t3avg">Keine Daten</p>
<p style="font-size:0.7em;">Letzte 24h</p>
</div>
</div>
</div>
<!--div class="ui-grid-solo" style="text-align: center;">
<div class="ui-grid-a">
<img src="" id="t3img" style="width: 100%;"/>
</div>
</div-->
</article>
<footer data-role="footer" data-position="fixed">
<p style="padding-left: 15px; font-size:0.7em;">Basierend auf Daten der letzten 24h<p>
</footer>
</div>
<!-- Temperature 4 Details -->
<div data-role="page" id="t4detail">
<header data-role="header">
<h1>RasPiControl: Netzwerkschrank</h1>
<a href="#temp" data-icon="grid" data-iconpos="notext">&Uuml;bersicht</a>
</header>
<article data-role="content">
<div class="ui-grid-b">
<div class="ui-block-a">
<div class="ui-bar ui-bar-c">
<h3>&dArr;</h3>
<p id="t4min">Keine Daten</p>
<p id="t4mindat" style="font-size:0.7em;">Keine Daten</p>
</div>
</div>
<div class="ui-block-b">
<div class="ui-bar ui-bar-c">
<h3>&uArr;</h3>
<p id="t4max">Keine Daten</p>
<p id="t4maxdat" style="font-size:0.7em;">Keine Daten</p>
</div>
</div>
<div class="ui-block-c">
<div class="ui-bar ui-bar-c">
<h3>&Oslash;</h3>
<p id="t4avg">Keine Daten</p>
<p style="font-size:0.7em;">Letzte 24h</p>
</div>
</div>
</div>
<!--div class="ui-grid-solo" style="text-align: center;">
<div class="ui-grid-b">
<img src="" id="t4img" style="width: 100%;"/>
</div>
</div-->
</article>
<footer data-role="footer" data-position="fixed">
<p style="padding-left: 15px; font-size:0.7em;">Basierend auf Daten der letzten 24h<p>
</footer>
</div>
<!-- Wireless Switches -->
<div data-role="page" id="switch">
<header data-role="header">
<a href="#main" data-icon="bars" data-iconpos="notext">Übersicht</a>
<h1>RasPiControl: Schalter</h1>
</header>
<article data-role="content">
<ul data-role="listview">
<li data-icon="info">
<h1>Monitor & Co.</h1>
<div data-role="controlgroup" data-type="horizontal">
<a href="#" onclick="pswitch(1, 2, 1)" data-role="button">An</a>
<a href="#" onclick="pswitch(1, 2, 0)" data-role="button">Aus</a>
</div>
</li>
<li data-icon="info">
<h1>Drucker</h1>
<div data-role="controlgroup" data-type="horizontal">
<a href="#" onclick="pswitch(1, 1, 1)" data-role="button">An</a>
<a href="#" onclick="pswitch(1, 1, 0)" data-role="button">Aus</a>
</div>
</li>
<li data-icon="info">
<h1>Rechner</h1>
<div data-role="controlgroup" data-type="horizontal">
<a href="#" onclick="pswitch(1, 4, 1)" data-role="button">An</a>
<a href="#" onclick="pswitch(1, 4, 0)" data-role="button">Aus</a>
</div>
</li>
</ul>
</article>
<footer data-role="footer" data-position="fixed">
<p style="padding-left: 15px; font-size:0.7em;">Controlled by a Raspberry Pi</p>
</footer>
</div>
</body>

View file

@ -4,7 +4,9 @@
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.x",
"sqlite3": "3.x"
"express": "4.x",
"sqlite3": "3.x",
"body-parser": "1.x",
"multer": "0.x"
}
}

33
routes/index.js Normal file
View file

@ -0,0 +1,33 @@
var fs = require("fs");
exports.index = function(req, res) {
var id = fs.readFile("./html/index.html", function(err, data) {
if (err == null) {
res.type("text/html");
res.status(200).send(data);
} else {
res.type("text/html");
res.status(404).send("<html><head><title>404</title><body><h1>404</h1><p>Page not found</p></body></html>");
}
});
}
exports.inc = function(req, res) {
var options = { root: global.rootpath + "/html/inc/" };
res.sendFile(req.params.file, options, function(err) {
if (err) {
console.log(err);
res.status(err.status).end();
}
});
}
exports.img = function(req, res) {
var options = { root: global.rootpath + "/html/inc/images" };
res.sendFile(req.params.file, options, function(err) {
if (err) {
console.log(err);
res.status(err.status).end();
}
});
}

View file

@ -1,18 +1,131 @@
//
// tempdb.js - SQLite Backend for storing and retrieving temperatures
//
var sqlite3 = require("sqlite3");
var sqlite3 = require("sqlite3").verbose();
var file = "./temperatures.db"
exports.getAll = function(req, res) {
res.send([{id: '1', name: 'Wohnzimmer', temp: 20.1}, {id: '2', name: "Schlafzimmer", temp: 18.4}]);
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;
}
});
});
}
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);
}
}
exports.getTemp = function(req, res) {
res.send({id: req.params.id, temp: 20.1});
var db = new sqlite3.Database(file);
db.serialize(function() {
db.get("SELECT name, lasttemp, lastupd FROM sensors WHERE id = ?;", req.params.id, function(err, row) {
if (row !== undefined) {
res.type("application/json");
res.status(200).send({"name": row.name, "temp": row.lasttemp, "ts": row.lastupd});
db.close();
} else {
db.close();
res.status(400).send({"errid": 3, "errtxt": "Sensor does not exist"});
}
});
});
}
exports.addTemp = function(req, res) {
console.log("Adding Temp: " + req.body);
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) {
console.log("Updating Temp: " + req.params.id + " Data: " + req.body);
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"});
}
});
});
}

View file

@ -2,19 +2,25 @@
// Load the modules
var express = require("express");
var bodyParser = require('body-parser');
var tempdb = require("./routes/tempdb.js");
var index = require("./routes/index.js");
global.rootpath = __dirname;
var srv = express();
srv.configure(function() {
srv.use(express.logger('dev'));
srv.use(express.bodyParser());
});
srv.use(bodyParser());
srv.get("/temp", tempdb.getAll);
srv.get("/temp/:id", tempdb.getTemp);
srv.post("/temp", tempdb.addTemp);
srv.put("/temp/:id", tempdb.updTemp);
srv.get("/", index.index);
srv.get("/inc/:file", index.inc);
srv.get("/inc/images/:file", index.img);
srv.listen(3000);
console.log("Server started on port 3000.");