Skip to content

Commit

Permalink
Main Job reworked
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfisto committed Sep 21, 2014
1 parent a62accb commit 9e7b0df
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules
/db
Binary file removed db/reefmonitor.db
Binary file not shown.
49 changes: 49 additions & 0 deletions lib/OneWire.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* 1 Wire reading
*/

global.__base = __dirname+'/../';

// system imports
var fs = require('fs');

// custom imports


module.exports = function() {
function OneWire() {
};

OneWire.prototype.read = function(probe){
fs.readFile('/sys/bus/w1/devices/'+probe.systemId+'/w1_slave', function(err, data){
if (err) {
throw err;
}
_internal.processData(probe, data);
});
};

return OneWire;
};


var _internal = {
processData: function(probe, data){
var array = data.toString().split("\n");
for (i in array) {
console.log(array[i]);
}
if(array[0].indexOf("YES") > -1) {
var tempStartIndex = array[1].indexOf("t=")+2;
var tempStr = array[1].substring(tempStartIndex);
var tempNum = parseFloat(tempStr)/1000;
console.log("Temp: "+tempNum);
dao.saveMeasurement(probe, tempNum, function(){
if (util.debug()){
dao.dumpTables();
}
dao.finalize();
});
}
}
};
5 changes: 3 additions & 2 deletions lib/dao/ProbeDao.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ module.exports = function() {
});
};

ProbeDao.prototype.readProbes = function(callback) {
ProbeDao.prototype.readProbes = function(withLastValue, callback) {
var probes = [];
db.serialize(function() {
db.each(_internal.SQL_BASE_WITH_LAST_VALUE, function(err, row) {
var sql = withLastValue ? _internal.SQL_BASE_WITH_LAST_VALUE : _internal.SQL_BASE;
db.each(sql, function(err, row) {
if (err) {
throw err;
}
Expand Down
6 changes: 6 additions & 0 deletions lib/model/Probe.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@ module.exports = function(){
this.lastReplacementDate = null;
};

Probe.prototype.CONNECTION_TYPES = {
ONE_WIRE: '1Wire',
ATLAS: 'Atlas',
I2C: 'I2C'
};

return Probe;
};
File renamed without changes.
2 changes: 1 addition & 1 deletion routes/probes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var probeDao = new ProbeDao();
var Probe = require(__base+"lib/model/Probe")();

exports.list = function(req, res){
probeDao.readProbes(function(probes){
probeDao.readProbes(true, function(probes){
res.render('probes', { "title": 'Reef Monitor', "probes": probes});
// probeDao.finalize(); TODO
});
Expand Down
3 changes: 2 additions & 1 deletion scripts/init_db.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/**
* Init DB script
*/
global.__base = __dirname+'/../';

var Dao = require("./lib/dao/GenericDao")();
var Dao = require(__base+"lib/dao/GenericDao")();

var dao = new Dao();

Expand Down
23 changes: 22 additions & 1 deletion scripts/main_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,34 @@ global.__base = __dirname+'/../';
var ParamsDao = require(__base+"lib/dao/ParamsDao")();
var paramsDao = new ParamsDao();

var ProbeDao = require(__base+"lib/dao/ProbeDao")();
var probeDao = new ProbeDao();

var OneWire = require(__base+"lib/OneWire")();
var oneWire = new OneWire();

var Atlas = require(__base+"lib/Atlas")();
var atlas = new Atlas();


var mainJob = function(tick){
console.log('Tick: '+tick);

// read measurements of all active probes (every 10 minutes)
if (tick % 10 == 0){
console.log("Fire!");
probeDao.readProbes(false, function(probes){
for (probe in probes){
switch (probe.connectionType){
case probe.CONNECTION_TYPES.ONE_WIRE:
oneWire.read(probe);
break;
case probe.CONNECTION_TYPES.ATLAS:
atlas.read(probe);
break;
}

}
});
}

// generate alerts
Expand Down
54 changes: 0 additions & 54 deletions scripts/readTemp.js

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dao.readProbe('PH1', function(probe){
});
});

dao.readProbes(function(probes){
dao.readProbes(true, function(probes){
console.log(probes[0].code);
});

Expand Down

0 comments on commit 9e7b0df

Please sign in to comment.