@hardwarebridge/client 1.0.0 โ 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +517 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,517 @@
|
|
|
1
|
+
# ๐ Hardware Bridge Client
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/%40hardwarebridge%2Fclient)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](http://www.typescriptlang.org/)
|
|
6
|
+
|
|
7
|
+
A professional TypeScript client library for connecting to Hardware Bridge WebSocket services. Control hardware devices including printers, serial ports, and USB HID devices with ease.
|
|
8
|
+
|
|
9
|
+
## ๐ Quick Start
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @hardwarebridge/client
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { HardwareBridgeClient } from '@hardwarebridge/client';
|
|
17
|
+
|
|
18
|
+
const client = new HardwareBridgeClient({
|
|
19
|
+
url: 'ws://localhost:8443',
|
|
20
|
+
autoReconnect: true
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
await client.connect();
|
|
24
|
+
const devices = await client.enumerateDevices();
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## ๐ Table of Contents
|
|
28
|
+
|
|
29
|
+
- [Features](#features)
|
|
30
|
+
- [Installation](#installation)
|
|
31
|
+
- [Basic Usage](#basic-usage)
|
|
32
|
+
- [API Reference](#api-reference)
|
|
33
|
+
- [Hardware Support](#hardware-support)
|
|
34
|
+
- [Examples](#examples)
|
|
35
|
+
- [TypeScript Support](#typescript-support)
|
|
36
|
+
- [Error Handling](#error-handling)
|
|
37
|
+
- [Contributing](#contributing)
|
|
38
|
+
- [License](#license)
|
|
39
|
+
|
|
40
|
+
## โจ Features
|
|
41
|
+
|
|
42
|
+
- ๐ **WebSocket Communication** - Real-time connection to Hardware Bridge server
|
|
43
|
+
- ๐จ๏ธ **Printer Support** - ESC/POS, ZPL, EPL protocols
|
|
44
|
+
- ๐ **Serial Port Control** - Full serial communication capabilities
|
|
45
|
+
- ๐ฑ **USB HID Devices** - Human Interface Device communication
|
|
46
|
+
- ๐ฆ **Queue Management** - Built-in job queuing system
|
|
47
|
+
- ๐ **TypeScript Support** - Full type definitions and IntelliSense
|
|
48
|
+
- ๐ **Auto-reconnection** - Robust connection handling
|
|
49
|
+
- ๐ **Device Discovery** - Automatic device enumeration
|
|
50
|
+
- โก **Multi-format Support** - CommonJS, ES Modules, UMD builds
|
|
51
|
+
|
|
52
|
+
## ๐ฆ Installation
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
# npm
|
|
56
|
+
npm install @hardwarebridge/client
|
|
57
|
+
|
|
58
|
+
# yarn
|
|
59
|
+
yarn add @hardwarebridge/client
|
|
60
|
+
|
|
61
|
+
# pnpm
|
|
62
|
+
pnpm add @hardwarebridge/client
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## ๐ง Basic Usage
|
|
66
|
+
|
|
67
|
+
### Connecting to the Server
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { HardwareBridgeClient } from '@hardwarebridge/client';
|
|
71
|
+
|
|
72
|
+
const client = new HardwareBridgeClient({
|
|
73
|
+
url: 'ws://localhost:8443',
|
|
74
|
+
autoReconnect: true,
|
|
75
|
+
reconnectInterval: 5000
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// Connect to the server
|
|
79
|
+
await client.connect();
|
|
80
|
+
console.log('Connected to Hardware Bridge server!');
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Device Discovery
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
// Discover all available devices
|
|
87
|
+
const devices = await client.enumerateDevices();
|
|
88
|
+
console.log(`Found ${devices.length} devices`);
|
|
89
|
+
|
|
90
|
+
devices.forEach(device => {
|
|
91
|
+
console.log(`${device.name} (${device.type}) - ${device.status}`);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// Get detailed information about a specific device
|
|
95
|
+
const printer = await client.getDevice('printer_test1');
|
|
96
|
+
console.log('Printer details:', printer);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Printer Operations
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
// Get printer status
|
|
103
|
+
const status = await client.getPrinterStatus('printer_test1');
|
|
104
|
+
console.log('Printer status:', status.status);
|
|
105
|
+
|
|
106
|
+
// Get printer capabilities
|
|
107
|
+
const capabilities = await client.getPrinterCapabilities('printer_test1');
|
|
108
|
+
console.log('Supported protocols:', capabilities.supportedProtocols);
|
|
109
|
+
|
|
110
|
+
// Print content
|
|
111
|
+
const printResult = await client.print('printer_test1', 'Hello World!', 'raw');
|
|
112
|
+
console.log('Print job:', printResult.jobId);
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Queue Management
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
// Get queue status
|
|
119
|
+
const queueStatus = await client.getQueueStatus();
|
|
120
|
+
console.log('Queue status:', queueStatus);
|
|
121
|
+
|
|
122
|
+
// Get queue jobs
|
|
123
|
+
const jobs = await client.getQueueJobs();
|
|
124
|
+
console.log(`Found ${jobs.length} jobs in queue`);
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## ๐ API Reference
|
|
128
|
+
|
|
129
|
+
### HardwareBridgeClient
|
|
130
|
+
|
|
131
|
+
#### Constructor Options
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
interface ClientOptions {
|
|
135
|
+
url: string; // WebSocket URL
|
|
136
|
+
autoReconnect?: boolean; // Auto-reconnect on disconnect
|
|
137
|
+
reconnectInterval?: number; // Reconnection interval in ms
|
|
138
|
+
timeout?: number; // Request timeout in ms
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
#### Methods
|
|
143
|
+
|
|
144
|
+
### Connection Management
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
// Connect to server
|
|
148
|
+
await client.connect();
|
|
149
|
+
|
|
150
|
+
// Disconnect from server
|
|
151
|
+
await client.disconnect();
|
|
152
|
+
|
|
153
|
+
// Check connection status
|
|
154
|
+
const isConnected = client.isConnected();
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Device Operations
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
// Enumerate all devices
|
|
161
|
+
const devices = await client.enumerateDevices();
|
|
162
|
+
|
|
163
|
+
// Get specific device info
|
|
164
|
+
const device = await client.getDevice(deviceId);
|
|
165
|
+
|
|
166
|
+
// Listen for device events
|
|
167
|
+
client.onDeviceEvent((event) => {
|
|
168
|
+
console.log('Device event:', event);
|
|
169
|
+
});
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Printer Operations
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
// Get printer status
|
|
176
|
+
const status = await client.getPrinterStatus(deviceId);
|
|
177
|
+
|
|
178
|
+
// Get printer capabilities
|
|
179
|
+
const capabilities = await client.getPrinterCapabilities(deviceId);
|
|
180
|
+
|
|
181
|
+
// Print content
|
|
182
|
+
const result = await client.print(deviceId, data, format);
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### Queue Operations
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
// Get queue status
|
|
189
|
+
const status = await client.getQueueStatus();
|
|
190
|
+
|
|
191
|
+
// Get queue jobs
|
|
192
|
+
const jobs = await client.getQueueJobs({
|
|
193
|
+
deviceId?: string,
|
|
194
|
+
status?: string,
|
|
195
|
+
limit?: number
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// Cancel a queue job
|
|
199
|
+
await client.cancelQueueJob(jobId);
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## ๐จ๏ธ Hardware Support
|
|
203
|
+
|
|
204
|
+
### Supported Protocols
|
|
205
|
+
|
|
206
|
+
- **ESC/POS** - Epson Standard Code for Point of Sale
|
|
207
|
+
- **ZPL** - Zebra Programming Language
|
|
208
|
+
- **EPL** - Eltron Programming Language
|
|
209
|
+
- **Raw** - Direct data transmission
|
|
210
|
+
|
|
211
|
+
### Device Types
|
|
212
|
+
|
|
213
|
+
- **Printers** - Thermal, label, receipt printers
|
|
214
|
+
- **Serial Ports** - COM ports, UART communication
|
|
215
|
+
- **USB HID** - Human Interface Devices
|
|
216
|
+
- **Custom Devices** - Extensible device support
|
|
217
|
+
|
|
218
|
+
## ๐ Examples
|
|
219
|
+
|
|
220
|
+
### Complete Printer Example
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
import { HardwareBridgeClient } from '@hardwarebridge/client';
|
|
224
|
+
|
|
225
|
+
async function printReceipt() {
|
|
226
|
+
const client = new HardwareBridgeClient({
|
|
227
|
+
url: 'ws://localhost:8443'
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
try {
|
|
231
|
+
await client.connect();
|
|
232
|
+
|
|
233
|
+
// Find printer
|
|
234
|
+
const devices = await client.enumerateDevices();
|
|
235
|
+
const printer = devices.find(d => d.type === 'printer');
|
|
236
|
+
|
|
237
|
+
if (!printer) {
|
|
238
|
+
throw new Error('No printer found');
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Print receipt in ESC/POS format
|
|
242
|
+
const receipt = `
|
|
243
|
+
RECEIPT #12345
|
|
244
|
+
================
|
|
245
|
+
Date: ${new Date().toLocaleString()}
|
|
246
|
+
|
|
247
|
+
Item Qty Price
|
|
248
|
+
Coffee 2 $5.00
|
|
249
|
+
Sandwich 1 $8.50
|
|
250
|
+
----------------
|
|
251
|
+
Total: $13.50
|
|
252
|
+
|
|
253
|
+
Thank you!
|
|
254
|
+
================
|
|
255
|
+
`;
|
|
256
|
+
|
|
257
|
+
const result = await client.print(printer.id, receipt, 'escpos');
|
|
258
|
+
console.log('Receipt printed:', result.jobId);
|
|
259
|
+
|
|
260
|
+
} catch (error) {
|
|
261
|
+
console.error('Print error:', error);
|
|
262
|
+
} finally {
|
|
263
|
+
await client.disconnect();
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
printReceipt();
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### Serial Port Communication
|
|
271
|
+
|
|
272
|
+
```typescript
|
|
273
|
+
async function serialCommunication() {
|
|
274
|
+
const client = new HardwareBridgeClient({
|
|
275
|
+
url: 'ws://localhost:8443'
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
await client.connect();
|
|
279
|
+
|
|
280
|
+
// Find serial device
|
|
281
|
+
const devices = await client.enumerateDevices();
|
|
282
|
+
const serialDevice = devices.find(d => d.type === 'serial');
|
|
283
|
+
|
|
284
|
+
if (serialDevice) {
|
|
285
|
+
// Open serial port
|
|
286
|
+
await client.openSerialPort(serialDevice.id, {
|
|
287
|
+
baudRate: 9600,
|
|
288
|
+
dataBits: 8,
|
|
289
|
+
parity: 'none',
|
|
290
|
+
stopBits: 1
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
// Send data
|
|
294
|
+
await client.sendSerialData(serialDevice.id, 'Hello Serial!');
|
|
295
|
+
|
|
296
|
+
// Receive data
|
|
297
|
+
const data = await client.receiveSerialData(serialDevice.id);
|
|
298
|
+
console.log('Received:', data);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
await client.disconnect();
|
|
302
|
+
}
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
### Queue Management Example
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
async function queueManagement() {
|
|
309
|
+
const client = new HardwareBridgeClient({
|
|
310
|
+
url: 'ws://localhost:8443'
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
await client.connect();
|
|
314
|
+
|
|
315
|
+
// Get queue status
|
|
316
|
+
const status = await client.getQueueStatus();
|
|
317
|
+
console.log('Queue status:', {
|
|
318
|
+
total: status.totalJobs,
|
|
319
|
+
pending: status.pendingJobs,
|
|
320
|
+
processing: status.processingJobs,
|
|
321
|
+
completed: status.completedJobs
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
// Get recent jobs
|
|
325
|
+
const jobs = await client.getQueueJobs({ limit: 10 });
|
|
326
|
+
jobs.forEach(job => {
|
|
327
|
+
console.log(`${job.id}: ${job.operation} - ${job.status}`);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
await client.disconnect();
|
|
331
|
+
}
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
## ๐จ Print Format Examples
|
|
335
|
+
|
|
336
|
+
### ESC/POS (Receipt Format)
|
|
337
|
+
```typescript
|
|
338
|
+
const escposReceipt = `
|
|
339
|
+
RECEIPT #12345
|
|
340
|
+
================
|
|
341
|
+
Date: ${new Date().toLocaleString()}
|
|
342
|
+
|
|
343
|
+
Item Qty Price
|
|
344
|
+
Coffee 2 $5.00
|
|
345
|
+
Sandwich 1 $8.50
|
|
346
|
+
Cookie 3 $4.50
|
|
347
|
+
----------------
|
|
348
|
+
Total: $18.00
|
|
349
|
+
|
|
350
|
+
Thank you!
|
|
351
|
+
================
|
|
352
|
+
`;
|
|
353
|
+
|
|
354
|
+
await client.print(printerId, escposReceipt, 'escpos');
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
### ZPL (Zebra Programming Language)
|
|
358
|
+
```typescript
|
|
359
|
+
const zplLabel = `
|
|
360
|
+
^XA
|
|
361
|
+
^FO50,30^ADN,36,20^FDHardware Bridge Demo^FS
|
|
362
|
+
^FO50,80^ADN,18,10^FDZPL Format Test^FS
|
|
363
|
+
^FO50,120^BCN,80,Y,N,N^FD123456789^FS
|
|
364
|
+
^XZ
|
|
365
|
+
`;
|
|
366
|
+
|
|
367
|
+
await client.print(printerId, zplLabel, 'zpl');
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
### EPL (Eltron Programming Language)
|
|
371
|
+
```typescript
|
|
372
|
+
const eplLabel = `
|
|
373
|
+
N
|
|
374
|
+
q609
|
|
375
|
+
Q203,26
|
|
376
|
+
A5,26,0,2,1,1,N,"Hardware Bridge Demo"
|
|
377
|
+
A5,56,0,2,1,1,N,"EPL Format Test"
|
|
378
|
+
B5,86,0,1,2,2,100,B,"TEST123456"
|
|
379
|
+
P1
|
|
380
|
+
`;
|
|
381
|
+
|
|
382
|
+
await client.print(printerId, eplLabel, 'epl');
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
## ๐ Error Handling
|
|
386
|
+
|
|
387
|
+
```typescript
|
|
388
|
+
import { HardwareBridgeClient, HardwareBridgeError } from '@hardwarebridge/client';
|
|
389
|
+
|
|
390
|
+
const client = new HardwareBridgeClient({
|
|
391
|
+
url: 'ws://localhost:8443',
|
|
392
|
+
autoReconnect: true
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
// Handle connection errors
|
|
396
|
+
client.onConnectionStateChange((connected) => {
|
|
397
|
+
if (!connected) {
|
|
398
|
+
console.log('Connection lost, will attempt to reconnect...');
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
// Handle device events
|
|
403
|
+
client.onDeviceEvent((event) => {
|
|
404
|
+
console.log('Device event:', event);
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
// Handle errors gracefully
|
|
408
|
+
try {
|
|
409
|
+
await client.connect();
|
|
410
|
+
|
|
411
|
+
// Your code here
|
|
412
|
+
const devices = await client.enumerateDevices();
|
|
413
|
+
|
|
414
|
+
} catch (error) {
|
|
415
|
+
if (error instanceof HardwareBridgeError) {
|
|
416
|
+
console.error('Hardware Bridge Error:', error.message);
|
|
417
|
+
console.error('Error Code:', error.code);
|
|
418
|
+
} else {
|
|
419
|
+
console.error('Unknown Error:', error);
|
|
420
|
+
}
|
|
421
|
+
} finally {
|
|
422
|
+
await client.disconnect();
|
|
423
|
+
}
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
## ๐ Browser Usage
|
|
427
|
+
|
|
428
|
+
```html
|
|
429
|
+
<!DOCTYPE html>
|
|
430
|
+
<html>
|
|
431
|
+
<head>
|
|
432
|
+
<title>Hardware Bridge Demo</title>
|
|
433
|
+
</head>
|
|
434
|
+
<body>
|
|
435
|
+
<h1>Hardware Bridge Client Demo</h1>
|
|
436
|
+
<button id="connect">Connect</button>
|
|
437
|
+
<button id="print">Print Test</button>
|
|
438
|
+
<div id="status">Disconnected</div>
|
|
439
|
+
<div id="devices"></div>
|
|
440
|
+
|
|
441
|
+
<script type="module">
|
|
442
|
+
import { HardwareBridgeClient } from 'https://unpkg.com/@hardwarebridge/client@latest/dist/index.esm.js';
|
|
443
|
+
|
|
444
|
+
const client = new HardwareBridgeClient({
|
|
445
|
+
url: 'ws://localhost:8443'
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
document.getElementById('connect').addEventListener('click', async () => {
|
|
449
|
+
try {
|
|
450
|
+
await client.connect();
|
|
451
|
+
document.getElementById('status').textContent = 'Connected!';
|
|
452
|
+
|
|
453
|
+
const devices = await client.enumerateDevices();
|
|
454
|
+
document.getElementById('devices').innerHTML =
|
|
455
|
+
devices.map(d => `<p>${d.name} (${d.type})</p>`).join('');
|
|
456
|
+
} catch (error) {
|
|
457
|
+
document.getElementById('status').textContent = 'Connection failed: ' + error.message;
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
document.getElementById('print').addEventListener('click', async () => {
|
|
462
|
+
try {
|
|
463
|
+
const devices = await client.enumerateDevices();
|
|
464
|
+
const printer = devices.find(d => d.type === 'printer');
|
|
465
|
+
|
|
466
|
+
if (printer) {
|
|
467
|
+
await client.print(printer.id, 'Hello from Browser!', 'raw');
|
|
468
|
+
alert('Printed successfully!');
|
|
469
|
+
} else {
|
|
470
|
+
alert('No printer found');
|
|
471
|
+
}
|
|
472
|
+
} catch (error) {
|
|
473
|
+
alert('Print failed: ' + error.message);
|
|
474
|
+
}
|
|
475
|
+
});
|
|
476
|
+
</script>
|
|
477
|
+
</body>
|
|
478
|
+
</html>
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
## ๐ ๏ธ Configuration
|
|
482
|
+
|
|
483
|
+
### Server Configuration
|
|
484
|
+
|
|
485
|
+
```typescript
|
|
486
|
+
const client = new HardwareBridgeClient({
|
|
487
|
+
url: 'ws://localhost:8443', // WebSocket URL
|
|
488
|
+
autoReconnect: true, // Auto-reconnect on disconnect
|
|
489
|
+
reconnectInterval: 5000, // Reconnection interval (ms)
|
|
490
|
+
timeout: 10000, // Request timeout (ms)
|
|
491
|
+
maxRetries: 3, // Maximum reconnection attempts
|
|
492
|
+
enableLogging: true // Enable debug logging
|
|
493
|
+
});
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
### Security Configuration
|
|
497
|
+
|
|
498
|
+
```typescript
|
|
499
|
+
const client = new HardwareBridgeClient({
|
|
500
|
+
url: 'wss://secure-server.com:8443', // Use WSS for secure connections
|
|
501
|
+
authToken: 'your-auth-token', // Authentication token
|
|
502
|
+
validateCertificate: true // Validate SSL certificates
|
|
503
|
+
});
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
## ๐งช Testing
|
|
507
|
+
|
|
508
|
+
```bash
|
|
509
|
+
# Run the test applications
|
|
510
|
+
cd src/TestApp
|
|
511
|
+
npm install
|
|
512
|
+
npm start
|
|
513
|
+
|
|
514
|
+
# Or run specific tests
|
|
515
|
+
node simple-test-app.js
|
|
516
|
+
node enhanced-test-app.js
|
|
517
|
+
```
|