-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcurrentcost.js
30 lines (30 loc) · 948 Bytes
/
currentcost.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(function(){
var events = require('events'),
spawn = require("child_process").spawn,
xml = require('./lib/xml2json'),
device = '';
var CurrentCost = exports.CurrentCost = function(dev){device = dev};
CurrentCost.prototype = new events.EventEmitter();
CurrentCost.prototype.begin = function(){
var that = this;
var dataprocess = spawn("python", ["./python/tailserial.py", device]);
dataprocess.stdout.on('data', function (data) {
try{
var tosend = formatData(xml.parse(new String(data), "", false));
if(tosend)
that.emit(( tosend.hasOwnProperty('hist')? 'history' : 'incremental' ), tosend);
}
catch(err){
that.emit('error', data+"");
}
});
dataprocess.stderr.on('data', function (data) {
that.emit('error', data+"");
});
};
function formatData(data){
if(data.hasOwnProperty('msg')){
return data['msg'];
}
}
})();