-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-http.html
More file actions
483 lines (433 loc) · 14.8 KB
/
Copy pathtest-http.html
File metadata and controls
483 lines (433 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Printer Bridge HTTP API Test</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
h1 {
color: #333;
margin-top: 0;
}
.test-section {
margin: 20px 0;
padding: 20px;
border: 1px solid #e0e0e0;
border-radius: 4px;
background-color: #fafafa;
}
h2 {
font-size: 18px;
margin-top: 0;
color: #555;
}
button {
background-color: #007bff;
color: white;
padding: 12px 24px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
margin: 5px;
}
button:hover {
background-color: #0056b3;
}
button.secondary {
background-color: #6c757d;
}
button.secondary:hover {
background-color: #545b62;
}
button.success {
background-color: #28a745;
}
button.success:hover {
background-color: #218838;
}
.response {
margin-top: 15px;
padding: 15px;
background-color: #f8f9fa;
border-left: 4px solid #007bff;
border-radius: 4px;
font-family: 'Courier New', monospace;
font-size: 13px;
white-space: pre-wrap;
word-wrap: break-word;
display: none;
}
.response.show {
display: block;
}
.response.error {
border-left-color: #dc3545;
background-color: #f8d7da;
color: #721c24;
}
.response.success {
border-left-color: #28a745;
background-color: #d4edda;
color: #155724;
}
textarea {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-family: 'Courier New', monospace;
font-size: 13px;
margin-bottom: 10px;
box-sizing: border-box;
}
.status {
padding: 10px;
border-radius: 4px;
margin-bottom: 20px;
}
.status.connected {
background-color: #d4edda;
color: #155724;
}
.status.disconnected {
background-color: #f8d7da;
color: #721c24;
}
select {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>Printer Bridge HTTP API Test</h1>
<div id="connectionStatus" class="status disconnected">
<strong>Status:</strong> Checking connection...
</div>
<!-- Test 1: Get Printers -->
<div class="test-section">
<h2>1. Get Available Printers</h2>
<button onclick="getPrinters()">Get Printers List</button>
<div id="printers-response" class="response"></div>
</div>
<!-- Test 2: Get Settings -->
<div class="test-section">
<h2>2. Get Current Settings</h2>
<button class="secondary" onclick="getSettings()">Get Settings</button>
<div id="settings-response" class="response"></div>
</div>
<!-- Test 3: Print Simple Text -->
<div class="test-section">
<h2>3. Print Simple Text</h2>
<textarea id="simple-text" rows="3" placeholder="Enter text to print...">Hello from Lakasir Printer Bridge!</textarea>
<button class="success" onclick="printSimpleText()">Print Text</button>
<div id="print-simple-response" class="response"></div>
</div>
<!-- Test 4: Print Receipt -->
<div class="test-section">
<h2>4. Print Sample Receipt</h2>
<button class="success" onclick="printReceipt()">Print Receipt</button>
<div id="print-receipt-response" class="response"></div>
</div>
<!-- Test 5: Print Custom Items -->
<div class="test-section">
<h2>5. Print Custom Items (Array)</h2>
<textarea id="custom-items" rows="6" placeholder="Enter items (one per line)...">Item 1: Coffee - $3.50
Item 2: Tea - $2.50
Item 3: Cake - $5.00
========================
Total: $11.00</textarea>
<button class="success" onclick="printCustomItems()">Print Items</button>
<div id="print-custom-response" class="response"></div>
</div>
<!-- Test 6: Print Image from URL -->
<div class="test-section">
<h2>6. Print Image from URL</h2>
<input type="text" id="image-url" placeholder="Enter image URL..." value="https://via.placeholder.com/150" style="width: 100%; padding: 8px; margin-bottom: 10px; border: 1px solid #ddd; border-radius: 4px;">
<button class="success" onclick="printImageUrl()">Print Image URL</button>
<div id="print-image-url-response" class="response"></div>
</div>
<!-- Test 7: Print Image from File -->
<div class="test-section">
<h2>7. Print Image from File (Base64)</h2>
<input type="file" id="image-file" accept="image/*" style="margin-bottom: 10px;">
<button class="success" onclick="printImageFile()">Print Image File</button>
<div id="print-image-file-response" class="response"></div>
</div>
<!-- Test 8: Print JSON Data -->
<div class="test-section">
<h2>8. Test with Raw JSON</h2>
<textarea id="raw-json" rows="8" placeholder="Enter JSON data...">{
"text": "Header Text",
"items": [
"Line 1",
"Line 2",
"Line 3"
]
}</textarea>
<button class="success" onclick="printRawJSON()">Send JSON</button>
<div id="print-json-response" class="response"></div>
</div>
</div>
<script>
const API_URL = 'http://localhost:5463';
// Check connection status on load
window.addEventListener('load', checkConnection);
async function checkConnection() {
const statusDiv = document.getElementById('connectionStatus');
try {
const response = await fetch(`${API_URL}/settings`);
if (response.ok) {
statusDiv.className = 'status connected';
statusDiv.innerHTML = '<strong>Status:</strong> Connected to Printer Bridge ✓';
} else {
statusDiv.className = 'status disconnected';
statusDiv.innerHTML = '<strong>Status:</strong> Server error';
}
} catch (error) {
statusDiv.className = 'status disconnected';
statusDiv.innerHTML = '<strong>Status:</strong> Not connected. Make sure the app is running (npm start)';
}
}
function showResponse(elementId, data, isError = false) {
const element = document.getElementById(elementId);
element.className = isError ? 'response error show' : 'response success show';
element.textContent = typeof data === 'string' ? data : JSON.stringify(data, null, 2);
}
async function getPrinters() {
try {
const response = await fetch(`${API_URL}/printers`);
const data = await response.json();
showResponse('printers-response', data);
} catch (error) {
showResponse('printers-response', `Error: ${error.message}`, true);
}
}
async function getSettings() {
try {
const response = await fetch(`${API_URL}/settings`);
const data = await response.json();
showResponse('settings-response', data);
} catch (error) {
showResponse('settings-response', `Error: ${error.message}`, true);
}
}
async function printSimpleText() {
try {
const text = document.getElementById('simple-text').value;
const response = await fetch(`${API_URL}/print`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text })
});
const data = await response.json();
if (response.ok) {
showResponse('print-simple-response', data);
} else {
showResponse('print-simple-response', data, true);
}
} catch (error) {
showResponse('print-simple-response', `Error: ${error.message}`, true);
}
}
// Receipt text formatter
function createFormatter(paperWidth = 32) {
const repeat = (char) => char.repeat(paperWidth);
const center = (text) => {
const len = text.length;
if (len >= paperWidth) return text.slice(0, paperWidth);
const spaces = Math.floor((paperWidth - len) / 2);
return " ".repeat(spaces) + text;
};
const table2 = (left, right) => {
const total = paperWidth;
const leftText = left.slice(0, total);
const spaceCount = total - leftText.length - right.length;
const spaces = spaceCount > 0 ? " ".repeat(spaceCount) : "";
return `${leftText}${spaces}${right}`;
};
const table3 = (col1, col2, col3, widths = [6, 18, 8]) => {
// widths must sum to paperWidth
const totalWidth = widths.reduce((a, b) => a + b, 0);
if (totalWidth !== paperWidth) {
throw new Error(`Sum of column widths (${totalWidth}) must equal paperWidth (${paperWidth})`);
}
const c1 = col1.toString().padEnd(widths[0]).slice(0, widths[0]);
const c2 = col2.toString().padEnd(widths[1]).slice(0, widths[1]);
const c3 = col3.toString().padStart(widths[2]).slice(0, widths[2]);
return `${c1}${c2}${c3}`;
};
return { repeat, center, table2, table3 };
}
async function printReceipt() {
try {
const settingsResponse = await fetch(`${API_URL}/settings`);
const settings = await settingsResponse.json();
const paperWidth = settings.paperWidth || 32;
const f = createFormatter(paperWidth);
const separator = f.repeat("=");
const dashedLine = f.repeat("-");
const items = [
{ name: "Coffee", qty: 1, price: 3.5 },
{ name: "Donut", qty: 2, price: 4.0 },
{ name: "Sandwich", qty: 1, price: 6.5 },
];
let lines = [];
lines.push(f.center("LAKASIR POS"));
lines.push(separator);
lines.push(f.table2("Date:", new Date().toLocaleString()));
lines.push("");
lines.push(f.center("ITEMS"));
lines.push(dashedLine);
lines.push(f.table3("QTY", "ITEM", "PRICE"));
let subtotal = 0;
items.forEach((item) => {
const total = item.qty * item.price;
subtotal += total;
lines.push(f.table3(item.qty, item.name, `$${total.toFixed(2)}`));
});
const tax = subtotal * 0.1;
const total = subtotal + tax;
lines.push(dashedLine);
lines.push(f.table2("Subtotal:", `$${subtotal.toFixed(2)}`));
lines.push(f.table2("Tax (10%):", `$${tax.toFixed(2)}`));
lines.push(dashedLine);
lines.push(f.table2("TOTAL:", `$${total.toFixed(2)}`));
lines.push(separator);
lines.push(f.center("Thank you for visiting!"));
lines.push(separator);
const receiptData = { text: separator, items: lines };
const response = await fetch(`${API_URL}/print`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(receiptData),
});
const data = await response.json();
if (response.ok) {
showResponse("print-receipt-response", data);
} else {
showResponse("print-receipt-response", data, true);
}
} catch (error) {
showResponse("print-receipt-response", `Error: ${error.message}`, true);
}
}
async function printCustomItems() {
try {
const text = document.getElementById('custom-items').value;
const items = text.split('\n').filter(line => line.trim());
const response = await fetch(`${API_URL}/print`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ items })
});
const data = await response.json();
if (response.ok) {
showResponse('print-custom-response', data);
} else {
showResponse('print-custom-response', data, true);
}
} catch (error) {
showResponse('print-custom-response', `Error: ${error.message}`, true);
}
}
async function printImageUrl() {
try {
const imageUrl = document.getElementById('image-url').value;
if (!imageUrl) {
showResponse('print-image-url-response', 'Please enter an image URL', true);
return;
}
const response = await fetch(`${API_URL}/print`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text: "Image from URL:",
imageUrl
})
});
const data = await response.json();
if (response.ok) {
showResponse('print-image-url-response', data);
} else {
showResponse('print-image-url-response', data, true);
}
} catch (error) {
showResponse('print-image-url-response', `Error: ${error.message}`, true);
}
}
async function printImageFile() {
try {
const fileInput = document.getElementById('image-file');
const file = fileInput.files[0];
if (!file) {
showResponse('print-image-file-response', 'Please select an image file', true);
return;
}
const reader = new FileReader();
reader.onload = async function(e) {
try {
const base64Image = e.target.result.split(',')[1];
const response = await fetch(`${API_URL}/print`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text: `Image from file: ${file.name}`,
image: base64Image
})
});
const data = await response.json();
if (response.ok) {
showResponse('print-image-file-response', data);
} else {
showResponse('print-image-file-response', data, true);
}
} catch (error) {
showResponse('print-image-file-response', `Error: ${error.message}`, true);
}
};
reader.readAsDataURL(file);
} catch (error) {
showResponse('print-image-file-response', `Error: ${error.message}`, true);
}
}
async function printRawJSON() {
try {
const jsonText = document.getElementById('raw-json').value;
const jsonData = JSON.parse(jsonText);
const response = await fetch(`${API_URL}/print`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(jsonData)
});
const data = await response.json();
if (response.ok) {
showResponse('print-json-response', data);
} else {
showResponse('print-json-response', data, true);
}
} catch (error) {
showResponse('print-json-response', `Error: ${error.message}`, true);
}
}
</script>
</body>
</html>