webserial-core 1.0.0
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 +297 -0
- package/dist/webserial-core.js +647 -0
- package/dist/webserial-core.umd.cjs +4 -0
- package/package.json +57 -0
package/README.md
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
# WebSerial Core
|
|
2
|
+
|
|
3
|
+
And easy way to connect to a serial port from a web page.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install webserial-core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
You need to create a new class to configure your device functions. In this example, we are going to create a class to
|
|
14
|
+
connect to an Arduino device.
|
|
15
|
+
|
|
16
|
+
The first step is having the Arduino code ready. In this case, we are going to use the following code.
|
|
17
|
+
|
|
18
|
+
```cpp
|
|
19
|
+
// serial.ino
|
|
20
|
+
|
|
21
|
+
void setup() {
|
|
22
|
+
Serial.begin(9600);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
void loop() {
|
|
26
|
+
if (Serial.available() > 0) { // Check if data to read is available
|
|
27
|
+
String comando = Serial.readStringUntil('\n'); // read the data until a new line is found
|
|
28
|
+
|
|
29
|
+
if(comando.startsWith("CONNECT")){
|
|
30
|
+
Serial.println("connected");
|
|
31
|
+
} else if (comando.startsWith("CREDITS")) {
|
|
32
|
+
Serial.println("created by danidoble");
|
|
33
|
+
} else if (comando.startsWith("HI")) {
|
|
34
|
+
Serial.println("hello there");
|
|
35
|
+
} else {
|
|
36
|
+
Serial.println("ara ara, what are you doing?");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Create a new class to connect to the device.
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
// arduino.js
|
|
46
|
+
import { Core } from 'webserial-core';
|
|
47
|
+
|
|
48
|
+
export class Arduino extends Core {
|
|
49
|
+
constructor(
|
|
50
|
+
{
|
|
51
|
+
filters = null,
|
|
52
|
+
config_port = {
|
|
53
|
+
baudRate: 9600,
|
|
54
|
+
dataBits: 8,
|
|
55
|
+
stopBits: 1,
|
|
56
|
+
parity: "none",
|
|
57
|
+
bufferSize: 32768,
|
|
58
|
+
flowControl: "none",
|
|
59
|
+
},
|
|
60
|
+
no_device = 1,
|
|
61
|
+
} = {
|
|
62
|
+
filters: null,
|
|
63
|
+
config_port: {
|
|
64
|
+
baudRate: 9600,
|
|
65
|
+
dataBits: 8,
|
|
66
|
+
stopBits: 1,
|
|
67
|
+
parity: "none",
|
|
68
|
+
bufferSize: 32768,
|
|
69
|
+
flowControl: "none",
|
|
70
|
+
},
|
|
71
|
+
no_device: 1,
|
|
72
|
+
}
|
|
73
|
+
) {
|
|
74
|
+
super({ filters, config_port, no_device });
|
|
75
|
+
this.__internal__.device.type = "arduino";
|
|
76
|
+
Devices.registerType(this.__internal__.device.type);
|
|
77
|
+
if (Devices.getByNumber(this.typeDevice, no_device)) {
|
|
78
|
+
throw new Error(`Device ${this.typeDevice} ${no_device} already exists`);
|
|
79
|
+
}
|
|
80
|
+
this.__internal__.time.response_connection = 2e3;
|
|
81
|
+
this.__internal__.time.response_general = 2e3;
|
|
82
|
+
this.__internal__.serial.delay_first_connection = 1_000;
|
|
83
|
+
this.#registerAvailableListenersArduino();
|
|
84
|
+
this.#touch();
|
|
85
|
+
this.getResponseAsString();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
#touch() {
|
|
89
|
+
Devices.add(this);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
#registerAvailableListenersArduino() {
|
|
93
|
+
/*const _ = [
|
|
94
|
+
'my_own_event_dispatched',
|
|
95
|
+
'my_other_own_event_dispatched',
|
|
96
|
+
];
|
|
97
|
+
for (const event of _) {
|
|
98
|
+
this.serialRegisterAvailableListener(event)
|
|
99
|
+
}
|
|
100
|
+
*/
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
serialMessage(codex) {
|
|
104
|
+
const message = {
|
|
105
|
+
code: [],
|
|
106
|
+
name: "",
|
|
107
|
+
description: "",
|
|
108
|
+
request: "",
|
|
109
|
+
no_code: 0,
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
message.code = codex;
|
|
113
|
+
|
|
114
|
+
switch (codex) {
|
|
115
|
+
case "connected":
|
|
116
|
+
message.name = "connected";
|
|
117
|
+
message.description = "Connection established";
|
|
118
|
+
message.request = "connect";
|
|
119
|
+
message.no_code = 100;
|
|
120
|
+
break;
|
|
121
|
+
case "created by danidoble":
|
|
122
|
+
message.name = "thanks";
|
|
123
|
+
message.description = "thanks for using this software";
|
|
124
|
+
message.request = "credits";
|
|
125
|
+
message.no_code = 101;
|
|
126
|
+
break;
|
|
127
|
+
case "hello there":
|
|
128
|
+
message.name = "hello there";
|
|
129
|
+
message.description = "hi human";
|
|
130
|
+
message.request = "hi";
|
|
131
|
+
message.no_code = 102;
|
|
132
|
+
break;
|
|
133
|
+
case "ara ara":
|
|
134
|
+
message.name = "ara ara";
|
|
135
|
+
message.description = "troll";
|
|
136
|
+
message.request = "ara ara";
|
|
137
|
+
message.no_code = 404;
|
|
138
|
+
break;
|
|
139
|
+
default:
|
|
140
|
+
message.name = "unknown";
|
|
141
|
+
message.description = "Unknown command";
|
|
142
|
+
message.request = "unknown";
|
|
143
|
+
message.no_code = 400;
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
this.dispatch("serial:message", message);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
serialSetConnectionConstant() {
|
|
151
|
+
return this.add0x(this.parseStringToBytes("CONNECT"));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async sayCredits() {
|
|
155
|
+
const arr = this.parseStringToBytes("CREDITS");
|
|
156
|
+
await this.appendToQueue(arr, "credits");
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async sayHi() {
|
|
160
|
+
const arr = this.parseStringToBytes("HI");
|
|
161
|
+
await this.appendToQueue(arr, "hi");
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async sayAra() {
|
|
165
|
+
const arr = this.parseStringToBytes("OTHER");
|
|
166
|
+
await this.appendToQueue(arr, "ara");
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async sendCustomCode({ code = "" } = { code: "" }) {
|
|
170
|
+
if (typeof code !== "string") throw new Error("Invalid string");
|
|
171
|
+
const arr = this.parseStringToBytes(code);
|
|
172
|
+
await this.appendToQueue(arr, "custom");
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Then you can use the class to connect to your device.
|
|
178
|
+
|
|
179
|
+
```javascript
|
|
180
|
+
// serialConnection.js
|
|
181
|
+
|
|
182
|
+
import { Arduino } from './arduino.js';
|
|
183
|
+
|
|
184
|
+
const arduino = new Arduino();
|
|
185
|
+
|
|
186
|
+
arduino.on('serial:message', (message) => {
|
|
187
|
+
console.log(message);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
arduino.on('serial:timeout', (data) => {
|
|
191
|
+
console.log('serial:timeout', data.detail);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// if you need to debug the data sent
|
|
195
|
+
// arduino.on('serial:sent', data => {
|
|
196
|
+
// console.log('serial:sent',data.detail);
|
|
197
|
+
// });
|
|
198
|
+
|
|
199
|
+
arduino.on('serial:error', (event) => {
|
|
200
|
+
document.getElementById('log').innerText += event.detail.message + '\n\n';
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// eslint-disable-next-line no-unused-vars
|
|
204
|
+
arduino.on('serial:disconnected', (event) => {
|
|
205
|
+
document.getElementById('log').innerText += 'Disconnected\n\n';
|
|
206
|
+
|
|
207
|
+
document.getElementById('disconnected').classList.remove('hidden');
|
|
208
|
+
document.getElementById('connect').classList.remove('hidden');
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// eslint-disable-next-line no-unused-vars
|
|
212
|
+
arduino.on('serial:connecting', (event) => {
|
|
213
|
+
document.getElementById('log').innerText += 'Connecting\n\n';
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// eslint-disable-next-line no-unused-vars
|
|
217
|
+
arduino.on('serial:connected', (event) => {
|
|
218
|
+
document.getElementById('log').innerText += 'Connected\n\n';
|
|
219
|
+
|
|
220
|
+
document.getElementById('disconnected').classList.add('hidden');
|
|
221
|
+
document.getElementById('need-permission').classList.add('hidden');
|
|
222
|
+
document.getElementById('connect').classList.add('hidden');
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
// eslint-disable-next-line no-unused-vars
|
|
226
|
+
arduino.on('serial:need-permission', (event) => {
|
|
227
|
+
document.getElementById('disconnected').classList.remove('hidden');
|
|
228
|
+
document.getElementById('need-permission').classList.remove('hidden');
|
|
229
|
+
document.getElementById('connect').classList.remove('hidden');
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// eslint-disable-next-line no-unused-vars
|
|
233
|
+
arduino.on('serial:soft-reload', (event) => {
|
|
234
|
+
// reset your variables
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// eslint-disable-next-line no-unused-vars
|
|
238
|
+
arduino.on('serial:unsupported', (event) => {
|
|
239
|
+
document.getElementById('unsupported').classList.remove('hidden');
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
function tryConnect() {
|
|
243
|
+
arduino
|
|
244
|
+
.connect()
|
|
245
|
+
.then(() => {})
|
|
246
|
+
.catch(console.error);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
250
|
+
tryConnect();
|
|
251
|
+
document.getElementById('connect').addEventListener('click', tryConnect);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
But wait still need to create the HTML file.
|
|
257
|
+
|
|
258
|
+
```html
|
|
259
|
+
<!doctype html>
|
|
260
|
+
<html lang="en">
|
|
261
|
+
|
|
262
|
+
<head>
|
|
263
|
+
<meta charset="UTF-8">
|
|
264
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
265
|
+
<title>Webserial</title>
|
|
266
|
+
<script src="./serialConnection.js" type="module"></script>
|
|
267
|
+
</head>
|
|
268
|
+
|
|
269
|
+
<body class="bg-neutral-950 text-white p-4 w-full">
|
|
270
|
+
|
|
271
|
+
<div class="webserial w-full max-w-xl mx-auto grid grid-cols-1 gap-y-4">
|
|
272
|
+
<div class="my-6"></div>
|
|
273
|
+
<button id="connect" class="hidden px-4 py-3 bg-gray-800 rounded-lg">Connect to serial</button>
|
|
274
|
+
|
|
275
|
+
<div id="need-permission" class="hidden p-4 bg-rose-900 rounded-lg">
|
|
276
|
+
Woooah! It seems that you need to give permission to access the serial port.
|
|
277
|
+
Please, click the button 'Connect to serial' to try again.
|
|
278
|
+
</div>
|
|
279
|
+
|
|
280
|
+
<div id="disconnected" class="hidden p-4 bg-neutral-900 w-full">
|
|
281
|
+
The arduino is disconnected. Please, check the connection.
|
|
282
|
+
</div>
|
|
283
|
+
|
|
284
|
+
<div id="unsupported" class="hidden p-4 bg-orange-700 w-full absolute bottom-0 left-0">
|
|
285
|
+
This browser does not support the WebSerial API. Please, use a compatible browser.
|
|
286
|
+
</div>
|
|
287
|
+
|
|
288
|
+
<div id="log" class="bg-neutral-800 p-4 rounded-lg">
|
|
289
|
+
Log: <br>
|
|
290
|
+
</div>
|
|
291
|
+
</div>
|
|
292
|
+
|
|
293
|
+
<script src="https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio,container-queries"></script>
|
|
294
|
+
</body>
|
|
295
|
+
|
|
296
|
+
</html>
|
|
297
|
+
```
|
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
var N = Object.defineProperty;
|
|
2
|
+
var b = (r) => {
|
|
3
|
+
throw TypeError(r);
|
|
4
|
+
};
|
|
5
|
+
var O = (r, i, e) => i in r ? N(r, i, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[i] = e;
|
|
6
|
+
var h = (r, i, e) => O(r, typeof i != "symbol" ? i + "" : i, e), H = (r, i, e) => i.has(r) || b("Cannot " + e);
|
|
7
|
+
var m = (r, i, e) => i.has(r) ? b("Cannot add the same private member more than once") : i instanceof WeakSet ? i.add(r) : i.set(r, e);
|
|
8
|
+
var l = (r, i, e) => (H(r, i, "access private method"), e);
|
|
9
|
+
const _ = [];
|
|
10
|
+
for (let r = 0; r < 256; ++r)
|
|
11
|
+
_.push((r + 256).toString(16).slice(1));
|
|
12
|
+
function j(r, i = 0) {
|
|
13
|
+
return (_[r[i + 0]] + _[r[i + 1]] + _[r[i + 2]] + _[r[i + 3]] + "-" + _[r[i + 4]] + _[r[i + 5]] + "-" + _[r[i + 6]] + _[r[i + 7]] + "-" + _[r[i + 8]] + _[r[i + 9]] + "-" + _[r[i + 10]] + _[r[i + 11]] + _[r[i + 12]] + _[r[i + 13]] + _[r[i + 14]] + _[r[i + 15]]).toLowerCase();
|
|
14
|
+
}
|
|
15
|
+
let p;
|
|
16
|
+
const B = new Uint8Array(16);
|
|
17
|
+
function F() {
|
|
18
|
+
if (!p) {
|
|
19
|
+
if (typeof crypto > "u" || !crypto.getRandomValues)
|
|
20
|
+
throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
|
|
21
|
+
p = crypto.getRandomValues.bind(crypto);
|
|
22
|
+
}
|
|
23
|
+
return p(B);
|
|
24
|
+
}
|
|
25
|
+
const $ = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), w = { randomUUID: $ };
|
|
26
|
+
function V(r, i, e) {
|
|
27
|
+
var n;
|
|
28
|
+
if (w.randomUUID && !r)
|
|
29
|
+
return w.randomUUID();
|
|
30
|
+
r = r || {};
|
|
31
|
+
const t = r.random ?? ((n = r.rng) == null ? void 0 : n.call(r)) ?? F();
|
|
32
|
+
if (t.length < 16)
|
|
33
|
+
throw new Error("Random bytes length must be >= 16");
|
|
34
|
+
return t[6] = t[6] & 15 | 64, t[8] = t[8] & 63 | 128, j(t);
|
|
35
|
+
}
|
|
36
|
+
class v extends CustomEvent {
|
|
37
|
+
constructor(i, e) {
|
|
38
|
+
super(i, e);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
class S extends EventTarget {
|
|
42
|
+
constructor() {
|
|
43
|
+
super(...arguments);
|
|
44
|
+
h(this, "__listeners__", {
|
|
45
|
+
debug: !1
|
|
46
|
+
});
|
|
47
|
+
h(this, "__debug__", !1);
|
|
48
|
+
}
|
|
49
|
+
dispatch(e, t = null) {
|
|
50
|
+
const n = new v(e, { detail: t });
|
|
51
|
+
this.dispatchEvent(n), this.__debug__ && this.dispatchEvent(new v("debug", { detail: { type: e, data: t } }));
|
|
52
|
+
}
|
|
53
|
+
dispatchAsync(e, t = null, n = 100) {
|
|
54
|
+
const s = this;
|
|
55
|
+
setTimeout(() => {
|
|
56
|
+
s.dispatch(e, t);
|
|
57
|
+
}, n);
|
|
58
|
+
}
|
|
59
|
+
on(e, t) {
|
|
60
|
+
typeof this.__listeners__[e] < "u" && !this.__listeners__[e] && (this.__listeners__[e] = !0), this.addEventListener(e, t);
|
|
61
|
+
}
|
|
62
|
+
off(e, t) {
|
|
63
|
+
this.removeEventListener(e, t);
|
|
64
|
+
}
|
|
65
|
+
serialRegisterAvailableListener(e) {
|
|
66
|
+
this.__listeners__[e] || (this.__listeners__[e] = !1);
|
|
67
|
+
}
|
|
68
|
+
get availableListeners() {
|
|
69
|
+
return Object.keys(this.__listeners__).sort().map((t) => ({
|
|
70
|
+
type: t,
|
|
71
|
+
listening: this.__listeners__[t]
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function x(r = 100) {
|
|
76
|
+
return new Promise(
|
|
77
|
+
(i) => setTimeout(() => i(), r)
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
function Q() {
|
|
81
|
+
return "serial" in navigator;
|
|
82
|
+
}
|
|
83
|
+
const f = {
|
|
84
|
+
baudRate: 9600,
|
|
85
|
+
dataBits: 8,
|
|
86
|
+
stopBits: 1,
|
|
87
|
+
parity: "none",
|
|
88
|
+
bufferSize: 32768,
|
|
89
|
+
flowControl: "none"
|
|
90
|
+
};
|
|
91
|
+
var a, C, g, c, A, E, T, U, k, D, I, L, P, q, R, M;
|
|
92
|
+
class z extends S {
|
|
93
|
+
constructor({
|
|
94
|
+
filters: e = null,
|
|
95
|
+
config_port: t = f,
|
|
96
|
+
no_device: n = 1,
|
|
97
|
+
device_listen_on_channel: s = 1
|
|
98
|
+
} = {
|
|
99
|
+
filters: null,
|
|
100
|
+
config_port: f,
|
|
101
|
+
no_device: 1,
|
|
102
|
+
device_listen_on_channel: 1
|
|
103
|
+
}) {
|
|
104
|
+
super();
|
|
105
|
+
m(this, a);
|
|
106
|
+
h(this, "__internal__", {
|
|
107
|
+
auto_response: !1,
|
|
108
|
+
device_number: 1,
|
|
109
|
+
aux_port_connector: 0,
|
|
110
|
+
last_error: {
|
|
111
|
+
message: null,
|
|
112
|
+
action: null,
|
|
113
|
+
code: null,
|
|
114
|
+
no_code: 0
|
|
115
|
+
},
|
|
116
|
+
serial: {
|
|
117
|
+
connected: !1,
|
|
118
|
+
port: null,
|
|
119
|
+
last_action: null,
|
|
120
|
+
response: {
|
|
121
|
+
length: null,
|
|
122
|
+
buffer: new Uint8Array([]),
|
|
123
|
+
as: "hex"
|
|
124
|
+
},
|
|
125
|
+
reader: null,
|
|
126
|
+
input_done: null,
|
|
127
|
+
output_done: null,
|
|
128
|
+
input_stream: null,
|
|
129
|
+
output_stream: null,
|
|
130
|
+
keep_reading: !0,
|
|
131
|
+
time_until_send_bytes: void 0,
|
|
132
|
+
delay_first_connection: 200,
|
|
133
|
+
bytes_connection: null,
|
|
134
|
+
filters: [],
|
|
135
|
+
config_port: f,
|
|
136
|
+
queue: []
|
|
137
|
+
},
|
|
138
|
+
device: {
|
|
139
|
+
type: "unknown",
|
|
140
|
+
id: V(),
|
|
141
|
+
listen_on_port: null
|
|
142
|
+
},
|
|
143
|
+
time: {
|
|
144
|
+
response_connection: 500,
|
|
145
|
+
response_general: 2e3
|
|
146
|
+
},
|
|
147
|
+
timeout: {
|
|
148
|
+
until_response: 0
|
|
149
|
+
},
|
|
150
|
+
interval: {
|
|
151
|
+
reconnection: 0
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
e && (this.serialFilters = e), t && (this.serialConfigPort = t), n && l(this, a, R).call(this, n), s && ["number", "string"].includes(typeof s) && (this.listenOnChannel = s), l(this, a, I).call(this), l(this, a, L).call(this);
|
|
155
|
+
}
|
|
156
|
+
set listenOnChannel(e) {
|
|
157
|
+
if (typeof e == "string" && (e = parseInt(e)), isNaN(e) || e < 1 || e > 255)
|
|
158
|
+
throw new Error("Invalid port number");
|
|
159
|
+
this.__internal__.device.listen_on_port = e, this.__internal__.serial.bytes_connection = this.serialSetConnectionConstant(e);
|
|
160
|
+
}
|
|
161
|
+
get lastAction() {
|
|
162
|
+
return this.__internal__.serial.last_action;
|
|
163
|
+
}
|
|
164
|
+
get listenOnChannel() {
|
|
165
|
+
return this.__internal__.device.listen_on_port ?? 1;
|
|
166
|
+
}
|
|
167
|
+
set serialFilters(e) {
|
|
168
|
+
this.__internal__.serial.filters = e;
|
|
169
|
+
}
|
|
170
|
+
get serialFilters() {
|
|
171
|
+
return this.__internal__.serial.filters;
|
|
172
|
+
}
|
|
173
|
+
set serialConfigPort(e) {
|
|
174
|
+
this.__internal__.serial.config_port = e;
|
|
175
|
+
}
|
|
176
|
+
get serialConfigPort() {
|
|
177
|
+
return this.__internal__.serial.config_port;
|
|
178
|
+
}
|
|
179
|
+
get isConnected() {
|
|
180
|
+
return this.__internal__.serial.connected;
|
|
181
|
+
}
|
|
182
|
+
get isDisconnected() {
|
|
183
|
+
return !this.__internal__.serial.connected;
|
|
184
|
+
}
|
|
185
|
+
get deviceNumber() {
|
|
186
|
+
return this.__internal__.device_number;
|
|
187
|
+
}
|
|
188
|
+
get uuid() {
|
|
189
|
+
return this.__internal__.device.id;
|
|
190
|
+
}
|
|
191
|
+
get typeDevice() {
|
|
192
|
+
return this.__internal__.device.type;
|
|
193
|
+
}
|
|
194
|
+
get queue() {
|
|
195
|
+
return this.__internal__.serial.queue;
|
|
196
|
+
}
|
|
197
|
+
async timeout(e, t) {
|
|
198
|
+
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", {})) : t === "connection:start" && (await this.serialDisconnect(), this.__internal__.serial.connected = !1, this.__internal__.aux_port_connector += 1, await this.serialConnect()), this.dispatch("serial:timeout", {
|
|
199
|
+
...this.__internal__.last_error,
|
|
200
|
+
bytes: e,
|
|
201
|
+
action: t
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
async disconnect(e = null) {
|
|
205
|
+
await this.serialDisconnect(), this.__internal__.serial.connected = !1, this.__internal__.aux_port_connector = 0, this.dispatch("serial:disconnected", e);
|
|
206
|
+
}
|
|
207
|
+
async connect() {
|
|
208
|
+
return new Promise((e, t) => {
|
|
209
|
+
Q() || t("Web Serial not supported"), setTimeout(async () => {
|
|
210
|
+
await x(499), await this.serialConnect(), this.isConnected ? e(`${this.typeDevice} device ${this.deviceNumber} connected`) : t(`${this.typeDevice} device ${this.deviceNumber} not connected`);
|
|
211
|
+
}, 1);
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
async serialDisconnect() {
|
|
215
|
+
try {
|
|
216
|
+
const e = this.__internal__.serial.reader, t = this.__internal__.serial.output_stream;
|
|
217
|
+
e && (await e.cancel().catch((s) => this.serialErrors(s)), 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();
|
|
218
|
+
} catch (e) {
|
|
219
|
+
this.serialErrors(e);
|
|
220
|
+
} finally {
|
|
221
|
+
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;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
getResponseAsArrayBuffer() {
|
|
225
|
+
this.__internal__.serial.response.as = "arraybuffer";
|
|
226
|
+
}
|
|
227
|
+
getResponseAsArrayHex() {
|
|
228
|
+
this.__internal__.serial.response.as = "hex";
|
|
229
|
+
}
|
|
230
|
+
getResponseAsUint8Array() {
|
|
231
|
+
this.__internal__.serial.response.as = "uint8";
|
|
232
|
+
}
|
|
233
|
+
getResponseAsString() {
|
|
234
|
+
this.__internal__.serial.response.as = "string";
|
|
235
|
+
}
|
|
236
|
+
async serialPortsSaved(e) {
|
|
237
|
+
const t = this.serialFilters;
|
|
238
|
+
if (this.__internal__.aux_port_connector < e.length) {
|
|
239
|
+
const n = this.__internal__.aux_port_connector;
|
|
240
|
+
this.__internal__.serial.port = e[n];
|
|
241
|
+
} else
|
|
242
|
+
this.__internal__.aux_port_connector = 0, this.__internal__.serial.port = await navigator.serial.requestPort({
|
|
243
|
+
filters: t
|
|
244
|
+
});
|
|
245
|
+
if (!this.__internal__.serial.port)
|
|
246
|
+
throw new Error("Select another port please");
|
|
247
|
+
}
|
|
248
|
+
serialErrors(e) {
|
|
249
|
+
const t = e.toString().toLowerCase();
|
|
250
|
+
switch (!0) {
|
|
251
|
+
case t.includes("must be handling a user gesture to show a permission request"):
|
|
252
|
+
case t.includes("the port is closed."):
|
|
253
|
+
case t.includes("the port is closed or is not writable"):
|
|
254
|
+
case t.includes("select another port please"):
|
|
255
|
+
case t.includes("no port selected by the user"):
|
|
256
|
+
case t.includes(
|
|
257
|
+
"this readable stream reader has been released and cannot be used to cancel its previous owner stream"
|
|
258
|
+
):
|
|
259
|
+
this.dispatch("serial:need-permission", {});
|
|
260
|
+
break;
|
|
261
|
+
case t.includes("the port is already open."):
|
|
262
|
+
case t.includes("failed to open serial port"):
|
|
263
|
+
this.serialDisconnect().then(async () => {
|
|
264
|
+
this.__internal__.aux_port_connector += 1, await this.serialConnect();
|
|
265
|
+
});
|
|
266
|
+
break;
|
|
267
|
+
case t.includes("cannot read properties of undefined (reading 'writable')"):
|
|
268
|
+
case t.includes("cannot read properties of null (reading 'writable')"):
|
|
269
|
+
case t.includes("cannot read property 'writable' of null"):
|
|
270
|
+
case t.includes("cannot read property 'writable' of undefined"):
|
|
271
|
+
this.serialDisconnect().then(async () => {
|
|
272
|
+
await this.serialConnect();
|
|
273
|
+
});
|
|
274
|
+
break;
|
|
275
|
+
case t.includes("'close' on 'serialport': a call to close() is already in progress."):
|
|
276
|
+
break;
|
|
277
|
+
case t.includes("failed to execute 'open' on 'serialport': a call to open() is already in progress."):
|
|
278
|
+
break;
|
|
279
|
+
case t.includes("the port is already closed."):
|
|
280
|
+
break;
|
|
281
|
+
case t.includes("the device has been lost"):
|
|
282
|
+
this.dispatch("serial:lost", {});
|
|
283
|
+
break;
|
|
284
|
+
case t.includes("navigator.serial is undefined"):
|
|
285
|
+
this.dispatch("serial:unsupported", {});
|
|
286
|
+
break;
|
|
287
|
+
default:
|
|
288
|
+
console.error(e);
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
this.dispatch("serial:error", e);
|
|
292
|
+
}
|
|
293
|
+
async serialConnect() {
|
|
294
|
+
try {
|
|
295
|
+
this.dispatch("serial:connecting", {});
|
|
296
|
+
const e = await l(this, a, A).call(this);
|
|
297
|
+
if (e.length > 0)
|
|
298
|
+
await this.serialPortsSaved(e);
|
|
299
|
+
else {
|
|
300
|
+
const s = this.serialFilters;
|
|
301
|
+
this.__internal__.serial.port = await navigator.serial.requestPort({
|
|
302
|
+
filters: s
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
const t = this.__internal__.serial.port;
|
|
306
|
+
if (!t)
|
|
307
|
+
throw new Error("No port selected by the user");
|
|
308
|
+
await t.open(this.serialConfigPort);
|
|
309
|
+
const n = this;
|
|
310
|
+
t.onconnect = (s) => {
|
|
311
|
+
n.dispatch("serial:connected", s), n.__internal__.serial.queue.length > 0 && n.dispatch("internal:queue", {});
|
|
312
|
+
}, t.ondisconnect = async () => {
|
|
313
|
+
await n.disconnect();
|
|
314
|
+
}, await x(this.__internal__.serial.delay_first_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
|
|
315
|
+
await n.timeout(n.__internal__.serial.bytes_connection ?? [], "connection:start");
|
|
316
|
+
}, this.__internal__.time.response_connection), this.__internal__.serial.last_action = "connect", await l(this, a, g).call(this, this.__internal__.serial.bytes_connection ?? []), this.dispatch("serial:sent", {
|
|
317
|
+
action: "connect",
|
|
318
|
+
bytes: this.__internal__.serial.bytes_connection
|
|
319
|
+
}), this.__internal__.auto_response && l(this, a, c).call(this, ["DD", "DD"], null), await l(this, a, k).call(this);
|
|
320
|
+
} catch (e) {
|
|
321
|
+
this.serialErrors(e);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
async serialForget() {
|
|
325
|
+
return await l(this, a, D).call(this);
|
|
326
|
+
}
|
|
327
|
+
decToHex(e) {
|
|
328
|
+
return typeof e == "string" && (e = parseInt(e, 10)), e.toString(16);
|
|
329
|
+
}
|
|
330
|
+
hexToDec(e) {
|
|
331
|
+
return parseInt(e, 16);
|
|
332
|
+
}
|
|
333
|
+
hexMaker(e = "00", t = 2) {
|
|
334
|
+
return e.toString().padStart(t, "0").toLowerCase();
|
|
335
|
+
}
|
|
336
|
+
add0x(e) {
|
|
337
|
+
const t = [];
|
|
338
|
+
return e.forEach((n, s) => {
|
|
339
|
+
t[s] = "0x" + n;
|
|
340
|
+
}), t;
|
|
341
|
+
}
|
|
342
|
+
bytesToHex(e) {
|
|
343
|
+
return this.add0x(Array.from(e, (t) => this.hexMaker(t)));
|
|
344
|
+
}
|
|
345
|
+
async appendToQueue(e, t) {
|
|
346
|
+
const n = this.bytesToHex(e);
|
|
347
|
+
if (["connect", "connection:start"].includes(t)) {
|
|
348
|
+
if (this.__internal__.serial.connected) return;
|
|
349
|
+
await this.serialConnect();
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
this.__internal__.serial.queue.push({ bytes: n, action: t }), this.dispatch("internal:queue", {});
|
|
353
|
+
}
|
|
354
|
+
serialSetConnectionConstant(e = 1) {
|
|
355
|
+
throw new Error(`Method not implemented 'serialSetConnectionConstant' to listen on channel ${e}`);
|
|
356
|
+
}
|
|
357
|
+
serialMessage(e) {
|
|
358
|
+
throw console.log(e), new Error("Method not implemented 'serialMessage'");
|
|
359
|
+
}
|
|
360
|
+
serialCorruptMessage(e, t) {
|
|
361
|
+
throw console.log(e, t), new Error("Method not implemented 'serialCorruptMessage'");
|
|
362
|
+
}
|
|
363
|
+
clearSerialQueue() {
|
|
364
|
+
this.__internal__.serial.queue = [];
|
|
365
|
+
}
|
|
366
|
+
sumHex(e) {
|
|
367
|
+
let t = 0;
|
|
368
|
+
return e.forEach((n) => {
|
|
369
|
+
t += parseInt(n, 16);
|
|
370
|
+
}), t.toString(16);
|
|
371
|
+
}
|
|
372
|
+
toString() {
|
|
373
|
+
return JSON.stringify({
|
|
374
|
+
__class: this.typeDevice,
|
|
375
|
+
device_number: this.deviceNumber,
|
|
376
|
+
uuid: this.uuid,
|
|
377
|
+
connected: this.isConnected,
|
|
378
|
+
connection: this.__internal__.serial.bytes_connection
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
softReload() {
|
|
382
|
+
l(this, a, M).call(this), this.dispatch("serial:soft-reload", {});
|
|
383
|
+
}
|
|
384
|
+
async sendConnect() {
|
|
385
|
+
if (!this.__internal__.serial.bytes_connection)
|
|
386
|
+
throw new Error("No connection bytes defined");
|
|
387
|
+
await this.appendToQueue(this.__internal__.serial.bytes_connection, "connect");
|
|
388
|
+
}
|
|
389
|
+
// @ts-expect-error code is required but can be empty
|
|
390
|
+
async sendCustomCode({ code: e = [] } = { code: [] }) {
|
|
391
|
+
if (e === null || e.length === 0)
|
|
392
|
+
throw new Error("No data to send");
|
|
393
|
+
await this.appendToQueue(e, "custom");
|
|
394
|
+
}
|
|
395
|
+
stringToArrayHex(e) {
|
|
396
|
+
return Array.from(e).map((t) => t.charCodeAt(0).toString(16));
|
|
397
|
+
}
|
|
398
|
+
stringToArrayBuffer(e, t = `
|
|
399
|
+
`) {
|
|
400
|
+
return this.parseStringToTextEncoder(e, t).buffer;
|
|
401
|
+
}
|
|
402
|
+
parseStringToTextEncoder(e = "", t = `
|
|
403
|
+
`) {
|
|
404
|
+
const n = new TextEncoder();
|
|
405
|
+
return e += t, n.encode(e);
|
|
406
|
+
}
|
|
407
|
+
parseStringToBytes(e = "", t = `
|
|
408
|
+
`) {
|
|
409
|
+
const n = this.parseStringToTextEncoder(e, t);
|
|
410
|
+
return Array.from(n).map((s) => s.toString(16));
|
|
411
|
+
}
|
|
412
|
+
parseUint8ToHex(e) {
|
|
413
|
+
return Array.from(e).map((t) => t.toString(16));
|
|
414
|
+
}
|
|
415
|
+
parseHexToUint8(e) {
|
|
416
|
+
return new Uint8Array(e.map((t) => parseInt(t, 16)));
|
|
417
|
+
}
|
|
418
|
+
stringArrayToUint8Array(e) {
|
|
419
|
+
const t = [];
|
|
420
|
+
return e.forEach((n) => {
|
|
421
|
+
const s = n.replace("0x", "");
|
|
422
|
+
t.push(parseInt(s, 16));
|
|
423
|
+
}), new Uint8Array(t);
|
|
424
|
+
}
|
|
425
|
+
parseUint8ArrayToString(e) {
|
|
426
|
+
const t = this.stringArrayToUint8Array(e);
|
|
427
|
+
e = this.parseUint8ToHex(t);
|
|
428
|
+
const n = e.map((s) => parseInt(s, 16));
|
|
429
|
+
return String.fromCharCode(...n).replace(/[\n\r]+/g, "");
|
|
430
|
+
}
|
|
431
|
+
hexToAscii(e) {
|
|
432
|
+
const t = e.toString();
|
|
433
|
+
let n = "";
|
|
434
|
+
for (let s = 0; s < t.length; s += 2)
|
|
435
|
+
n += String.fromCharCode(parseInt(t.substring(s, 2), 16));
|
|
436
|
+
return n;
|
|
437
|
+
}
|
|
438
|
+
asciiToHex(e) {
|
|
439
|
+
const t = [];
|
|
440
|
+
for (let n = 0, s = e.length; n < s; n++) {
|
|
441
|
+
const u = Number(e.charCodeAt(n)).toString(16);
|
|
442
|
+
t.push(u);
|
|
443
|
+
}
|
|
444
|
+
return t.join("");
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
a = new WeakSet(), C = function(e) {
|
|
448
|
+
return !!(e.readable && e.writable);
|
|
449
|
+
}, g = async function(e) {
|
|
450
|
+
const t = this.__internal__.serial.port;
|
|
451
|
+
if (!t || !t.writable)
|
|
452
|
+
throw new Error("The port is closed or is not writable");
|
|
453
|
+
const n = this.stringArrayToUint8Array(e), s = t.writable.getWriter();
|
|
454
|
+
await s.write(n), s.releaseLock();
|
|
455
|
+
}, c = function(e = [], t = null) {
|
|
456
|
+
if (e && e.length > 0) {
|
|
457
|
+
this.__internal__.serial.connected || this.dispatch("serial:connected"), this.__internal__.serial.connected = !0, 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);
|
|
458
|
+
const n = [];
|
|
459
|
+
for (const s in e)
|
|
460
|
+
n.push(e[s].toString().padStart(2, "0").toLowerCase());
|
|
461
|
+
if (this.__internal__.serial.response.as === "hex")
|
|
462
|
+
this.serialMessage(n);
|
|
463
|
+
else if (this.__internal__.serial.response.as === "uint8")
|
|
464
|
+
this.serialMessage(this.parseHexToUint8(this.add0x(n)));
|
|
465
|
+
else if (this.__internal__.serial.response.as === "string")
|
|
466
|
+
this.serialMessage(this.parseUint8ArrayToString(this.add0x(n)));
|
|
467
|
+
else {
|
|
468
|
+
const s = this.stringToArrayBuffer(
|
|
469
|
+
this.parseUint8ArrayToString(this.add0x(n))
|
|
470
|
+
);
|
|
471
|
+
this.serialMessage(s);
|
|
472
|
+
}
|
|
473
|
+
} else
|
|
474
|
+
this.serialCorruptMessage(e, t);
|
|
475
|
+
this.__internal__.serial.queue.length !== 0 && this.dispatch("internal:queue", {});
|
|
476
|
+
}, A = async function() {
|
|
477
|
+
const e = this.serialFilters, t = await navigator.serial.getPorts({ filters: e });
|
|
478
|
+
return e.length === 0 ? t : t.filter((s) => {
|
|
479
|
+
const u = s.getInfo();
|
|
480
|
+
return e.some((y) => u.usbProductId === y.usbProductId && u.usbVendorId === y.usbVendorId);
|
|
481
|
+
}).filter((s) => !l(this, a, C).call(this, s));
|
|
482
|
+
}, E = function(e) {
|
|
483
|
+
if (e) {
|
|
484
|
+
const t = this.__internal__.serial.response.buffer, n = new Uint8Array(t.length + e.byteLength);
|
|
485
|
+
n.set(t, 0), n.set(new Uint8Array(e), t.length), this.__internal__.serial.response.buffer = n;
|
|
486
|
+
}
|
|
487
|
+
}, T = async function() {
|
|
488
|
+
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(() => {
|
|
489
|
+
const e = [];
|
|
490
|
+
for (const t in this.__internal__.serial.response.buffer)
|
|
491
|
+
e.push(this.__internal__.serial.response.buffer[t].toString(16));
|
|
492
|
+
this.__internal__.serial.response.buffer && l(this, a, c).call(this, e), this.__internal__.serial.response.buffer = new Uint8Array(0);
|
|
493
|
+
}, 400);
|
|
494
|
+
}, U = async function() {
|
|
495
|
+
if (this.__internal__.serial.response.length !== null) {
|
|
496
|
+
if (this.__internal__.serial.response.length === this.__internal__.serial.response.buffer.length) {
|
|
497
|
+
const e = [];
|
|
498
|
+
for (const t in this.__internal__.serial.response.buffer)
|
|
499
|
+
e.push(this.__internal__.serial.response.buffer[t].toString(16));
|
|
500
|
+
l(this, a, c).call(this, e), this.__internal__.serial.response.buffer = new Uint8Array(0);
|
|
501
|
+
} else if (this.__internal__.serial.response.length < this.__internal__.serial.response.buffer.length) {
|
|
502
|
+
let e = new Uint8Array(0);
|
|
503
|
+
for (let n = 0; n < this.__internal__.serial.response.length; n++)
|
|
504
|
+
e[n] = this.__internal__.serial.response.buffer[n];
|
|
505
|
+
if (e.length === this.__internal__.serial.response.length) {
|
|
506
|
+
const n = [];
|
|
507
|
+
for (const s in e)
|
|
508
|
+
n.push(e[s].toString(16));
|
|
509
|
+
l(this, a, c).call(this, n), this.__internal__.serial.response.buffer = new Uint8Array(0);
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
e = new Uint8Array(0);
|
|
513
|
+
const t = this.__internal__.serial.response.length * 2;
|
|
514
|
+
if (this.__internal__.serial.response.buffer.length === t) {
|
|
515
|
+
for (let n = 14; n < t; n++)
|
|
516
|
+
e[n - this.__internal__.serial.response.length] = this.__internal__.serial.response.buffer[n];
|
|
517
|
+
if (e.length === this.__internal__.serial.response.length) {
|
|
518
|
+
const n = [];
|
|
519
|
+
for (const s in e)
|
|
520
|
+
n.push(e[s].toString(16));
|
|
521
|
+
l(this, a, c).call(this, n), this.__internal__.serial.response.buffer = new Uint8Array(0);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}, k = async function() {
|
|
527
|
+
const e = this.__internal__.serial.port;
|
|
528
|
+
if (!e || !e.readable) throw new Error("Port is not readable");
|
|
529
|
+
for (; e.readable && this.__internal__.serial.keep_reading; ) {
|
|
530
|
+
const t = e.readable.getReader();
|
|
531
|
+
this.__internal__.serial.reader = t;
|
|
532
|
+
try {
|
|
533
|
+
let n = !0;
|
|
534
|
+
for (; n; ) {
|
|
535
|
+
const { value: s, done: u } = await t.read();
|
|
536
|
+
if (u) {
|
|
537
|
+
t.releaseLock(), this.__internal__.serial.keep_reading = !1, n = !1;
|
|
538
|
+
break;
|
|
539
|
+
}
|
|
540
|
+
l(this, a, E).call(this, s), this.__internal__.serial.response.length === null ? await l(this, a, T).call(this) : await l(this, a, U).call(this);
|
|
541
|
+
}
|
|
542
|
+
} catch (n) {
|
|
543
|
+
this.serialErrors(n);
|
|
544
|
+
} finally {
|
|
545
|
+
t.releaseLock();
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
this.__internal__.serial.keep_reading = !0, this.__internal__.serial.port && await this.__internal__.serial.port.close();
|
|
549
|
+
}, D = async function() {
|
|
550
|
+
return typeof window > "u" ? !1 : "serial" in navigator && "forget" in SerialPort.prototype && this.__internal__.serial.port ? (await this.__internal__.serial.port.forget(), !0) : !1;
|
|
551
|
+
}, I = function() {
|
|
552
|
+
[
|
|
553
|
+
"serial:connected",
|
|
554
|
+
"serial:connecting",
|
|
555
|
+
"serial:reconnect",
|
|
556
|
+
"serial:timeout",
|
|
557
|
+
"serial:disconnected",
|
|
558
|
+
"serial:sent",
|
|
559
|
+
"serial:soft-reload",
|
|
560
|
+
"serial:message",
|
|
561
|
+
"unknown",
|
|
562
|
+
"serial:need-permission",
|
|
563
|
+
"serial:lost",
|
|
564
|
+
"serial:unsupported",
|
|
565
|
+
"serial:error",
|
|
566
|
+
"debug"
|
|
567
|
+
].forEach((t) => {
|
|
568
|
+
this.serialRegisterAvailableListener(t);
|
|
569
|
+
});
|
|
570
|
+
}, L = function() {
|
|
571
|
+
const e = this;
|
|
572
|
+
this.on("internal:queue", async () => {
|
|
573
|
+
var t;
|
|
574
|
+
await l(t = e, a, q).call(t);
|
|
575
|
+
}), l(this, a, P).call(this);
|
|
576
|
+
}, P = function() {
|
|
577
|
+
const e = this;
|
|
578
|
+
navigator.serial.addEventListener("connect", async () => {
|
|
579
|
+
e.isDisconnected && await e.serialConnect().catch(() => {
|
|
580
|
+
});
|
|
581
|
+
});
|
|
582
|
+
}, q = async function() {
|
|
583
|
+
if (!this.__internal__.serial.connected) {
|
|
584
|
+
await this.serialConnect();
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
587
|
+
if (this.__internal__.timeout.until_response || this.__internal__.serial.queue.length === 0) return;
|
|
588
|
+
const e = this.__internal__.serial.queue[0];
|
|
589
|
+
let t = this.__internal__.time.response_general;
|
|
590
|
+
e.action === "connect" && (t = this.__internal__.time.response_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
|
|
591
|
+
await this.timeout(e.bytes, e.action);
|
|
592
|
+
}, t), this.__internal__.serial.last_action = e.action ?? "unknown", await l(this, a, g).call(this, e.bytes), this.dispatch("serial:sent", {
|
|
593
|
+
action: e.action,
|
|
594
|
+
bytes: e.bytes
|
|
595
|
+
}), this.__internal__.auto_response && l(this, a, c).call(this, ["DD", "DD"], null);
|
|
596
|
+
const n = [...this.__internal__.serial.queue];
|
|
597
|
+
this.__internal__.serial.queue = n.splice(1);
|
|
598
|
+
}, R = function(e = 1) {
|
|
599
|
+
this.__internal__.device_number = e, this.__internal__.serial.bytes_connection = this.serialSetConnectionConstant(e);
|
|
600
|
+
}, M = function() {
|
|
601
|
+
this.__internal__.last_error = {
|
|
602
|
+
message: null,
|
|
603
|
+
action: null,
|
|
604
|
+
code: null,
|
|
605
|
+
no_code: 0
|
|
606
|
+
};
|
|
607
|
+
};
|
|
608
|
+
const o = class o extends S {
|
|
609
|
+
static typeError(i) {
|
|
610
|
+
const e = new Error();
|
|
611
|
+
throw e.message = `Type ${i} is not supported`, e.name = "DeviceTypeError", e;
|
|
612
|
+
}
|
|
613
|
+
static registerType(i) {
|
|
614
|
+
typeof o.devices[i] > "u" && (o.devices[i] = {});
|
|
615
|
+
}
|
|
616
|
+
static add(i) {
|
|
617
|
+
const e = i.typeDevice;
|
|
618
|
+
typeof o.devices[e] > "u" && (o.devices[e] = {});
|
|
619
|
+
const t = i.uuid;
|
|
620
|
+
if (typeof o.devices[e] > "u" && o.typeError(e), this.instance.dispatch("change", o.devices), o.devices[e][t])
|
|
621
|
+
throw new Error(`Device with id ${t} already exists`);
|
|
622
|
+
return o.devices[e][t] = i, this.instance.dispatch("change", o.devices), Object.keys(o.devices[e]).indexOf(t);
|
|
623
|
+
}
|
|
624
|
+
static get(i, e) {
|
|
625
|
+
return typeof o.devices[i] > "u" && (o.devices[i] = {}), typeof o.devices[i] > "u" && o.typeError(i), o.devices[i][e];
|
|
626
|
+
}
|
|
627
|
+
static getAll(i = null) {
|
|
628
|
+
return i === null ? o.devices : (typeof o.devices[i] > "u" && o.typeError(i), o.devices[i]);
|
|
629
|
+
}
|
|
630
|
+
static getList() {
|
|
631
|
+
return Object.values(o.devices).map((e) => Object.values(e)).flat();
|
|
632
|
+
}
|
|
633
|
+
static getByNumber(i, e) {
|
|
634
|
+
return typeof o.devices[i] > "u" && o.typeError(i), Object.values(o.devices[i]).find((n) => n.deviceNumber === e) ?? null;
|
|
635
|
+
}
|
|
636
|
+
static getCustom(i, e = 1) {
|
|
637
|
+
return typeof o.devices[i] > "u" && o.typeError(i), Object.values(o.devices[i]).find((n) => n.deviceNumber === e) ?? null;
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
h(o, "instance"), h(o, "devices", {});
|
|
641
|
+
let d = o;
|
|
642
|
+
d.instance || (d.instance = new d());
|
|
643
|
+
export {
|
|
644
|
+
z as Core,
|
|
645
|
+
d as Devices,
|
|
646
|
+
S as Dispatcher
|
|
647
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
(function(_,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(_=typeof globalThis<"u"?globalThis:_||self,s(_.WebSerialCore={}))})(this,function(_){"use strict";var V=Object.defineProperty;var C=_=>{throw TypeError(_)};var W=(_,s,u)=>s in _?V(_,s,{enumerable:!0,configurable:!0,writable:!0,value:u}):_[s]=u;var f=(_,s,u)=>W(_,typeof s!="symbol"?s+"":s,u),Q=(_,s,u)=>s.has(_)||C("Cannot "+u);var T=(_,s,u)=>s.has(_)?C("Cannot add the same private member more than once"):s instanceof WeakSet?s.add(_):s.set(_,u);var c=(_,s,u)=>(Q(_,s,"access private method"),u);var a,A,m,h,E,U,D,k,I,L,P,q,M,R,N,O;const s=[];for(let o=0;o<256;++o)s.push((o+256).toString(16).slice(1));function u(o,i=0){return(s[o[i+0]]+s[o[i+1]]+s[o[i+2]]+s[o[i+3]]+"-"+s[o[i+4]]+s[o[i+5]]+"-"+s[o[i+6]]+s[o[i+7]]+"-"+s[o[i+8]]+s[o[i+9]]+"-"+s[o[i+10]]+s[o[i+11]]+s[o[i+12]]+s[o[i+13]]+s[o[i+14]]+s[o[i+15]]).toLowerCase()}let g;const H=new Uint8Array(16);function j(){if(!g){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");g=crypto.getRandomValues.bind(crypto)}return g(H)}const w={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function B(o,i,e){var n;if(w.randomUUID&&!o)return w.randomUUID();o=o||{};const t=o.random??((n=o.rng)==null?void 0:n.call(o))??j();if(t.length<16)throw new Error("Random bytes length must be >= 16");return t[6]=t[6]&15|64,t[8]=t[8]&63|128,u(t)}class v extends CustomEvent{constructor(i,e){super(i,e)}}class y extends EventTarget{constructor(){super(...arguments);f(this,"__listeners__",{debug:!1});f(this,"__debug__",!1)}dispatch(e,t=null){const n=new v(e,{detail:t});this.dispatchEvent(n),this.__debug__&&this.dispatchEvent(new v("debug",{detail:{type:e,data:t}}))}dispatchAsync(e,t=null,n=100){const r=this;setTimeout(()=>{r.dispatch(e,t)},n)}on(e,t){typeof this.__listeners__[e]<"u"&&!this.__listeners__[e]&&(this.__listeners__[e]=!0),this.addEventListener(e,t)}off(e,t){this.removeEventListener(e,t)}serialRegisterAvailableListener(e){this.__listeners__[e]||(this.__listeners__[e]=!1)}get availableListeners(){return Object.keys(this.__listeners__).sort().map(t=>({type:t,listening:this.__listeners__[t]}))}}function x(o=100){return new Promise(i=>setTimeout(()=>i(),o))}function F(){return"serial"in navigator}const b={baudRate:9600,dataBits:8,stopBits:1,parity:"none",bufferSize:32768,flowControl:"none"};class $ extends y{constructor({filters:e=null,config_port:t=b,no_device:n=1,device_listen_on_channel:r=1}={filters:null,config_port:b,no_device:1,device_listen_on_channel:1}){super();T(this,a);f(this,"__internal__",{auto_response:!1,device_number:1,aux_port_connector:0,last_error:{message:null,action:null,code:null,no_code:0},serial:{connected:!1,port:null,last_action:null,response:{length:null,buffer:new Uint8Array([]),as:"hex"},reader:null,input_done:null,output_done:null,input_stream:null,output_stream:null,keep_reading:!0,time_until_send_bytes:void 0,delay_first_connection:200,bytes_connection:null,filters:[],config_port:b,queue:[]},device:{type:"unknown",id:B(),listen_on_port:null},time:{response_connection:500,response_general:2e3},timeout:{until_response:0},interval:{reconnection:0}});e&&(this.serialFilters=e),t&&(this.serialConfigPort=t),n&&c(this,a,N).call(this,n),r&&["number","string"].includes(typeof r)&&(this.listenOnChannel=r),c(this,a,P).call(this),c(this,a,q).call(this)}set listenOnChannel(e){if(typeof e=="string"&&(e=parseInt(e)),isNaN(e)||e<1||e>255)throw new Error("Invalid port number");this.__internal__.device.listen_on_port=e,this.__internal__.serial.bytes_connection=this.serialSetConnectionConstant(e)}get lastAction(){return this.__internal__.serial.last_action}get listenOnChannel(){return this.__internal__.device.listen_on_port??1}set serialFilters(e){this.__internal__.serial.filters=e}get serialFilters(){return this.__internal__.serial.filters}set serialConfigPort(e){this.__internal__.serial.config_port=e}get serialConfigPort(){return this.__internal__.serial.config_port}get isConnected(){return this.__internal__.serial.connected}get isDisconnected(){return!this.__internal__.serial.connected}get deviceNumber(){return this.__internal__.device_number}get uuid(){return this.__internal__.device.id}get typeDevice(){return this.__internal__.device.type}get queue(){return this.__internal__.serial.queue}async timeout(e,t){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",{})):t==="connection:start"&&(await this.serialDisconnect(),this.__internal__.serial.connected=!1,this.__internal__.aux_port_connector+=1,await this.serialConnect()),this.dispatch("serial:timeout",{...this.__internal__.last_error,bytes:e,action:t})}async disconnect(e=null){await this.serialDisconnect(),this.__internal__.serial.connected=!1,this.__internal__.aux_port_connector=0,this.dispatch("serial:disconnected",e)}async connect(){return new Promise((e,t)=>{F()||t("Web Serial not supported"),setTimeout(async()=>{await x(499),await this.serialConnect(),this.isConnected?e(`${this.typeDevice} device ${this.deviceNumber} connected`):t(`${this.typeDevice} device ${this.deviceNumber} not connected`)},1)})}async serialDisconnect(){try{const e=this.__internal__.serial.reader,t=this.__internal__.serial.output_stream;e&&(await e.cancel().catch(r=>this.serialErrors(r)),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()}catch(e){this.serialErrors(e)}finally{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}}getResponseAsArrayBuffer(){this.__internal__.serial.response.as="arraybuffer"}getResponseAsArrayHex(){this.__internal__.serial.response.as="hex"}getResponseAsUint8Array(){this.__internal__.serial.response.as="uint8"}getResponseAsString(){this.__internal__.serial.response.as="string"}async serialPortsSaved(e){const t=this.serialFilters;if(this.__internal__.aux_port_connector<e.length){const n=this.__internal__.aux_port_connector;this.__internal__.serial.port=e[n]}else this.__internal__.aux_port_connector=0,this.__internal__.serial.port=await navigator.serial.requestPort({filters:t});if(!this.__internal__.serial.port)throw new Error("Select another port please")}serialErrors(e){const t=e.toString().toLowerCase();switch(!0){case t.includes("must be handling a user gesture to show a permission request"):case t.includes("the port is closed."):case t.includes("the port is closed or is not writable"):case t.includes("select another port please"):case t.includes("no port selected by the user"):case t.includes("this readable stream reader has been released and cannot be used to cancel its previous owner stream"):this.dispatch("serial:need-permission",{});break;case t.includes("the port is already open."):case t.includes("failed to open serial port"):this.serialDisconnect().then(async()=>{this.__internal__.aux_port_connector+=1,await this.serialConnect()});break;case t.includes("cannot read properties of undefined (reading 'writable')"):case t.includes("cannot read properties of null (reading 'writable')"):case t.includes("cannot read property 'writable' of null"):case t.includes("cannot read property 'writable' of undefined"):this.serialDisconnect().then(async()=>{await this.serialConnect()});break;case t.includes("'close' on 'serialport': a call to close() is already in progress."):break;case t.includes("failed to execute 'open' on 'serialport': a call to open() is already in progress."):break;case t.includes("the port is already closed."):break;case t.includes("the device has been lost"):this.dispatch("serial:lost",{});break;case t.includes("navigator.serial is undefined"):this.dispatch("serial:unsupported",{});break;default:console.error(e);break}this.dispatch("serial:error",e)}async serialConnect(){try{this.dispatch("serial:connecting",{});const e=await c(this,a,E).call(this);if(e.length>0)await this.serialPortsSaved(e);else{const r=this.serialFilters;this.__internal__.serial.port=await navigator.serial.requestPort({filters:r})}const t=this.__internal__.serial.port;if(!t)throw new Error("No port selected by the user");await t.open(this.serialConfigPort);const n=this;t.onconnect=r=>{n.dispatch("serial:connected",r),n.__internal__.serial.queue.length>0&&n.dispatch("internal:queue",{})},t.ondisconnect=async()=>{await n.disconnect()},await x(this.__internal__.serial.delay_first_connection),this.__internal__.timeout.until_response=setTimeout(async()=>{await n.timeout(n.__internal__.serial.bytes_connection??[],"connection:start")},this.__internal__.time.response_connection),this.__internal__.serial.last_action="connect",await c(this,a,m).call(this,this.__internal__.serial.bytes_connection??[]),this.dispatch("serial:sent",{action:"connect",bytes:this.__internal__.serial.bytes_connection}),this.__internal__.auto_response&&c(this,a,h).call(this,["DD","DD"],null),await c(this,a,I).call(this)}catch(e){this.serialErrors(e)}}async serialForget(){return await c(this,a,L).call(this)}decToHex(e){return typeof e=="string"&&(e=parseInt(e,10)),e.toString(16)}hexToDec(e){return parseInt(e,16)}hexMaker(e="00",t=2){return e.toString().padStart(t,"0").toLowerCase()}add0x(e){const t=[];return e.forEach((n,r)=>{t[r]="0x"+n}),t}bytesToHex(e){return this.add0x(Array.from(e,t=>this.hexMaker(t)))}async appendToQueue(e,t){const n=this.bytesToHex(e);if(["connect","connection:start"].includes(t)){if(this.__internal__.serial.connected)return;await this.serialConnect();return}this.__internal__.serial.queue.push({bytes:n,action:t}),this.dispatch("internal:queue",{})}serialSetConnectionConstant(e=1){throw new Error(`Method not implemented 'serialSetConnectionConstant' to listen on channel ${e}`)}serialMessage(e){throw console.log(e),new Error("Method not implemented 'serialMessage'")}serialCorruptMessage(e,t){throw console.log(e,t),new Error("Method not implemented 'serialCorruptMessage'")}clearSerialQueue(){this.__internal__.serial.queue=[]}sumHex(e){let t=0;return e.forEach(n=>{t+=parseInt(n,16)}),t.toString(16)}toString(){return JSON.stringify({__class:this.typeDevice,device_number:this.deviceNumber,uuid:this.uuid,connected:this.isConnected,connection:this.__internal__.serial.bytes_connection})}softReload(){c(this,a,O).call(this),this.dispatch("serial:soft-reload",{})}async sendConnect(){if(!this.__internal__.serial.bytes_connection)throw new Error("No connection bytes defined");await this.appendToQueue(this.__internal__.serial.bytes_connection,"connect")}async sendCustomCode({code:e=[]}={code:[]}){if(e===null||e.length===0)throw new Error("No data to send");await this.appendToQueue(e,"custom")}stringToArrayHex(e){return Array.from(e).map(t=>t.charCodeAt(0).toString(16))}stringToArrayBuffer(e,t=`
|
|
2
|
+
`){return this.parseStringToTextEncoder(e,t).buffer}parseStringToTextEncoder(e="",t=`
|
|
3
|
+
`){const n=new TextEncoder;return e+=t,n.encode(e)}parseStringToBytes(e="",t=`
|
|
4
|
+
`){const n=this.parseStringToTextEncoder(e,t);return Array.from(n).map(r=>r.toString(16))}parseUint8ToHex(e){return Array.from(e).map(t=>t.toString(16))}parseHexToUint8(e){return new Uint8Array(e.map(t=>parseInt(t,16)))}stringArrayToUint8Array(e){const t=[];return e.forEach(n=>{const r=n.replace("0x","");t.push(parseInt(r,16))}),new Uint8Array(t)}parseUint8ArrayToString(e){const t=this.stringArrayToUint8Array(e);e=this.parseUint8ToHex(t);const n=e.map(r=>parseInt(r,16));return String.fromCharCode(...n).replace(/[\n\r]+/g,"")}hexToAscii(e){const t=e.toString();let n="";for(let r=0;r<t.length;r+=2)n+=String.fromCharCode(parseInt(t.substring(r,2),16));return n}asciiToHex(e){const t=[];for(let n=0,r=e.length;n<r;n++){const p=Number(e.charCodeAt(n)).toString(16);t.push(p)}return t.join("")}}a=new WeakSet,A=function(e){return!!(e.readable&&e.writable)},m=async function(e){const t=this.__internal__.serial.port;if(!t||!t.writable)throw new Error("The port is closed or is not writable");const n=this.stringArrayToUint8Array(e),r=t.writable.getWriter();await r.write(n),r.releaseLock()},h=function(e=[],t=null){if(e&&e.length>0){this.__internal__.serial.connected||this.dispatch("serial:connected"),this.__internal__.serial.connected=!0,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);const n=[];for(const r in e)n.push(e[r].toString().padStart(2,"0").toLowerCase());if(this.__internal__.serial.response.as==="hex")this.serialMessage(n);else if(this.__internal__.serial.response.as==="uint8")this.serialMessage(this.parseHexToUint8(this.add0x(n)));else if(this.__internal__.serial.response.as==="string")this.serialMessage(this.parseUint8ArrayToString(this.add0x(n)));else{const r=this.stringToArrayBuffer(this.parseUint8ArrayToString(this.add0x(n)));this.serialMessage(r)}}else this.serialCorruptMessage(e,t);this.__internal__.serial.queue.length!==0&&this.dispatch("internal:queue",{})},E=async function(){const e=this.serialFilters,t=await navigator.serial.getPorts({filters:e});return e.length===0?t:t.filter(r=>{const p=r.getInfo();return e.some(S=>p.usbProductId===S.usbProductId&&p.usbVendorId===S.usbVendorId)}).filter(r=>!c(this,a,A).call(this,r))},U=function(e){if(e){const t=this.__internal__.serial.response.buffer,n=new Uint8Array(t.length+e.byteLength);n.set(t,0),n.set(new Uint8Array(e),t.length),this.__internal__.serial.response.buffer=n}},D=async function(){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(()=>{const e=[];for(const t in this.__internal__.serial.response.buffer)e.push(this.__internal__.serial.response.buffer[t].toString(16));this.__internal__.serial.response.buffer&&c(this,a,h).call(this,e),this.__internal__.serial.response.buffer=new Uint8Array(0)},400)},k=async function(){if(this.__internal__.serial.response.length!==null){if(this.__internal__.serial.response.length===this.__internal__.serial.response.buffer.length){const e=[];for(const t in this.__internal__.serial.response.buffer)e.push(this.__internal__.serial.response.buffer[t].toString(16));c(this,a,h).call(this,e),this.__internal__.serial.response.buffer=new Uint8Array(0)}else if(this.__internal__.serial.response.length<this.__internal__.serial.response.buffer.length){let e=new Uint8Array(0);for(let n=0;n<this.__internal__.serial.response.length;n++)e[n]=this.__internal__.serial.response.buffer[n];if(e.length===this.__internal__.serial.response.length){const n=[];for(const r in e)n.push(e[r].toString(16));c(this,a,h).call(this,n),this.__internal__.serial.response.buffer=new Uint8Array(0);return}e=new Uint8Array(0);const t=this.__internal__.serial.response.length*2;if(this.__internal__.serial.response.buffer.length===t){for(let n=14;n<t;n++)e[n-this.__internal__.serial.response.length]=this.__internal__.serial.response.buffer[n];if(e.length===this.__internal__.serial.response.length){const n=[];for(const r in e)n.push(e[r].toString(16));c(this,a,h).call(this,n),this.__internal__.serial.response.buffer=new Uint8Array(0)}}}}},I=async function(){const e=this.__internal__.serial.port;if(!e||!e.readable)throw new Error("Port is not readable");for(;e.readable&&this.__internal__.serial.keep_reading;){const t=e.readable.getReader();this.__internal__.serial.reader=t;try{let n=!0;for(;n;){const{value:r,done:p}=await t.read();if(p){t.releaseLock(),this.__internal__.serial.keep_reading=!1,n=!1;break}c(this,a,U).call(this,r),this.__internal__.serial.response.length===null?await c(this,a,D).call(this):await c(this,a,k).call(this)}}catch(n){this.serialErrors(n)}finally{t.releaseLock()}}this.__internal__.serial.keep_reading=!0,this.__internal__.serial.port&&await this.__internal__.serial.port.close()},L=async function(){return typeof window>"u"?!1:"serial"in navigator&&"forget"in SerialPort.prototype&&this.__internal__.serial.port?(await this.__internal__.serial.port.forget(),!0):!1},P=function(){["serial:connected","serial:connecting","serial:reconnect","serial:timeout","serial:disconnected","serial:sent","serial:soft-reload","serial:message","unknown","serial:need-permission","serial:lost","serial:unsupported","serial:error","debug"].forEach(t=>{this.serialRegisterAvailableListener(t)})},q=function(){const e=this;this.on("internal:queue",async()=>{var t;await c(t=e,a,R).call(t)}),c(this,a,M).call(this)},M=function(){const e=this;navigator.serial.addEventListener("connect",async()=>{e.isDisconnected&&await e.serialConnect().catch(()=>{})})},R=async function(){if(!this.__internal__.serial.connected){await this.serialConnect();return}if(this.__internal__.timeout.until_response||this.__internal__.serial.queue.length===0)return;const e=this.__internal__.serial.queue[0];let t=this.__internal__.time.response_general;e.action==="connect"&&(t=this.__internal__.time.response_connection),this.__internal__.timeout.until_response=setTimeout(async()=>{await this.timeout(e.bytes,e.action)},t),this.__internal__.serial.last_action=e.action??"unknown",await c(this,a,m).call(this,e.bytes),this.dispatch("serial:sent",{action:e.action,bytes:e.bytes}),this.__internal__.auto_response&&c(this,a,h).call(this,["DD","DD"],null);const n=[...this.__internal__.serial.queue];this.__internal__.serial.queue=n.splice(1)},N=function(e=1){this.__internal__.device_number=e,this.__internal__.serial.bytes_connection=this.serialSetConnectionConstant(e)},O=function(){this.__internal__.last_error={message:null,action:null,code:null,no_code:0}};const l=class l extends y{static typeError(i){const e=new Error;throw e.message=`Type ${i} is not supported`,e.name="DeviceTypeError",e}static registerType(i){typeof l.devices[i]>"u"&&(l.devices[i]={})}static add(i){const e=i.typeDevice;typeof l.devices[e]>"u"&&(l.devices[e]={});const t=i.uuid;if(typeof l.devices[e]>"u"&&l.typeError(e),this.instance.dispatch("change",l.devices),l.devices[e][t])throw new Error(`Device with id ${t} already exists`);return l.devices[e][t]=i,this.instance.dispatch("change",l.devices),Object.keys(l.devices[e]).indexOf(t)}static get(i,e){return typeof l.devices[i]>"u"&&(l.devices[i]={}),typeof l.devices[i]>"u"&&l.typeError(i),l.devices[i][e]}static getAll(i=null){return i===null?l.devices:(typeof l.devices[i]>"u"&&l.typeError(i),l.devices[i])}static getList(){return Object.values(l.devices).map(e=>Object.values(e)).flat()}static getByNumber(i,e){return typeof l.devices[i]>"u"&&l.typeError(i),Object.values(l.devices[i]).find(n=>n.deviceNumber===e)??null}static getCustom(i,e=1){return typeof l.devices[i]>"u"&&l.typeError(i),Object.values(l.devices[i]).find(n=>n.deviceNumber===e)??null}};f(l,"instance"),f(l,"devices",{});let d=l;d.instance||(d.instance=new d),_.Core=$,_.Devices=d,_.Dispatcher=y,Object.defineProperty(_,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "webserial-core",
|
|
3
|
+
"description": "Webserial Core to easy connections with serial devices",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "danidoble",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/danidoble/webserial-core.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/danidoble/webserial-core/issues"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/danidoble/webserial-core#readme",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"webserial",
|
|
18
|
+
"serial",
|
|
19
|
+
"core",
|
|
20
|
+
"web"
|
|
21
|
+
],
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"main": "./dist/webserial-core.umd.cjs",
|
|
27
|
+
"module": "./dist/webserial-core.js",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"import": "./dist/webserial-core.js",
|
|
31
|
+
"require": "./dist/webserial-core.umd.cjs"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"dev": "vite",
|
|
36
|
+
"build": "prettier --write ./lib/**/*.ts && tsc && vite build",
|
|
37
|
+
"preview": "vite preview",
|
|
38
|
+
"lint": "eslint --ext .ts ./lib",
|
|
39
|
+
"format": "prettier --write ./lib/**/*.ts"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@eslint/js": "^9.21.0",
|
|
43
|
+
"@types/w3c-web-serial": "^1.0.8",
|
|
44
|
+
"eslint": "^9.21.0",
|
|
45
|
+
"globals": "^15.15.0",
|
|
46
|
+
"prettier": "3.4.2",
|
|
47
|
+
"puppeteer": "^24.4.0",
|
|
48
|
+
"semantic-release": "^24.2.3",
|
|
49
|
+
"typescript": "~5.7.3",
|
|
50
|
+
"typescript-eslint": "^8.26.0",
|
|
51
|
+
"uuid": "^11.1.0",
|
|
52
|
+
"vite": "^6.2.0"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
}
|
|
57
|
+
}
|