bugs fixed

This commit is contained in:
Guillermo Bonet 2022-11-09 18:34:27 +01:00
parent ec027ac2f2
commit f57ad11409
1 changed files with 28 additions and 24 deletions

View File

@ -22,16 +22,16 @@ class PrintServer {
let decoration = '△▽△▽△▽△▽△▽△▽△▽△▽△▽△▽'
console.clear();
console.log(decoration, `${colors.bgBlack.white.bold(' Print')}${colors.bgBlack.green.bold('Natura ')}`, decoration, '\n')
await this.getToken();
await this.init();
}
async init() {
this.conn = await mysql.createConnection(this.conf.db);
this.conn.on('error', err => this.onDbError(err));
await this.getToken();
await this.poll();
}
async stop() {
await axios.post(`${this.conf.salix.url}/api/Accounts/logout?access_token=${this.token}`);
await this.end();
}
async end() {
@ -51,12 +51,12 @@ class PrintServer {
}
async onDbError(err) {
switch(err.code) {
case 'CONNECTION_CLOSE':
case 1927:
try {
await this.end();
} catch(e) {}
await this.reconnect();
break;
break;
}
}
async reconnect() {
@ -64,7 +64,7 @@ class PrintServer {
try {
await this.init();
} catch (err) {
this.reconnectTimeout = setTimeout(reconnect, this.conf.reconnectTimeout * 1000);
this.reconnectTimeout = setTimeout(reconnect(), this.conf.reconnectTimeout * 1000);
}
}
async poll() {
@ -101,22 +101,35 @@ class PrintServer {
const res = await conn.query(jobArgsQuery, jobId);
for (const row of res[0])
args[row.name] = row.value;
const response = await axios({
method: 'get',
url: `${conf.salix.url}/api/Tickets/${args.param}/collection-label?access_token=${this.token}`,
responseType: 'arraybuffer',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/pdf'
try {
const response = await axios({
method: 'get',
url: `${conf.salix.url}/api/Tickets/${args.param}/collection-label?access_token=${this.token}`,
responseType: 'arraybuffer',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/pdf'
}
});
this.pdfData = response.data
}
catch (err) {
switch(err.response.statusText) {
case 'Unauthorized':
try {
await this.getToken();
} catch(err) {
console.error(err);
}
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`);
await fs.writeFile(tmpFilePath, response.data, 'binary');
await fs.writeFile(tmpFilePath, this.pdfData, 'binary');
try {
await pExec(`lp -d "${printer}" "${tmpFilePath}"`);
@ -132,15 +145,6 @@ class PrintServer {
await fs.unlink(tmpFilePath);
} catch (err) {
switch(err.code) {
case 'ERR_BAD_REQUEST':
try {
await this.getToken();
} catch(err) {
console.error(err);
}
break;
}
await this.conn.query(updateQuery, ['error', err.message, jobId]);
throw new Error(`(${jobId}) ${err.message}`);
}