-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcom_serial_fileExplore.js
More file actions
244 lines (235 loc) · 5.82 KB
/
com_serial_fileExplore.js
File metadata and controls
244 lines (235 loc) · 5.82 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
https://web.dev/serial/
*/
let serial = new GenericSerial();
serial.connect_callback = connect_callback;
serial.disconnect_callback = disconnect_callback;
serial.send_callback = send_callback;
serial.receive_callback = receive_callback;
let connected = false;
let path = "/";
const type_enum = Object.freeze({
dir: 1,
file: 2,
error: 3
});
let files = [];
const mode_enum = Object.freeze({
load: 1,
view: 2,
delete: 3
});
let mode = mode_enum.load;
let progress = -1;
const CLI_PROMPT = ">: ";
const CLI_EOL = "\r\n";
function connect_callback() {
connected = true;
path = "/";
serial.send("device_info\r").then(() => {
serial.read_until("hardware_model").then(() => {
serial.read_until(CLI_PROMPT).then(() => {
get_filelist();
connected = true;
});
});
});
}
function disconnect_callback() {
connected = false;
}
function decode(data) {
return new TextDecoder().decode(data);
}
function send_callback(data) {}
function receive_callback(data) {
terminal.add_line(decode(data));
}
async function get_filelist() {
await serial.send('storage list "' + path + '"\r');
await serial.read_until(CLI_EOL);
serial.read_until(CLI_PROMPT).then((data) => {
let lines = [];
let start = 0;
let stop = 0;
for (let i = 0; i < data.length; i++) {
// TODO fix overflow
if (data[i] == 13 && data[i + 1] == 10) {
stop = i;
lines.push(data.slice(start, stop));
start = i + 2;
}
}
files = [];
lines.forEach(function(item) {
let text = "";
try {
text = decode(item);
} catch (error) {}
text = text.trim();
if (text.length > 0) {
if (text == "Empty") {
// nothing
} else {
text = text.split(" ");
let type = type_enum.error;
let size = 0;
let name = "";
if (text[0] == "[D]") {
type = type_enum.dir;
text.shift();
name = text.join(" ");
files.push({
type: type,
name: name,
size: size
});
} else if (text[0] == "[F]") {
type = type_enum.file;
size = parseInt(text.at(-1).replace("b", ""));
text.pop();
text.shift();
name = text.join(" ");
files.push({
type: type,
name: name,
size: size
});
} else {
name = text.join(" ");
files.push({
type: type,
name: name,
size: size
});
}
}
}
});
files.sort(function(a, b) {
if (a.type == type_enum.dir && b.type == type_enum.dir) {
if (a.name > b.name) {
return 1;
}
if (a.name < b.name) {
return -1;
}
return 0;
} else if (a.type == type_enum.file && b.type == type_enum.file) {
if (a.name > b.name) {
return 1;
}
if (a.name < b.name) {
return -1;
}
return 0;
} else {
if (a.type == type_enum.dir) {
return -1;
} else {
return 1;
}
}
});
});
}
async function connect() {
await serial.try_to_connect();
}
async function update_path(name) {
path += "/" + name;
path = path.replace("//", "/");
get_filelist();
}
async function update_path_up() {
path = path.substring(0, path.lastIndexOf("/"));
path = path.replace("//", "/");
if (path.length == 0) path = "/";
get_filelist();
}
async function download(name) {
let filepath = path + "/" + name;
const buffer_size = 512;
progress = 0;
await serial.send('storage read_chunks "' + filepath + '" ' + buffer_size + "\r");
await serial.read_until(CLI_EOL);
await serial.read_until(CLI_EOL).then(async (data) => {
data = decode(data);
let size = parseInt(data.split(": ")[1]);
var file_data = new Uint8Array(size);
let readed_size = 0;
while (readed_size < size) {
await serial.read_until("Ready?" + CLI_EOL);
await serial.send("y");
let read_size = Math.min(size - readed_size, buffer_size);
await serial.read_count(read_size).then((buffer) => {
for (let i = 0; i < buffer.length; i++, readed_size++) {
file_data[readed_size] = buffer[i];
}
});
progress = Math.floor((readed_size / size) * 100);
}
if (mode == mode_enum.load) {
var file_blob = new Blob([file_data], {
type: "application/octet-stream",
});
saveAs(file_blob, name);
} else if (mode == mode_enum.view) {
console.log(decode(file_data));
}
await serial.read_until(CLI_PROMPT);
get_filelist();
progress = -1;
});
}
async function may_be_connected() {
await serial.on_connect();
}
may_be_connected();
async function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.dataTransfer.files; // FileList object.
var reader = new FileReader();
//for (var i = 0, f; (f = files[i]); i++) {
// TODO multiload
let f = files[0];
let name = f.name;
let size = f.size;
reader.readAsArrayBuffer(f);
reader.onload = async () => {
let data = new Uint8Array(reader.result);
console.log(name, size, data);
progress = 0;
await serial.send('storage remove "' + path + "/" + name + '"\r');
await serial.read_until(CLI_EOL);
await serial.read_until(CLI_PROMPT);
const fpath = path + "/" + name;
const buffer_size = 512;
let sended_size = 0;
while (sended_size < data.length) {
let send_size = Math.min(data.length - sended_size, buffer_size);
let buffer = data.slice(sended_size, sended_size + send_size);
sended_size += send_size;
console.log("Sending ", sended_size, buffer, " to ", fpath);
//terminal.add_line("[" + decode(buffer) + "]");
await serial.send('storage write_chunk "' + fpath + '" ' + buffer.length + "\r");
await serial.read_until(CLI_EOL);
await serial.read_until(CLI_EOL);
await serial.send_raw(buffer);
await serial.read_until(CLI_PROMPT);
progress = Math.floor((sended_size / size) * 100);
}
get_filelist();
progress = -1;
};
//}
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = "copy"; // Explicitly show this is a copy.
}
function switch_mode(new_mode) {
mode = new_mode;
}