Skip to content

Commit

Permalink
geolocation: Astract the Simulator controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinnl committed Mar 6, 2019
1 parent 105290f commit 35c2dd3
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions lib/geolocation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,40 @@

'use strict';

var Sensor = require('./simulator');

/**
* Class inspired by W3C's generic-sensor
* @related: https://www.w3.org/TR/ambient-light/
**/
function Geolocation(options) {
throw('TODO: unimplemented');
this.type = 'geolocation';
this.latitude = 0;
this.longitude = 0;
this.onerror = function (err) {
throw new Error(err)
}
this.options = options || {};
this.options.frequency = this.options.frequency || 1;
return this;
}

Geolocation.prototype.update = function update() {
var self = this;
try {
self.hasReading = false;
throw('TODO: unimplemented');
self.sensor.read(function(err, data) {
if (err || data === null || typeof data === 'undefined') {
return self.onerror(data)
}
self.timestamp = new Date();
self.latitude = data.latitude;
self.longitude = data.longitude;
self.hasReading = true;
if (self.onreading) {
self.onreading();
}
});
} catch (err) {
self.onerror(err);
}
Expand All @@ -47,7 +67,7 @@ Geolocation.prototype.start = function start() {
this.state = 'activating';
if (!this.sensor) {
try {
throw('TODO: unimplemented');
this.sensor = new Sensor();
} catch (err) {
if (this.onerror) {
return this.onerror(err)
Expand All @@ -59,7 +79,9 @@ Geolocation.prototype.start = function start() {
if (!self.interval) {
self.interval = setInterval(function() { self.update(); },
1000. / self.options.frequency);
self.onactivate();
if (self.onactivate) {
self.onactivate();
}
self.state = 'activated';
}
} catch(err) {
Expand All @@ -73,7 +95,7 @@ module.exports = Geolocation;
if (module.parent === null) {
var sensor = new Geolocation();
sensor.onreading = function() {
console.log('log: ' + this.hasReading)
console.log('log: lat=' + this.latitude + ', long=' + this.longitude + ' - ' + this.timestamp);
this.stop();
}
sensor.start();
Expand Down

0 comments on commit 35c2dd3

Please sign in to comment.