Refactor PoleDisplay class and update Auth user_log method
- Converted PoleDisplay from a function-based structure to a class-based structure for improved readability and maintainability. - Simplified the Open and Close methods to handle port state more clearly. - Enhanced the Send method to support both string and array inputs for sending data. - Updated the cd_Text method to use modern JavaScript features for padding calculations. - Modified the user_log method in Auth class to streamline database fetch and update logic.
This commit is contained in:
+63
-67
@@ -4,90 +4,86 @@
|
||||
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.
|
||||
|
||||
*******************************************************************/
|
||||
|
||||
var PoleDisplay = function(port, options){
|
||||
var _cd_port = '';
|
||||
var _cd_pole = '';
|
||||
var _options = {
|
||||
rx: { start: '\x02', end: '\x0D', width: null },
|
||||
baudRate: 9600,
|
||||
dataBits: 8,
|
||||
stopBits: 1,
|
||||
parity: 'NONE',
|
||||
flowControl: 'NONE'
|
||||
};
|
||||
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);
|
||||
}
|
||||
|
||||
this.construct = function (port, options){
|
||||
$.extend(_options, options);
|
||||
_cd_port = port;
|
||||
};
|
||||
Open() {
|
||||
if (this.pole) return;
|
||||
|
||||
this.Open = function (){
|
||||
if (_cd_pole === '') {
|
||||
qz.serial.openPort(_cd_port, _options).then(() => {
|
||||
_cd_pole = _cd_port;
|
||||
this.cd_Error(_cd_port+' opened');
|
||||
}).then(() => {
|
||||
qz.serial.sendData(_cd_pole, {type:'HEX',data:'x1Bx40'}, _options).catch(this.cd_Error);
|
||||
}).catch(this.cd_Error);
|
||||
}
|
||||
};
|
||||
qz.serial.openPort(this.port, this.options)
|
||||
.then(() => {
|
||||
this.pole = this.port;
|
||||
this.cd_Error(`${this.port} opened`);
|
||||
return qz.serial.sendData(this.pole, { type: 'HEX', data: 'x1Bx40' }, this.options);
|
||||
})
|
||||
.catch(this.cd_Error);
|
||||
}
|
||||
|
||||
this.Close = function (){
|
||||
qz.serial.closePort(_cd_port).then(() => {
|
||||
_cd_pole = '';
|
||||
this.cd_Error(_cd_port+' closed');
|
||||
}).catch(this.cd_Error);
|
||||
};
|
||||
Close() {
|
||||
if (!this.pole) return;
|
||||
|
||||
this.Send = function (txt){
|
||||
if (_cd_pole === '') return false;
|
||||
var ascii_to_hexa = function(str) {
|
||||
var arr1 = [];
|
||||
for (var n = 0, l = str.length; n < l; n++) {
|
||||
var hex = Number(str.charCodeAt(n)).toString(16);
|
||||
if (hex.length==1) hex = '0' + hex;
|
||||
arr1.push(hex.toUpperCase());
|
||||
}
|
||||
return 'x' + arr1.join('x');
|
||||
};
|
||||
var _txt = txt;
|
||||
qz.serial.closePort(this.port)
|
||||
.then(() => {
|
||||
this.pole = '';
|
||||
this.cd_Error(`${this.port} closed`);
|
||||
})
|
||||
.catch(this.cd_Error);
|
||||
}
|
||||
|
||||
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)) {
|
||||
_txt = '';
|
||||
txt.forEach((ele) => {
|
||||
_txt += this.cd_Text(ele['t'], ele['a']);
|
||||
});
|
||||
textToSend = txt.map(ele => this.cd_Text(ele.t, ele.a)).join('');
|
||||
}
|
||||
qz.serial.sendData(_cd_pole, {type:'HEX',data:'x10C'+ascii_to_hexa(_txt)}, _options).catch(this.cd_Error);
|
||||
};
|
||||
|
||||
this.cd_Text = function (text, align){
|
||||
const hexData = `x10C${asciiToHex(textToSend)}`;
|
||||
qz.serial.sendData(this.pole, { type: 'HEX', data: hexData }, this.options)
|
||||
.catch(this.cd_Error);
|
||||
}
|
||||
|
||||
cd_Text(text, align = 'L') {
|
||||
const totWidth = 20;
|
||||
const txtLength = text.length;
|
||||
if (txtLength >= totWidth) {
|
||||
return text.substring(0, totWidth);
|
||||
}
|
||||
const padding = totWidth - txtLength;
|
||||
var lPadding = 0;
|
||||
var rPadding = padding;
|
||||
if (align === null) align = 'L';
|
||||
if (align == 'C'){
|
||||
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'){
|
||||
} else if (align === 'R') {
|
||||
lPadding = padding;
|
||||
rPadding = 0;
|
||||
}
|
||||
return " ".repeat(lPadding) + text + " ".repeat(rPadding);
|
||||
};
|
||||
|
||||
this.cd_Error = function (err){
|
||||
return ' '.repeat(lPadding) + text + ' '.repeat(rPadding);
|
||||
}
|
||||
|
||||
cd_Error(err) {
|
||||
console.error(err);
|
||||
};
|
||||
|
||||
this.construct(port, options);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user