|
| 1 | +class ZplCommands { |
| 2 | + constructor(configs = {}) { |
| 3 | + this.configs = configs; |
| 4 | + this.commands = { |
| 5 | + getStatus: '~HS', // Host Status |
| 6 | + getInfo: '~HI', // Host Identification |
| 7 | + getConfig: '~WC', // Configuration Label |
| 8 | + getDirectory: '~WD' // Directory listing |
| 9 | + }; |
| 10 | + } |
| 11 | + |
| 12 | + matchCommand(data) { |
| 13 | + return Object.values(this.commands).includes(data.trim().toUpperCase()); |
| 14 | + } |
| 15 | + |
| 16 | + getResponse(data) { |
| 17 | + const cmd = data.trim().toUpperCase(); |
| 18 | + switch (cmd) { |
| 19 | + case this.commands.getStatus: |
| 20 | + return Buffer.from(this.getPrinterStatus(), 'utf8'); |
| 21 | + case this.commands.getInfo: |
| 22 | + return Buffer.from(this.getPrinterInfo(), 'utf8'); |
| 23 | + case this.commands.getConfig: |
| 24 | + return Buffer.from(this.getPrinterConfig(), 'utf8'); |
| 25 | + case this.commands.getDirectory: |
| 26 | + return Buffer.from(this.getPrinterDirectory(), 'utf8'); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + getPrinterStatus() { |
| 31 | + const string1 = '100,111,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0'; |
| 32 | + const string3 = '24,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0'; |
| 33 | + const string2Arr = new Array(32).fill(0); |
| 34 | + string2Arr[0] = [1, "1", true, "true"].includes(this.configs.zplHeadOpen) ? 1 : 0; |
| 35 | + string2Arr[1] = [1, "1", true, "true"].includes(this.configs.zplPaperOut) ? 1 : 0; |
| 36 | + string2Arr[2] = [1, "1", true, "true"].includes(this.configs.zplRibbonOut) ? 1 : 0; |
| 37 | + string2Arr[3] = [1, "1", true, "true"].includes(this.configs.zplCutterFault) ? 1 : 0; |
| 38 | + string2Arr[4] = [1, "1", true, "true"].includes(this.configs.zplHeadTooHot) ? 1 : 0; |
| 39 | + string2Arr[5] = [1, "1", true, "true"].includes(this.configs.zplMotorOverheat) ? 1 : 0; |
| 40 | + string2Arr[6] = [1, "1", true, "true"].includes(this.configs.zplPrintheadFault) ? 1 : 0; |
| 41 | + string2Arr[7] = [1, "1", true, "true"].includes(this.configs.zplPrinterPaused) ? 1 : 0; |
| 42 | + string2Arr[8] = [1, "1", true, "true"].includes(this.configs.zplRewindFault) ? 1 : 0; |
| 43 | + string2Arr[9] = [1, "1", true, "true"].includes(this.configs.zplPaperJam) ? 1 : 0; |
| 44 | + |
| 45 | + return ( |
| 46 | + '\x02' + string1 + '\x03\r\n' + |
| 47 | + '\x02' + string2Arr.join(',') + '\x03\r\n' + |
| 48 | + '\x02' + string3 + '\x03\r\n' |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + getPrinterInfo() { |
| 53 | + return ( |
| 54 | + '\x02EN SYSTEMS CORP.\x03\r\n' + |
| 55 | + '\x02ZEBRA ' + this.configs.density + 'dpmm EMULATOR\x03\r\n' + |
| 56 | + '\x02FIRMWARE' + (this.configs.version||'') + ' (JS EMULATOR)\x03\r\n' |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + getPrinterConfig() { |
| 61 | + return '^XA^MMT^PR5,5,5^MD0^LH0,0^JMA^XZ'; |
| 62 | + } |
| 63 | + |
| 64 | + getPrinterDirectory() { |
| 65 | + return ( |
| 66 | + '\x02R:FILE1.ZPL,1024\r\n' + |
| 67 | + 'R:LABEL.ZPL,512\r\n' + |
| 68 | + 'R:LOGO.GRF,8192\r\n\x03\r\n' |
| 69 | + ); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +module.exports = ZplCommands; |
0 commit comments