linx-simulator2/src/simulator.js

78 lines
1.7 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;
// 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([
this._manageLogs.bind(this),
this._unregisterAsstes.bind(this)
],
(err, result) => {
if(err) {
console.log(err)
return;
}
this._start();
});
}
_start() {
this.assetIds.forEach(id => {
new Asset(id, this.configs);
});
}
_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;
});
}
fs.openSync('./logs/simulator.log', 'w');
utils.writeLog('START');
utils.writeLog(`process.env["NODE_CONFIG_DIR"]: ${process.env["NODE_CONFIG_DIR"]}`);
fs.openSync('./logs/error.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();
}
}
);
}
}
module.exports = Simulator;