Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
104 lines (97 loc) · 3.64 KB
/
Copy pathtest.js
File metadata and controls
104 lines (97 loc) · 3.64 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
'use strict';
const loadStatus = document.getElementById('load-status');
const status = document.getElementById('status');
const initializeButton = document.getElementById('initialize');
const queryButton = document.getElementById('query');
const result = document.getElementById('result');
const resultStatus = document.getElementById('result-status');
const resultValue = document.getElementById('result-value');
const log = document.getElementById('log');
let connected = false;
let hasLog = false;
function appendLog(message) {
const line = new Date().toLocaleTimeString() + ' ' + message;
log.textContent = hasLog ? log.textContent + '\n' + line : line;
hasLog = true;
}
window.onBarcodeEvent = function (event) {
if (!event || typeof event !== 'object') {
appendLog('收到無效的 App 事件');
return;
}
appendLog(JSON.stringify(event));
switch (event.event) {
case 'initializing':
status.textContent = '正在初始化讀頭服務';
break;
case 'waiting':
status.textContent = '等待 ReaderService 連線';
break;
case 'connected':
connected = true;
status.textContent = '已連線,可以查詢讀頭類型';
queryButton.disabled = false;
break;
case 'querying':
status.textContent = '正在查詢讀頭類型';
queryButton.disabled = true;
break;
case 'result': {
const valid = event.ok === true && event.method === 'GetReaderType' &&
typeof event.readerType === 'string' && event.readerType.length > 0 &&
event.readerType !== 'None' && typeof event.readerTypeValue === 'number' &&
Number.isFinite(event.readerTypeValue);
result.hidden = false;
resultStatus.textContent = valid ? '測試通過' : '結果無效,請查看事件紀錄';
resultStatus.className = valid ? 'pass' : 'fail';
resultValue.textContent = JSON.stringify(event, null, 2);
status.textContent = valid ? '已取得讀頭類型' : '查詢結果不完整';
queryButton.disabled = !connected;
break;
}
case 'timeout':
status.textContent = '連線逾時,請查看事件紀錄並確認服務狀態';
break;
case 'error':
case 'blocked':
status.textContent = 'App 回報錯誤,請查看事件紀錄';
queryButton.disabled = !connected;
break;
default:
status.textContent = '收到 App 事件,請查看事件紀錄';
}
};
initializeButton.addEventListener('click', () => {
initializeButton.disabled = true;
status.textContent = '正在要求初始化';
appendLog('呼叫 NativeBarcode.initialize()');
try {
window.NativeBarcode.initialize();
} catch (error) {
status.textContent = '初始化呼叫失敗,請查看事件紀錄';
appendLog(String(error));
}
});
queryButton.addEventListener('click', () => {
if (!connected) return;
queryButton.disabled = true;
result.hidden = true;
status.textContent = '正在要求查詢讀頭類型';
appendLog('呼叫 NativeBarcode.readReaderType()');
try {
window.NativeBarcode.readReaderType();
} catch (error) {
status.textContent = '查詢呼叫失敗,請查看事件紀錄';
appendLog(String(error));
queryButton.disabled = false;
}
});
loadStatus.textContent = 'JavaScript 已載入';
if (window.NativeBarcode && typeof window.NativeBarcode.initialize === 'function' &&
typeof window.NativeBarcode.readReaderType === 'function') {
status.textContent = '已偵測 App 介面,請初始化讀頭';
initializeButton.disabled = false;
} else {
status.textContent = '找不到讀頭介面,請使用支援 NativeBarcode 的 App';
appendLog('未偵測到完整的 NativeBarcode 介面。');
}