code improvements
This commit is contained in:
parent
11f960b7a5
commit
b516156e2b
|
@ -9,5 +9,8 @@ salix:
|
|||
url: http://localhost:3000
|
||||
user: user
|
||||
password: password
|
||||
methods:
|
||||
login: method/login
|
||||
logout: method/logout
|
||||
reconnectTimeout: 30
|
||||
refreshRate: 1000
|
||||
|
|
|
@ -19,7 +19,7 @@ class PrintServer {
|
|||
conf = Object.assign({}, conf, yml(localConfFile));
|
||||
this.conf = conf;
|
||||
|
||||
let decoration = '△▽△▽△▽△▽△▽△▽△▽△▽△▽△▽'
|
||||
const decoration = '△▽'.repeat(10)
|
||||
console.clear();
|
||||
console.log(decoration, `${colors.bgBlack.white.bold(' Print')}${colors.bgBlack.green.bold('Natura ')}`, decoration, '\n')
|
||||
await this.getToken();
|
||||
|
@ -31,7 +31,8 @@ class PrintServer {
|
|||
await this.poll();
|
||||
}
|
||||
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();
|
||||
}
|
||||
async end() {
|
||||
|
@ -43,9 +44,10 @@ class PrintServer {
|
|||
}
|
||||
}
|
||||
async getToken() {
|
||||
let response = await axios.post(`${this.conf.salix.url}/api/Accounts/login`, {
|
||||
user: this.conf.salix.user,
|
||||
password: this.conf.salix.password
|
||||
const salix = this.conf.salix
|
||||
let response = await axios.post(`${salix.url}/api/${salix.methods.login}`, {
|
||||
user: salix.user,
|
||||
password: salix.password
|
||||
});
|
||||
this.token = response.data.token
|
||||
}
|
||||
|
@ -121,29 +123,29 @@ class PrintServer {
|
|||
try {
|
||||
await this.getToken();
|
||||
} catch(err) {
|
||||
console.error(err);
|
||||
throw new Error(`Could not get token: ${err.message}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const printer = printJob.printer;
|
||||
if (!fs.existsSync(path.join(appDir, 'tmp')))
|
||||
fs.mkdirSync(path.join(appDir, 'tmp'))
|
||||
const tmpFilePath = path.join(appDir, 'tmp', `${Math.random().toString(36).substring(7)}.pdf`);
|
||||
const tmpPath = path.join(appDir, 'tmp')
|
||||
if (!fs.existsSync(tmpPath))
|
||||
fs.mkdirSync(tmpPath)
|
||||
const tmpFilePath = path.join(tmpPath, `${Math.random().toString(36).substring(7)}.pdf`);
|
||||
await fs.writeFile(tmpFilePath, this.pdfData, 'binary');
|
||||
|
||||
try {
|
||||
await pExec(`lp -d "${printer}" "${tmpFilePath}"`);
|
||||
} catch(err) {
|
||||
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]);
|
||||
|
||||
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);
|
||||
} catch (err) {
|
||||
|
|
Loading…
Reference in New Issue