diff --git a/docs/architecture/web-pandar-print-host.md b/docs/architecture/web-pandar-print-host.md index f54fdcc322..114a20853b 100644 --- a/docs/architecture/web-pandar-print-host.md +++ b/docs/architecture/web-pandar-print-host.md @@ -1,4 +1,4 @@ -# Web print host: send a sliced plate to Pandar +# Web print host: Pandar Cloud sign-in, Devices tab and sending a sliced plate ## Source boundary @@ -22,8 +22,13 @@ Plate sliced file (`libslic3r`, the rewrite boundary): Print host UI (GUI, ported only as browser behaviour): -- `PhysicalPrinterDialog` (`src/slic3r/GUI/PhysicalPrinterDialog.cpp`) stores a host type, the - host address (`print_host`) and the API key (`printhost_apikey`). +- `GUI_App::request_login` and `ShowUserLogin` (`src/slic3r/GUI/GUI_App.cpp:5135`, `:5363`) open the + cloud sign-in WebView. `GUI_App::request_user_logout` (`:5448`) forgets the account and its + devices (`DeviceManager::clean_user_info`). +- `MainFrame` adds the Device tab (`m_monitor_page`, `MonitorPanel`, `_L("Device")`, + `tab_monitor_active`; `src/slic3r/GUI/MainFrame.cpp:1606-1608`). `MainFrame::show_device` + (`:1661-1745`) inserts it after Preview and takes it away again. `StatusPanel` shows each + printer's temperatures and print progress. - `PrintHostSendDialog` (`src/slic3r/GUI/PrintHostDialogs.cpp`) confirms the upload name and the post-upload action (upload, or upload and print). @@ -38,10 +43,25 @@ Print host UI (GUI, ported only as browser behaviour): plate 1 of a saved single-plate 3MF (the bytes `saveProjectWithProfiles` returns) with archive capture and returns the `min_save` `.gcode.3mf`. Slicing and packaging stay in Rust. - Web app: - - "Physical printer" settings (host type `Pandar`, Hub URL, tenant ID, tenant API token), kept in - browser `localStorage` like the rest of the app config. - - "Print plate" side button: opens the physical printer settings when none are set, then the send - dialog. The dialog lists the tenant's printers (`GET /api/v1/tenants/{tenant}/printers`) and + - A "Cloud" menu next to "File" signs in to a Pandar Hub. The sign-in dialog asks only for the Hub + URL and remembers the last one. A popup opens `{hub}/web-sign-in` with `redirect_url` (the + app's `cloud-callback.html`), a PKCE S256 `code_challenge` and a random `state`. The Hub sends + the popup back to `cloud-callback.html` with a one-use ticket. That page posts + `{ ticket, state }` on the `ares.cloud` BroadcastChannel: the app's + `Cross-Origin-Opener-Policy: same-origin` severs `window.opener` across the Hub hop. The app + exchanges the ticket and its verifier at `POST /api/v1/web/login-tickets/exchange` for a + 30-day `web:session` tenant token and its profile. + - The session (token, expiry, tenant, user name) and the Hub URL are kept in the `cloud` field of + the `ares.config` `localStorage` record. An expired session is not restored. "Sign out" and any + 401 from the tenant API drop the session and keep the Hub URL. The manual `printHost` field of + earlier builds is dropped on load, not migrated. + - A "Devices" tab (`#/devices`) follows Preview while signed in. It lists the tenant's printers + (`GET /api/v1/tenants/{tenant}/printers`) with their status, nozzle, bed and chamber + temperatures and the running print's name, layer, time left and progress. It then opens the + printer-event socket (`POST /api/v1/tenants/{tenant}/printer-events/tickets`, then + `GET /api/v1/tenants/{tenant}/printer-events?ticket=`), and each `printer_snapshot` replaces + its printer. A closed socket reconnects after 5 s. The socket is open only while the tab is shown. + - "Print plate" side button: opens the Cloud sign-in when signed out, then the send dialog. The dialog lists the tenant's printers (`GET /api/v1/tenants/{tenant}/printers`) and uploads. A Bambu printer offers the Hub's calibration modes (0 off, 1 on, 2 auto) with the paired booleans set to `mode != 0`, plus AMS and timelapse. - The upload goes to the Pandar Hub client API: @@ -51,7 +71,8 @@ Print host UI (GUI, ported only as browser behaviour): - A Moonraker printer in Pandar accepts only `.gcode` (Hub `artifact_requires_gcode`), so it gets the plate's plain G-code. A Bambu printer gets the `.gcode.3mf`. - The Hub must allow the web origin through its `PANDAR_CORS_ALLOWED_ORIGINS` allowlist - (ProjectPandar/pandar#17). The browser sends no cookies. + (ProjectPandar/pandar#17). The same allowlist bounds the sign-in `redirect_url` origin. The + browser sends no cookies. ## Included / deferred @@ -67,6 +88,8 @@ Deferred, rejected with a message rather than approximated: thumbnail renderer port yet. - AMS slot mapping (`ams_mapping`); the job is sent without a mapping and `use_ams` as chosen. - Other host types (OctoPrint, Moonraker direct, …). +- Device control from the Devices tab (pause, stop, light, AMS), print history and the camera view. + The tab is read-only. - "Export plate sliced file" in the File menu. It can reuse the same export, but it is a separate menu slice. @@ -90,10 +113,20 @@ reuse the preview slice. Two consequences follow, both upstream CLI behaviour: - Web E2E (`web/e2e/pandarSend.spec.ts`): the P1S project `tests/parity/auto-lift-query-frame/project.3mf` is opened with the P1S installed and sliced. A fake cross-origin Hub answers the CORS preflight, checks the bearer token and records each job. The spec asserts the following: - - the physical printer config is stored; + - the Cloud sign-in through the popup is stored; - each job's path and fields; - the Bambu `.gcode.3mf` has exactly the 11 `--min-save` members; - its G-code equals the preview G-code apart from the three CLI-kept config lines; - the Moonraker `.gcode` equals the preview G-code. `target/web-e2e/pandar-send/` keeps both uploads and `requests.json` as the repeatable artifact. +- Web E2E (`web/e2e/pandarCloud.spec.ts`): the fake Hub (`web/e2e/pandarHub.ts`) redirects the + sign-in popup with a ticket and accepts the exchange only for the verifier whose SHA-256 is the + issued challenge. The spec asserts the following: + - `#/devices` falls back to Prepare and no Devices tab shows while signed out; + - the stored session after sign-in; + - the printer cards, then a `printer_snapshot` updating temperatures and print progress; + - the session outlives a reload; + - sign-out leaves Devices, drops the session and keeps the Hub URL for the next sign-in. + + `target/web-e2e/pandar-cloud/` keeps the two Devices screenshots and `session.json`. diff --git a/web/app/public/cloud-callback.html b/web/app/public/cloud-callback.html new file mode 100644 index 0000000000..d579b2b56c --- /dev/null +++ b/web/app/public/cloud-callback.html @@ -0,0 +1,16 @@ + + + + + Ares Cloud sign-in + + +

