Преглед на файлове

Add simple logging of data to csv.

master
flabbergast преди 8 години
родител
ревизия
fb2003f9cc
  1. 1
      .gitignore
  2. 4
      modules/sensors/TM.js
  3. 26
      modules/simple_log.js
  4. 3
      package.json

1
.gitignore

@ -3,3 +3,4 @@ node_modules
*.sublime-*
public/css/style.css
serial_incoming*.log
data_log/*

4
modules/sensors/TM.js

@ -21,6 +21,8 @@ var TMtempTime = "no reading yet";
var TMbatt = "??";
var TMbattTime = "no reading yet";
var logger = require('../simple_log.js');
exports.init = function(socket){
};
@ -35,10 +37,12 @@ exports.onDataOverSerial = function(sockets,message){
TMtemp = message.substring(4,9);
sockets.emit('received-TM-temp',
{ content: ("Temp: "+TMtemp+" ºC")});
logger.log('TM-temp',TMtemp);
}
if(message.substring(0,4) == "BATT") {
TMbatt = message.substring(4,9);
sockets.emit('received-TM-batt',
{ content: ("Batt: "+TMbatt+" V")});
logger.log('TM-batt',TMbatt);
}
};

26
modules/simple_log.js

@ -0,0 +1,26 @@
/*
* - implement logging
* - well, I got tired of fighting with streams, so here goes a trivial data logger
* NOTES:
* - logging should be done to a transport, which then takes care to chunk to
* files by itself
* - reading on streams:
* - https://github.com/substack/stream-handbook
* - http://codewinds.com/blog/2013-08-19-nodejs-writable-streams.html
*/
// this is relative to process.cwd();
var logDir = './data_log/';
var fs = require('fs');
var strftime = require('strftime');
exports.log = function(sensor, data){
fs.appendFile(logDir+sensor+strftime('-%Y-%m.csv'),
strftime('%Y,%m,%d,%H,%M,%S,')+data+'\n',
function (err) {
if (err)
console.log(strftime('%F %H:%M:%S: ')+sensor+': could not log to csv file: '+err);
});
};

3
package.json

@ -16,7 +16,8 @@
"stylus": "0.41.0",
"socket.io": "0.9.16",
"serialport": "1.2.5",
"winston": "0.7.2"
"winston": "0.7.2",
"strftime": "0.7.0"
},
"engines": {
"node": "0.10.22",

Зареждане…
Отказ
Запис