2019-09-18 08:11:16 +00:00
|
|
|
const Asset = require('./asset');
|
|
|
|
const Request = require('request');
|
|
|
|
const async = require('async');
|
2019-09-25 11:02:03 +00:00
|
|
|
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;
|
|
|
|
|
2019-09-18 08:11:16 +00:00
|
|
|
// Read assets ids from configs
|
|
|
|
this.assetIds = configs.assets.ids;
|
|
|
|
|
|
|
|
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([
|
2019-09-25 11:02:03 +00:00
|
|
|
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() {
|
2020-04-09 14:02:44 +00:00
|
|
|
const settings = this.configs.settings;
|
2020-06-17 12:13:21 +00:00
|
|
|
|
|
|
|
this.assetIds.forEach((id, i) => {
|
|
|
|
let testing_period = 500;
|
2020-04-09 14:02:44 +00:00
|
|
|
if(settings.hasOwnProperty('testing_period')) {
|
|
|
|
testing_period = parseInt(settings.testing_period, 10);
|
|
|
|
}
|
2020-06-17 12:13:21 +00:00
|
|
|
console.log('id', id, i * testing_period+'ms')
|
|
|
|
|
2020-04-09 14:02:44 +00:00
|
|
|
testing_period = this._getRandomInt(testing_period);
|
2020-06-17 12:13:21 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
|
|
|
|
new Asset(id, this.configs, this.token);
|
|
|
|
}, i * testing_period);
|
2020-04-09 14:02:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_getRandomInt(max) {
|
|
|
|
return Math.floor(Math.random() * Math.floor(max))
|
2019-09-18 08:11:16 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 11:02:03 +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 => {
|
2019-09-25 11:02:03 +00:00
|
|
|
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');
|
2019-09-25 11:02:03 +00:00
|
|
|
utils.writeLog('START');
|
2019-11-07 13:11:05 +00:00
|
|
|
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
|
|
|
|
2019-09-25 11:02: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 {
|
|
|
|
utils.writeLog(`Unregister assets: ${this.assetIds}`)
|
|
|
|
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;
|