Signed in. You can close this window.

+ + + diff --git a/web/app/src/assets/orca/tab_monitor_active.svg b/web/app/src/assets/orca/tab_monitor_active.svg new file mode 100644 index 0000000000..6270ca49e7 --- /dev/null +++ b/web/app/src/assets/orca/tab_monitor_active.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/web/app/src/devices/DevicesPage.tsx b/web/app/src/devices/DevicesPage.tsx new file mode 100644 index 0000000000..db591752c6 --- /dev/null +++ b/web/app/src/devices/DevicesPage.tsx @@ -0,0 +1,83 @@ +import type { HubPrinter } from "../state/cloud"; +import { useDevices } from "../state/devices"; + +const temperature = (current?: string | null, target?: string | null) => + current == null ? "—" : target && Number(target) > 0 ? `${current} / ${target} °C` : `${current} °C`; + +const remaining = (minutes: number) => + minutes >= 60 ? `${Math.floor(minutes / 60)} h ${minutes % 60} min` : `${minutes} min`; + +/** StatusPanel's temperatures and print progress (StatusPanel.cpp) for one Hub printer. */ +function DeviceCard({ printer }: { printer: HubPrinter }) { + const { print } = printer; + const active = print?.gcode_state && !["IDLE", "FINISH", "FAILED"].includes(print.gcode_state); + const rows: [string, string][] = [ + ...(printer.nozzle_temperatures ?? []).map( + ({ label, current_celsius, target_celsius }, index): [string, string] => [ + label ?? `Nozzle ${index + 1}`, + temperature(current_celsius, target_celsius), + ], + ), + ["Bed", temperature(printer.bed_temperature_celsius, printer.bed_target_temperature_celsius)], + ]; + if (printer.chamber_temperature_celsius != null) + rows.push([ + "Chamber", + temperature(printer.chamber_temperature_celsius, printer.chamber_target_temperature_celsius), + ]); + return ( +
+
+

{printer.name}

+ + {printer.status} + +
+ {printer.model &&

{printer.model}

} +
+ {rows.map(([name, value]) => ( +
+
{name}
+
{value}
+
+ ))} +
+ {active && ( +
+

+ {print.subtask_name ?? print.gcode_state} + {print.current_layer != null && print.total_layers != null && ( + + {" "} + · Layer {print.current_layer}/{print.total_layers} + + )} + {print.remaining_time_minutes != null && · {remaining(print.remaining_time_minutes)} left} +

+ +
+ )} +
+ ); +} + +/** MainFrame's Device tab (MonitorPanel, MainFrame.cpp:1606-1608): the signed-in tenant's printers. */ +export function DevicesPage() { + const { printers, error } = useDevices(); + return ( +
+ {error && ( +

+ {error} +

+ )} + {printers?.length === 0 &&

No printers in this Pandar tenant.

} + {!printers && !error &&

Loading printers…

} +
+ {printers?.map((printer) => ( + + ))} +
+
+ ); +} diff --git a/web/app/src/router.tsx b/web/app/src/router.tsx index b912256626..dc589ba587 100644 --- a/web/app/src/router.tsx +++ b/web/app/src/router.tsx @@ -1,5 +1,6 @@ import { createHashHistory, createRootRoute, createRoute, createRouter, redirect } from "@tanstack/react-router"; import { Shell } from "./shell/Shell"; +import { useApp } from "./state/store"; const rootRoute = createRootRoute({ component: Shell }); @@ -21,8 +22,17 @@ const previewRoute = createRoute({ path: "/preview", }); +/** MainFrame's Device tab, only while signed in to Pandar Cloud. */ +const devicesRoute = createRoute({ + getParentRoute: () => rootRoute, + path: "/devices", + beforeLoad: () => { + if (!useApp.getState().cloud) throw redirect({ to: "/prepare" }); + }, +}); + export const router = createRouter({ - routeTree: rootRoute.addChildren([indexRoute, prepareRoute, previewRoute]), + routeTree: rootRoute.addChildren([indexRoute, prepareRoute, previewRoute, devicesRoute]), history: createHashHistory(), }); diff --git a/web/app/src/shell/CloudMenu.tsx b/web/app/src/shell/CloudMenu.tsx new file mode 100644 index 0000000000..e2775d2c8e --- /dev/null +++ b/web/app/src/shell/CloudMenu.tsx @@ -0,0 +1,56 @@ +import { useEffect, useRef, useState } from "react"; +import { signOut } from "../state/cloud"; +import { useApp } from "../state/store"; + +/** + * The top bar's Pandar account menu: sign in (GUI_App::request_login, GUI_App.cpp:5363) or, once + * signed in, the account and sign out (GUI_App::request_user_logout, :5448). Closes on a pick, + * Escape or a click elsewhere. + */ +export function CloudMenu({ onSignIn }: { onSignIn: () => void }) { + const cloud = useApp((state) => state.cloud); + const [open, setOpen] = useState(false); + const root = useRef(null); + useEffect(() => { + if (!open) return; + const close = (event: Event) => { + if (event instanceof KeyboardEvent ? event.key === "Escape" : !root.current?.contains(event.target as Node)) + setOpen(false); + }; + document.addEventListener("pointerdown", close); + document.addEventListener("keydown", close); + return () => { + document.removeEventListener("pointerdown", close); + document.removeEventListener("keydown", close); + }; + }, [open]); + const pick = (action: () => void) => () => { + setOpen(false); + action(); + }; + return ( +
+ + {open && ( +
+ {cloud ? ( + <> + + {cloud.userName} · {cloud.tenantName} + + + + ) : ( + + )} +
+ )} +
+ ); +} diff --git a/web/app/src/shell/PrintHostDialogs.tsx b/web/app/src/shell/PrintHostDialogs.tsx index 13de3948db..34a61d4633 100644 --- a/web/app/src/shell/PrintHostDialogs.tsx +++ b/web/app/src/shell/PrintHostDialogs.tsx @@ -1,15 +1,7 @@ import { useEffect, useRef, useState } from "react"; -import { - type CalibrationMode, - type HubPrinter, - listPrinters, - type PrintHost, - type PrintOptions, - printHost, - sendBlocker, - sendPlate, - setPrintHost, -} from "../state/printHost"; +import { cloudHubUrl, type HubPrinter, listPrinters, signIn } from "../state/cloud"; +import { type CalibrationMode, type PrintOptions, sendBlocker, sendPlate } from "../state/printHost"; +import { useApp } from "../state/store"; function useModal(onClose: () => void) { const dialog = useRef(null); @@ -28,42 +20,66 @@ function Header({ id, title, close }: { id: string; title: string; close: () => ); } -/** PhysicalPrinterDialog (PhysicalPrinterDialog.cpp) for the `Pandar` host type. */ -export function PhysicalPrinterDialog({ onClose, onSaved }: { onClose: () => void; onSaved: () => void }) { +/** + * The Cloud menu's sign-in (GUI_App::request_login, GUI_App.cpp:5363; ZUserLogin's WebView): the + * Pandar Hub URL, then the Hub's own sign-in page in a popup until it hands back a ticket. + */ +export function CloudSignInDialog({ onClose, onSignedIn }: { onClose: () => void; onSignedIn: () => void }) { const { ref, close } = useModal(onClose); - const [host, setHost] = useState(printHost() ?? { hubUrl: "", tenantId: "", token: "" }); - const field = (key: keyof PrintHost, label: string, type = "text") => ( - - ); + const [hubUrl, setHubUrl] = useState(cloudHubUrl); + const [waiting, setWaiting] = useState(); + const [error, setError] = useState(); + useEffect(() => () => waiting?.abort(), [waiting]); return ( - -
+ +
{ event.preventDefault(); - setPrintHost(host); - close(); - onSaved(); + const controller = new AbortController(); + setWaiting(controller); + setError(undefined); + signIn(hubUrl, controller.signal).then( + () => { + close(); + onSignedIn(); + }, + (reason: Error) => { + if (controller.signal.aborted) return; + setWaiting(undefined); + setError(reason.message); + }, + ); }} > -

Host Type: Pandar

- {field("hubUrl", "Hub URL", "url")} - {field("tenantId", "Tenant ID")} - {field("token", "API Token", "password")} + + {waiting &&

Continue in the Pandar sign-in window…

} + {error && ( +

+ {error} +

+ )}
- + {waiting ? ( + + ) : ( + + )}
@@ -77,9 +93,9 @@ const MODES: [CalibrationMode, string][] = [ ]; /** PrintHostSendDialog (PrintHostDialogs.cpp): picks a Hub printer and its print options, then uploads. */ -export function PrintHostSendDialog({ onClose, onEditHost }: { onClose: () => void; onEditHost: () => void }) { +export function PrintHostSendDialog({ onClose }: { onClose: () => void }) { const { ref, close } = useModal(onClose); - const host = printHost()!; + const cloud = useApp((state) => state.cloud)!; const [printers, setPrinters] = useState(); const [printerId, setPrinterId] = useState(""); const [options, setOptions] = useState({ @@ -91,14 +107,14 @@ export function PrintHostSendDialog({ onClose, onEditHost }: { onClose: () => vo }); const [state, setState] = useState<{ sending?: true; error?: string; sent?: true }>({}); useEffect(() => { - listPrinters(host).then( + listPrinters().then( (list) => { setPrinters(list); setPrinterId(list[0]?.id ?? ""); }, (error: Error) => setState({ error: error.message }), ); - }, [host]); + }, []); const printer = printers?.find(({ id }) => id === printerId); const blocker = printer && sendBlocker(printer); const bambu = printer?.printer_family === "bambu_lab"; @@ -135,17 +151,14 @@ export function PrintHostSendDialog({ onClose, onEditHost }: { onClose: () => vo onSubmit={(event) => { event.preventDefault(); setState({ sending: true }); - sendPlate(host, printer!, options).then( + sendPlate(printer!, options).then( () => setState({ sent: true }), (error: Error) => setState({ error: error.message }), ); }} >

- {host.hubUrl}{" "} - + {cloud.tenantName} · {cloud.hubUrl}