webserial-core 1.1.3 → 1.1.5
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 +16 -1
- package/dist/types/Core.d.ts.map +1 -1
- package/dist/types/Devices.d.ts +32 -0
- package/dist/types/Devices.d.ts.map +1 -1
- package/dist/types/Dispatcher.d.ts +63 -0
- package/dist/types/Dispatcher.d.ts.map +1 -1
- package/dist/types/SerialError.d.ts +61 -0
- package/dist/types/SerialError.d.ts.map +1 -0
- package/dist/types/Socket.d.ts +12 -2
- package/dist/types/Socket.d.ts.map +1 -1
- package/dist/types/main.d.ts +1 -0
- package/dist/types/main.d.ts.map +1 -1
- package/dist/webserial-core.js +371 -177
- package/dist/webserial-core.js.map +1 -0
- package/dist/webserial-core.umd.cjs +4 -3
- package/dist/webserial-core.umd.cjs.map +1 -0
- package/package.json +30 -8
package/dist/webserial-core.js
CHANGED
|
@@ -10,31 +10,94 @@ class m extends EventTarget {
|
|
|
10
10
|
};
|
|
11
11
|
__debug__ = !1;
|
|
12
12
|
__listenersCallbacks__ = [];
|
|
13
|
+
/**
|
|
14
|
+
* Dispatches an event with the specified type and data
|
|
15
|
+
* @param type - The event type to dispatch
|
|
16
|
+
* @param data - Optional data to attach to the event
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* dispatcher.dispatch('connected', { port: 'COM3' });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
13
22
|
dispatch(e, t = null) {
|
|
14
|
-
const
|
|
15
|
-
this.dispatchEvent(
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
23
|
+
const n = new g(e, { detail: t });
|
|
24
|
+
this.dispatchEvent(n), this.__debug__ && this.dispatchEvent(new g("debug", { detail: { type: e, data: t } }));
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Dispatches an event asynchronously after a specified delay
|
|
28
|
+
* @param type - The event type to dispatch
|
|
29
|
+
* @param data - Optional data to attach to the event
|
|
30
|
+
* @param ms - Delay in milliseconds (default: 100)
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* dispatcher.dispatchAsync('timeout', { reason: 'no response' }, 500);
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
dispatchAsync(e, t = null, n = 100) {
|
|
37
|
+
const i = this;
|
|
19
38
|
setTimeout(() => {
|
|
20
|
-
|
|
21
|
-
},
|
|
22
|
-
}
|
|
39
|
+
i.dispatch(e, t);
|
|
40
|
+
}, n);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Registers an event listener for the specified event type
|
|
44
|
+
* @param type - The event type to listen to
|
|
45
|
+
* @param callback - The callback function to execute when the event is triggered
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* dispatcher.on('connected', (event) => {
|
|
49
|
+
* console.log('Device connected', event.detail);
|
|
50
|
+
* });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
23
53
|
on(e, t) {
|
|
24
54
|
typeof this.__listeners__[e] < "u" && !this.__listeners__[e] && (this.__listeners__[e] = !0), this.__listenersCallbacks__.push({ key: e, callback: t }), this.addEventListener(e, t);
|
|
25
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Removes an event listener for the specified event type
|
|
58
|
+
* @param type - The event type to stop listening to
|
|
59
|
+
* @param callback - The callback function to remove
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const handler = (event) => console.log(event.detail);
|
|
63
|
+
* dispatcher.on('data', handler);
|
|
64
|
+
* dispatcher.off('data', handler);
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
26
67
|
off(e, t) {
|
|
27
|
-
this.__listenersCallbacks__ = this.__listenersCallbacks__.filter((
|
|
68
|
+
this.__listenersCallbacks__ = this.__listenersCallbacks__.filter((n) => !(n.key === e && n.callback === t)), this.removeEventListener(e, t);
|
|
28
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Registers an available listener type for tracking
|
|
72
|
+
* @param type - The event type to register
|
|
73
|
+
* @internal
|
|
74
|
+
*/
|
|
29
75
|
serialRegisterAvailableListener(e) {
|
|
30
76
|
this.__listeners__[e] || (this.__listeners__[e] = !1);
|
|
31
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Gets the list of all available listeners and their state
|
|
80
|
+
* @returns Array of listener objects with type and listening status
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const listeners = dispatcher.availableListeners;
|
|
84
|
+
* console.log(listeners); // [{ type: 'connected', listening: true }, ...]
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
32
87
|
get availableListeners() {
|
|
33
88
|
return Object.keys(this.__listeners__).sort().map((t) => ({
|
|
34
89
|
type: t,
|
|
35
90
|
listening: this.__listeners__[t]
|
|
36
91
|
}));
|
|
37
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Removes all event listeners except internal ones (like queue listeners)
|
|
95
|
+
* Resets all listener states to false
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* dispatcher.removeAllListeners();
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
38
101
|
removeAllListeners() {
|
|
39
102
|
for (const e of this.__listenersCallbacks__)
|
|
40
103
|
["internal:queue"].includes(e.key) || (this.__listenersCallbacks__ = this.__listenersCallbacks__.filter((t) => !(t.key === e.key && t.callback === e.callback)), this.removeEventListener(e.key, e.callback));
|
|
@@ -57,17 +120,44 @@ class s extends m {
|
|
|
57
120
|
const t = new Error();
|
|
58
121
|
throw t.message = `Type ${e} is not supported`, t.name = "DeviceTypeError", t;
|
|
59
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Registers a new device type in the registry
|
|
125
|
+
* @param type - The type name of the device (e.g., 'arduino', 'esp32')
|
|
126
|
+
* @internal
|
|
127
|
+
*/
|
|
60
128
|
static registerType(e) {
|
|
61
129
|
typeof s.devices[e] > "u" && (s.devices = { ...s.devices, [e]: {} });
|
|
62
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Adds a device to the registry
|
|
133
|
+
* @param device - The Core device instance to add
|
|
134
|
+
* @returns The index of the device in its type registry
|
|
135
|
+
* @throws {Error} If device with the same ID already exists
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const arduino = new Arduino();
|
|
139
|
+
* Devices.add(arduino);
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
63
142
|
static add(e) {
|
|
64
143
|
const t = e.typeDevice;
|
|
65
144
|
typeof s.devices[t] > "u" && s.registerType(t);
|
|
66
|
-
const
|
|
67
|
-
if (typeof s.devices[t] > "u" && s.typeError(t), s.devices[t][
|
|
68
|
-
throw new Error(`Device with id ${
|
|
69
|
-
return s.devices[t][
|
|
70
|
-
}
|
|
145
|
+
const n = e.uuid;
|
|
146
|
+
if (typeof s.devices[t] > "u" && s.typeError(t), s.devices[t][n])
|
|
147
|
+
throw new Error(`Device with id ${n} already exists`);
|
|
148
|
+
return s.devices[t][n] = e, s.$dispatchChange(e), Object.keys(s.devices[t]).indexOf(n);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Gets a specific device by type and UUID
|
|
152
|
+
* @param type - The device type
|
|
153
|
+
* @param id - The device UUID
|
|
154
|
+
* @returns The device instance
|
|
155
|
+
* @throws {Error} If the device type is not supported
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* const device = Devices.get('arduino', 'uuid-123');
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
71
161
|
static get(e, t) {
|
|
72
162
|
return typeof s.devices[e] > "u" && s.registerType(e), typeof s.devices[e] > "u" && s.typeError(e), s.devices[e][t];
|
|
73
163
|
}
|
|
@@ -78,10 +168,10 @@ class s extends m {
|
|
|
78
168
|
return Object.values(s.devices).map((t) => Object.values(t)).flat();
|
|
79
169
|
}
|
|
80
170
|
static getByNumber(e, t) {
|
|
81
|
-
return typeof s.devices[e] > "u" && s.typeError(e), Object.values(s.devices[e]).find((
|
|
171
|
+
return typeof s.devices[e] > "u" && s.typeError(e), Object.values(s.devices[e]).find((i) => i.deviceNumber === t) ?? null;
|
|
82
172
|
}
|
|
83
173
|
static getCustom(e, t = 1) {
|
|
84
|
-
return typeof s.devices[e] > "u" && s.typeError(e), Object.values(s.devices[e]).find((
|
|
174
|
+
return typeof s.devices[e] > "u" && s.typeError(e), Object.values(s.devices[e]).find((i) => i.deviceNumber === t) ?? null;
|
|
85
175
|
}
|
|
86
176
|
static async connectToAll() {
|
|
87
177
|
const e = s.getList();
|
|
@@ -117,63 +207,97 @@ class s extends m {
|
|
|
117
207
|
}
|
|
118
208
|
}
|
|
119
209
|
s.instance || (s.instance = new s());
|
|
120
|
-
function y(
|
|
210
|
+
function y(o = 100) {
|
|
121
211
|
return new Promise(
|
|
122
|
-
(e) => setTimeout(() => e(),
|
|
212
|
+
(e) => setTimeout(() => e(), o)
|
|
123
213
|
);
|
|
124
214
|
}
|
|
125
|
-
class
|
|
126
|
-
#
|
|
127
|
-
#
|
|
215
|
+
class C {
|
|
216
|
+
#i = "http://localhost:3001";
|
|
217
|
+
#t = {
|
|
128
218
|
transports: ["websocket"]
|
|
129
219
|
};
|
|
130
|
-
#e;
|
|
131
|
-
#
|
|
132
|
-
#
|
|
220
|
+
#e = null;
|
|
221
|
+
#s = !1;
|
|
222
|
+
#o = !1;
|
|
223
|
+
#n;
|
|
224
|
+
constructor(e, t) {
|
|
225
|
+
e && (this.#i = e), t && (this.#t = { ...this.#t, ...t }), this.#n = {
|
|
226
|
+
onResponse: this.onResponse.bind(this),
|
|
227
|
+
onDisconnect: () => {
|
|
228
|
+
this.#s = !1, window.dispatchEvent(new Event("serial:socket:disconnected"));
|
|
229
|
+
},
|
|
230
|
+
onConnect: () => {
|
|
231
|
+
this.#s = !0, window.dispatchEvent(new Event("serial:socket:connected"));
|
|
232
|
+
},
|
|
233
|
+
onConnectError: (n) => {
|
|
234
|
+
console.debug("Socket connection error", n), this.#s = !1, window.dispatchEvent(new Event("serial:socket:disconnected"));
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
}
|
|
133
238
|
set uri(e) {
|
|
134
239
|
const t = new URL(e);
|
|
135
240
|
if (!["http:", "https:", "ws:", "wss:"].includes(t.protocol))
|
|
136
241
|
throw new Error("URI must start with http://, https://, ws://, or wss://");
|
|
137
|
-
this.#
|
|
242
|
+
this.#i = e;
|
|
138
243
|
}
|
|
139
244
|
get uri() {
|
|
140
|
-
return this.#
|
|
245
|
+
return this.#i;
|
|
141
246
|
}
|
|
142
247
|
set options(e) {
|
|
143
248
|
if (typeof e != "object")
|
|
144
249
|
throw new Error("Options must be an object");
|
|
145
|
-
this.#
|
|
250
|
+
this.#t = e;
|
|
146
251
|
}
|
|
147
252
|
get options() {
|
|
148
|
-
return this.#
|
|
253
|
+
return this.#t;
|
|
149
254
|
}
|
|
150
|
-
|
|
151
|
-
this.#
|
|
255
|
+
get socketId() {
|
|
256
|
+
return this.#e && this.#e.id ? this.#e.id : null;
|
|
257
|
+
}
|
|
258
|
+
configure(e, t) {
|
|
259
|
+
if (this.#o)
|
|
260
|
+
throw new Error("Cannot configure socket after it has been initialized. Call configure() before prepare().");
|
|
261
|
+
e && (this.uri = e), t && (this.#t = { ...this.#t, ...t });
|
|
152
262
|
}
|
|
153
263
|
disconnect() {
|
|
154
|
-
this.#e && (this.#e.off("response", this.#
|
|
264
|
+
this.#e && (this.#e.off("response", this.#n.onResponse), this.#e.off("disconnect", this.#n.onDisconnect), this.#e.off("connect", this.#n.onConnect), this.#e.off("connect_error", this.#n.onConnectError), this.#e.disconnect(), this.#e = null, this.#o = !1), this.#s = !1;
|
|
155
265
|
}
|
|
156
266
|
prepare() {
|
|
157
|
-
this.#
|
|
267
|
+
this.#s || this.#o || (this.#e = b(this.#i, this.#t), this.#o = !0, this.#e.on("disconnect", this.#n.onDisconnect), this.#e.on("response", this.#n.onResponse), this.#e.on("connect", this.#n.onConnect), this.#e.on("connect_error", this.#n.onConnectError));
|
|
158
268
|
}
|
|
159
269
|
connectDevice(e) {
|
|
270
|
+
if (!this.#e)
|
|
271
|
+
throw new Error("Socket not connected. Call prepare() first.");
|
|
160
272
|
this.#e.emit("connectDevice", { config: e });
|
|
161
273
|
}
|
|
162
274
|
disconnectDevice(e) {
|
|
275
|
+
if (!this.#e)
|
|
276
|
+
throw new Error("Socket not connected. Call prepare() first.");
|
|
163
277
|
this.#e.emit("disconnectDevice", { config: e });
|
|
164
278
|
}
|
|
165
279
|
disconnectAllDevices() {
|
|
280
|
+
if (!this.#e)
|
|
281
|
+
throw new Error("Socket not connected. Call prepare() first.");
|
|
166
282
|
this.#e.emit("disconnectAll");
|
|
167
283
|
}
|
|
168
284
|
write(e) {
|
|
285
|
+
if (!this.#e)
|
|
286
|
+
throw new Error("Socket not connected. Call prepare() first.");
|
|
169
287
|
this.#e.emit("cmd", e);
|
|
170
288
|
}
|
|
171
289
|
onResponse(e) {
|
|
172
290
|
let t = s.get(e.name, e.uuid);
|
|
173
291
|
t || (t = s.getByNumber(e.name, e.deviceNumber)), t && t.socketResponse(e);
|
|
174
292
|
}
|
|
293
|
+
isConnected() {
|
|
294
|
+
return this.#s;
|
|
295
|
+
}
|
|
296
|
+
isDisconnected() {
|
|
297
|
+
return !this.#s;
|
|
298
|
+
}
|
|
175
299
|
}
|
|
176
|
-
const
|
|
300
|
+
const c = new C(), f = {
|
|
177
301
|
baudRate: 9600,
|
|
178
302
|
dataBits: 8,
|
|
179
303
|
stopBits: 1,
|
|
@@ -181,7 +305,7 @@ const u = new w(), p = {
|
|
|
181
305
|
bufferSize: 32768,
|
|
182
306
|
flowControl: "none"
|
|
183
307
|
};
|
|
184
|
-
class
|
|
308
|
+
class S extends m {
|
|
185
309
|
__internal__ = {
|
|
186
310
|
bypassSerialBytesConnection: !1,
|
|
187
311
|
auto_response: !1,
|
|
@@ -229,7 +353,7 @@ class v extends m {
|
|
|
229
353
|
delay_first_connection: 200,
|
|
230
354
|
bytes_connection: null,
|
|
231
355
|
filters: [],
|
|
232
|
-
config_port:
|
|
356
|
+
config_port: f,
|
|
233
357
|
queue: [],
|
|
234
358
|
running_queue: !1,
|
|
235
359
|
auto_response: null,
|
|
@@ -255,17 +379,17 @@ class v extends m {
|
|
|
255
379
|
reconnection: 0
|
|
256
380
|
}
|
|
257
381
|
};
|
|
258
|
-
#
|
|
382
|
+
#i = null;
|
|
259
383
|
constructor({
|
|
260
384
|
filters: e = null,
|
|
261
|
-
config_port: t =
|
|
262
|
-
no_device:
|
|
263
|
-
device_listen_on_channel:
|
|
385
|
+
config_port: t = f,
|
|
386
|
+
no_device: n = 1,
|
|
387
|
+
device_listen_on_channel: i = 1,
|
|
264
388
|
bypassSerialBytesConnection: a = !1,
|
|
265
389
|
socket: r = !1
|
|
266
390
|
} = {
|
|
267
391
|
filters: null,
|
|
268
|
-
config_port:
|
|
392
|
+
config_port: f,
|
|
269
393
|
no_device: 1,
|
|
270
394
|
device_listen_on_channel: 1,
|
|
271
395
|
bypassSerialBytesConnection: !1,
|
|
@@ -273,7 +397,7 @@ class v extends m {
|
|
|
273
397
|
}) {
|
|
274
398
|
if (super(), !("serial" in navigator))
|
|
275
399
|
throw new Error("Web Serial not supported");
|
|
276
|
-
e && (this.serialFilters = e), t && (this.serialConfigPort = t), a && (this.__internal__.bypassSerialBytesConnection = a),
|
|
400
|
+
e && (this.serialFilters = e), t && (this.serialConfigPort = t), a && (this.__internal__.bypassSerialBytesConnection = a), n && this.#b(n), i && ["number", "string"].includes(typeof i) && (this.listenOnChannel = i), this.__internal__.serial.socket = r, this.#g(), this.#y();
|
|
277
401
|
}
|
|
278
402
|
set listenOnChannel(e) {
|
|
279
403
|
if (typeof e == "string" && (e = parseInt(e)), isNaN(e) || e < 1 || e > 255)
|
|
@@ -307,15 +431,15 @@ class v extends m {
|
|
|
307
431
|
this.__internal__.serial.useRTSCTS = e;
|
|
308
432
|
}
|
|
309
433
|
get isConnected() {
|
|
310
|
-
const e = this.__internal__.serial.connected, t = this.#
|
|
434
|
+
const e = this.__internal__.serial.connected, t = this.#t(this.__internal__.serial.port);
|
|
311
435
|
return e && !t && this.#e({ error: "Port is closed, not readable or writable." }), this.__internal__.serial.connected = t, this.__internal__.serial.connected;
|
|
312
436
|
}
|
|
313
437
|
get isConnecting() {
|
|
314
438
|
return this.__internal__.serial.connecting;
|
|
315
439
|
}
|
|
316
440
|
get isDisconnected() {
|
|
317
|
-
const e = this.__internal__.serial.connected, t = this.#
|
|
318
|
-
return !e && t && (this.dispatch("serial:connected"), this.#
|
|
441
|
+
const e = this.__internal__.serial.connected, t = this.#t(this.__internal__.serial.port);
|
|
442
|
+
return !e && t && (this.dispatch("serial:connected"), this.#a(!1), s.$dispatchChange(this)), this.__internal__.serial.connected = t, !this.__internal__.serial.connected;
|
|
319
443
|
}
|
|
320
444
|
get deviceNumber() {
|
|
321
445
|
return this.__internal__.device_number;
|
|
@@ -488,8 +612,8 @@ class v extends m {
|
|
|
488
612
|
}
|
|
489
613
|
};
|
|
490
614
|
}
|
|
491
|
-
#
|
|
492
|
-
return this.useSocket ? this.__internal__.serial.connected : !!(e && e.readable && e.writable);
|
|
615
|
+
#t(e) {
|
|
616
|
+
return this.useSocket ? this.__internal__.serial.connected && c.isConnected() : !!(e && e.readable && e.writable);
|
|
493
617
|
}
|
|
494
618
|
async timeout(e, t) {
|
|
495
619
|
this.__internal__.last_error.message = "Operation response timed out.", this.__internal__.last_error.action = t, this.__internal__.last_error.code = e, this.__internal__.timeout.until_response && (clearTimeout(this.__internal__.timeout.until_response), this.__internal__.timeout.until_response = 0), t === "connect" ? (this.__internal__.serial.connected = !1, this.dispatch("serial:reconnect", {}), s.$dispatchChange(this)) : t === "connection:start" && (await this.serialDisconnect(), this.__internal__.serial.connected = !1, this.__internal__.aux_port_connector += 1, s.$dispatchChange(this), await this.serialConnect()), this.__internal__.serial.queue.length > 0 && this.dispatch("internal:queue", {}), this.dispatch("serial:timeout", {
|
|
@@ -504,24 +628,24 @@ class v extends m {
|
|
|
504
628
|
#e(e = null) {
|
|
505
629
|
this.__internal__.serial.connected = !1, this.__internal__.aux_port_connector = 0, this.dispatch("serial:disconnected", e), s.$dispatchChange(this);
|
|
506
630
|
}
|
|
507
|
-
#
|
|
631
|
+
#s(e) {
|
|
508
632
|
this.__internal__.serial.aux_connecting = e.detail.active ? "connecting" : "finished";
|
|
509
633
|
}
|
|
510
634
|
socketResponse(e) {
|
|
511
635
|
const t = this.__internal__.serial.connected;
|
|
512
|
-
if (e.type === "disconnect" || e.type === "error" && e.data === "DISCONNECTED" ? this.__internal__.serial.connected = !1 : e.type === "success" && (this.__internal__.serial.connected = !0), s.$dispatchChange(this), !t && this.__internal__.serial.connected && (this.dispatch("serial:connected"), this.#
|
|
513
|
-
this.#
|
|
636
|
+
if (e.type === "disconnect" || e.type === "error" && e.data === "DISCONNECTED" ? this.__internal__.serial.connected = !1 : e.type === "success" && (this.__internal__.serial.connected = !0), s.$dispatchChange(this), !t && this.__internal__.serial.connected && (this.dispatch("serial:connected"), this.#a(!1)), e.type === "success")
|
|
637
|
+
this.#r(new Uint8Array(e.data));
|
|
514
638
|
else if (e.type === "error") {
|
|
515
|
-
const
|
|
516
|
-
this.serialErrors(
|
|
639
|
+
const n = new Error("The port is closed or is not readable/writable");
|
|
640
|
+
this.serialErrors(n);
|
|
517
641
|
} else e.type === "timeout" && this.timeout(e.data.bytes ?? [], this.lastAction || "unknown");
|
|
518
642
|
this.__internal__.serial.last_action = null;
|
|
519
643
|
}
|
|
520
644
|
async connect() {
|
|
521
645
|
return this.isConnected ? !0 : (this.__internal__.serial.aux_connecting = "idle", new Promise((e, t) => {
|
|
522
|
-
this.#
|
|
523
|
-
const
|
|
524
|
-
this.__internal__.serial.aux_connecting === "finished" ? (clearInterval(
|
|
646
|
+
this.#i || (this.#i = this.#s.bind(this)), this.on("internal:connecting", this.#i);
|
|
647
|
+
const n = setInterval(() => {
|
|
648
|
+
this.__internal__.serial.aux_connecting === "finished" ? (clearInterval(n), this.__internal__.serial.aux_connecting = "idle", this.#i !== null && this.off("internal:connecting", this.#i), this.isConnected ? e(!0) : t(`${this.typeDevice} device ${this.deviceNumber} not connected`)) : this.__internal__.serial.aux_connecting === "connecting" && (this.__internal__.serial.aux_connecting = "idle", this.dispatch("internal:connecting", { active: !0 }), this.dispatch("serial:connecting", { active: !0 }));
|
|
525
649
|
}, 100);
|
|
526
650
|
this.serialConnect();
|
|
527
651
|
}));
|
|
@@ -529,10 +653,10 @@ class v extends m {
|
|
|
529
653
|
async serialDisconnect() {
|
|
530
654
|
try {
|
|
531
655
|
if (this.useSocket)
|
|
532
|
-
|
|
656
|
+
c.isConnected() && c.disconnectDevice(this.configDeviceSocket);
|
|
533
657
|
else {
|
|
534
658
|
const e = this.__internal__.serial.reader, t = this.__internal__.serial.output_stream;
|
|
535
|
-
e && (await e.cancel().catch((
|
|
659
|
+
e && (await e.cancel().catch((i) => this.serialErrors(i)), await this.__internal__.serial.input_done), t && (await t.getWriter().close(), await this.__internal__.serial.output_done), this.__internal__.serial.connected && this.__internal__.serial && this.__internal__.serial.port && await this.__internal__.serial.port.close();
|
|
536
660
|
}
|
|
537
661
|
} catch (e) {
|
|
538
662
|
this.serialErrors(e);
|
|
@@ -540,53 +664,55 @@ class v extends m {
|
|
|
540
664
|
this.__internal__.serial.reader = null, this.__internal__.serial.input_done = null, this.__internal__.serial.output_stream = null, this.__internal__.serial.output_done = null, this.__internal__.serial.connected = !1, this.__internal__.serial.port = null, s.$dispatchChange(this);
|
|
541
665
|
}
|
|
542
666
|
}
|
|
543
|
-
async #
|
|
667
|
+
async #o(e) {
|
|
668
|
+
if (c.isDisconnected())
|
|
669
|
+
throw this.#e({ error: "Socket is disconnected." }), new Error("The socket is disconnected");
|
|
544
670
|
if (this.isDisconnected)
|
|
545
671
|
throw this.#e({ error: "Port is closed, not readable or writable." }), new Error("The port is closed or is not readable/writable");
|
|
546
672
|
const t = this.validateBytes(e);
|
|
547
|
-
|
|
673
|
+
c.write({ config: this.configDeviceSocket, bytes: Array.from(t) });
|
|
548
674
|
}
|
|
549
|
-
async #
|
|
675
|
+
async #n(e) {
|
|
550
676
|
if (this.useSocket) {
|
|
551
|
-
await this.#
|
|
677
|
+
await this.#o(e);
|
|
552
678
|
return;
|
|
553
679
|
}
|
|
554
680
|
const t = this.__internal__.serial.port;
|
|
555
681
|
if (!t || t && (!t.readable || !t.writable))
|
|
556
682
|
throw this.#e({ error: "Port is closed, not readable or writable." }), new Error("The port is closed or is not readable/writable");
|
|
557
|
-
const
|
|
683
|
+
const n = this.validateBytes(e);
|
|
558
684
|
if (this.useRTSCTS && await this.#l(t, 5e3), t.writable === null) return;
|
|
559
|
-
const
|
|
560
|
-
await
|
|
685
|
+
const i = t.writable.getWriter();
|
|
686
|
+
await i.write(n), i.releaseLock();
|
|
561
687
|
}
|
|
562
688
|
async #l(e, t = 5e3) {
|
|
563
|
-
const
|
|
689
|
+
const n = Date.now();
|
|
564
690
|
for (; ; ) {
|
|
565
|
-
if (Date.now() -
|
|
691
|
+
if (Date.now() - n > t)
|
|
566
692
|
throw new Error("Timeout waiting for clearToSend signal");
|
|
567
|
-
const { clearToSend:
|
|
568
|
-
if (
|
|
693
|
+
const { clearToSend: i } = await e.getSignals();
|
|
694
|
+
if (i) return;
|
|
569
695
|
await y(100);
|
|
570
696
|
}
|
|
571
697
|
}
|
|
572
|
-
#
|
|
698
|
+
#r(e = new Uint8Array([]), t = !1) {
|
|
573
699
|
if (e && e.length > 0) {
|
|
574
|
-
const
|
|
575
|
-
if (this.__internal__.serial.connected = this.#
|
|
700
|
+
const n = this.__internal__.serial.connected;
|
|
701
|
+
if (this.__internal__.serial.connected = this.#t(this.__internal__.serial.port), s.$dispatchChange(this), !n && this.__internal__.serial.connected && (this.dispatch("serial:connected"), this.#a(!1)), this.__internal__.interval.reconnection && (clearInterval(this.__internal__.interval.reconnection), this.__internal__.interval.reconnection = 0), this.__internal__.timeout.until_response && (clearTimeout(this.__internal__.timeout.until_response), this.__internal__.timeout.until_response = 0), this.__internal__.serial.response.as === "hex")
|
|
576
702
|
t ? this.serialCorruptMessage(this.parseUint8ToHex(e)) : this.serialMessage(this.parseUint8ToHex(e));
|
|
577
703
|
else if (this.__internal__.serial.response.as === "uint8")
|
|
578
704
|
t ? this.serialCorruptMessage(e) : this.serialMessage(e);
|
|
579
705
|
else if (this.__internal__.serial.response.as === "string") {
|
|
580
|
-
const
|
|
706
|
+
const i = this.parseUint8ArrayToString(e);
|
|
581
707
|
if (this.__internal__.serial.response.limiter !== null) {
|
|
582
|
-
const a =
|
|
708
|
+
const a = i.split(this.__internal__.serial.response.limiter);
|
|
583
709
|
for (const r in a)
|
|
584
710
|
a[r] && (t ? this.serialCorruptMessage(a[r]) : this.serialMessage(a[r]));
|
|
585
711
|
} else
|
|
586
|
-
t ? this.serialCorruptMessage(
|
|
712
|
+
t ? this.serialCorruptMessage(i) : this.serialMessage(i);
|
|
587
713
|
} else {
|
|
588
|
-
const
|
|
589
|
-
t ? this.serialCorruptMessage(
|
|
714
|
+
const i = this.stringToArrayBuffer(this.parseUint8ArrayToString(e));
|
|
715
|
+
t ? this.serialCorruptMessage(i) : this.serialMessage(i);
|
|
590
716
|
}
|
|
591
717
|
}
|
|
592
718
|
if (this.__internal__.serial.queue.length === 0) {
|
|
@@ -609,16 +735,16 @@ class v extends m {
|
|
|
609
735
|
}
|
|
610
736
|
async #_() {
|
|
611
737
|
const e = this.serialFilters, t = await navigator.serial.getPorts({ filters: e });
|
|
612
|
-
return e.length === 0 ? t : t.filter((
|
|
613
|
-
const a =
|
|
738
|
+
return e.length === 0 ? t : t.filter((i) => {
|
|
739
|
+
const a = i.getInfo();
|
|
614
740
|
return e.some((r) => a.usbProductId === r.usbProductId && a.usbVendorId === r.usbVendorId);
|
|
615
|
-
}).filter((
|
|
741
|
+
}).filter((i) => !this.#t(i));
|
|
616
742
|
}
|
|
617
743
|
async serialPortsSaved(e) {
|
|
618
744
|
const t = this.serialFilters;
|
|
619
745
|
if (this.__internal__.aux_port_connector < e.length) {
|
|
620
|
-
const
|
|
621
|
-
this.__internal__.serial.port = e[
|
|
746
|
+
const n = this.__internal__.aux_port_connector;
|
|
747
|
+
this.__internal__.serial.port = e[n];
|
|
622
748
|
} else
|
|
623
749
|
this.__internal__.aux_port_connector = 0, this.__internal__.serial.port = await navigator.serial.requestPort({
|
|
624
750
|
filters: t
|
|
@@ -675,13 +801,13 @@ class v extends m {
|
|
|
675
801
|
}
|
|
676
802
|
#c(e) {
|
|
677
803
|
if (e) {
|
|
678
|
-
const t = this.__internal__.serial.response.buffer,
|
|
679
|
-
|
|
804
|
+
const t = this.__internal__.serial.response.buffer, n = new Uint8Array(t.length + e.byteLength);
|
|
805
|
+
n.set(t, 0), n.set(new Uint8Array(e), t.length), this.__internal__.serial.response.buffer = n;
|
|
680
806
|
}
|
|
681
807
|
}
|
|
682
808
|
async #h() {
|
|
683
809
|
this.__internal__.serial.time_until_send_bytes && (clearTimeout(this.__internal__.serial.time_until_send_bytes), this.__internal__.serial.time_until_send_bytes = 0), this.__internal__.serial.time_until_send_bytes = setTimeout(() => {
|
|
684
|
-
this.__internal__.serial.response.buffer && this.#
|
|
810
|
+
this.__internal__.serial.response.buffer && this.#r(this.__internal__.serial.response.buffer), this.__internal__.serial.response.buffer = new Uint8Array(0);
|
|
685
811
|
}, this.__internal__.serial.free_timeout_ms || 50);
|
|
686
812
|
}
|
|
687
813
|
async #u() {
|
|
@@ -689,11 +815,11 @@ class v extends m {
|
|
|
689
815
|
let t = this.__internal__.serial.response.buffer;
|
|
690
816
|
if (this.__internal__.serial.time_until_send_bytes && (clearTimeout(this.__internal__.serial.time_until_send_bytes), this.__internal__.serial.time_until_send_bytes = 0), !(e === null || !t || t.length === 0)) {
|
|
691
817
|
for (; t.length >= e; ) {
|
|
692
|
-
const
|
|
693
|
-
this.#n
|
|
818
|
+
const n = t.slice(0, e);
|
|
819
|
+
this.#r(n), t = t.slice(e);
|
|
694
820
|
}
|
|
695
821
|
this.__internal__.serial.response.buffer = t, t.length > 0 && (this.__internal__.serial.time_until_send_bytes = setTimeout(() => {
|
|
696
|
-
this.#
|
|
822
|
+
this.#r(this.__internal__.serial.response.buffer, !0);
|
|
697
823
|
}, this.__internal__.serial.free_timeout_ms || 50));
|
|
698
824
|
}
|
|
699
825
|
}
|
|
@@ -701,120 +827,122 @@ class v extends m {
|
|
|
701
827
|
const {
|
|
702
828
|
limiter: e,
|
|
703
829
|
prefixLimiter: t = !1,
|
|
704
|
-
sufixLimiter:
|
|
830
|
+
sufixLimiter: n = !0
|
|
705
831
|
} = this.__internal__.serial.response;
|
|
706
832
|
if (!e)
|
|
707
833
|
throw new Error("No limiter defined for delimited serial response");
|
|
708
|
-
const
|
|
709
|
-
if (!e || !
|
|
834
|
+
const i = this.__internal__.serial.response.buffer;
|
|
835
|
+
if (!e || !i || i.length === 0) return;
|
|
710
836
|
this.__internal__.serial.time_until_send_bytes && (clearTimeout(this.__internal__.serial.time_until_send_bytes), this.__internal__.serial.time_until_send_bytes = 0);
|
|
711
|
-
let r = new TextDecoder().decode(
|
|
712
|
-
const
|
|
837
|
+
let r = new TextDecoder().decode(i);
|
|
838
|
+
const u = [];
|
|
713
839
|
if (typeof e == "string") {
|
|
714
|
-
let
|
|
715
|
-
if (t &&
|
|
716
|
-
|
|
840
|
+
let l;
|
|
841
|
+
if (t && n)
|
|
842
|
+
l = new RegExp(`${e}([^${e}]+)${e}`, "g");
|
|
717
843
|
else if (t)
|
|
718
|
-
|
|
719
|
-
else if (
|
|
720
|
-
|
|
844
|
+
l = new RegExp(`${e}([^${e}]*)`, "g");
|
|
845
|
+
else if (n)
|
|
846
|
+
l = new RegExp(`([^${e}]+)${e}`, "g");
|
|
721
847
|
else
|
|
722
848
|
return;
|
|
723
|
-
let
|
|
724
|
-
for (; (
|
|
725
|
-
|
|
726
|
-
r = r.slice(
|
|
849
|
+
let h, _ = 0;
|
|
850
|
+
for (; (h = l.exec(r)) !== null; )
|
|
851
|
+
u.push(new TextEncoder().encode(h[1])), _ = l.lastIndex;
|
|
852
|
+
r = r.slice(_);
|
|
727
853
|
} else if (e instanceof RegExp) {
|
|
728
|
-
let
|
|
729
|
-
if (t &&
|
|
730
|
-
const
|
|
731
|
-
for (; (
|
|
732
|
-
|
|
733
|
-
} else if (
|
|
734
|
-
for (; (
|
|
735
|
-
const
|
|
736
|
-
|
|
854
|
+
let l, h = 0;
|
|
855
|
+
if (t && n) {
|
|
856
|
+
const _ = new RegExp(`${e.source}(.*?)${e.source}`, "g");
|
|
857
|
+
for (; (l = _.exec(r)) !== null; )
|
|
858
|
+
u.push(new TextEncoder().encode(l[1])), h = _.lastIndex;
|
|
859
|
+
} else if (n)
|
|
860
|
+
for (; (l = e.exec(r)) !== null; ) {
|
|
861
|
+
const _ = l.index, d = r.slice(h, _);
|
|
862
|
+
u.push(new TextEncoder().encode(d)), h = e.lastIndex;
|
|
737
863
|
}
|
|
738
864
|
else if (t) {
|
|
739
|
-
const
|
|
740
|
-
|
|
741
|
-
for (const d of
|
|
742
|
-
|
|
865
|
+
const _ = r.split(e);
|
|
866
|
+
_.shift();
|
|
867
|
+
for (const d of _)
|
|
868
|
+
u.push(new TextEncoder().encode(d));
|
|
743
869
|
r = "";
|
|
744
870
|
}
|
|
745
|
-
r = r.slice(
|
|
871
|
+
r = r.slice(h);
|
|
746
872
|
}
|
|
747
|
-
for (const
|
|
748
|
-
this.#
|
|
749
|
-
const
|
|
750
|
-
this.__internal__.serial.response.buffer =
|
|
751
|
-
this.#
|
|
873
|
+
for (const l of u)
|
|
874
|
+
this.#r(l);
|
|
875
|
+
const p = new TextEncoder().encode(r);
|
|
876
|
+
this.__internal__.serial.response.buffer = p, p.length > 0 && (this.__internal__.serial.time_until_send_bytes = setTimeout(() => {
|
|
877
|
+
this.#r(this.__internal__.serial.response.buffer, !0), this.__internal__.serial.response.buffer = new Uint8Array(0);
|
|
752
878
|
}, this.__internal__.serial.free_timeout_ms ?? 50));
|
|
753
879
|
}
|
|
754
|
-
async #
|
|
880
|
+
async #f() {
|
|
755
881
|
const e = this.__internal__.serial.port;
|
|
756
882
|
if (!e || !e.readable) throw new Error("Port is not readable");
|
|
757
883
|
const t = e.readable.getReader();
|
|
758
884
|
this.__internal__.serial.reader = t;
|
|
759
885
|
try {
|
|
760
886
|
for (; this.__internal__.serial.keep_reading; ) {
|
|
761
|
-
const { value:
|
|
762
|
-
if (
|
|
763
|
-
this.#c(
|
|
887
|
+
const { value: n, done: i } = await t.read();
|
|
888
|
+
if (i) break;
|
|
889
|
+
this.#c(n), this.__internal__.serial.response.delimited ? await this.#d() : this.__internal__.serial.response.length === null ? await this.#h() : await this.#u();
|
|
764
890
|
}
|
|
765
|
-
} catch (
|
|
766
|
-
this.serialErrors(
|
|
891
|
+
} catch (n) {
|
|
892
|
+
this.serialErrors(n);
|
|
767
893
|
} finally {
|
|
768
894
|
t.releaseLock(), this.__internal__.serial.keep_reading = !0, this.__internal__.serial.port && await this.__internal__.serial.port.close();
|
|
769
895
|
}
|
|
770
896
|
}
|
|
771
|
-
#
|
|
897
|
+
#a(e) {
|
|
772
898
|
e !== this.__internal__.serial.connecting && (this.__internal__.serial.connecting = e, this.dispatch("serial:connecting", { active: e }), this.dispatch("internal:connecting", { active: e }));
|
|
773
899
|
}
|
|
774
900
|
async serialConnect() {
|
|
775
901
|
try {
|
|
776
|
-
if (this.#
|
|
777
|
-
|
|
902
|
+
if (this.#a(!0), this.useSocket) {
|
|
903
|
+
if (c.prepare(), this.__internal__.serial.last_action = "connect", this.__internal__.timeout.until_response = setTimeout(async () => {
|
|
778
904
|
await this.timeout(this.__internal__.serial.bytes_connection ?? [], "connection:start");
|
|
779
|
-
}, this.__internal__.time.response_connection),
|
|
905
|
+
}, this.__internal__.time.response_connection), c.isDisconnected())
|
|
906
|
+
return;
|
|
907
|
+
c.connectDevice(this.configDeviceSocket), this.dispatch("serial:sent", {
|
|
780
908
|
action: "connect",
|
|
781
909
|
bytes: this.__internal__.serial.bytes_connection
|
|
782
910
|
});
|
|
783
|
-
else {
|
|
911
|
+
} else {
|
|
784
912
|
const e = await this.#_();
|
|
785
913
|
if (e.length > 0)
|
|
786
914
|
await this.serialPortsSaved(e);
|
|
787
915
|
else {
|
|
788
|
-
const
|
|
916
|
+
const i = this.serialFilters;
|
|
789
917
|
this.__internal__.serial.port = await navigator.serial.requestPort({
|
|
790
|
-
filters:
|
|
918
|
+
filters: i
|
|
791
919
|
});
|
|
792
920
|
}
|
|
793
921
|
const t = this.__internal__.serial.port;
|
|
794
922
|
if (!t)
|
|
795
923
|
throw new Error("No port selected by the user");
|
|
796
924
|
await t.open(this.serialConfigPort);
|
|
797
|
-
const
|
|
798
|
-
t.onconnect = (
|
|
799
|
-
|
|
925
|
+
const n = this;
|
|
926
|
+
t.onconnect = (i) => {
|
|
927
|
+
n.dispatch("serial:connected", i), n.#a(!1), s.$dispatchChange(this), n.__internal__.serial.queue.length > 0 ? n.dispatch("internal:queue", {}) : n.__internal__.serial.running_queue = !1;
|
|
800
928
|
}, t.ondisconnect = async () => {
|
|
801
|
-
await
|
|
929
|
+
await n.disconnect();
|
|
802
930
|
}, await y(this.__internal__.serial.delay_first_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
|
|
803
|
-
await
|
|
804
|
-
}, this.__internal__.time.response_connection), this.__internal__.serial.last_action = "connect", await this.#
|
|
931
|
+
await n.timeout(n.__internal__.serial.bytes_connection ?? [], "connection:start");
|
|
932
|
+
}, this.__internal__.time.response_connection), this.__internal__.serial.last_action = "connect", await this.#n(this.__internal__.serial.bytes_connection ?? []), this.dispatch("serial:sent", {
|
|
805
933
|
action: "connect",
|
|
806
934
|
bytes: this.__internal__.serial.bytes_connection
|
|
807
|
-
}), this.__internal__.auto_response && this.#
|
|
935
|
+
}), this.__internal__.auto_response && this.#r(this.__internal__.serial.auto_response), await this.#f();
|
|
808
936
|
}
|
|
809
937
|
} catch (e) {
|
|
810
|
-
this.#
|
|
938
|
+
this.#a(!1), this.serialErrors(e);
|
|
811
939
|
}
|
|
812
940
|
}
|
|
813
|
-
async #
|
|
941
|
+
async #p() {
|
|
814
942
|
return typeof window > "u" ? !1 : "serial" in navigator && "forget" in SerialPort.prototype && this.__internal__.serial.port ? (await this.__internal__.serial.port.forget(), !0) : !1;
|
|
815
943
|
}
|
|
816
944
|
async serialForget() {
|
|
817
|
-
return await this.#
|
|
945
|
+
return await this.#p();
|
|
818
946
|
}
|
|
819
947
|
decToHex(e) {
|
|
820
948
|
return typeof e == "string" && (e = parseInt(e, 10)), e.toString(16);
|
|
@@ -827,8 +955,8 @@ class v extends m {
|
|
|
827
955
|
}
|
|
828
956
|
add0x(e) {
|
|
829
957
|
const t = [];
|
|
830
|
-
return e.forEach((
|
|
831
|
-
t[
|
|
958
|
+
return e.forEach((n, i) => {
|
|
959
|
+
t[i] = "0x" + n;
|
|
832
960
|
}), t;
|
|
833
961
|
}
|
|
834
962
|
bytesToHex(e) {
|
|
@@ -858,8 +986,15 @@ class v extends m {
|
|
|
858
986
|
#y() {
|
|
859
987
|
const e = this;
|
|
860
988
|
this.on("internal:queue", async () => {
|
|
861
|
-
await e.#
|
|
862
|
-
})
|
|
989
|
+
await e.#w();
|
|
990
|
+
});
|
|
991
|
+
const t = () => {
|
|
992
|
+
e.isConnected && e.#e({ error: "Socket disconnected." });
|
|
993
|
+
}, n = () => {
|
|
994
|
+
e.isDisconnected && !e.isConnecting && e.serialConnect().catch(() => {
|
|
995
|
+
});
|
|
996
|
+
};
|
|
997
|
+
this.useSocket && (window.addEventListener("serial:socket:disconnected", t), window.addEventListener("serial:socket:connected", n)), this.#m();
|
|
863
998
|
}
|
|
864
999
|
#m() {
|
|
865
1000
|
const e = this;
|
|
@@ -868,8 +1003,10 @@ class v extends m {
|
|
|
868
1003
|
});
|
|
869
1004
|
});
|
|
870
1005
|
}
|
|
871
|
-
async #
|
|
872
|
-
if (
|
|
1006
|
+
async #w() {
|
|
1007
|
+
if (this.useSocket && c.isDisconnected())
|
|
1008
|
+
return;
|
|
1009
|
+
if (!this.#t(this.__internal__.serial.port)) {
|
|
873
1010
|
this.#e({ error: "Port is closed, not readable or writable." }), await this.serialConnect();
|
|
874
1011
|
return;
|
|
875
1012
|
}
|
|
@@ -883,20 +1020,20 @@ class v extends m {
|
|
|
883
1020
|
let t = this.__internal__.time.response_general;
|
|
884
1021
|
if (e.action === "connect" && (t = this.__internal__.time.response_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
|
|
885
1022
|
await this.timeout(e.bytes, e.action);
|
|
886
|
-
}, t), this.__internal__.serial.last_action = e.action ?? "unknown", await this.#
|
|
1023
|
+
}, t), this.__internal__.serial.last_action = e.action ?? "unknown", await this.#n(e.bytes), this.dispatch("serial:sent", {
|
|
887
1024
|
action: e.action,
|
|
888
1025
|
bytes: e.bytes
|
|
889
1026
|
}), this.__internal__.auto_response) {
|
|
890
|
-
let
|
|
1027
|
+
let i = new Uint8Array(0);
|
|
891
1028
|
try {
|
|
892
|
-
|
|
1029
|
+
i = this.validateBytes(this.__internal__.serial.auto_response);
|
|
893
1030
|
} catch (a) {
|
|
894
1031
|
this.serialErrors(a);
|
|
895
1032
|
}
|
|
896
|
-
this.#
|
|
1033
|
+
this.#r(i);
|
|
897
1034
|
}
|
|
898
|
-
const
|
|
899
|
-
this.__internal__.serial.queue =
|
|
1035
|
+
const n = [...this.__internal__.serial.queue];
|
|
1036
|
+
this.__internal__.serial.queue = n.splice(1), this.__internal__.serial.queue.length > 0 && (this.__internal__.serial.running_queue = !0);
|
|
900
1037
|
}
|
|
901
1038
|
validateBytes(e) {
|
|
902
1039
|
let t = new Uint8Array(0);
|
|
@@ -913,15 +1050,15 @@ class v extends m {
|
|
|
913
1050
|
return t;
|
|
914
1051
|
}
|
|
915
1052
|
async appendToQueue(e, t) {
|
|
916
|
-
const
|
|
1053
|
+
const n = this.validateBytes(e);
|
|
917
1054
|
if (["connect", "connection:start"].includes(t)) {
|
|
918
1055
|
if (this.__internal__.serial.connected) return;
|
|
919
1056
|
await this.serialConnect();
|
|
920
1057
|
return;
|
|
921
1058
|
}
|
|
922
|
-
this.__internal__.serial.queue.push({ bytes:
|
|
1059
|
+
this.__internal__.serial.queue.push({ bytes: n, action: t }), this.dispatch("internal:queue", {});
|
|
923
1060
|
}
|
|
924
|
-
#
|
|
1061
|
+
#b(e = 1) {
|
|
925
1062
|
this.__internal__.device_number = e, !this.__internal__.bypassSerialBytesConnection && (this.__internal__.serial.bytes_connection = this.serialSetConnectionConstant(e));
|
|
926
1063
|
}
|
|
927
1064
|
serialSetConnectionConstant(e = 1) {
|
|
@@ -947,8 +1084,8 @@ class v extends m {
|
|
|
947
1084
|
}
|
|
948
1085
|
sumHex(e) {
|
|
949
1086
|
let t = 0;
|
|
950
|
-
return e.forEach((
|
|
951
|
-
t += parseInt(
|
|
1087
|
+
return e.forEach((n) => {
|
|
1088
|
+
t += parseInt(n, 16);
|
|
952
1089
|
}), t.toString(16);
|
|
953
1090
|
}
|
|
954
1091
|
toString() {
|
|
@@ -982,13 +1119,13 @@ class v extends m {
|
|
|
982
1119
|
}
|
|
983
1120
|
parseStringToTextEncoder(e = "", t = `
|
|
984
1121
|
`) {
|
|
985
|
-
const
|
|
986
|
-
return e += t,
|
|
1122
|
+
const n = new TextEncoder();
|
|
1123
|
+
return e += t, n.encode(e);
|
|
987
1124
|
}
|
|
988
1125
|
parseStringToBytes(e = "", t = `
|
|
989
1126
|
`) {
|
|
990
|
-
const
|
|
991
|
-
return Array.from(
|
|
1127
|
+
const n = this.parseStringToTextEncoder(e, t);
|
|
1128
|
+
return Array.from(n).map((i) => i.toString(16));
|
|
992
1129
|
}
|
|
993
1130
|
parseUint8ToHex(e) {
|
|
994
1131
|
return Array.from(e).map((t) => t.toString(16).padStart(2, "0").toLowerCase());
|
|
@@ -998,28 +1135,28 @@ class v extends m {
|
|
|
998
1135
|
}
|
|
999
1136
|
stringArrayToUint8Array(e) {
|
|
1000
1137
|
const t = [];
|
|
1001
|
-
return typeof e == "string" ? this.parseStringToTextEncoder(e).buffer : (e.forEach((
|
|
1002
|
-
const
|
|
1003
|
-
t.push(parseInt(
|
|
1138
|
+
return typeof e == "string" ? this.parseStringToTextEncoder(e).buffer : (e.forEach((n) => {
|
|
1139
|
+
const i = n.replace("0x", "");
|
|
1140
|
+
t.push(parseInt(i, 16));
|
|
1004
1141
|
}), new Uint8Array(t));
|
|
1005
1142
|
}
|
|
1006
1143
|
parseUint8ArrayToString(e) {
|
|
1007
1144
|
let t = new Uint8Array(0);
|
|
1008
1145
|
e instanceof Uint8Array ? t = e : t = this.stringArrayToUint8Array(e), e = this.parseUint8ToHex(t);
|
|
1009
|
-
const
|
|
1010
|
-
return this.__internal__.serial.response.replacer ? String.fromCharCode(...
|
|
1146
|
+
const n = e.map((i) => parseInt(i, 16));
|
|
1147
|
+
return this.__internal__.serial.response.replacer ? String.fromCharCode(...n).replace(this.__internal__.serial.response.replacer, "") : String.fromCharCode(...n);
|
|
1011
1148
|
}
|
|
1012
1149
|
hexToAscii(e) {
|
|
1013
1150
|
const t = e.toString();
|
|
1014
|
-
let
|
|
1015
|
-
for (let
|
|
1016
|
-
|
|
1017
|
-
return
|
|
1151
|
+
let n = "";
|
|
1152
|
+
for (let i = 0; i < t.length; i += 2)
|
|
1153
|
+
n += String.fromCharCode(parseInt(t.substring(i, 2), 16));
|
|
1154
|
+
return n;
|
|
1018
1155
|
}
|
|
1019
1156
|
asciiToHex(e) {
|
|
1020
1157
|
const t = [];
|
|
1021
|
-
for (let
|
|
1022
|
-
const a = Number(e.charCodeAt(
|
|
1158
|
+
for (let n = 0, i = e.length; n < i; n++) {
|
|
1159
|
+
const a = Number(e.charCodeAt(n)).toString(16);
|
|
1023
1160
|
t.push(a);
|
|
1024
1161
|
}
|
|
1025
1162
|
return t.join("");
|
|
@@ -1028,9 +1165,66 @@ class v extends m {
|
|
|
1028
1165
|
return this.isConnected;
|
|
1029
1166
|
}
|
|
1030
1167
|
}
|
|
1168
|
+
var E = /* @__PURE__ */ ((o) => (o.CONNECTION_FAILED = "CONNECTION_FAILED", o.DISCONNECTION_FAILED = "DISCONNECTION_FAILED", o.WRITE_FAILED = "WRITE_FAILED", o.READ_FAILED = "READ_FAILED", o.TIMEOUT = "TIMEOUT", o.PORT_NOT_FOUND = "PORT_NOT_FOUND", o.PERMISSION_DENIED = "PERMISSION_DENIED", o.DEVICE_NOT_SUPPORTED = "DEVICE_NOT_SUPPORTED", o.INVALID_CONFIGURATION = "INVALID_CONFIGURATION", o.SOCKET_ERROR = "SOCKET_ERROR", o.UNKNOWN_ERROR = "UNKNOWN_ERROR", o))(E || {});
|
|
1169
|
+
class w extends Error {
|
|
1170
|
+
/**
|
|
1171
|
+
* Error code identifying the type of error
|
|
1172
|
+
*/
|
|
1173
|
+
code;
|
|
1174
|
+
/**
|
|
1175
|
+
* Additional context about the error
|
|
1176
|
+
*/
|
|
1177
|
+
context;
|
|
1178
|
+
/**
|
|
1179
|
+
* Timestamp when the error occurred
|
|
1180
|
+
*/
|
|
1181
|
+
timestamp;
|
|
1182
|
+
/**
|
|
1183
|
+
* Creates a new SerialError
|
|
1184
|
+
* @param message - Human-readable error message
|
|
1185
|
+
* @param code - Error code from SerialErrorCode enum
|
|
1186
|
+
* @param context - Additional context information
|
|
1187
|
+
* @example
|
|
1188
|
+
* ```typescript
|
|
1189
|
+
* throw new SerialError(
|
|
1190
|
+
* 'Failed to connect to device',
|
|
1191
|
+
* SerialErrorCode.CONNECTION_FAILED,
|
|
1192
|
+
* { port: 'COM3', baudRate: 9600 }
|
|
1193
|
+
* );
|
|
1194
|
+
* ```
|
|
1195
|
+
*/
|
|
1196
|
+
constructor(e, t = "UNKNOWN_ERROR", n) {
|
|
1197
|
+
super(e), this.name = "SerialError", this.code = t, this.context = n, this.timestamp = /* @__PURE__ */ new Date(), Error.captureStackTrace && Error.captureStackTrace(this, w);
|
|
1198
|
+
}
|
|
1199
|
+
/**
|
|
1200
|
+
* Returns a JSON representation of the error
|
|
1201
|
+
* @returns Serialized error object
|
|
1202
|
+
*/
|
|
1203
|
+
toJSON() {
|
|
1204
|
+
return {
|
|
1205
|
+
name: this.name,
|
|
1206
|
+
message: this.message,
|
|
1207
|
+
code: this.code,
|
|
1208
|
+
context: this.context,
|
|
1209
|
+
timestamp: this.timestamp.toISOString(),
|
|
1210
|
+
stack: this.stack
|
|
1211
|
+
};
|
|
1212
|
+
}
|
|
1213
|
+
/**
|
|
1214
|
+
* Returns a formatted string representation of the error
|
|
1215
|
+
* @returns Formatted error string
|
|
1216
|
+
*/
|
|
1217
|
+
toString() {
|
|
1218
|
+
const e = this.context ? ` | Context: ${JSON.stringify(this.context)}` : "";
|
|
1219
|
+
return `${this.name} [${this.code}]: ${this.message}${e}`;
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1031
1222
|
export {
|
|
1032
|
-
|
|
1223
|
+
S as Core,
|
|
1033
1224
|
s as Devices,
|
|
1034
1225
|
m as Dispatcher,
|
|
1035
|
-
|
|
1226
|
+
w as SerialError,
|
|
1227
|
+
E as SerialErrorCode,
|
|
1228
|
+
c as Socket
|
|
1036
1229
|
};
|
|
1230
|
+
//# sourceMappingURL=webserial-core.js.map
|