2022-11-04 13:41:36 +00:00
|
|
|
const mysql = require('mysql2/promise');
|
|
|
|
const exec = require('child_process').exec;
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
|
|
|
const colors = require('colors');
|
|
|
|
const axios = require('axios');
|
|
|
|
const yml = require('require-yml');
|
|
|
|
|
|
|
|
const selectQuery = fs.readFileSync(`sql/selectQueued.sql`).toString();
|
|
|
|
const jobArgsQuery = fs.readFileSync(`sql/jobArgs.sql`).toString();
|
|
|
|
const updateQuery = fs.readFileSync(`sql/updateState.sql`).toString();
|
|
|
|
const appDir = path.dirname(require.main.filename);
|
|
|
|
|
|
|
|
class PrintServer {
|
|
|
|
async start() {
|
|
|
|
let conf = yml(path.join(__dirname, 'config.yml'));
|
|
|
|
const localConfFile = path.join(__dirname, 'config.local.yml');
|
|
|
|
if (fs.existsSync(localConfFile))
|
|
|
|
conf = Object.assign({}, conf, yml(localConfFile));
|
|
|
|
this.conf = conf;
|
|
|
|
|
2022-11-10 18:21:13 +00:00
|
|
|
const decoration = '△▽'.repeat(10)
|
2022-11-04 13:41:36 +00:00
|
|
|
console.clear();
|
2022-11-07 09:24:16 +00:00
|
|
|
console.log(decoration, `${colors.bgBlack.white.bold(' Print')}${colors.bgBlack.green.bold('Natura ')}`, decoration, '\n')
|
2022-11-09 17:34:27 +00:00
|
|
|
await this.getToken();
|
2022-11-04 13:41:36 +00:00
|
|
|
await this.init();
|
|
|
|
}
|
|
|
|
async init() {
|
|
|
|
this.conn = await mysql.createConnection(this.conf.db);
|
|
|
|
this.conn.on('error', err => this.onDbError(err));
|
|
|
|
await this.poll();
|
|
|
|
}
|
|
|
|
async stop() {
|
2022-11-10 18:21:13 +00:00
|
|
|
const salix = this.conf.salix
|
|
|
|
await axios.post(`${salix.url}/api/${salix.methods.logout}?access_token=${this.token}`);
|
2022-11-04 13:41:36 +00:00
|
|
|
await this.end();
|
|
|
|
}
|
|
|
|
async end() {
|
|
|
|
clearTimeout(this.pollTimeout);
|
|
|
|
await this.conn.end();
|
|
|
|
if (this.reconnectTimeout) {
|
|
|
|
clearTimeout(this.reconnectTimeout);
|
|
|
|
this.reconnectTimeout = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async getToken() {
|
2022-11-10 18:21:13 +00:00
|
|
|
const salix = this.conf.salix
|
|
|
|
let response = await axios.post(`${salix.url}/api/${salix.methods.login}`, {
|
|
|
|
user: salix.user,
|
|
|
|
password: salix.password
|
2022-11-04 13:41:36 +00:00
|
|
|
});
|
|
|
|
this.token = response.data.token
|
|
|
|
}
|
|
|
|
async onDbError(err) {
|
|
|
|
switch(err.code) {
|
2022-11-09 17:34:27 +00:00
|
|
|
case 1927:
|
2022-11-04 13:41:36 +00:00
|
|
|
try {
|
|
|
|
await this.end();
|
|
|
|
} catch(e) {}
|
|
|
|
await this.reconnect();
|
2022-11-09 17:34:27 +00:00
|
|
|
break;
|
2022-11-04 13:41:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
async reconnect() {
|
|
|
|
this.reconnectTimeout = null;
|
|
|
|
try {
|
|
|
|
await this.init();
|
|
|
|
} catch (err) {
|
2022-11-09 17:34:27 +00:00
|
|
|
this.reconnectTimeout = setTimeout(reconnect(), this.conf.reconnectTimeout * 1000);
|
2022-11-04 13:41:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
async poll() {
|
|
|
|
try {
|
|
|
|
await this.printJob();
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
this.reconnect();
|
|
|
|
}
|
|
|
|
this.pollTimeout = setTimeout(() => this.poll(), this.conf.refreshRate);
|
|
|
|
}
|
|
|
|
async printJob() {
|
|
|
|
const conn = this.conn;
|
|
|
|
const conf = this.conf;
|
|
|
|
let printJob;
|
|
|
|
let jobId;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await conn.beginTransaction();
|
|
|
|
[[printJob]] = await conn.query(selectQuery, 'LabelCollection');
|
|
|
|
if (!printJob) return;
|
|
|
|
|
|
|
|
jobId = printJob.id;
|
2022-11-10 17:25:21 +00:00
|
|
|
this.method = printJob.method;
|
|
|
|
|
2022-11-04 13:41:36 +00:00
|
|
|
await conn.query(updateQuery, ['printing', null, jobId]);
|
|
|
|
await conn.commit();
|
|
|
|
} catch (err) {
|
|
|
|
conn.rollback();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const args = {};
|
|
|
|
const res = await conn.query(jobArgsQuery, jobId);
|
|
|
|
for (const row of res[0])
|
|
|
|
args[row.name] = row.value;
|
2022-11-09 17:34:27 +00:00
|
|
|
try {
|
2022-11-10 17:25:21 +00:00
|
|
|
let methodPath = this.method.replace('{id}', args.param);
|
2022-11-09 17:34:27 +00:00
|
|
|
const response = await axios({
|
|
|
|
method: 'get',
|
2022-11-10 17:25:21 +00:00
|
|
|
url: `${conf.salix.url}/api/${methodPath}?access_token=${this.token}`,
|
2022-11-09 17:34:27 +00:00
|
|
|
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) {
|
2022-11-10 18:21:13 +00:00
|
|
|
throw new Error(`Could not get token: ${err.message}`);
|
2022-11-09 17:34:27 +00:00
|
|
|
}
|
|
|
|
break;
|
2022-11-04 13:41:36 +00:00
|
|
|
}
|
2022-11-09 17:34:27 +00:00
|
|
|
}
|
2022-11-04 13:41:36 +00:00
|
|
|
const printer = printJob.printer;
|
2022-11-10 18:21:13 +00:00
|
|
|
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`);
|
2022-11-09 17:34:27 +00:00
|
|
|
await fs.writeFile(tmpFilePath, this.pdfData, 'binary');
|
2022-11-04 13:41:36 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await pExec(`lp -d "${printer}" "${tmpFilePath}"`);
|
|
|
|
} catch(err) {
|
|
|
|
await fs.unlink(tmpFilePath);
|
2022-11-10 18:21:13 +00:00
|
|
|
throw new Error(`The printer ${printer} is not installed: ${err.message}`);
|
2022-11-04 13:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await conn.query(updateQuery, ['printed', null, jobId]);
|
|
|
|
|
|
|
|
if (conf.debug)
|
2022-11-10 18:21:13 +00:00
|
|
|
console.debug(`(${colors.yellow(jobId)}) Document has been printed`, `[${args.param}, ${printJob.report}, ${printer}]`.green);
|
2022-11-04 13:41:36 +00:00
|
|
|
|
|
|
|
await fs.unlink(tmpFilePath);
|
|
|
|
} catch (err) {
|
|
|
|
await this.conn.query(updateQuery, ['error', err.message, jobId]);
|
|
|
|
throw new Error(`(${jobId}) ${err.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PrintServer;
|
|
|
|
|
|
|
|
function pExec(command) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
exec(command, function(err, stdout, stderr) {
|
|
|
|
if (err) return reject(err);
|
|
|
|
resolve({stdout, stderr})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|