Skip to content

Commit

Permalink
ambientlight: Add ambient light API simulator
Browse files Browse the repository at this point in the history
Add the simulator driver for ambientlight API.

- Write a simulator driver for ambientlight
- Use it as a default driver if no driver is specified

Issue-Id: rzr#5 Add simulators
Environment:
    Darwin 18.2.0
    Node 10.15.1
    Node 9.5.0

Signed-off-by: Anthony Maton <me@anthony-maton.me>
  • Loading branch information
MatonAnthony committed Mar 10, 2019
1 parent 153f027 commit 32c0ed8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
13 changes: 10 additions & 3 deletions lib/ambientlight/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
'use strict';

var console = require('console');
var BH1750 = require('bh1750');
try {
var BH1750 = require('bh1750');
} catch (err) {
// console.log(err);
}
var Simulator = require('./simulator');

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

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

return this;
}
Expand Down Expand Up @@ -77,7 +82,9 @@ AmbientLight.prototype.start = function start() {
this.state = 'activating';
if (!this.sensor) {
try {
if (this.options.controller === 'bh1750') {
if (this.options.controller === 'simulator') {
this.sensor = new Simulator();
} else if (this.options.controller === 'bh1750') {
this.sensor = new BH1750(this.options.sensor);
} else {
throw new Error("TODO: unsupported controller:" + this.options.controller);
Expand Down
42 changes: 42 additions & 0 deletions lib/ambientlight/simulator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* -*- 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 = 0;
}

Simulator.prototype.readLight = function (callback) {
this.value = (Math.random() * 200);
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));
}
});
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
"url": "git+https://github.com/rzr/generic-sensors-lite.git"
},
"dependencies": {
"bh1750": "^0.0.5",
"bmp085-sensor": "^0.0.5",
"color-sensor-js": "0.0.4"
},
"optionalDependencies": {
"bh1750": "^0.0.5",
"bmp085-sensor": "^0.0.5"
},
"keywords": [
"ambiant",
"ambiantlight",
Expand Down

0 comments on commit 32c0ed8

Please sign in to comment.