From 30198d1b1abb8ab7b4a216fce3389f6f48e4391b Mon Sep 17 00:00:00 2001 From: sergiu Date: Sun, 30 Apr 2023 14:26:44 +0300 Subject: [PATCH] LINXD-2396: Get Hub address from asset configuration; Take default main group if not specified in config; Update config to not declare hub address and voice server --- config/dev/default.toml | 7 -- index.js | 4 +- src/asset.js | 146 +++++++++++++++++++++++++++------------- src/mumble.js | 15 +++-- 4 files changed, 112 insertions(+), 60 deletions(-) diff --git a/config/dev/default.toml b/config/dev/default.toml index 5b87f1d..c8bf391 100644 --- a/config/dev/default.toml +++ b/config/dev/default.toml @@ -1,10 +1,3 @@ -[mumble] -port = '5668' -host = 'dev.linx.safemobile.com' - -[hub] -address = 'https://hub.dev.linx.safemobile.com/' - [elogs] name = 'dev' diff --git a/index.js b/index.js index 9c6406b..4a26c8f 100644 --- a/index.js +++ b/index.js @@ -6,8 +6,8 @@ const Simulator = require('./src/simulator'); // Read configs. let configs = { - mumble: config.get('mumble'), - hub: config.get('hub'), + // mumble: config.get('mumble'), + // hub: config.get('hub'), api: config.get('api'), assets: config.get('assets'), sounds: config.get('sounds'), diff --git a/src/asset.js b/src/asset.js index a889470..614f11e 100644 --- a/src/asset.js +++ b/src/asset.js @@ -21,9 +21,14 @@ class Asset { constructor(asset, configs, token) { this.id = asset.asset_id; + this.mumbleHost = null; + this.mumblePort = null; + this.murmurUser = null; + this.murmurPassword = null; + this.hubAddress = null; this.group_id = asset.group_id; - this.generate_voice = asset.generate_voice; - this.generate_gps = asset.generate_gps; + this.generate_voice = asset.generate_voice || false; + this.generate_gps = asset.generate_gps || false; this.token = token; this.configs = configs this._processConfigs(); @@ -36,7 +41,9 @@ class Asset { // Do async work: Init asset. async.waterfall([ + this._getConfiguration.bind(this), this._getDataFromApi.bind(this), + this._checkGroupToJoin.bind(this), this._connectToHub.bind(this), this._connectToMurmur.bind(this), this._register.bind(this), @@ -56,12 +63,70 @@ class Asset { }); } - _processConfigs() { + _processConfigs(callback) { let apiConfig = this.configs.api; this.apiEndpoint = apiConfig.use_secure ? 'https' : 'http'; this.apiEndpoint += '://' + apiConfig.host + (apiConfig.port ? (':' + apiConfig.port) : ''); } + _getConfiguration(callback) { + console.log('_getConfiguration', this.id); + Request.get( + this.apiEndpoint + '/asset/' + this.id + '/account', + { + headers: { + Authorization: `Bearer ${this.token}` + } + }, + (error, response, body) => { + if (!error && (response.statusCode === 200 || response.statusCode === 201)) { + const bodyObj = JSON.parse(body); + const data = bodyObj.data; + // console.log('_getConfiguration', data); + this.mumbleHost = data.configuration.mumble_address; + this.mumblePort = data.configuration.voice_rtp_port; + this.murmurUser = bodyObj.data.configuration.mumble_username; + this.murmurPassword = bodyObj.data.configuration.mumble_password; + this.hubAddress = data.configuration.hub_address; + return callback(); + } else { + utils.writeLog(`ERROR getting informations about asset ${this.id}`, error); + utils.writeErrorLog(`ERROR_API`); + return callback(error); + } + } + ); + } + + _checkGroupToJoin(callback) { + console.log('_getConfiguration', this.id); + Request.get( + this.apiEndpoint + '/assets/' + this.id + '/groups', + { + headers: { + Authorization: `Bearer ${this.token}` + } + }, + (error, response, body) => { + if (!error && (response.statusCode === 200 || response.statusCode === 201)) { + const bodyObj = JSON.parse(body); + const data = bodyObj.data.map(group => group.group_id); + if (data.includes(this.group_id)) { + utils.writeLog(`Asset ${this.id} have group ${this.group_id}`); + return callback(); + } else { + utils.writeLog(`ERROR Asset ${this.id} is not in group ${this.group_id}`); + return; + } + } else { + utils.writeLog(`ERROR getting informations about asset ${this.id}`, error); + utils.writeErrorLog(`ERROR_API`); + return callback(error); + } + } + ); + } + _getDataFromApi(callback) { utils.writeLog(`Get informations about asset ${this.id}`); Request.get( @@ -74,35 +139,7 @@ class Asset { (error, response, body) => { if (!error && (response.statusCode === 200 || response.statusCode === 201)) { let bodyObj = JSON.parse(body); - // Here are the asset fields. this.assetProps = bodyObj.data; - // console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!', this.assetProps) - return this._getMumblePassword(callback); - } else { - utils.writeLog(`ERROR getting informations about asset ${this.id}`, error); - utils.writeErrorLog(`ERROR_API`); - return callback(error); - } - } - ); - } - - _getMumblePassword(callback) { - utils.writeLog(`Get Mumble password for asset ${this.id}`); - Request.get( - this.apiEndpoint + '/asset/' + this.id + '/account', - { - headers: { - Authorization: `Bearer ${this.token}` - } - }, - (error, response, body) => { - if (!error && (response.statusCode === 200 || response.statusCode === 201)) { - let bodyObj = JSON.parse(body); - - // Here are the asset fields. - this.murmurPassword = bodyObj.data.configuration.mumble_password; - // console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!', this.assetProps) return this._getGroupsFromApi(callback); } else { utils.writeLog(`ERROR getting informations about asset ${this.id}`, error); @@ -129,17 +166,28 @@ class Asset { this.groups = bodyObj.data; - // Find what group this asset is monitoring. - this.groups.forEach((g) => { - if (g.id === this.group_id) { - this.groupSipId = g.sip_id; - this.groupName = g.name; - } - }); + if (this.group_id) { + this.groups.forEach((g) => { + if (g.id === this.group_id) { + this.groupSipId = g.sip_id; + this.groupName = g.name; + } + }); + } else { + this.groups.forEach((g) => { + if (g.is_talk_group && g.monitoring.indexOf(this.id) != -1) { + this.groupSipId = g.sip_id; + this.groupName = g.name; + this.group_id = g.id; + } + }) + } - // if (!this.groupId) { - // return callback('No talk group assigned to ' + this.id); - // } + if (!this.group_id) { + utils.writeErrorLog(`Group/Default voice group not found for asset: ${this.group_id}`); + } + + utils.writeLog(`Group: ${this.group_id} found for asset: ${this.id}`); utils.writeLog(`Informations about asset ${this.id} received | groupId: ${this.group_id} | groupName: ${this.groupName}`); return callback(); } else { @@ -152,10 +200,8 @@ class Asset { } _connectToHub(callback) { - - let hubAddress = this.configs.hub.address; let options = {rejectUnauthorized: false, secure: true}; - let hub = io(hubAddress, options); + let hub = io(this.hubAddress, options); this.hub = hub; // Disconnect event @@ -210,10 +256,16 @@ class Asset { } _connectToMurmur(callback) { + !this.group_id && return callback(); + console.log(`Asset ${this.id} connecting to Murmur`); - this.configs.murmurPassword = this.murmurPassword; - - this.mumble = new Mumble(this.id, this.configs, (err) => { + const murmurConnectionDetails = { + mumbleHost: this.mumbleHost, + mumblePort: this.mumblePort, + murmurUser: this.murmurUser, + murmurPassword: this.murmurPassword + }; + this.mumble = new Mumble(this.id, murmurConnectionDetails, (err) => { if (err) { utils.writeLog(`Asset ${this.id} Murmur connection error`, err); utils.writeErrorLog(`ERROR_MURMUR`); diff --git a/src/mumble.js b/src/mumble.js index 1f5cf69..ebd8f8a 100644 --- a/src/mumble.js +++ b/src/mumble.js @@ -11,12 +11,19 @@ const OpusEncoder = require('node-opus').OpusEncoder; class Mumble { - constructor(id, configs, connectedCallback) { + constructor(id, murmurConnectionDetails, connectedCallback) { this.id = id; - this.configs = configs; - let mumble = configs.mumble; + this.mumbleHost = murmurConnectionDetails.mumbleHost; + this.mumblePort = murmurConnectionDetails.mumblePort; + this.murmurUser = murmurConnectionDetails.murmurUser; + this.murmurPassword = murmurConnectionDetails.murmurPassword; + console.log('mumbleHost', this.mumbleHost) + console.log('mumblePort', this.mumblePort) + console.log('murmurUser', this.murmurUser) + console.log('murmurPassword', this.murmurPassword) + this._createCertificatesForUser(id, () => { - this._makeMumbleConnection(id, mumble.port, mumble.host, id, this.configs.murmurPassword, connectedCallback); + this._makeMumbleConnection(id, this.mumblePort, this.mumbleHost, id, this.murmurPassword, connectedCallback); }) }