-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.ts
362 lines (313 loc) · 10 KB
/
helper.ts
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
import {
Ecard250,
EmitEkt250TransformStream,
Mtr4TransformStream,
serialOptions250,
serialOptionsMtr4,
EcardMtr,
MtrStatusMessage,
PackageType,
EmitEscanUnpacker,
UsbFrame,
} from "@mikaello/emit-punch-cards-communication";
import { SerialPort, SerialOptions } from "./serial-types";
import { getStatusCommand } from "@mikaello/emit-punch-cards-communication/dist/mtr4-commands";
import {
createUSBCommand,
USBCommand,
} from "@mikaello/emit-punch-cards-communication/dist/escan-commands";
let port250: SerialPort | null = null;
let inputDone250: Promise<void> | null = null;
let reader250: ReadableStreamReader<Ecard250> | null = null;
let portMtr4: SerialPort | null = null;
let inputDoneMtr4: Promise<void> | null = null;
let readerMtr4: ReadableStreamReader<EcardMtr | MtrStatusMessage> | null = null;
let escanUsbDevice: USBDevice | null = null;
const ESCAN_USB_INTERFACE = 0;
const ESCAN_USB_CONFIGURATION = 1;
const ESCAN_USB_BULK_ENDPOINT = 3;
const disconnectEventListener = async () => await disconnectEscan();
export const connect250 = async () => {
try {
port250 = await navigator.serial.requestPort({
/* filters: [
{
usbVendorId: 0x0403, // FTDI
usbProductId: 0x6001,
},
],*/
});
console.log("250 port acquired", port250);
await port250.open(<SerialOptions>serialOptions250);
toggleHtmlElementsWithId(["connect-device-250", "disconnect-device-250"]);
let emitTransformer = new EmitEkt250TransformStream(false);
inputDone250 = port250.readable.pipeTo(emitTransformer.writable);
reader250 = emitTransformer.readable.getReader();
while (true) {
const { value, done } = await reader250.read();
if (done) {
console.log("[readLoop] DONE", done);
break;
}
if (value) {
console.log("250 value", value);
appendCardToList(value);
}
}
reader250.releaseLock();
} catch (e) {
console.error("250 serial", e);
// Permission to access a device was denied implicitly or explicitly by the user.
}
};
/**
* Credits to the web-serial Codelab: https://codelabs.developers.google.com/codelabs/web-serial/#0
*/
export const disconnect250 = async () => {
if (reader250 && inputDone250 && port250) {
await reader250.cancel();
await inputDone250.catch(() => {});
reader250 = null;
inputDone250 = null;
await port250?.close();
port250 = null;
toggleHtmlElementsWithId(["connect-device-250", "disconnect-device-250"]);
} else {
console.error(
"something is not defined when disconnecting 250 (reader/inputDone/port)",
reader250,
inputDone250,
port250,
);
}
};
/** @see https://developers.google.com/web/updates/2016/03/access-usb-devices-on-the-web */
export const connectEscan = async () => {
try {
escanUsbDevice = await navigator.usb.requestDevice({
filters: [
{
vendorId: 0x2047, // EMIT
productId: 0x300, // eScan
},
],
});
console.log("eScan acquired", escanUsbDevice);
await escanUsbDevice.open();
toggleHtmlElementsWithId([
"connect-device-escan",
"disconnect-device-escan",
]);
navigator.usb.addEventListener("disconnect", disconnectEventListener);
if (escanUsbDevice.configuration === null) {
await escanUsbDevice.selectConfiguration(ESCAN_USB_CONFIGURATION);
}
await escanUsbDevice.claimInterface(ESCAN_USB_INTERFACE);
await escanUsbDevice.transferOut(
ESCAN_USB_BULK_ENDPOINT,
createUSBCommand(USBCommand.GREEN),
);
const unpack = new EmitEscanUnpacker();
unpack.onChunk = (chunk: UsbFrame) => {
if (chunk.productName != null) {
addLastEscanStatus(chunk);
}
};
// const pieces = [];
while (true) {
const result = await escanUsbDevice.transferIn(
ESCAN_USB_BULK_ENDPOINT,
64,
);
if (result.status === "stall") {
console.warn("Endpoint stalled. Clearing.");
await escanUsbDevice.clearHalt("in", ESCAN_USB_BULK_ENDPOINT);
} else {
const buff = new Uint8Array(result.data.buffer);
unpack.addBinaryData(buff);
}
/*
if (result.status === "ok") {
if (
buff.length === 3 &&
buff[0] == 3 &&
buff[1] == 13 &&
buff[2] == 10
) {
pieces.push(result.data.buffer);
console.log(
"Bytes",
pieces.map((p) => new Uint8Array(p).toString()),
);
console.log(decoder.decode(concatArrayBuffers(pieces)));
pieces = [];
} else {
pieces.push(result.data.buffer);
}
}*/
/*
console.log(
"Received: ",
decoder.decode(result.data).padEnd(15),
"Bytes: <" + (new Uint8Array(result.data.buffer)).toString() + ">",
);
*/
/*
*/
}
} catch (e) {
console.error("eScan Web USB", e);
// Permission to access a device was denied implicitly or explicitly by the user.
}
};
/**
* Credits to the web-serial Codelab: https://codelabs.developers.google.com/codelabs/web-serial/#0
*/
export const disconnectEscan = async () => {
if (escanUsbDevice) {
try {
await escanUsbDevice.close();
} catch {
console.log("eScan already closed when attempting.");
}
escanUsbDevice = null;
toggleHtmlElementsWithId([
"connect-device-escan",
"disconnect-device-escan",
]);
} else {
console.error(
"something is not defined when disconnecting eScan",
escanUsbDevice,
);
}
navigator.usb.removeEventListener("disconnect", disconnectEventListener);
};
export const connectMtr4 = async () => {
try {
portMtr4 = await navigator.serial.requestPort({
/* filters: [
{
vendorId: 0x0403, // FTDI
productId: 0x6001,
},
],*/
});
console.log("MTR4 port acquired", portMtr4);
await portMtr4.open(<SerialOptions>serialOptionsMtr4);
toggleHtmlElementsWithId(["connect-device-mtr4", "disconnect-device-mtr4"]);
let emitTransformer = new Mtr4TransformStream();
inputDoneMtr4 = portMtr4.readable.pipeTo(emitTransformer.writable);
readerMtr4 = emitTransformer.readable.getReader();
let ts = new TransformStream();
ts.readable.pipeTo(portMtr4.writable);
let writer = ts.writable.getWriter();
writer.write(getStatusCommand());
writer.releaseLock();
while (true) {
const { value, done } = await readerMtr4.read();
if (done) {
console.log("[readLoop] DONE", done);
break;
}
if (value) {
console.log("MTR4 value", value);
if (value.packageType === PackageType.EcardMtr) {
appendCardToList(value);
}
}
}
readerMtr4.releaseLock();
} catch (e) {
console.error("MTR4 serial", e);
// Permission to access a device was denied implicitly or explicitly by the user.
}
};
/**
* Credits to the web-serial Codelab: https://codelabs.developers.google.com/codelabs/web-serial/#0
*/
export const disconnectMtr4 = async () => {
if (readerMtr4 && inputDoneMtr4 && portMtr4) {
await readerMtr4.cancel();
await inputDoneMtr4.catch(() => {});
readerMtr4 = null;
inputDoneMtr4 = null;
await portMtr4?.close();
portMtr4 = null;
toggleHtmlElementsWithId(["connect-device-mtr4", "disconnect-device-mtr4"]);
} else {
console.error(
"something is not defined when disconnecting MTR4 (reader/inputDone/port)",
readerMtr4,
inputDoneMtr4,
portMtr4,
);
}
};
const appendCardToList = (ecard: Ecard250 | EcardMtr) => {
const cardId = document.createElement("td");
cardId.innerText = ecard.ecardNumber + "";
const cardYear = document.createElement("td");
cardYear.innerText = ecard.ecardProductionYear + "";
const cardWeek = document.createElement("td");
cardWeek.innerText = ecard.ecardProductionWeek + "";
const cardRow = document.createElement("tr");
cardRow.append(cardId, cardYear, cardWeek);
ecard.controlCodes.forEach(({ code }) => {
const control = document.createElement("td");
control.innerText = code + "";
cardRow.append(control);
});
const tableBody = document.getElementById("emit-card-list-body");
tableBody.prepend(cardRow);
};
const addLastEscanStatus = (frame: UsbFrame) => {
const createLi = (dt: string, dd: string) => {
const dtEl = document.createElement("dt");
const ddEl = document.createElement("dd");
dtEl.innerText = dt;
ddEl.innerText = dd;
return [dtEl, ddEl];
};
const status = document.createElement("dl");
status.append(...createLi("Name", frame.productName));
status.append(...createLi("HW", frame.hardwareVersion));
status.append(...createLi("SW", frame.softwareVersion));
status.append(...createLi("Version", frame.version));
status.append(...createLi("eLine code", frame.elineCode));
status.append(...createLi("Serial number", frame.serialNumber));
status.append(...createLi("Date", frame.eScanISODate));
status.append(...createLi("Time", frame.timeMilliseconds));
const lastEscanStatus = document.getElementById("last-escan-status");
lastEscanStatus.innerHTML = status.outerHTML;
};
const toggleHtmlElementsWithId = (ids: string[]) => {
ids.forEach((id) => {
const element = document.getElementById(id);
if (element.style.display === "none") {
element.style.display = "initial";
} else {
element.style.display = "none";
}
});
};
export function concatArrayBuffers(buffers: ArrayBuffer[]) {
let offset = 0;
let bytes = 0;
buffers.forEach(function (buffer) {
bytes += buffer.byteLength;
});
const mergedBuffer = new ArrayBuffer(bytes);
const store = new Uint8Array(mergedBuffer);
buffers.forEach(function (buffer) {
store.set(new Uint8Array(buffer), offset);
offset += buffer.byteLength;
});
return mergedBuffer;
}
window.connect250 = connect250;
window.connectMtr4 = connectMtr4;
window.connectEscan = connectEscan;
window.disconnect250 = disconnect250;
window.disconnectMtr4 = disconnectMtr4;
window.disconnectEscan = disconnectEscan;