-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
190 lines (181 loc) · 5.89 KB
/
index.js
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
let capture, devices;
let decodedData = [];
let deviceMap = {};
let dataId = 10;
let appInfo = {
appId: "web:socketmobile.com.test",
developerId: "289d7d25-20ea-eb11-bacb-000d3a5b57ef",
appKey: "MC4CFQDTXgyqNtrLf0MODRcyn2slp3K4/wIVAMe4pKxYfyqGbvVeqv46nqMcEC/P",
};
const onCaptureEvent = (e, handle) => {
const { CaptureEventIds, Capture } = SocketMobile;
if (!e) {
return;
}
switch (e.id) {
// **********************************
// Device Arrival Event
// a device needs to be opened in
// to receive the decoded data
// e = {
// id: CaptureEventIds.DeviceArrival,
// type: CaptureEventTypes.DeviceInfo,
// value: {
// guid: "b876d9a8-85b6-1bb5-f1f6-1bb5d78a2c6e",
// name: "Socket S740 [E2ABB4]",
// type: CaptureDeviceType.ScannerS740
// }
// }
// **********************************
case CaptureEventIds.DeviceArrival:
const newDevice = new Capture();
const { guid, name } = e.value;
newDevice
.openDevice(guid, capture)
.then((result) => {
updateStatus(`result of opening ${name} : ${result}`);
devices = devices || [];
if (!deviceMap[name]) {
devices.push({
guid,
name,
handle: newDevice.clientOrDeviceHandle,
device: newDevice,
});
deviceMap[name] = true;
}
updateDevices();
})
.catch((err) => {
updateStatus(`error opening a device: ${err}`);
});
break;
// **********************************
// Device Removal Event
// it is better to close the device
// e = {
// id: CaptureEventIds.DeviceRemoval,
// type: CaptureEventTypes.DeviceInfo,
// value: {
// guid: "b876d9a8-85b6-1bb5-f1f6-1bb5d78a2c6e",
// name: "Socket S740 [E2ABB4]",
// type: CaptureDeviceType.ScannerS740
// }
// }
// **********************************
case CaptureEventIds.DeviceRemoval:
const removeDevice = devices.find((d) => d.guid === e.value.guid);
if (!removeDevice) {
updateStatus(`no matching devices found for ${e.value.name}`);
return;
}
removeDevice.device
.close()
.then((result) => {
updateStatus(`result of closing ${removeDevice.name}: ${result}`);
devices = devices.filter((x) => x.guid !== removeDevice.guid);
updateDevices();
})
.catch((err) => {
updateStatus(`error closing a device: ${err}`, err);
});
break;
// **********************************
// Decoded Data
// receive the decoded data from
// a specific device
// e = {
// id: CaptureEventIds.DecodedData,
// type: CaptureEventTypes.DecodedData,
// value: {
// data: [55, 97, 100, 57, 53, 100, 97, 98, 48, 102, 102, 99, 52, 53, 57, 48, 97, 52, 57, 54, 49, 97, 51, 49, 57, 50, 99, 49, 102, 51, 53, 55],
// id: CaptureDataSourceID.SymbologyQRCode,
// name: "QR Code"
// }
// }
// **********************************
case CaptureEventIds.DecodedData:
const deviceSource = devices.find((d) => d.handle === handle);
if (!deviceSource) {
updateStatus(`no matching devices found for ${e.value.name}`);
}
decodedData.push({
id: dataId,
name: e.value.name,
data: String.fromCharCode.apply(null, e.value.data),
});
updateData();
break;
}
};
//to ensure the cdn is loaded before you try to use prefix
window.addEventListener("DOMContentLoaded", () => {
capture = new SocketMobile.Capture();
capture
.open(appInfo, onCaptureEvent)
.then((result) => {
console.log("opening Capture result: ", result);
updateStatus(`opening Capture result: ${result}`);
})
.catch((err) => {
var finalErr = err.error || err;
var val;
// error code to watch for to check if the Companion service is running
if (finalErr === SocketMobile.SktErrors.ESKT_UNABLEOPENDEVICE) {
val =
"not able to connect to the service, is it running?" + ` ${finalErr}`;
console.log("no able to connect to the service, is it running?");
} else {
console.log(JSON.stringify(finalErr));
val = `opening Capture error \n ${finalErr.code}: ${finalErr.message}`;
}
console.log(val);
updateStatus(val, finalErr);
});
});
const updateStatus = (val, err) => {
var color = err ? "red" : "green";
document.getElementById("status").innerText = val;
document.getElementById("status").style.color = color;
};
const updateDevices = () => {
var table = document.getElementById("devices");
table.children[0].innerHTML = `<tr>
<th>Name</th>
<th>GUID</th>
</tr>`;
for (var i = 0; i < devices.length; ++i) {
var row = table.insertRow(i + 1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerText = devices[i].name;
cell2.innerText = devices[i].guid;
}
if (devices.length > 0) {
document.getElementById("device-warning").style.visibility = "hidden";
} else {
document.getElementById("device-warning").style.visibility = "visible";
}
};
const onClear = () => {
dataId = 10;
decodedData = [];
document.getElementById("data-list").innerHTML = "";
document.getElementById("decoded-data").value = "";
};
const updateData = () => {
dataId++;
var list = document.getElementById("data-list");
list.innerHTML = "";
for (var i = 0; i < decodedData.length; ++i) {
var l = document.createElement("LI");
l.innerText = `${decodedData[i].name.toUpperCase()} (${
decodedData[i].data.length
}): ${decodedData[i].data}`;
list.appendChild(l);
if (i === decodedData.length - 1) {
//assign scanned value to input
document.getElementById("decoded-data").value = l.innerText;
}
}
};