121 lines
3.0 KiB
JavaScript
121 lines
3.0 KiB
JavaScript
const Asset = require('./asset');
|
|
const Request = require('request');
|
|
const async = require('async');
|
|
const utils = require('./utils');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
class Simulator {
|
|
|
|
constructor(configs) {
|
|
|
|
this.configs = configs;
|
|
this.token;
|
|
|
|
this.assets = configs.assets;
|
|
this.assetIds = this.assets.map(asset => asset.asset_id);
|
|
|
|
this.apiEndpoint = this.configs.api.use_secure ? 'https' : 'http';
|
|
this.apiEndpoint += '://' + this.configs.api.host + (this.configs.api.port ? (':' + this.configs.api.port) : '');
|
|
|
|
async.waterfall([
|
|
this._manageLogs.bind(this),
|
|
this._unregisterAsstes.bind(this),
|
|
this._getToken.bind(this),
|
|
],
|
|
(err, result) => {
|
|
if(err) {
|
|
console.log(err)
|
|
return;
|
|
}
|
|
utils.writeLog(`Using token: ${this.token}`);
|
|
this._start();
|
|
});
|
|
}
|
|
|
|
_start() {
|
|
const settings = this.configs.settings;
|
|
|
|
// this.assetIds.forEach((id, i) => {
|
|
this.assets.forEach((asset, i) => {
|
|
let delay_between_clients = 500;
|
|
if(settings.hasOwnProperty('delay_between_clients')) {
|
|
delay_between_clients = parseInt(settings.delay_between_clients, 10);
|
|
}
|
|
console.log('asset_id', asset.asset_id, i * delay_between_clients+'ms')
|
|
|
|
delay_between_clients = this._getRandomInt(delay_between_clients);
|
|
setTimeout(() => {
|
|
new Asset(asset, this.configs, this.token);
|
|
// console.log('id, this.configs, this.token', id, this.configs, this.token)
|
|
}, i * delay_between_clients);
|
|
})
|
|
}
|
|
|
|
_getRandomInt(max) {
|
|
return Math.floor(Math.random() * Math.floor(max))
|
|
}
|
|
|
|
_manageLogs(callback) {
|
|
try {
|
|
fs.unlinkSync(path.join('logs/', 'error_'+this.configs.elogs.name+'.log'), err => {
|
|
if (err) throw err;
|
|
});
|
|
fs.unlinkSync(path.join('logs/', 'simulator_'+this.configs.elogs.name+'.log'), err => {
|
|
if (err) throw err;
|
|
});
|
|
} catch(e) { console.log(e) }
|
|
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"]}`);
|
|
|
|
fs.openSync('./logs/error_'+this.configs.elogs.name+'.log', 'w');
|
|
|
|
return callback();
|
|
}
|
|
|
|
_unregisterAsstes(callback) {
|
|
|
|
let url = this.apiEndpoint + '/audio/un-register/[' + this.assetIds + ']/';
|
|
Request.post(
|
|
url,
|
|
{timeout: 15000},
|
|
(error, response, body) => {
|
|
if(error) {
|
|
utils.writeLog(`ERROR can't unregister users ${this.assetIds}`, error);
|
|
utils.writeErrorLog(`ERROR_API`);
|
|
return callback(error);
|
|
}
|
|
else {
|
|
utils.writeLog(`Unregister assets: ${this.assetIds}`);
|
|
return callback();
|
|
}
|
|
}
|
|
);
|
|
}
|
|
|
|
_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();
|
|
}
|
|
}
|
|
);
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = Simulator;
|