linx-simulator2/src/simulator.js

79 lines
1.8 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-11-22 10:38:03 +00:00
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([
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
});
}
_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 {
utils.writeLog(`Unregister assets: ${this.assetIds}`)
return callback();
}
2019-09-18 08:11:16 +00:00
}
);
}
}
module.exports = Simulator;