Skip to content

Commit

Permalink
temperature: Fixes #5 add Temperature simulator
Browse files Browse the repository at this point in the history
Change-Id: I7109a2a45493477f5dba45fe3538a76ff73bdf4e
  • Loading branch information
lhirlimann authored and rzr committed Mar 25, 2019
1 parent f8316b3 commit 630cdaf
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/temperature/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
'use strict';

var console = require('console');
var BMP085 = require('bmp085-sensor');
try {
var BMP085 = require('bmp085-sensor');
} catch (err){
// console.log(err);
}

var Simulator = require('./simulator.js');

/**
* Class inspired by W3C's generic-sensor
Expand All @@ -41,7 +47,7 @@ function TemperatureSensor(options) {

this.options = options || {};
this.options.frequency = this.options.frequency || 1;
this.options.controller = this.options.controller || 'bmp085';
this.options.controller = this.options.controller || 'simulator';

return this;
}
Expand Down Expand Up @@ -78,9 +84,12 @@ TemperatureSensor.prototype.stop = function stop() {
TemperatureSensor.prototype.start = function start() {
var self = this;
this.state = 'activating';
var BMP085 = null;
if (!this.sensor) {
try {
if ((this.options.controller === 'bmp085') || (this.options.controller === 'bmp180')) {
if (this.options.controller === 'simulator') {
this.sensor = new Simulator();
} else if ((this.options.controller === 'bmp085') || (this.options.controller === 'bmp180')) {
this.sensor = new BMP085(this.options.sensor);
} else {
throw new Error("TODO: unsupported controller:" + this.options.controller);
Expand Down
52 changes: 52 additions & 0 deletions lib/temperature/simulator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* -*- mode: js; js-indent-level:2; -
* SPDX-License-Identifier: Apache-2.0
* Copyright 2019-present Samsung Electronics France and other contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

var console = require('console');

function Simulator () {
this.value = {
temperature: 0,
};
}

Simulator.prototype.start = function (callback) {
return callback(null);
}

Simulator.prototype.calibrate = function (callback) {
return callback(null);
}

Simulator.prototype.read = function (callback) {
this.value.temperature = (Math.random() * 100.);
if (callback) return callback(null, this.value);
};

module.exports = Simulator;

if (module.parent === null) {
var sensor = new Simulator();
sensor.read(function (err, value) {
if (err) {
console.error('error: ' + err);
throw err;
} else {
console.log('log: value=' + JSON.stringify(value));
}
});
}

0 comments on commit 630cdaf

Please sign in to comment.