Permalink
Browse files

Added incremental and history updates, documentation, and xml parsing…

… bug fix
  • Loading branch information...
1 parent 4457921 commit 6f4458d5868c52fa1c01eca1a69ad107990350cd @robrighter committed Aug 13, 2010
Showing with 76 additions and 15 deletions.
  1. +58 −2 README.md
  2. +7 −5 currentcost.js
  3. +8 −1 example.js
  4. +1 −1 lib/xml2json.js
  5. +2 −6 python/tailserial.py
View
@@ -1,2 +1,58 @@
-node-currentcost provides an easy way to pull data from current cost home energy devices. For more information about CurrentCost devices go to:
-http://www.currentcost.com
+Currentcost Driver for Node.js
+==============================
+
+node-currentcost provides an easy way to pull data from current cost home energy devices. For more information about CurrentCost devices go to: http://www.currentcost.com
+
+This driver currently depends on a python serial library in order to provide compatibility across multiple flavors of Unix (Linux, OSX, BSD)
+
+API
+===
+
+The api is very simple. It consists of a single EventEmitter that can notify of 2 types of events:
+
+Incremental Events ('incremental')
+=================================
+These notifications come roughly once every 3 seconds. The data for incremental updates looks like this:
+
+ { src: 'CC128-v0.15'
+ , dsb: 2
+ , time: '10:02:03'
+ , tmprf: 77.2
+ , sensor: 0
+ , id: 1123
+ , type: 1
+ , ch1: { watts: 447 }
+ , ch2: { watts: 384 }
+ }
+
+
+History Events ('history')
+=================================
+These notifications come 1 minute past every odd hour that contains details of historical energy usage. The data for incremental updates looks like this:
+
+ { src: 'CC128-v0.15'
+ , dsb: 2
+ , time: '10:02:03'
+ , hist:
+ { dsw: 26
+ , type: 1
+ , units: 'kwhr'
+ , data: [ { sensor: 0
+ , h024: '01.1'
+ , h022: '000.9'
+ , h020: '000.3'
+ , h018: '000.4'
+ }
+ , { sensor: 1
+ , units: 'kwhr'
+ , h024: '000.0'
+ , h022: '000.0'
+ , h020: '000.0'
+ , h018: '000.0'
+ }
+ ]
+ }
+ }
+
+For more information about the Currentcost data schema see the products data specification (http://www.currentcost.com/cc128/xml.htm). All information in this driver is provided in JSON format, but the Currentcost data spec refers to the schema in its xml form:
+http://www.currentcost.com/cc128/xml.htm
View
@@ -1,15 +1,17 @@
(function(){
- events = require('events');
- var spawn = require("child_process").spawn;
- var xml = require('./lib/xml2json');
- var device = '';
+ 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) {
- that.emit('data', formatData(xml.parse(new String(data), "", false)));
+ var tosend = formatData(xml.parse(new String(data), "", false));
+ if(tosend)
+ that.emit(( tosend.hasOwnProperty('hist')? 'history' : 'incremental' ), tosend);
});
dataprocess.stderr.on('data', function (data) {
that.emit('error', data+"");
View
@@ -3,10 +3,17 @@ sys = require('sys');
var tryit = new CurrentCost('/dev/tty.usbserial');
-tryit.on('data', function(data){
+tryit.on('incremental', function(data){
+ console.log('Incremental Update:')
console.log(sys.inspect(data));
});
+tryit.on('history', function(data){
+ console.log('History Update:')
+ console.log(sys.inspect(data));
+ data.hist['data'].forEach(function(item){console.log(sys.inspect(item));});
+});
+
tryit.on('error', function(data){
console.log('ERROR: ' + data);
});
View
@@ -75,7 +75,7 @@ Software Foundation. It is distributed without any warranty.
if (already && !eval(objname+".sort")){preeval+=objname+"=["+objname+"];\n"};
var before="=";after="";
if (already){before=".push(";after=")"};
- var toeval=preeval+objname+before+rest+after;
+ var toeval=preeval+objname+before+(((rest+"").indexOf('.')!=-1 ) ? '"'+rest+'"' : rest)+after;
eval(toeval);
if(eval(objname+".sort")){objname+="["+eval(objname+".length-1")+"]"};
oldniva=niva
View
@@ -4,14 +4,10 @@
try:
if len(sys.argv) == 2:
- # ser = serial.Serial(sys.argv[1], 57600)
- hour=1
+ ser = serial.Serial(sys.argv[1], 57600)
while 1:
- hour=hour+1
- print("<msg><src>CC128-v0.15</src><dsb>00001</dsb><time>"+str(hour)+":06:52</time><tmprF>79.3</tmprF><sensor>0</sensor><id>02143</id><type>1</type><ch1><watts>03275</watts></ch1><ch2><watts>00349</watts></ch2></msg>\n"),
- #print( ser.readline() ),
+ print( ser.readline() ),
sys.stdout.flush()
- time.sleep( 1 )
else:
sys.stderr.write('No Device Provided')
except:

0 comments on commit 6f4458d

Please sign in to comment.