code improvements

This commit is contained in:
Guillermo Bonet 2022-11-10 19:21:13 +01:00
parent 11f960b7a5
commit b516156e2b
2 changed files with 17 additions and 12 deletions

View File

@ -9,5 +9,8 @@ salix:
url: http://localhost:3000 url: http://localhost:3000
user: user user: user
password: password password: password
methods:
login: method/login
logout: method/logout
reconnectTimeout: 30 reconnectTimeout: 30
refreshRate: 1000 refreshRate: 1000

View File

@ -19,7 +19,7 @@ class PrintServer {
conf = Object.assign({}, conf, yml(localConfFile)); conf = Object.assign({}, conf, yml(localConfFile));
this.conf = conf; this.conf = conf;
let decoration = '△▽△▽△▽△▽△▽△▽△▽△▽△▽△▽' const decoration = '△▽'.repeat(10)
console.clear(); console.clear();
console.log(decoration, `${colors.bgBlack.white.bold(' Print')}${colors.bgBlack.green.bold('Natura ')}`, decoration, '\n') console.log(decoration, `${colors.bgBlack.white.bold(' Print')}${colors.bgBlack.green.bold('Natura ')}`, decoration, '\n')
await this.getToken(); await this.getToken();
@ -31,7 +31,8 @@ class PrintServer {
await this.poll(); await this.poll();
} }
async stop() { async stop() {
await axios.post(`${this.conf.salix.url}/api/Accounts/logout?access_token=${this.token}`); const salix = this.conf.salix
await axios.post(`${salix.url}/api/${salix.methods.logout}?access_token=${this.token}`);
await this.end(); await this.end();
} }
async end() { async end() {
@ -43,9 +44,10 @@ class PrintServer {
} }
} }
async getToken() { async getToken() {
let response = await axios.post(`${this.conf.salix.url}/api/Accounts/login`, { const salix = this.conf.salix
user: this.conf.salix.user, let response = await axios.post(`${salix.url}/api/${salix.methods.login}`, {
password: this.conf.salix.password user: salix.user,
password: salix.password
}); });
this.token = response.data.token this.token = response.data.token
} }
@ -121,29 +123,29 @@ class PrintServer {
try { try {
await this.getToken(); await this.getToken();
} catch(err) { } catch(err) {
console.error(err); throw new Error(`Could not get token: ${err.message}`);
} }
break; break;
} }
} }
const printer = printJob.printer; const printer = printJob.printer;
if (!fs.existsSync(path.join(appDir, 'tmp'))) const tmpPath = path.join(appDir, 'tmp')
fs.mkdirSync(path.join(appDir, 'tmp')) if (!fs.existsSync(tmpPath))
const tmpFilePath = path.join(appDir, 'tmp', `${Math.random().toString(36).substring(7)}.pdf`); fs.mkdirSync(tmpPath)
const tmpFilePath = path.join(tmpPath, `${Math.random().toString(36).substring(7)}.pdf`);
await fs.writeFile(tmpFilePath, this.pdfData, 'binary'); await fs.writeFile(tmpFilePath, this.pdfData, 'binary');
try { try {
await pExec(`lp -d "${printer}" "${tmpFilePath}"`); await pExec(`lp -d "${printer}" "${tmpFilePath}"`);
} catch(err) { } catch(err) {
await fs.unlink(tmpFilePath); await fs.unlink(tmpFilePath);
throw new Error(`The printer could not be accessed: ${printer}: ${err.message}`); throw new Error(`The printer ${printer} is not installed: ${err.message}`);
} }
await conn.query(updateQuery, ['printed', null, jobId]); await conn.query(updateQuery, ['printed', null, jobId]);
if (conf.debug) if (conf.debug)
console.debug(`(${colors.yellow(jobId)}) Document has been printed`, `[${printer}, ${printJob.report}]`.green); console.debug(`(${colors.yellow(jobId)}) Document has been printed`, `[${args.param}, ${printJob.report}, ${printer}]`.green);
await fs.unlink(tmpFilePath); await fs.unlink(tmpFilePath);
} catch (err) { } catch (err) {