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;
|
|
|
|
|
|
|
|
// 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),
|
|
|
|
this._unregisterAsstes.bind(this)
|
2019-09-18 08:11:16 +00:00
|
|
|
],
|
|
|
|
(err, result) => {
|
|
|
|
if(err) {
|
|
|
|
console.log(err)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._start();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_start() {
|
|
|
|
this.assetIds.forEach(id => {
|
2019-10-02 09:11:42 +00:00
|
|
|
new Asset(id, this.configs);
|
2019-09-18 08:11:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-25 11:02:03 +00:00
|
|
|
_manageLogs(callback) {
|
|
|
|
fs.readdir('logs', (err, files) => {
|
|
|
|
if (err) throw err;
|
|
|
|
for (const file of files) {
|
|
|
|
fs.unlinkSync(path.join('logs', file), err => {
|
|
|
|
if (err) throw err;
|
|
|
|
});
|
|
|
|
}
|
2019-10-07 13:14:37 +00:00
|
|
|
fs.openSync('./logs/simulator.log', 'w');
|
2019-09-25 11:02:03 +00:00
|
|
|
utils.writeLog('START');
|
2019-10-07 13:14:37 +00:00
|
|
|
|
|
|
|
fs.openSync('./logs/error.log', 'w');
|
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,
|
|
|
|
{},
|
|
|
|
(error, response, body) => {
|
|
|
|
if (!error && (response.statusCode === 200 || response.statusCode === 201)) {
|
2019-09-25 11:02:03 +00:00
|
|
|
utils.writeLog(`Unregister assets: ${this.assetIds}`)
|
2019-09-18 08:11:16 +00:00
|
|
|
return callback();
|
|
|
|
} else {
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Simulator;
|