@acaprojects/a2-composer
Version:
Angular 2 Interface for composer
174 lines • 6.62 kB
JavaScript
import { Injectable } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { System } from './classes';
import { DataStoreService } from '../data-store.service';
import { ComposerDebugService } from '../debug.service';
import { Resources } from '../resources';
import { $WebSocket } from '../websocket';
import { $WebSocketMock } from '../websocket.mock';
import { COMPOSER } from '../../settings';
var SystemsService = (function () {
function SystemsService(r, route, store, debug) {
var _this = this;
this.r = r;
this.route = route;
this.store = store;
this.debug = debug;
this.is_setup = false;
this.systems = [];
this.bound_systems = [];
this.connected = false;
this.request_id = 0;
this.mock = false;
this.fixed_device = false;
this.sub = null;
this.system_promises = {};
this.system_exists = {};
this.debug.services.systems = this;
store.local.getItem("fixed_device").then(function (value) {
_this.fixed_device = (value === 'true');
});
this.sub = this.route.queryParams.subscribe(function (params) {
_this.fixed_device = params.fixed_device === 'true' ? params.fixed_device === 'true' : _this.fixed_device;
store.local.setItem('fixed_device', _this.fixed_device ? 'true' : 'false');
});
var auth = null;
if (r) {
auth = r;
}
setInterval(function () {
_this.updateSystems();
}, 60 * 1000);
}
Object.defineProperty(SystemsService.prototype, "resources", {
get: function () {
return this.r;
},
enumerable: true,
configurable: true
});
SystemsService.prototype.setup = function (options) {
COMPOSER.loadSettings();
this.mock = options.mock ? true : false;
this.is_setup = true;
var o = options;
if (this.r) {
this.r.setup(options);
}
var host = o.host ? o.host : location.hostname;
var port = o.port ? o.port : location.port;
var prot = o.protocol ? o.protocol : location.protocol;
if (options.mock) {
COMPOSER.log('Systems', 'Setting up mock websocket.');
if (this.io) {
delete this.io;
}
this.io = new $WebSocketMock(this, this.r, this.fixed_device);
this.io.setup(this.r, host, port, prot);
return this.r.init(options.api_endpoint, true).then(function () { return true; }, function (err) { return false; });
}
else {
COMPOSER.log('Systems', 'Setting up websocket.');
if (!this.io) {
this.io = new $WebSocket(this, this.r, this.fixed_device);
}
this.io.setup(this.r, host, port, prot);
return this.r.init(o.api_endpoint).then(function () { return true; }, function (err) { return false; });
}
};
SystemsService.prototype.get = function (sys_id) {
var _this = this;
var system = this.r.get('System');
if (!this.mock) {
if (!this.system_promises[sys_id] && system) {
this.system_promises[sys_id] = new Promise(function (resolve) {
if (_this.system_exists[sys_id]) {
var s = _this.getSystem(sys_id);
s.exists = true;
_this.system_promises[sys_id] = null;
resolve();
}
else {
system.get({ id: sys_id }).then(function (check_sys) {
_this.system_exists[sys_id] = true;
var s = _this.getSystem(sys_id);
s.exists = true;
_this.system_promises[sys_id] = null;
resolve();
}, function (err) {
_this.system_exists[sys_id] = false;
var sys = _this.getSystem(sys_id);
sys.exists = false;
_this.system_promises[sys_id] = null;
resolve();
});
}
});
}
}
return this.getSystem(sys_id);
};
SystemsService.prototype.getModule = function (sys_id, id, i) {
if (i === void 0) { i = 1; }
var system = this.get(sys_id);
var module = system.get(id, i);
return module;
};
SystemsService.prototype.isConnected = function () {
return this.io ? this.io.connected : false;
};
SystemsService.prototype.rebind = function () {
for (var i = 0; this.systems && i < this.systems.length; i++) {
this.systems[i].rebind();
}
};
SystemsService.prototype.getSystem = function (sys_id) {
var system = null;
for (var i = 0; this.systems && i < this.systems.length; i++) {
if (this.systems[i].id === sys_id) {
system = this.systems[i];
}
}
if (system === null) {
system = new System(this, sys_id);
this.systems.push(system);
}
return system;
};
SystemsService.prototype.updateSystems = function () {
if (this.r && this.io) {
var _loop_1 = function (i) {
var system = this_1.systems[i];
if (!system.exists) {
var sys = this_1.r.get('System');
if (sys) {
var mod = sys.get({ id: system.id });
if (mod) {
mod.then(function (check_sys) {
system.exists = true;
}, function (err) {
return false;
});
}
}
}
};
var this_1 = this;
for (var i = 0; this.systems && i < this.systems.length; i++) {
_loop_1(i);
}
}
};
return SystemsService;
}());
export { SystemsService };
SystemsService.decorators = [
{ type: Injectable },
];
SystemsService.ctorParameters = function () { return [
{ type: Resources, },
{ type: ActivatedRoute, },
{ type: DataStoreService, },
{ type: ComposerDebugService, },
]; };
//# sourceMappingURL=systems.service.js.map