feat: add locking styles
This commit is contained in:
@@ -127,16 +127,61 @@ export async function create(params: {
|
||||
}
|
||||
|
||||
export function forDSP408(dsp: DSP408, setDsps: SetDsps, notify: NotifyFn) {
|
||||
async function updateDSPState(): Promise<void> {
|
||||
async function updateDSPState(): Promise<DSP408['state']> {
|
||||
try {
|
||||
const state = await invoke<DSP408['state']>('get_dsp_state', { id: dsp.id });
|
||||
updateDSP(setDsps, dsp.id, (d) => ({ ...d, state }) as DSP408);
|
||||
|
||||
return state;
|
||||
} catch (err) {
|
||||
notify(`Failed to fetch state: ${String(err)}`, 'error', dsp.name);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
const isLocked = (s: DSP408['state']) => s.flags.is_locked;
|
||||
|
||||
async function abortLocked() {
|
||||
await invoke('disconnect_dsp408', { id: dsp.id }).catch(() => {});
|
||||
updateDSP(setDsps, dsp.id, (d) => ({ ...d, status: 'disconnected', passwordRequired: true }));
|
||||
}
|
||||
|
||||
async function connectInternal(password?: string) {
|
||||
updateDSP(setDsps, dsp.id, (d) => ({ ...d, status: 'connecting', passwordRequired: false }));
|
||||
try {
|
||||
await invoke('connect_dsp408', { id: dsp.id });
|
||||
|
||||
let state = await updateDSPState();
|
||||
|
||||
if (isLocked(state)) {
|
||||
if (password === undefined) {
|
||||
await abortLocked();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await invoke('unlock_device', { id: dsp.id, password }); // adjust name/signature
|
||||
} catch {
|
||||
// wrong password
|
||||
}
|
||||
|
||||
state = await updateDSPState();
|
||||
if (isLocked(state)) {
|
||||
await abortLocked();
|
||||
notify('Wrong password', 'error', dsp.name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
updateDSP(setDsps, dsp.id, (d) => ({ ...d, status: 'connected', pollingPaused: false }));
|
||||
notify('Connected', 'success', dsp.name);
|
||||
startPolling(dsp, setDsps);
|
||||
} catch (err) {
|
||||
updateDSP(setDsps, dsp.id, (d) => ({ ...d, status: 'disconnected' }));
|
||||
notify(`Connection failed: ${String(err)}`, 'error', dsp.name);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'DSP408' as const,
|
||||
|
||||
@@ -155,22 +200,10 @@ export function forDSP408(dsp: DSP408, setDsps: SetDsps, notify: NotifyFn) {
|
||||
|
||||
updateDSPState,
|
||||
|
||||
async connect() {
|
||||
updateDSP(setDsps, dsp.id, (d) => ({ ...d, status: 'connecting' }));
|
||||
try {
|
||||
await invoke('connect_dsp408', { id: dsp.id });
|
||||
|
||||
await updateDSPState();
|
||||
|
||||
updateDSP(setDsps, dsp.id, (d) => ({ ...d, status: 'connected', pollingPaused: false }));
|
||||
|
||||
notify('Connected', 'success', dsp.name);
|
||||
|
||||
startPolling(dsp, setDsps);
|
||||
} catch (err) {
|
||||
updateDSP(setDsps, dsp.id, (d) => ({ ...d, status: 'disconnected' }));
|
||||
notify(`Connection failed: ${String(err)}`, 'error', dsp.name);
|
||||
}
|
||||
connect: () => connectInternal(),
|
||||
connectWithPassword: (password: string) => connectInternal(password),
|
||||
cancelPassword() {
|
||||
updateDSP(setDsps, dsp.id, (d) => ({ ...d, passwordRequired: false }));
|
||||
},
|
||||
|
||||
async disconnect() {
|
||||
|
||||
Reference in New Issue
Block a user