Skip to content

Commit 897d936

Browse files
committed
Update for RRF/DSF 3.5
1 parent ba8a32f commit 897d936

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

js/dueui-loader.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,13 @@ function addHeadElement(str, element, options = {}) {
119119

120120
async function tryFetch(url, options = {}) {
121121
var response = {};
122+
let headers = {};
123+
if (resolvedSettings.sessionKey) {
124+
headers["X-Session-Key"] = resolvedSettings.sessionKey;
125+
}
122126
url = url.replace(document.location.origin, "");
123127
try {
124-
response = await fetch(url, { mode: "cors", ...options});
128+
response = await fetch(url, { mode: "cors", headers: headers, ...options});
125129
} catch(error) {
126130
response.statusText = error;
127131
response.status = 900;
@@ -311,12 +315,11 @@ function setTheme(theme_name, theme_path) {
311315
}
312316

313317
async function isStandalone(duet_host, duet_password) {
314-
console.log(`Testing ${duet_host} as Standalone`);
315-
response = await tryFetch(duet_host + "/rr_connect?password=" + duet_password );
318+
console.log(`Testing ${duet_host} as Standalone`);
319+
response = await getJSON(duet_host + "/rr_connect?password=" + duet_password);
316320
if (response.ok) {
317-
response.text();
318-
if (response.status === 200) {
319-
console.log(`${duet_host} is Standalone`);
321+
if (response.status === 200 && !response.data.isEmulated) {
322+
console.log(`${duet_host} is Standalone: ${response.data}`);
320323
return true;
321324
}
322325
}

js/dueui.js

+29-11
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,15 @@ class DueUI {
289289
let resp = await this.getFileList("/www");
290290
let need_cleanup = resp.find(element => element.name === "dueui.html.gz");
291291
if (need_cleanup) {
292-
await this.deleteFile("/www/dueui.html.gz");
292+
await this.deleteFile("/www/dueui.html.gz", true);
293293
}
294294
resp = await this.getFileList("/www/js");
295295
let oldfiles = ["dueui-bundle.js.gz", "dueui-vendor-bundle.js.gz",
296296
"dueui-loader.js.gz", "dueui_element.js.gz"];
297297
for (let f of oldfiles.values()) {
298298
need_cleanup = resp.find(element => element.name === f);
299299
if (need_cleanup) {
300-
await this.deleteFile("/www/js/" + f);
300+
await this.deleteFile("/www/js/" + f, true);
301301
}
302302
}
303303
}
@@ -336,7 +336,21 @@ class DueUI {
336336
class DueUI_DSF extends DueUI {
337337

338338
async connect_once(host) {
339-
let resp = await getJSONFromDuet("/machine/status");
339+
let resp = await getJSONFromDuet(`/rr_connect?password=${encodeURI(resolvedSettings.duet_password)}`);
340+
341+
let connect_url = "/machine/connect";
342+
if (resolvedSettings.duet_password.length > 0) {
343+
connect_url += `?password=${encodeURI(resolvedSettings.duet_password)}`;
344+
}
345+
resp = await getJSONFromDuet(connect_url);
346+
if (!resp.ok) {
347+
this.logMessage("W", `Initial connect failed: ${resp.status} ${resp.statusText}`);
348+
this.connected = false;
349+
return resp;
350+
}
351+
resolvedSettings.sessionKey = resp.data.sessionKey;
352+
353+
resp = await getJSONFromDuet("/machine/status");
340354
if (!resp.ok) {
341355
this.logMessage("W", `Initial connect failed: ${resp.status} ${resp.statusText}`);
342356
this.connected = false;
@@ -376,26 +390,26 @@ class DueUI_DSF extends DueUI {
376390
if (resp.length == 0) {
377391
return;
378392
}
379-
await this.deleteFile("/www/dueui/index.html");
393+
await this.deleteFile("/www/dueui/index.html", true);
380394

381395
for (let d of ["css", "fonts", "js"].values()) {
382396
resp = await this.getFileList("/www/dueui/" + d);
383397
for (let f of resp.values()) {
384-
await this.deleteFile("/www/dueui/" + d + "/" + f.name);
398+
await this.deleteFile("/www/dueui/" + d + "/" + f.name, true);
385399
}
386-
await this.deleteFile("/www/dueui/" + d);
400+
await this.deleteFile("/www/dueui/" + d, true);
387401
}
388402

389-
await this.deleteFile("/www/dueui");
403+
await this.deleteFile("/www/dueui", true);
390404
}
391405

392-
async deleteFile(path) {
406+
async deleteFile(path, suppressError = false) {
393407
let resp = await tryFetch(`http://${resolvedSettings.duet_host}/machine/file/${path}`, {
394408
"method": "DELETE"
395409
});
396410
console.log({ action: "DeleteFile", path: path, resp, respText: await resp.text()});
397411

398-
if (!resp.ok) {
412+
if (!resp.ok && !suppressError) {
399413
this.logMessage("W", `DELETE of '${path}' failed: ${resp.status} ${resp.statusText}`);
400414
}
401415

@@ -504,6 +518,10 @@ class DueUI_DSF extends DueUI {
504518
resp.ok = true;
505519

506520
let ws_url = `ws://${resolvedSettings.duet_host}/machine`;
521+
if (resolvedSettings.sessionKey) {
522+
ws_url += `?sessionKey=${encodeURI(resolvedSettings.sessionKey)}`;
523+
}
524+
507525
try {
508526
this.websocket = new WebSocket(ws_url);
509527

@@ -596,11 +614,11 @@ class DueUI_Standalone extends DueUI {
596614
}
597615
}
598616

599-
async deleteFile(path) {
617+
async deleteFile(path, suppressError = false) {
600618
let resp = await getJSONFromDuet(`/rr_delete?name=${path}`);
601619
console.log({ action: "DeleteFile", path: path, resp, respText: await resp.data});
602620

603-
if (!resp.ok) {
621+
if (!resp.ok && !suppressError) {
604622
this.logMessage("W", `DELETE of '${path}' failed: ${resp.status} ${resp.statusText}`);
605623
}
606624

0 commit comments

Comments
 (0)