Repository navigation
Expand file tree
/
Copy pathsendController.js
More file actions
159 lines (148 loc) · 5.64 KB
/
Copy pathsendController.js
File metadata and controls
159 lines (148 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const sendByteUtils = typeof module !== 'undefined'
? require('./byteUtils').ByteUtils : globalThis.SerialPlotter.ByteUtils;
/** Owns send-panel state, file bytes, and repeated-send lifecycle. */
class SendController {
constructor({ mode, input, fileInput, interval, intervalUnit, button, loadButton,
getEngine, onSent, onInputError, onSendError }) {
this.mode = mode;
this.input = input;
this.fileInput = fileInput;
this.interval = interval;
this.intervalUnit = intervalUnit;
this.button = button;
this.loadButton = loadButton;
this.getEngine = getEngine;
this.onSent = onSent;
this.onInputError = onInputError || (() => {});
this.onSendError = onSendError || (() => {});
this.loadedBytes = null;
this.previewBytes = null;
this.previousMode = mode.value;
this.timer = null;
this.inFlight = false;
this.starting = false;
this.disposed = false;
this.listeners = [];
this.fileGeneration = 0;
this.listen(mode, 'change', () => this.changeMode());
this.listen(input, 'input', () => {
this.fileGeneration++;
this.loadedBytes = null;
this.previewBytes = null;
});
this.listen(button, 'click', () => { void this.handleClick(); });
this.listen(loadButton, 'click', () => fileInput.click());
this.listen(fileInput, 'change', event => this.loadFile(event));
}
listen(element, name, callback) {
element.addEventListener(name, callback);
this.listeners.push(() => element.removeEventListener?.(name, callback));
}
dispose() {
if (this.disposed) return;
this.disposed = true;
this.fileGeneration++;
this.stop();
for (const remove of this.listeners.splice(0)) remove();
}
stop() {
this.starting = false;
if (this.timer !== null) clearInterval(this.timer);
this.timer = null;
this.button.textContent = '发送';
this.button.className = 'btn btn-primary';
}
periodMs() {
const value = Number(this.interval.value);
if (!Number.isFinite(value) || value <= 0) return 0;
if (this.intervalUnit.value === 'hz') return 1000 / value;
if (this.intervalUnit.value === 's') return value * 1000;
return value;
}
async sendOnce() {
if (this.disposed || this.inFlight) return false;
const engine = this.getEngine();
if (!engine) { this.stop(); return false; }
let bytes;
try {
bytes = this.loadedBytes || this.previewBytes || (this.mode.value === 'hex'
? sendByteUtils.hexToBytes(this.input.value)
: sendByteUtils.textToBytes(this.input.value));
if (bytes.length === 0) return false;
} catch (error) {
this.onInputError(error);
this.stop();
return false;
}
this.inFlight = true;
try {
await engine.send(bytes);
if (!this.disposed) this.onSent(bytes);
return true;
} catch (error) {
if (!this.disposed) this.onSendError(error, bytes);
this.stop();
return false;
} finally {
this.inFlight = false;
}
}
async handleClick() {
if (this.disposed) return;
if (this.timer !== null || this.starting) { this.stop(); return; }
const period = this.periodMs();
if (period > 0) {
if (!this.getEngine()) return;
this.starting = true;
const sent = await this.sendOnce();
if (!sent || !this.starting || !this.getEngine()) {
this.starting = false;
return;
}
this.timer = setInterval(() => { void this.sendOnce(); }, period);
this.starting = false;
this.button.textContent = '停止';
this.button.className = 'btn btn-danger';
} else {
await this.sendOnce();
}
}
changeMode() {
try {
const bytes = this.loadedBytes || this.previewBytes ||
(this.previousMode === 'hex'
? sendByteUtils.hexToBytes(this.input.value)
: sendByteUtils.textToBytes(this.input.value));
this.previewBytes = bytes;
this.input.value = this.mode.value === 'hex'
? sendByteUtils.bytesToHex(bytes) : sendByteUtils.bytesToText(bytes);
} catch (_) {
// Keep the original input when it cannot be converted.
}
this.previousMode = this.mode.value;
}
async loadFile(event) {
if (this.disposed) return;
const file = event.target.files[0];
if (!file) return;
const generation = ++this.fileGeneration;
this.loadedBytes = null;
this.previewBytes = null;
this.input.value = '';
try {
const bytes = new Uint8Array(await file.arrayBuffer());
if (this.disposed || generation !== this.fileGeneration) return;
this.loadedBytes = bytes;
this.input.value = this.mode.value === 'hex'
? sendByteUtils.bytesToHex(this.loadedBytes)
: sendByteUtils.bytesToText(this.loadedBytes);
} catch (error) {
if (!this.disposed && generation === this.fileGeneration) this.onInputError(error);
} finally {
event.target.value = '';
}
}
}
globalThis.SerialPlotter ??= {};
globalThis.SerialPlotter.SendController = SendController;
if (typeof module !== 'undefined') module.exports = { SendController };