|
| 1 | +class EscposCommands { |
| 2 | + constructor(configs) { |
| 3 | + this.getStatusCommand = "\u0010\u0004\x01"; |
| 4 | + this.getOfflineCauseCommand = "\u0010\u0004\x02"; |
| 5 | + this.getErrorCauseCommand = "\u0010\u0004\x03"; |
| 6 | + this.getRollPaperStatusCommand = "\u0010\u0004\x04"; |
| 7 | + this.configs = configs; |
| 8 | + } |
| 9 | + |
| 10 | + /** |
| 11 | + * @returns {number[]} - The status byte for the escpos printer |
| 12 | + */ |
| 13 | + getEscposStatus() { |
| 14 | + let returnBytes = [0x00]; |
| 15 | + if (![1, "1", true, "true"].includes(this.configs.escposOnline)) { |
| 16 | + // Bit 3 set indicates that the printer is offline |
| 17 | + returnBytes[0] |= 0b00001000; |
| 18 | + } |
| 19 | + if ([1, "1", true, "true"].includes(this.configs.escposPaperFeedPressed)) { |
| 20 | + // Bit 6 set indicates that the paper feed button is pressed |
| 21 | + returnBytes[0] |= 0b01000000; |
| 22 | + } |
| 23 | + |
| 24 | + return returnBytes; |
| 25 | + } |
| 26 | + |
| 27 | + getOfflineCause() { |
| 28 | + let returnBytes = [0x00]; |
| 29 | + |
| 30 | + if ([1, "1", true, "true"].includes(this.configs.escposCoverOpen)) { |
| 31 | + // Bit 2 set indicates that the cover is open |
| 32 | + returnBytes[0] |= 0b00000100; |
| 33 | + } |
| 34 | + |
| 35 | + if ([1, "1", true, "true"].includes(this.configs.escposPaperBeingFed)) { |
| 36 | + // Bit 3 set indicates that the paper is being fed by the paper feed button |
| 37 | + returnBytes[0] |= 0b00001000; |
| 38 | + } |
| 39 | + |
| 40 | + if ([1, "1", true, "true"].includes(this.configs.escposPaperEnd)) { |
| 41 | + // Bit 5 set indicates that the paper is being fed by the paper feed button |
| 42 | + returnBytes[0] |= 0b00100000; |
| 43 | + } |
| 44 | + |
| 45 | + if ([1, "1", true, "true"].includes(this.configs.escposErrorOccurred)) { |
| 46 | + // Bit 6 set indicates that an error has occurred |
| 47 | + returnBytes[0] |= 0b01000000; |
| 48 | + } |
| 49 | + |
| 50 | + return returnBytes; |
| 51 | + } |
| 52 | + |
| 53 | + getErrorCause() { |
| 54 | + let returnBytes = [0x00]; |
| 55 | + |
| 56 | + if ([1, "1", true, "true"].includes(this.configs.escposRecoverableError)) { |
| 57 | + // Bit 2 set indicates that a recoverable error has occurred |
| 58 | + returnBytes[0] |= 0b00000100; |
| 59 | + } |
| 60 | + |
| 61 | + if ([1, "1", true, "true"].includes(this.configs.escposCutterError)) { |
| 62 | + // Bit 3 set indicates that an auto cutter error has occurred |
| 63 | + returnBytes[0] |= 0b00001000; |
| 64 | + } |
| 65 | + |
| 66 | + if ([1, "1", true, "true"].includes(this.configs.escposUnrecoverableError)) { |
| 67 | + // Bit 5 set indicates that an unrecoverable error has occurred |
| 68 | + returnBytes[0] |= 0b00100000; |
| 69 | + } |
| 70 | + |
| 71 | + if ([1, "1", true, "true"].includes(this.configs.escposAutoRecoverableError)) { |
| 72 | + // Bit 6 set indicates that an auto recoverable error has occurred |
| 73 | + returnBytes[0] |= 0b01000000; |
| 74 | + } |
| 75 | + |
| 76 | + return returnBytes; |
| 77 | + } |
| 78 | + |
| 79 | + getRollPaperStatus() { |
| 80 | + let returnBytes = [0x00]; |
| 81 | + |
| 82 | + if ([1, "1", true, "true"].includes(this.configs.escposPaperLow)) { |
| 83 | + // Bit 6 set indicates that the paper is low |
| 84 | + returnBytes[0] |= 0b00001100; |
| 85 | + } |
| 86 | + |
| 87 | + return returnBytes; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +module.exports = EscposCommands; |
0 commit comments