dpaw-monorail-plugin
Version:
JS library to link the brocket network to the QGIS/monorail server
82 lines (65 loc) • 1.82 kB
JavaScript
/**
* @fileOverview
* @name monorail.js
* @author Gavin Coombes
* @license BSD-3-Clause
*
* Harvest the QGIS map state from the monorail server
*
*/
let axios = require('axios');
let events = require('events');
let u = require('dpaw-brocket-utility');
// Local Aliases
var log = u.log;
var HarvestModule = (function(){
let obj = Object.create(new events.EventEmitter());
obj.$type = "HarvestModule";
return obj;
})();
var HarvestObject = Object.create(HarvestModule);
HarvestObject.init = function init(opts){
let self = this;
self.opts = opts;
this.type = 'HarvestObject';
this._port = opts.port || 30000;
this.address = opts.address || buildAddress(this._port);
this.interval = opts.interval || 10 * 1000;
};
HarvestObject.port = function port(value) {
let self = this;
if (arguments.length) {
self._port = value;
self.address = buildAddress(self._port);
log('HarvestObject.port: port now ', self._port);
} else {
return self._value;
}
};
HarvestObject.fetch = function fetch(address) {
let self = this;
return axios.get(self.address)
.then(self.notify.bind(self));
};
HarvestObject.command = function command(cmd) {
let self = this;
let {tag, payload} = cmd;
let params = `?${tag}=${payload}`;
let address = self.address + params;
log('MonorailObject.command: Command is', cmd);
log('MonorailObject.command: Address is', address);
return axios.post(address)
.then(self.notify.bind(self));
};
HarvestObject.run = function run() {
this.fetch(this.address)
.then(setTimeout(this.run.bind(this), this.interval));
};
HarvestObject.notify = function notify(res) {
let data = res.data;
this.emit('data', data);
};
function buildAddress(port) {
return `http://localhost:${port}/api/extent`;
}
module.exports = HarvestObject;