feat(zigbee): import as background scan run
Mirrors IP scan flow: POST /zigbee/import-pending now creates a ScanRun(kind=zigbee, status=running) and returns immediately. Networkmap fetch + pending upsert run in the background, status transitions to done/error when finished. Frontend: import modal closes on submit, scan history shows the run with a ZIG/IP kind chip and toasts on completion. Pending modal auto-refreshes when run finishes. scan_runs.kind column added (default 'ip', idempotent migration). Existing zigbee tests refactored to exercise _persist_pending_import directly (background tasks don't see the test session); route test verifies the run is created with kind=zigbee.
This commit is contained in:
+3
-19
@@ -541,25 +541,9 @@ export default function App() {
|
||||
open={zigbeeImportOpen}
|
||||
onClose={() => setZigbeeImportOpen(false)}
|
||||
onAddToCanvas={handleZigbeeAddToCanvas}
|
||||
onPendingImported={(coordinator) => {
|
||||
useCanvasStore.getState().notifyScanDeviceFound()
|
||||
if (coordinator) {
|
||||
const exists = useCanvasStore.getState().nodes.some((n) => n.id === coordinator.id)
|
||||
if (!exists) {
|
||||
addNode({
|
||||
id: coordinator.id,
|
||||
type: 'zigbee_coordinator',
|
||||
position: { x: 600, y: 100 },
|
||||
data: {
|
||||
label: coordinator.label,
|
||||
type: 'zigbee_coordinator' as NodeData['type'],
|
||||
status: 'unknown' as const,
|
||||
services: [],
|
||||
},
|
||||
})
|
||||
markUnsaved()
|
||||
}
|
||||
}
|
||||
onPendingImported={() => {
|
||||
setSidebarForceView(undefined)
|
||||
setTimeout(() => setSidebarForceView('history'), 0)
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -125,11 +125,13 @@ export const zigbeeApi = {
|
||||
mqtt_tls_insecure?: boolean
|
||||
}) =>
|
||||
api.post<{
|
||||
pending_created: number
|
||||
pending_updated: number
|
||||
coordinator: { id: string; label: string; ieee_address: string } | null
|
||||
coordinator_already_existed: boolean
|
||||
links_recorded: number
|
||||
device_count: number
|
||||
id: string
|
||||
status: string
|
||||
kind: string
|
||||
ranges: string[]
|
||||
devices_found: number
|
||||
started_at: string
|
||||
finished_at: string | null
|
||||
error: string | null
|
||||
}>('/zigbee/import-pending', data),
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ const PENDING_TRIGGERS: { kind: 'pending' | 'hidden'; icon: typeof ScanLine; lab
|
||||
interface ScanRun {
|
||||
id: string
|
||||
status: string
|
||||
kind?: string
|
||||
ranges: string[]
|
||||
devices_found: number
|
||||
started_at: string
|
||||
@@ -197,12 +198,19 @@ function ScanHistoryPanel() {
|
||||
const res = await scanApi.runs()
|
||||
const next: ScanRun[] = res.data
|
||||
|
||||
// Toast when a run transitions from running → error
|
||||
// Surface transitions and refresh dependent UI
|
||||
for (const run of next) {
|
||||
const prev = prevRunsRef.current.find((r) => r.id === run.id)
|
||||
if (prev?.status === 'running' && run.status === 'error') {
|
||||
toast.error(`Scan failed: ${run.error ?? 'unknown error'}`)
|
||||
}
|
||||
if (prev?.status === 'running' && run.status === 'done') {
|
||||
if (run.kind === 'zigbee') {
|
||||
toast.success(`Zigbee import done — ${run.devices_found} device${run.devices_found !== 1 ? 's' : ''}`)
|
||||
}
|
||||
// Notify pending modal/canvas to refresh
|
||||
useCanvasStore.getState().notifyScanDeviceFound()
|
||||
}
|
||||
}
|
||||
prevRunsRef.current = next
|
||||
setRuns(next)
|
||||
@@ -263,6 +271,14 @@ function ScanHistoryPanel() {
|
||||
<span className="w-1.5 h-1.5 rounded-full shrink-0" style={{ backgroundColor: statusColor(r.status) }} />
|
||||
<span className="font-mono text-foreground capitalize">{r.status}</span>
|
||||
{r.status === 'running' && <Loader2 size={10} className="animate-spin text-[#e3b341]" />}
|
||||
<span
|
||||
className="text-[9px] font-mono px-1 py-0.5 rounded uppercase tracking-wider"
|
||||
style={r.kind === 'zigbee'
|
||||
? { background: '#00d4ff22', color: '#00d4ff' }
|
||||
: { background: '#a855f722', color: '#a855f7' }}
|
||||
>
|
||||
{r.kind === 'zigbee' ? 'ZIG' : 'IP'}
|
||||
</span>
|
||||
<span className="ml-auto text-muted-foreground font-mono">{r.devices_found} found</span>
|
||||
{r.status === 'running' && (
|
||||
<Tooltip>
|
||||
|
||||
@@ -138,19 +138,9 @@ export function ZigbeeImportModal({ open, onClose, onAddToCanvas, onPendingImpor
|
||||
setLoading(true)
|
||||
try {
|
||||
if (importMode === 'pending') {
|
||||
const res = await zigbeeApi.importToPending(buildPayload())
|
||||
const { pending_created, pending_updated, coordinator, coordinator_already_existed, device_count } = res.data
|
||||
if (device_count === 0) {
|
||||
toast.info('No Zigbee devices found in the network map')
|
||||
} else {
|
||||
const coordMsg = coordinator_already_existed
|
||||
? 'coordinator already on canvas'
|
||||
: 'coordinator added to canvas'
|
||||
toast.success(
|
||||
`Imported ${pending_created} new, updated ${pending_updated} (${coordMsg})`,
|
||||
)
|
||||
}
|
||||
onPendingImported?.(coordinator)
|
||||
await zigbeeApi.importToPending(buildPayload())
|
||||
toast.success('Zigbee import started — track progress in Scan History')
|
||||
onPendingImported?.(null)
|
||||
handleClose()
|
||||
} else {
|
||||
const res = await zigbeeApi.importNetwork(buildPayload())
|
||||
|
||||
@@ -181,12 +181,14 @@ describe('ZigbeeImportModal', () => {
|
||||
it('imports to pending by default and notifies parent', async () => {
|
||||
vi.mocked(zigbeeApi.importToPending).mockResolvedValue({
|
||||
data: {
|
||||
pending_created: 2,
|
||||
pending_updated: 0,
|
||||
coordinator: { id: 'coord-uuid', label: 'Coordinator', ieee_address: '0x0000' },
|
||||
coordinator_already_existed: false,
|
||||
links_recorded: 1,
|
||||
device_count: 3,
|
||||
id: 'run-1',
|
||||
status: 'running',
|
||||
kind: 'zigbee',
|
||||
ranges: ['192.168.1.100:1883'],
|
||||
devices_found: 0,
|
||||
started_at: '2026-01-01T00:00:00Z',
|
||||
finished_at: null,
|
||||
error: null,
|
||||
},
|
||||
} as never)
|
||||
const onPendingImported = vi.fn()
|
||||
|
||||
Reference in New Issue
Block a user