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 3eb53b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
42 changes: 28 additions & 14 deletions lib/humidity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,44 @@
'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) {
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;
}

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();
Expand All @@ -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)
Expand All @@ -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();
Expand Down
12 changes: 5 additions & 7 deletions lib/humidity/simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3eb53b5

Please sign in to comment.