diff --git a/lib/humidity/index.js b/lib/humidity/index.js index 7100686..cee0781 100644 --- a/lib/humidity/index.js +++ b/lib/humidity/index.js @@ -18,7 +18,14 @@ 'use strict'; var console = require('console'); -var Sensor = require('./simulator'); + +var NTU21D = null; +try { + NTU21D = require('htu21d-i2c'); +} catch (err){ + console.log(err); +} +var Simulator = require('./simulator'); /** * Class inspired by W3C's generic-sensor @@ -26,18 +33,16 @@ var Sensor = require('./simulator'); **/ function Humidity(options) { var self = this; + this.state = 'idle'; this.type = 'humidity'; - this.properties = ['level']; - for (var property in self.properties) { - property = this.properties[property]; - self[property] = null; - } + this.level = 100; this.onerror = function (err) { throw new Error(err) } this.options = options || {}; this.options.frequency = this.options.frequency || 1; - + this.options.controller = this.options.controller || 'simulator'; + return this; } @@ -45,15 +50,12 @@ Humidity.prototype.update = function update() { var self = this; try { self.hasReading = false; - self.sensor.read(function(err, data) { + self.sensor.readHumidity(function(err, data) { if (err || data === null || typeof data === 'undefined') { return self.onerror(data); } self.timestamp = new Date(); - for (var property in self.properties) { - property = self.properties[property]; - self[property] = data[property] - } + self.level = data; self.hasReading = true; if (self.onreading) { self.onreading(); @@ -75,7 +77,16 @@ Humidity.prototype.start = function start() { this.state = 'activating'; if (!this.sensor) { try { - this.sensor = new Sensor(); + if (this.options.controller === 'simulator') { + this.sensor = new Simulator(); + } else if ((this.options.controller === 'htu21d') || + (this.options.controller === 'node-htu21d') || + (this.options.controller === 'htu21d-i2c') + ) { + this.sensor = new NTU21D(this.options.sensor); + } else { + throw new Error("TODO: unsupported controller:" + this.options.controller); + } } catch (err) { if (this.onerror) { return this.onerror(err) @@ -101,7 +112,10 @@ module.exports = Humidity; if (module.parent === null) { - var sensor = new Humidity(); + var config = process.argv[2] + ? JSON.parse(process.argv[2]) + : null; + var sensor = new Humidity(config); sensor.onreading = function() { console.log('log: level=' + this.level); this.stop(); diff --git a/lib/humidity/simulator.js b/lib/humidity/simulator.js index 2a6f878..761a200 100644 --- a/lib/humidity/simulator.js +++ b/lib/humidity/simulator.js @@ -19,21 +19,19 @@ var console = require('console'); function Simulator () { - this.value = { - level: 100. - }; + this.level = 100.; } -Simulator.prototype.read = function (callback) { - this.value.level = Math.random(); - if (callback) return callback(null, this.value); +Simulator.prototype.readHumidity = function (callback) { + this.level = Math.random(); + if (callback) return callback(null, this.level); }; module.exports = Simulator; if (module.parent === null) { var sensor = new Simulator(); - sensor.read(function (err, value) { + sensor.readHumidity(function (err, value) { if (err) { console.error('error: ' + err); throw err;