SilvrAPI/routes/temp.js

193 lines
4.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();
var EventEmitter = require('events');
var log4js = require('log4js');
var logger = log4js.getLogger();
2015-11-09 00:47:50 +00:00
var router = express.Router();
var EventEmitter = require('events');
var busEmitter = new EventEmitter();
2015-11-09 00:47:50 +00:00
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() {
logger.debug('Creating Temp db...');
logger.debug(file);
if (!fs.existsSync(file)) {
logger.debug('creating db file');
fs.openSync(file, 'w');
2015-11-09 11:11:14 +00:00
temp_db = new sqlite3.Database(file, createTable);
2015-11-09 13:52:03 +00:00
temp_db.close();
}
else {
logger.info('Database already created.');
connectDB();
}
2015-11-09 13:52:03 +00:00
2015-11-09 11:11:14 +00:00
}
2015-11-09 11:11:14 +00:00
function startEventHandler() {
"use strict";
2015-11-09 13:52:03 +00:00
2015-11-09 11:31:24 +00:00
}
function connectDB() {
"use strict";
logger.debug('Connect db.');
temp_db = new sqlite3.Database(file);
console.log(temp_db);
return temp_db;
2015-11-09 00:47:50 +00:00
}
function createTable() {
logger.debug('Creating temp table...');
temp_db.run('CREATE TABLE IF NOT EXISTS temp (date text, reading real);');
2015-11-09 00:47:50 +00:00
}
2015-11-09 08:10:52 +00:00
function closeDB() {
"use strict";
console.log('Closing db.');
temp_db.close();
2015-11-09 08:10:52 +00:00
}
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');
2015-11-09 08:10:52 +00:00
temp_db.run('INSERT into temp(date, reading) VALUES (?,?);', [time, reading]);
// temp_db.run('END');
temp_db.run('commit');
}
/*router.get('/', function (req, res, next) {
var now = new Date();
2015-11-09 00:47:50 +00:00
var t = req.query;
2015-11-09 00:47:50 +00:00
console.log('t:');
console.log(JSON.stringify(t));
if (req.query != {} && req.query.temp != null) {
// createDB();
2015-11-09 08:10:52 +00:00
2015-11-17 10:52:30 +00:00
res.writeHead(200, {"ContentType": "text/html"});
//res.send(JSON.stringify(t));
res.end('ok');
} else {
res.writeHead(200, {"ContentType": "text/html"});
//res.send(JSON.stringify(t));
res.end('error?');
}
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;
2016-01-17 22:44:21 +00:00
console.log('id: ' + id);
res.writeHead(200, {"ContentType": "text/html"});
//res.send(JSON.stringify(t));
res.end('ok');
});*/
2015-11-09 00:47:50 +00:00
router.get('/reading', function (req, res, next) {
var now = new Date();
2015-11-17 10:52:30 +00:00
var t = req.query;
2015-11-17 10:52:30 +00:00
console.log('t:');
console.log(JSON.stringify(t));
2015-11-09 08:10:52 +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
router.post('/', function (req, res, next) {
"use strict";
2015-11-19 10:19:00 +00:00
var t = req.body;
var now = new Date();
var dataJSON={};
console.log('~~~~');
logger.debug(t);
console.log(typeof t);
//t = JSON.parse(t);
if(t.hasOwnProperty('data')){
var data = t.data;
dataJSON = JSON.parse(data);
if(t.hasOwnProperty('temp')) {
}
else
{
logger.error('No temp data!');
}
}
else
{
logger.error('No data block!');
}
console.log(JSON.stringify(t));
if (dataJSON != {} && dataJSON.temp != null) {
busEmitter.emit("saveTempData", {time: now.toJSON(), value: parseFloat(dataJSON.temp)});
2015-11-19 10:19:00 +00:00
}
2015-11-17 10:56:32 +00:00
res.writeHead(200, {"ContentType": "text/html"});
2016-01-18 00:15:21 +00:00
res.end('ok');
2015-11-17 10:52:30 +00:00
});
2015-11-17 10:37:52 +00:00
router.get('/all', function (req, res, next) {
"use strict";
console.log('Selecint all..');
var db = connectDB();
2015-11-09 08:10:52 +00:00
db.all('SELECT * from temp', function (err, rows) {
2015-11-09 00:47:50 +00:00
var out = [];
console.log(err);
2015-11-09 07:47:43 +00:00
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});
2015-11-09 00:47:50 +00:00
});
res.writeHead(200, {"ContentType": "application/json"});
//res.send(JSON.stringify(t));
res.end(JSON.stringify(out));
2015-11-09 00:47:50 +00:00
//closeDB();
2015-11-09 00:47:50 +00:00
});
});
module.exports = router;
createDB();
var sendSocketHandler = (obj) => {
// logger.info('sendSocket: ' + JSON.stringify(obj));
insertTempReading(obj.time, obj.value);
}
busEmitter.on('saveTempData', sendSocketHandler);