feat: 设备管理页添加清理离线设备按钮,删除设备及其关联数据
This commit is contained in:
parent
31c1a09b86
commit
198494cd78
@ -744,6 +744,7 @@ func (u *UI) Routes() (chi.Router, error) {
|
|||||||
r.Post("/wizard/apply", u.actionWizardApply)
|
r.Post("/wizard/apply", u.actionWizardApply)
|
||||||
r.Post("/wizard/create-source", u.actionWizardCreateSource)
|
r.Post("/wizard/create-source", u.actionWizardCreateSource)
|
||||||
r.Get("/devices", u.pageDevices)
|
r.Get("/devices", u.pageDevices)
|
||||||
|
r.Post("/devices/cleanup-offline", u.actionDevicesCleanupOffline)
|
||||||
r.Get("/devices/{id}/control", u.pageDeviceControl)
|
r.Get("/devices/{id}/control", u.pageDeviceControl)
|
||||||
r.Get("/plans", u.redirectPlansToSceneTemplates)
|
r.Get("/plans", u.redirectPlansToSceneTemplates)
|
||||||
r.Get("/plans/{name}", u.redirectPlanToSceneTemplate)
|
r.Get("/plans/{name}", u.redirectPlanToSceneTemplate)
|
||||||
@ -1572,6 +1573,32 @@ func (u *UI) pageDevices(w http.ResponseWriter, r *http.Request) {
|
|||||||
u.render(w, r, "devices", u.deviceOverviewPageData(r, nil, ""))
|
u.render(w, r, "devices", u.deviceOverviewPageData(r, nil, ""))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *UI) actionDevicesCleanupOffline(w http.ResponseWriter, r *http.Request) {
|
||||||
|
u.ensureDevicesLoaded()
|
||||||
|
devices := u.registry.GetDevices()
|
||||||
|
deleted := 0
|
||||||
|
for _, dev := range devices {
|
||||||
|
if dev == nil || dev.Online || dev.DeviceID == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Delete related data from DB
|
||||||
|
if strings.TrimSpace(u.dbPath) != "" {
|
||||||
|
if store, err := storage.OpenSQLite(u.dbPath); err == nil {
|
||||||
|
db := store.DB()
|
||||||
|
db.Exec("DELETE FROM device_assignments WHERE device_id = ?", dev.DeviceID)
|
||||||
|
db.Exec("DELETE FROM recognition_units WHERE scene_template_name IN (SELECT profile_name FROM device_assignments WHERE device_id = ?)", dev.DeviceID)
|
||||||
|
db.Exec("DELETE FROM device_config_state WHERE device_id = ?", dev.DeviceID)
|
||||||
|
db.Exec("DELETE FROM device_features WHERE device_id = ?", dev.DeviceID)
|
||||||
|
db.Exec("DELETE FROM config_versions WHERE device_id = ?", dev.DeviceID)
|
||||||
|
db.Exec("DELETE FROM devices WHERE device_id = ?", dev.DeviceID)
|
||||||
|
store.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
deleted++
|
||||||
|
}
|
||||||
|
http.Redirect(w, r, "/devices?msg="+urlQueryEscape(fmt.Sprintf("已清理 %d 台离线设备", deleted)), http.StatusFound)
|
||||||
|
}
|
||||||
|
|
||||||
func (u *UI) pageDeviceAdd(w http.ResponseWriter, r *http.Request) {
|
func (u *UI) pageDeviceAdd(w http.ResponseWriter, r *http.Request) {
|
||||||
u.render(w, r, "device_add", PageData{Title: "新增设备"})
|
u.render(w, r, "device_add", PageData{Title: "新增设备"})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,6 +29,11 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="table-tools">
|
<div class="table-tools">
|
||||||
<input id="device-filter" placeholder="搜索设备名、ID 或 IP" />
|
<input id="device-filter" placeholder="搜索设备名、ID 或 IP" />
|
||||||
|
{{if gt .OfflineCount 0}}
|
||||||
|
<form method="post" action="/devices/cleanup-offline" style="display:inline" onsubmit="return confirm('确认清理全部 ' + {{.OfflineCount}} + ' 台离线设备及其相关数据?此操作不可恢复。')">
|
||||||
|
<button type="submit" class="btn danger">清理离线设备 ({{.OfflineCount}})</button>
|
||||||
|
</form>
|
||||||
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Loading…
Reference in New Issue
Block a user