linx-simulator2/src/simulator.js

113 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-09-18 08:11:16 +00:00
const Asset = require('./asset');
const Request = require('request');
const async = require('async');
const utils = require('./utils');
const fs = require('fs');
const path = require('path');
2019-09-18 08:11:16 +00:00
class Simulator {
constructor(configs) {
this.configs = configs;
2019-12-05 08:59:13 +00:00
this.token;
this.assets = configs.assets;
this.assetIds = this.assets.map(asset => asset.asset_id);
2019-09-18 08:11:16 +00:00
this.apiEndpoint = this.configs.api.use_secure ? 'https' : 'http';
this.apiEndpoint += '://' + this.configs.api.host + (this.configs.api.port ? (':' + this.configs.api.port) : '');
2019-09-18 08:11:16 +00:00
async.waterfall([
this._manageLogs.bind(this),
2019-12-05 08:59:13 +00:00
this._unregisterAsstes.bind(this),
this._getToken.bind(this),
2019-09-18 08:11:16 +00:00
],
(err, result) => {
if(err) {
console.log(err)
return;
}
2019-12-05 08:59:13 +00:00
utils.writeLog(`Using token: ${this.token}`);
2019-09-18 08:11:16 +00:00
this._start();
});
}
_start() {
const settings = this.configs.settings;
2020-06-17 12:13:21 +00:00
this.assets.forEach((asset, i) => {
console.log('asset_id', asset.asset_id, i * settings.delay_between_clients + 'ms');
2020-06-17 12:13:21 +00:00
setTimeout(() => {
new Asset(asset, this.configs, this.token);
}, i * settings.delay_between_clients);
})
}
_getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max))
2019-09-18 08:11:16 +00:00
}
_manageLogs(callback) {
2019-11-22 10:38:03 +00:00
try {
fs.unlinkSync(path.join('logs/', 'error_'+this.configs.elogs.name+'.log'), err => {
if (err) throw err;
2019-11-22 10:38:03 +00:00
});
fs.unlinkSync(path.join('logs/', 'simulator_'+this.configs.elogs.name+'.log'), err => {
if (err) throw err;
});
} catch(e) { console.log(e) }
2019-11-22 09:43:37 +00:00
fs.openSync('./logs/simulator_'+this.configs.elogs.name+'.log', 'w');
utils.writeLog('START');
utils.writeLog(`process.env["NODE_CONFIG_DIR"]: ${process.env["NODE_CONFIG_DIR"]}`);
2019-10-07 13:14:37 +00:00
2019-11-22 09:43:37 +00:00
fs.openSync('./logs/error_'+this.configs.elogs.name+'.log', 'w');
2019-11-22 10:38:03 +00:00
return callback();
}
_unregisterAsstes(callback) {
2019-09-18 08:11:16 +00:00
let url = this.apiEndpoint + '/audio/un-register/[' + this.assetIds + ']/';
Request.post(
url,
2019-10-24 13:36:42 +00:00
{timeout: 15000},
2019-09-18 08:11:16 +00:00
(error, response, body) => {
2019-10-24 13:36:42 +00:00
if(error) {
2019-10-09 08:26:26 +00:00
utils.writeLog(`ERROR can't unregister users ${this.assetIds}`, error);
utils.writeErrorLog(`ERROR_API`);
2019-09-18 08:11:16 +00:00
return callback(error);
}
2019-10-24 13:36:42 +00:00
else {
2022-01-04 14:44:42 +00:00
utils.writeLog(`Unregister assets: ${this.assetIds}`);
2019-10-24 13:36:42 +00:00
return callback();
}
2019-09-18 08:11:16 +00:00
}
);
}
2019-12-05 08:59:13 +00:00
_getToken(callback) {
let url = this.apiEndpoint + '/generate-token';
Request.get(
url,
{timeout: 15000},
(error, response, body) => {
if(error) {
utils.writeLog(`ERROR can't get token`, error);
utils.writeErrorLog(`ERROR_API`);
return callback(error);
}
else {
let res = JSON.parse(response.body)
this.token = res.data.token;
utils.writeLog(`API TOKEN: ${this.token}`)
return callback();
}
}
);
}
2019-09-18 08:11:16 +00:00
}
module.exports = Simulator;