Skip to content

Commit

Permalink
humidity: Support htu21d controller
Browse files Browse the repository at this point in the history
Test using:

   node lib/humidity '{ "controller": "htu21d" }'

Relate-to: #13
Change-Id: I8f4dfa53b478a42019f445e46da9f9de93768c4a
Signed-off-by: Philippe Coval <[email protected]>
  • Loading branch information
rzr committed Jan 17, 2020
1 parent cfbd4bf commit 04c4d00
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
29 changes: 25 additions & 4 deletions lib/humidity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@
'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
* @related: https://www.w3.org/TR/ambient-light/
**/
function Humidity(options) {
var self = this;
this.state = 'idle';
this.type = 'humidity';
this.properties = ['level'];
for (var property in self.properties) {
Expand All @@ -37,6 +45,7 @@ function Humidity(options) {
}
this.options = options || {};
this.options.frequency = this.options.frequency || 1;
this.options.controller = this.options.controller || 'simulator';

return this;
}
Expand All @@ -45,7 +54,7 @@ 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);
}
Expand Down Expand Up @@ -75,7 +84,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)
Expand All @@ -101,7 +119,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();
Expand Down
4 changes: 2 additions & 2 deletions lib/humidity/simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function Simulator () {
};
}

Simulator.prototype.read = function (callback) {
Simulator.prototype.readHumidity = function (callback) {
this.value.level = Math.random();
if (callback) return callback(null, this.value);
};
Expand All @@ -33,7 +33,7 @@ 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;
Expand Down

0 comments on commit 04c4d00

Please sign in to comment.