Skip to content

Commit 80c9676

Browse files
jctdeemenumqdeandradeerikn69
committed
Add escpos printer status support(#7)
Co-authored-by: Miguel De Andrade <[email protected]> Co-authored-by: ErikN <[email protected]>
1 parent bba4727 commit 80c9676

File tree

5 files changed

+252
-6
lines changed

5 files changed

+252
-6
lines changed

ZplEscPrinter/commands.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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;

ZplEscPrinter/css/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ input[type=checkbox]._big, input[type=radio]._big, ._big>input[type=checkbox], .
3535
margin-top: 5px;
3636
margin-right: 10px;
3737
}
38+
39+
.escpos-col > div > div > span {
40+
width: 100%;
41+
}

ZplEscPrinter/js/main.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ const { ipcRenderer } = require('electron');
55
const fs = require('fs');;
66
const net = require('net');
77

8+
const EscposCommands = require('./commands');
9+
10+
811
let clientSocketInfo;
912
let server;
1013
let configs = {};
14+
const escposCommands = new EscposCommands(configs);
1115

1216
const defaults = {
1317
isZpl: true,
@@ -23,7 +27,18 @@ const defaults = {
2327
saveLabels: false,
2428
filetype: '3',
2529
path: null,
26-
counter: 0
30+
counter: 0,
31+
escposOnline: true,
32+
escposPaperFeedPressed: false,
33+
escposCoverOpen: false,
34+
escposPaperBeingFed: false,
35+
escposPaperEnd: false,
36+
escposErrorOccurred: false,
37+
escposRecoverableError: false,
38+
escposCutterError: false,
39+
escposUnrecoverableError: false,
40+
escposAutoRecoverableError: false,
41+
escposPaperLow: false,
2742
};
2843

2944
$(function () {
@@ -151,10 +166,28 @@ async function zpl(data){
151166
}
152167
}
153168
}
169+
170+
/**
171+
*
172+
* @param {string} data - The incoming socket data from the client
173+
* @param {boolean} b64 - If true, data is base64 encoded
174+
* @returns {Promise<Buffer<ArrayBufferLike> | null>} - The response to send back to the client
175+
*/
154176
async function escpos(data,b64){
155177
let dataAux = data;
156178
try{ dataAux = base64DecodeUnicode(data.trim()); b64=true; }catch(e){}
157179

180+
if (dataAux === escposCommands.getStatusCommand) {
181+
return Buffer.from(escposCommands.getEscposStatus());
182+
} else if (dataAux === escposCommands.getOfflineCauseCommand) {
183+
return Buffer.from(escposCommands.getOfflineCause())
184+
} else if (dataAux === escposCommands.getErrorCauseCommand) {
185+
return Buffer.from(escposCommands.getErrorCause())
186+
} else if (dataAux === escposCommands.getRollPaperStatusCommand) {
187+
return Buffer.from(escposCommands.getRollPaperStatus())
188+
}
189+
190+
158191
if (!dataAux || !dataAux.trim().length) {
159192
console.warn(`esc/pos = '${data}', seems invalid`);
160193
return;
@@ -183,6 +216,8 @@ async function escpos(data,b64){
183216
if ([1, '1', true, 'true'].includes(configs.saveLabels)) {
184217
await saveLabel(data, "raw", getCounter());
185218
}
219+
220+
return null
186221
}
187222
async function displayEscPosLabel (data){
188223
let frame = $('<iframe class="label-esc w-100"></iframe>');
@@ -260,14 +295,12 @@ function startTcpServer() {
260295
const response = JSON.stringify({success: true});
261296
sock.write('HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: ' + Buffer.byteLength(response) + '\r\n\r\n' + response);
262297
data = data.replace(regex,'');
263-
} else {
264-
sock.write(Buffer.from("0x0"));
265298
}
266-
sock.end();
267299

268300
const code = data + '';
269301
if (code.includes('Host:') && code.includes('Connection: keep-alive') && code.includes('HTTP')) {
270302
console.log('It\'s an ajax call');
303+
sock.end();
271304
return;
272305
}
273306

@@ -279,7 +312,8 @@ function startTcpServer() {
279312
if ($('#isZpl').is(':checked')) {
280313
zpl(code);
281314
} else {
282-
escpos(code);
315+
const response = await escpos(code);
316+
if (response) sock.write(response);
283317
}
284318
}catch(err){
285319
console.error(err);

ZplEscPrinter/main.html

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,123 @@ <h5 id="mdlPrinterTest" class="modal-title modal-title">
195195
<button id="btn-path" type="button" class="btn btn-secondary">Choose</button>
196196
</div>
197197
</fieldset>
198+
<fieldset class="border rounded p-2 mb-1 is-esc">
199+
<legend class="float-none w-auto px-2 mb-0 fs-6 fw-semibold">ESC/POS Status</legend>
200+
<div class="container">
201+
<div class="row row-cols-2 escpos-col row-gap-2">
202+
<div class="col">
203+
<div class="input-group">
204+
<span class="input-group-text">
205+
<div class="form-check">
206+
<input id="escposOnline" type="checkbox" class="form-check-input _big">
207+
<label class="form-check-label" for="escposOnline">Online</label>
208+
</div>
209+
</span>
210+
</div>
211+
</div>
212+
<div class="col">
213+
<div class="input-group">
214+
<span class="input-group-text">
215+
<div class="form-check">
216+
<input id="escposPaperFeedPressed" type="checkbox" class="form-check-input _big">
217+
<label class="form-check-label" for="escposPaperFeedPressed">Paper Feed Pressed</label>
218+
</div>
219+
</span>
220+
</div>
221+
</div>
222+
<div class="col">
223+
<div class="input-group">
224+
<span class="input-group-text">
225+
<div class="form-check">
226+
<input id="escposCoverOpen" type="checkbox" class="form-check-input _big">
227+
<label class="form-check-label" for="escposCoverOpen">Cover Open</label>
228+
</div>
229+
</span>
230+
</div>
231+
</div>
232+
<div class="col">
233+
<div class="input-group">
234+
<span class="input-group-text">
235+
<div class="form-check">
236+
<input id="escposPaperBeingFed" type="checkbox" class="form-check-input _big">
237+
<label class="form-check-label" for="escposPaperBeingFed">Paper Being Fed</label>
238+
</div>
239+
</span>
240+
</div>
241+
</div>
242+
<div class="col">
243+
<div class="input-group">
244+
<span class="input-group-text">
245+
<div class="form-check">
246+
<input id="escposPaperEnd" type="checkbox" class="form-check-input _big">
247+
<label class="form-check-label" for="escposPaperEnd">Paper End</label>
248+
</div>
249+
</span>
250+
</div>
251+
</div>
252+
<div class="col">
253+
<div class="input-group">
254+
<span class="input-group-text">
255+
<div class="form-check">
256+
<input id="escposErrorOccurred" type="checkbox" class="form-check-input _big">
257+
<label class="form-check-label" for="escposErrorOccurred">Error Occurred</label>
258+
</div>
259+
</span>
260+
</div>
261+
</div>
262+
<div class="col">
263+
<div class="input-group">
264+
<span class="input-group-text">
265+
<div class="form-check">
266+
<input id="escposRecoverableError" type="checkbox" class="form-check-input _big">
267+
<label class="form-check-label" for="escposRecoverableError">Recoverable Error</label>
268+
</div>
269+
</span>
270+
</div>
271+
</div>
272+
<div class="col">
273+
<div class="input-group">
274+
<span class="input-group-text">
275+
<div class="form-check">
276+
<input id="escposCutterError" type="checkbox" class="form-check-input _big">
277+
<label class="form-check-label" for="escposCutterError">Auto Cutter Error</label>
278+
</div>
279+
</span>
280+
</div>
281+
</div>
282+
<div class="col">
283+
<div class="input-group">
284+
<span class="input-group-text">
285+
<div class="form-check">
286+
<input id="escposUnrecoverableError" type="checkbox" class="form-check-input _big">
287+
<label class="form-check-label" for="escposUnrecoverableError">Unrecoverable Error</label>
288+
</div>
289+
</span>
290+
</div>
291+
</div>
292+
<div class="col">
293+
<div class="input-group">
294+
<span class="input-group-text">
295+
<div class="form-check">
296+
<input id="escposAutoRecoverableError" type="checkbox" class="form-check-input _big">
297+
<label class="form-check-label" for="escposAutoRecoverableError">Auto Recoverable Error</label>
298+
</div>
299+
</span>
300+
</div>
301+
</div>
302+
<div class="col">
303+
<div class="input-group">
304+
<span class="input-group-text">
305+
<div class="form-check">
306+
<input id="escposPaperLow" type="checkbox" class="form-check-input _big">
307+
<label class="form-check-label" for="escposPaperLow">Paper Low</label>
308+
</div>
309+
</span>
310+
</div>
311+
</div>
312+
</div>
313+
</div>
314+
</fieldset>
198315
</div>
199316
<div class="modal-footer p-1">
200317
<button id="btn-close-save-settings" type="button" class="btn btn-secondary btn-close-save-settings" data-bs-dismiss="modal"><i class="glyphicon glyphicon-remove"></i> Close</button>

main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ updateElectronApp()
99
let win
1010
const createWindow = () => {
1111
win = new BrowserWindow({
12-
width: process.env.NODE_ENV === "development" ? 895 : 535,
12+
width: process.env.NODE_ENV === "development" ? 785 : 535,
1313
height: 765,
1414
frame: false,
1515
resizable: true,

0 commit comments

Comments
 (0)