SilvrAPI/routes/temp.js

166 lines
3.3 KiB
JavaScript
Raw Normal View History

2015-11-09 11:11:14 +00:00
var express = require('express'), fs = require('fs'), sqlite3 = require('sqlite3').verbose();
2015-11-09 00:47:50 +00:00
var router = express.Router();
var temp_db;
2015-11-09 13:52:03 +00:00
var file = process.env.DB_HOME + '/' + "temp.db";
2015-11-09 11:11:14 +00:00
var exists;
2015-11-09 00:47:50 +00:00
/* GET users listing. */
function createDB() {
console.log('Creating Temp db...');
2015-11-09 11:11:14 +00:00
if (!fs.existsSync(file)) {
console.log('creating db file');
fs.openSync(file,'w');
2015-11-09 13:52:03 +00:00
temp_db = new sqlite3.Database(file, createTable);
temp_db.close();
2015-11-09 11:11:14 +00:00
}
2015-11-09 13:52:03 +00:00
2015-11-09 11:31:24 +00:00
}
function connectDB() {
"use strict";
console.log('Connect db.');
temp_db = new sqlite3.Database(file);
2015-11-19 10:43:59 +00:00
console.log(temp_db);
2015-11-09 00:47:50 +00:00
}
function createTable() {
console.log('Creating temp table...');
temp_db.run('CREATE TABLE IF NOT EXISTS temp (date text, reading real);');
}
2015-11-09 08:10:52 +00:00
function closeDB() {
"use strict";
console.log('Closing db.');
temp_db.close();
}
2015-11-09 00:47:50 +00:00
function insertTempReading(time, reading) {
console.log('Inserting reading:' + reading);
// var sql = db.prepare('INSERT into temperature(date, reading) VALUES
// (?,?)',[time],[reading]);
temp_db.run('BEGIN TRANSACTION');
temp_db.run('INSERT into temp(date, reading) VALUES (?,?)',[time, reading]);
temp_db.run('END');
}
router.get('/', function(req, res, next) {
var now = new Date();
var t = req.query;
2015-11-17 10:52:30 +00:00
console.log('t:');
console.log(JSON.stringify(t));
2015-11-09 00:47:50 +00:00
if (req.query != {} && req.query.temp != null)
{
// createDB();
console.log(req.query);
2015-11-09 11:31:24 +00:00
connectDB();
2015-11-09 08:10:52 +00:00
2015-11-09 00:47:50 +00:00
insertTempReading(now.toJSON(),parseFloat(req.query.temp) );
2015-11-09 08:10:52 +00:00
closeDB();
2015-11-09 13:52:03 +00:00
res.writeHead(200, {"ContentType": "application/json"});
//res.send(JSON.stringify(t));
res.end(JSON.stringify(t));
} else
{
res.render('temp', { });
2015-11-09 00:47:50 +00:00
}
2015-11-09 08:10:52 +00:00
2015-11-17 10:52:30 +00:00
2016-01-17 22:44:21 +00:00
});
router.get('/:id', function(req, res, next) {
var id = req.params.id;
console.log('id: ' + id);
2015-11-17 10:37:52 +00:00
});
2015-11-09 00:47:50 +00:00
2015-11-17 10:52:30 +00:00
router.get('/reading', function(req, res, next) {
var now = new Date();
var t = req.query;
console.log('t:');
console.log(JSON.stringify(t));
2015-11-09 08:10:52 +00:00
2015-11-17 10:52:30 +00:00
res.writeHead(200, {"ContentType": "application/json"});
//res.send(JSON.stringify(t));
res.end(JSON.stringify({}));
2015-11-09 00:47:50 +00:00
});
2015-11-17 10:37:52 +00:00
2015-11-17 10:52:30 +00:00
router.post('/', function(req, res, next) {
"use strict";
console.log('POST');
2015-11-19 10:22:45 +00:00
var t = req.body;
2016-01-07 09:09:48 +00:00
var now = new Date();
console.log('t:');
2015-11-17 10:56:32 +00:00
console.log(JSON.stringify(t));
2015-11-19 10:19:00 +00:00
if (t != {} && t.temp != null)
{
// createDB();
2015-11-19 10:43:59 +00:00
console.log(t.temp);
console.log('A');
2015-11-19 10:19:00 +00:00
connectDB();
2015-11-19 10:43:59 +00:00
console.log('B');
2015-11-19 10:19:00 +00:00
insertTempReading(now.toJSON(),parseFloat(t.temp) );
2015-11-19 10:43:59 +00:00
console.log('C');
2015-11-19 10:19:00 +00:00
closeDB();
}
2015-11-17 10:56:32 +00:00
2015-11-17 10:54:39 +00:00
res.writeHead(200, {"ContentType": "application/json"});
//res.send(JSON.stringify(t));
res.end(JSON.stringify({}));
2015-11-17 10:52:30 +00:00
});
2015-11-17 10:37:52 +00:00
2015-11-09 00:47:50 +00:00
router.get('/all', function(req, res, next) {
"use strict";
2015-11-09 07:47:43 +00:00
console.log('Selecint all..');
2015-11-09 11:31:24 +00:00
connectDB();
2015-11-09 08:10:52 +00:00
2015-11-09 00:47:50 +00:00
temp_db.all('SELECT * from temp', function(err, rows) {
var out = [];
2015-11-09 07:47:43 +00:00
console.log(err);
console.log(rows);
2015-11-09 00:47:50 +00:00
rows.forEach(function (row) {
console.log(row.date + ', ' + row.reading);
// var t = ;
// out.push({"date": Date(row.date), "reading":row.reading});
out.push({"date": row.date, "reading":row.reading});
});
res.writeHead(200, {"ContentType": "application/json"});
//res.send(JSON.stringify(t));
res.end(JSON.stringify(out));
2015-11-09 08:10:52 +00:00
closeDB();
2015-11-09 00:47:50 +00:00
});
});
module.exports = router;
2016-01-07 09:09:48 +00:00
//createDB();