Files
webapp/assets/js/pole-display.js
T

90 lines
2.6 KiB
JavaScript

/*******************************************************************
Module : /assets/js/pole-display.js
Desc. : JS Class Tampil Data di Pole Display POS
Created By : Siau Pheng ([email protected]).
Created Date : October 12th, 2025.
Last Modified : November 1st, 2026.
(c) 3FONIA Software; WWW.3FONIA.COM.
*******************************************************************/
class PoleDisplay {
constructor(port, options = {}) {
this.port = port;
this.pole = '';
this.options = Object.assign({
rx: { start: '\x02', end: '\x0D', width: null },
baudRate: 9600,
dataBits: 8,
stopBits: 1,
parity: 'NONE',
flowControl: 'NONE'
}, options);
}
Open() {
if (this.pole) return;
qz.serial.openPort(this.port, this.options)
.then(() => {
this.pole = this.port;
this._conerr(`${this.port} opened`);
return qz.serial.sendData(this.pole, { type: 'HEX', data: 'x1Bx40' }, this.options);
})
.catch(this._conerr);
}
Close() {
if (!this.pole) return;
qz.serial.closePort(this.port)
.then(() => {
this.pole = '';
this._conerr(`${this.port} closed`);
})
.catch(this._conerr);
}
Send(txt) {
if (!this.pole) return false;
// Convert string to ASCII hex format
const asciiToHex = str =>
Array.from(str, char => char.charCodeAt(0).toString(16).padStart(2, '0').toUpperCase())
.join('x');
let textToSend = txt;
if (Array.isArray(txt)) {
textToSend = txt.map(ele => this._aligntext(ele.t, ele.a)).join('');
}
const hexData = `x10C${asciiToHex(textToSend)}`;
qz.serial.sendData(this.pole, { type: 'HEX', data: hexData }, this.options)
.catch(this._conerr);
}
_aligntext(text, align = 'L') {
const totWidth = 20;
if (text.length >= totWidth) return text.substring(0, totWidth);
const padding = totWidth - text.length;
let lPadding = 0, rPadding = padding;
if (align === 'C') {
lPadding = Math.floor(padding / 2);
rPadding = Math.ceil(padding / 2);
} else if (align === 'R') {
lPadding = padding;
rPadding = 0;
}
return ' '.repeat(lPadding) + text + ' '.repeat(rPadding);
}
_conerr(err) {
console.error(err);
}
}