-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandalone-test.html
More file actions
565 lines (485 loc) · 18.9 KB
/
standalone-test.html
File metadata and controls
565 lines (485 loc) · 18.9 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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TWAIN中间件 - 独立测试页面</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
padding: 30px;
}
h1 {
color: #333;
text-align: center;
margin-bottom: 30px;
}
.status-bar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px;
border-radius: 5px;
margin-bottom: 20px;
font-weight: bold;
}
.status-bar.connected {
background-color: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.status-bar.disconnected {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.button-group {
display: flex;
gap: 10px;
flex-wrap: wrap;
margin-bottom: 20px;
}
.btn {
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
font-weight: bold;
transition: all 0.3s ease;
}
.btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.btn-primary {
background-color: #007bff;
color: white;
}
.btn-primary:hover:not(:disabled) {
background-color: #0056b3;
}
.btn-success {
background-color: #28a745;
color: white;
}
.btn-success:hover:not(:disabled) {
background-color: #1e7e34;
}
.log-container {
background-color: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 5px;
padding: 15px;
height: 300px;
overflow-y: auto;
font-family: monospace;
font-size: 12px;
line-height: 1.4;
}
.log-entry {
margin-bottom: 5px;
padding: 2px 5px;
border-radius: 3px;
}
.log-info {
background-color: #e3f2fd;
color: #1976d2;
}
.log-success {
background-color: #e8f5e8;
color: #2e7d32;
}
.log-error {
background-color: #ffebee;
color: #c62828;
}
.log-warning {
background-color: #fff3e0;
color: #f57c00;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
font-weight: bold;
margin-bottom: 5px;
}
.form-group select,
.form-group input {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
}
</style>
</head>
<body>
<div class="container">
<h1>🖨️ TWAIN中间件 - 独立测试页面</h1>
<div class="status-bar disconnected" id="statusBar">
<span id="statusText">❌ 未连接</span>
<span id="statusTime"></span>
</div>
<div class="button-group">
<button class="btn btn-primary" id="connectBtn">🔗 连接中间件</button>
<button class="btn btn-primary" id="disconnectBtn" disabled>🔌 断开连接</button>
<button class="btn btn-success" id="getPrintersBtn" disabled>📋 获取打印机列表</button>
<button class="btn btn-success" id="clearLogBtn">🗑️ 清空日志</button>
</div>
<div class="form-group">
<label for="printerSelect">选择打印机:</label>
<select id="printerSelect" disabled>
<option value="">请先连接中间件</option>
</select>
</div>
<div id="printerDetails"></div>
<div class="log-container" id="logContainer"></div>
</div>
<script>
/**
* TWAIN扫描仪中间件 JavaScript SDK (内嵌版本)
* 版本: 1.2.0
*/
class TwainMiddlewareSDK {
constructor(options = {}) {
this.options = {
host: options.host || 'localhost',
port: options.port || 45677,
autoReconnect: options.autoReconnect || true,
reconnectInterval: options.reconnectInterval || 3000,
maxReconnectAttempts: options.maxReconnectAttempts || 3,
debug: options.debug || false
};
this.ws = null;
this.isConnected = false;
this.reconnectAttempts = 0;
this.requestCallbacks = new Map();
this.eventListeners = new Map();
this.requestId = 0;
// 初始化事件监听器映射
this.eventListeners.set('connected', []);
this.eventListeners.set('disconnected', []);
this.eventListeners.set('error', []);
this.eventListeners.set('printersUpdated', []);
}
/**
* 连接到中间件服务
*/
connect() {
return new Promise((resolve, reject) => {
const wsUrl = `ws://${this.options.host}:${this.options.port}/twain`;
this.log('正在连接到:', wsUrl);
try {
this.ws = new WebSocket(wsUrl);
this.ws.onopen = () => {
this.isConnected = true;
this.reconnectAttempts = 0;
this.log('WebSocket连接已建立');
this.emit('connected');
resolve();
};
this.ws.onmessage = (event) => {
this.handleMessage(event.data);
};
this.ws.onclose = (event) => {
this.isConnected = false;
this.log('WebSocket连接已关闭:', event.code, event.reason);
this.emit('disconnected', { code: event.code, reason: event.reason });
};
this.ws.onerror = (error) => {
this.log('WebSocket错误:', error);
this.emit('error', error);
if (!this.isConnected) {
reject(error);
}
};
} catch (error) {
this.log('连接失败:', error);
reject(error);
}
});
}
/**
* 断开连接
*/
disconnect() {
if (this.ws && this.isConnected) {
this.ws.close();
}
}
/**
* 发送命令到中间件
*/
sendCommand(action, data = null) {
return new Promise((resolve, reject) => {
if (!this.isConnected) {
reject(new Error('WebSocket未连接'));
return;
}
const requestId = `req-${++this.requestId}`;
const command = {
Id: requestId,
Action: action,
Data: data
};
// 存储回调函数
this.requestCallbacks.set(requestId, { resolve, reject });
// 设置超时
setTimeout(() => {
if (this.requestCallbacks.has(requestId)) {
this.requestCallbacks.delete(requestId);
reject(new Error('请求超时'));
}
}, 10000);
// 发送命令
try {
this.ws.send(JSON.stringify(command));
this.log('发送命令:', command);
} catch (error) {
this.requestCallbacks.delete(requestId);
reject(error);
}
});
}
/**
* 获取系统中的真实打印机列表
*/
async getPrinters() {
try {
const response = await this.sendCommand('getprinters');
this.emit('printersUpdated', response.Data);
return response.Data || [];
} catch (error) {
this.log('获取打印机列表失败:', error);
throw error;
}
}
/**
* 处理服务器消息
*/
handleMessage(data) {
try {
const message = JSON.parse(data);
this.log('收到消息:', message);
// 处理请求响应
if (message.Id && this.requestCallbacks.has(message.Id)) {
const callback = this.requestCallbacks.get(message.Id);
this.requestCallbacks.delete(message.Id);
if (message.Success) {
callback.resolve(message);
} else {
callback.reject(new Error(message.Message || '未知错误'));
}
}
} catch (error) {
this.log('处理消息失败:', error);
}
}
/**
* 添加事件监听器
*/
on(event, callback) {
if (!this.eventListeners.has(event)) {
this.eventListeners.set(event, []);
}
this.eventListeners.get(event).push(callback);
}
/**
* 触发事件
*/
emit(event, data) {
if (this.eventListeners.has(event)) {
this.eventListeners.get(event).forEach(callback => {
try {
callback(data);
} catch (error) {
this.log('事件回调错误:', error);
}
});
}
}
/**
* 获取连接状态
*/
getConnectionState() {
return this.isConnected;
}
/**
* 日志输出
*/
log(...args) {
if (this.options.debug) {
console.log('[TwainSDK]', ...args);
}
}
}
// 主程序
let twainSDK = null;
let isConnected = false;
// DOM元素
const elements = {
statusBar: document.getElementById('statusBar'),
statusText: document.getElementById('statusText'),
statusTime: document.getElementById('statusTime'),
connectBtn: document.getElementById('connectBtn'),
disconnectBtn: document.getElementById('disconnectBtn'),
getPrintersBtn: document.getElementById('getPrintersBtn'),
clearLogBtn: document.getElementById('clearLogBtn'),
printerSelect: document.getElementById('printerSelect'),
printerDetails: document.getElementById('printerDetails'),
logContainer: document.getElementById('logContainer')
};
// 初始化
document.addEventListener('DOMContentLoaded', function() {
initSDK();
bindEvents();
updateUI(false);
addLog('🚀 页面已加载,可以开始测试', 'info');
});
function initSDK() {
try {
twainSDK = new TwainMiddlewareSDK({
host: 'localhost',
port: 45677,
debug: true
});
twainSDK.on('connected', () => {
addLog('✅ WebSocket连接已建立', 'success');
updateUI(true);
// 自动获取打印机列表
setTimeout(() => {
getPrinters();
}, 500);
});
twainSDK.on('disconnected', (data) => {
addLog(`⚠️ WebSocket连接已断开 (代码: ${data.code})`, 'warning');
updateUI(false);
});
twainSDK.on('error', (error) => {
addLog(`❌ WebSocket错误: ${error.message || error}`, 'error');
});
twainSDK.on('printersUpdated', (printers) => {
addLog(`🖨️ 打印机列表已更新,找到 ${printers.length} 个打印机`, 'info');
});
addLog('✅ SDK已初始化完成', 'success');
} catch (error) {
addLog(`❌ SDK初始化失败: ${error.message}`, 'error');
}
}
function bindEvents() {
elements.connectBtn.onclick = connect;
elements.disconnectBtn.onclick = disconnect;
elements.getPrintersBtn.onclick = getPrinters;
elements.clearLogBtn.onclick = clearLog;
}
async function connect() {
try {
addLog('🔄 正在连接到中间件...', 'info');
await twainSDK.connect();
addLog('✅ 连接成功!', 'success');
} catch (error) {
addLog(`❌ 连接失败: ${error.message}`, 'error');
addLog('💡 请确保中间件程序正在运行', 'warning');
}
}
function disconnect() {
twainSDK.disconnect();
addLog('🔌 已断开连接', 'info');
}
async function getPrinters() {
try {
addLog('🔄 正在获取打印机列表...', 'info');
const printers = await twainSDK.getPrinters();
updatePrinterList(printers);
addLog(`✅ 获取打印机列表成功: ${printers.length} 个设备`, 'success');
// 显示详细信息
if (printers.length > 0) {
printers.forEach((printer, index) => {
addLog(` ${index + 1}. ${printer.Name} ${printer.IsDefault ? '(默认)' : ''}`, 'info');
});
}
} catch (error) {
addLog(`❌ 获取打印机列表失败: ${error.message}`, 'error');
}
}
function updatePrinterList(printers) {
elements.printerSelect.innerHTML = '';
if (printers.length === 0) {
elements.printerSelect.innerHTML = '<option value="">未找到打印机</option>';
elements.printerDetails.innerHTML = '';
return;
}
printers.forEach((printer, index) => {
const option = document.createElement('option');
option.value = index;
option.textContent = `${printer.Name}${printer.IsDefault ? ' (默认)' : ''}`;
elements.printerSelect.appendChild(option);
});
// 默认选择第一个打印机
if (printers.length > 0) {
elements.printerSelect.value = '0';
showPrinterDetails(printers[0]);
}
// 添加选择变化事件
elements.printerSelect.onchange = function() {
const selectedIndex = this.value;
if (selectedIndex !== '') {
showPrinterDetails(printers[parseInt(selectedIndex)]);
}
};
}
function showPrinterDetails(printer) {
let details = `
<h3>打印机详细信息</h3>
<p><strong>名称:</strong> ${printer.Name}</p>
<p><strong>是否默认:</strong> ${printer.IsDefault ? '是' : '否'}</p>
<p><strong>状态:</strong> ${printer.Status || '未知'}</p>
<p><strong>端口:</strong> ${printer.PortName || '未知'}</p>
<p><strong>驱动:</strong> ${printer.DriverName || '未知'}</p>
`;
if (printer.IsNetworkPrinter !== undefined) {
details += `<p><strong>网络打印机:</strong> ${printer.IsNetworkPrinter ? '是' : '否'}</p>`;
}
elements.printerDetails.innerHTML = details;
}
function updateUI(connected) {
isConnected = connected && twainSDK.getConnectionState();
elements.statusText.textContent = isConnected ? '✅ 已连接' : '❌ 未连接';
elements.statusTime.textContent = new Date().toLocaleTimeString();
elements.statusBar.className = `status-bar ${isConnected ? 'connected' : 'disconnected'}`;
elements.connectBtn.disabled = isConnected;
elements.disconnectBtn.disabled = !isConnected;
elements.getPrintersBtn.disabled = !isConnected;
elements.printerSelect.disabled = !isConnected;
if (!isConnected) {
elements.printerSelect.innerHTML = '<option value="">请先连接中间件</option>';
elements.printerDetails.innerHTML = '';
}
}
function addLog(message, type = 'info') {
const timestamp = new Date().toLocaleTimeString();
const logEntry = document.createElement('div');
logEntry.className = `log-entry log-${type}`;
logEntry.innerHTML = `[${timestamp}] ${message}`;
elements.logContainer.appendChild(logEntry);
elements.logContainer.scrollTop = elements.logContainer.scrollHeight;
}
function clearLog() {
elements.logContainer.innerHTML = '';
addLog('📝 日志已清空', 'info');
}
</script>
</body>
</html>