bugs fixed
This commit is contained in:
parent
ec027ac2f2
commit
f57ad11409
|
@ -22,16 +22,16 @@ class PrintServer {
|
||||||
let decoration = '△▽△▽△▽△▽△▽△▽△▽△▽△▽△▽'
|
let decoration = '△▽△▽△▽△▽△▽△▽△▽△▽△▽△▽'
|
||||||
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.init();
|
await this.init();
|
||||||
}
|
}
|
||||||
async init() {
|
async init() {
|
||||||
this.conn = await mysql.createConnection(this.conf.db);
|
this.conn = await mysql.createConnection(this.conf.db);
|
||||||
this.conn.on('error', err => this.onDbError(err));
|
this.conn.on('error', err => this.onDbError(err));
|
||||||
await this.getToken();
|
|
||||||
await this.poll();
|
await this.poll();
|
||||||
}
|
}
|
||||||
async stop() {
|
async stop() {
|
||||||
|
await axios.post(`${this.conf.salix.url}/api/Accounts/logout?access_token=${this.token}`);
|
||||||
await this.end();
|
await this.end();
|
||||||
}
|
}
|
||||||
async end() {
|
async end() {
|
||||||
|
@ -51,12 +51,12 @@ class PrintServer {
|
||||||
}
|
}
|
||||||
async onDbError(err) {
|
async onDbError(err) {
|
||||||
switch(err.code) {
|
switch(err.code) {
|
||||||
case 'CONNECTION_CLOSE':
|
case 1927:
|
||||||
try {
|
try {
|
||||||
await this.end();
|
await this.end();
|
||||||
} catch(e) {}
|
} catch(e) {}
|
||||||
await this.reconnect();
|
await this.reconnect();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async reconnect() {
|
async reconnect() {
|
||||||
|
@ -64,7 +64,7 @@ class PrintServer {
|
||||||
try {
|
try {
|
||||||
await this.init();
|
await this.init();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.reconnectTimeout = setTimeout(reconnect, this.conf.reconnectTimeout * 1000);
|
this.reconnectTimeout = setTimeout(reconnect(), this.conf.reconnectTimeout * 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async poll() {
|
async poll() {
|
||||||
|
@ -101,22 +101,35 @@ class PrintServer {
|
||||||
const res = await conn.query(jobArgsQuery, jobId);
|
const res = await conn.query(jobArgsQuery, jobId);
|
||||||
for (const row of res[0])
|
for (const row of res[0])
|
||||||
args[row.name] = row.value;
|
args[row.name] = row.value;
|
||||||
|
try {
|
||||||
const response = await axios({
|
const response = await axios({
|
||||||
method: 'get',
|
method: 'get',
|
||||||
url: `${conf.salix.url}/api/Tickets/${args.param}/collection-label?access_token=${this.token}`,
|
url: `${conf.salix.url}/api/Tickets/${args.param}/collection-label?access_token=${this.token}`,
|
||||||
responseType: 'arraybuffer',
|
responseType: 'arraybuffer',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Accept': 'application/pdf'
|
'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;
|
const printer = printJob.printer;
|
||||||
if (!fs.existsSync(path.join(appDir, 'tmp')))
|
if (!fs.existsSync(path.join(appDir, 'tmp')))
|
||||||
fs.mkdirSync(path.join(appDir, 'tmp'))
|
fs.mkdirSync(path.join(appDir, 'tmp'))
|
||||||
const tmpFilePath = path.join(appDir, 'tmp', `${Math.random().toString(36).substring(7)}.pdf`);
|
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 {
|
try {
|
||||||
await pExec(`lp -d "${printer}" "${tmpFilePath}"`);
|
await pExec(`lp -d "${printer}" "${tmpFilePath}"`);
|
||||||
|
@ -132,15 +145,6 @@ class PrintServer {
|
||||||
|
|
||||||
await fs.unlink(tmpFilePath);
|
await fs.unlink(tmpFilePath);
|
||||||
} catch (err) {
|
} 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]);
|
await this.conn.query(updateQuery, ['error', err.message, jobId]);
|
||||||
throw new Error(`(${jobId}) ${err.message}`);
|
throw new Error(`(${jobId}) ${err.message}`);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue