Review 問題修復: M1(寫已關閉 channel panic): - flash service goroutine 改成先等 driver.Flash() 返回,再寫 error 訊息,最後 close - driver.Flash 返回後保證不再寫 progressCh,消除 race condition M2(FlashTask 永不清除 memory leak): - service.go 新增 CleanupTask(taskID) 公開方法 - device_handler.go 的 goroutine 在 `for range progressCh` 結束後呼叫 CleanupTask M3(同裝置重複 flash taskID 衝突): - ProgressTracker.Create 改成:舊 task 未完成時返回 nil - StartFlash 檢查 nil → 回傳 "flash already in progress" 錯誤 M4(前端 flash store 全域不區分 deviceId): - flash-store.ts 新增 activeDeviceId 欄位 - updateProgress 改接 (deviceId, progress),比對 activeDeviceId 防止混裝 - use-flash-progress.ts 的 WebSocket callback 傳入 deviceId m5(flash_ws.go 雙重 conn.Close): - read pump goroutine 移除 defer conn.Close(),由外層 defer 統一關閉 額外修復(S4): - modelPath 為空時直接回 error 而非傳無效路徑給 driver Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
144 lines
3.7 KiB
Go
144 lines
3.7 KiB
Go
package flash
|
||
|
||
import (
|
||
"fmt"
|
||
"os"
|
||
"path/filepath"
|
||
"strings"
|
||
"time"
|
||
|
||
"visiona-local/server/internal/device"
|
||
"visiona-local/server/internal/driver"
|
||
"visiona-local/server/internal/model"
|
||
)
|
||
|
||
func isCompatible(modelHardware []string, deviceType string) bool {
|
||
dt := strings.ToUpper(deviceType)
|
||
for _, hw := range modelHardware {
|
||
if strings.ToUpper(hw) == dt || strings.Contains(dt, strings.ToUpper(hw)) {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
func resolveModelPath(filePath string, deviceType string) string {
|
||
if filePath == "" {
|
||
return filePath
|
||
}
|
||
|
||
targetChip := ""
|
||
if strings.Contains(strings.ToLower(deviceType), "kl720") {
|
||
targetChip = "kl720"
|
||
} else if strings.Contains(strings.ToLower(deviceType), "kl520") {
|
||
targetChip = "kl520"
|
||
}
|
||
if targetChip == "" {
|
||
return filePath
|
||
}
|
||
|
||
if strings.Contains(filePath, "/"+targetChip+"/") {
|
||
return filePath
|
||
}
|
||
|
||
dir := filepath.Dir(filePath)
|
||
base := filepath.Base(filePath)
|
||
|
||
sourceChip := ""
|
||
if strings.Contains(dir, "kl520") {
|
||
sourceChip = "kl520"
|
||
} else if strings.Contains(dir, "kl720") {
|
||
sourceChip = "kl720"
|
||
}
|
||
|
||
if sourceChip != "" && sourceChip != targetChip {
|
||
newDir := strings.Replace(dir, sourceChip, targetChip, 1)
|
||
newBase := strings.Replace(base, sourceChip, targetChip, 1)
|
||
candidate := filepath.Join(newDir, newBase)
|
||
if _, err := os.Stat(candidate); err == nil {
|
||
return candidate
|
||
}
|
||
}
|
||
|
||
return filePath
|
||
}
|
||
|
||
type Service struct {
|
||
deviceMgr *device.Manager
|
||
modelRepo *model.Repository
|
||
tracker *ProgressTracker
|
||
}
|
||
|
||
func NewService(deviceMgr *device.Manager, modelRepo *model.Repository) *Service {
|
||
return &Service{
|
||
deviceMgr: deviceMgr,
|
||
modelRepo: modelRepo,
|
||
tracker: NewProgressTracker(),
|
||
}
|
||
}
|
||
|
||
// CleanupTask 清除已完成的 flash task(由 handler goroutine 在讀取完 progressCh 後呼叫)。
|
||
func (s *Service) CleanupTask(taskID string) {
|
||
s.tracker.Remove(taskID)
|
||
}
|
||
|
||
func (s *Service) StartFlash(deviceID, modelID string) (string, <-chan driver.FlashProgress, error) {
|
||
session, err := s.deviceMgr.GetDevice(deviceID)
|
||
if err != nil {
|
||
return "", nil, fmt.Errorf("device not found: %w", err)
|
||
}
|
||
if !session.Driver.IsConnected() {
|
||
return "", nil, fmt.Errorf("device not connected")
|
||
}
|
||
|
||
m, err := s.modelRepo.GetByID(modelID)
|
||
if err != nil {
|
||
return "", nil, fmt.Errorf("model not found: %w", err)
|
||
}
|
||
|
||
deviceInfo := session.Driver.Info()
|
||
if !isCompatible(m.SupportedHardware, deviceInfo.Type) {
|
||
return "", nil, fmt.Errorf("model not compatible with device type %s", deviceInfo.Type)
|
||
}
|
||
|
||
modelPath := m.FilePath
|
||
if modelPath == "" {
|
||
return "", nil, fmt.Errorf("model %s has no .nef file path", modelID)
|
||
}
|
||
|
||
modelPath = resolveModelPath(modelPath, deviceInfo.Type)
|
||
|
||
taskID := fmt.Sprintf("flash-%s-%s", deviceID, modelID)
|
||
|
||
// M3 fix: 防止同裝置同模型重複 flash
|
||
task := s.tracker.Create(taskID, deviceID, modelID)
|
||
if task == nil {
|
||
return "", nil, fmt.Errorf("flash already in progress for device %s model %s", deviceID, modelID)
|
||
}
|
||
|
||
go func() {
|
||
// M1 fix: 先跑 driver.Flash,收集 error,最後才寫 error message + close channel。
|
||
// driver.Flash 內部會多次寫入 task.ProgressCh(進度更新),我們不能在它還在寫的時候 close。
|
||
// driver.Flash 返回時保證不會再寫入 progressCh。
|
||
|
||
time.Sleep(500 * time.Millisecond)
|
||
|
||
flashErr := session.Driver.Flash(modelPath, task.ProgressCh)
|
||
|
||
// Flash 完成或失敗後,driver 不會再寫 progressCh,安全地寫 error 訊息然後 close。
|
||
if flashErr != nil {
|
||
task.ProgressCh <- driver.FlashProgress{
|
||
Percent: -1,
|
||
Stage: "error",
|
||
Error: flashErr.Error(),
|
||
}
|
||
}
|
||
|
||
task.Done = true
|
||
close(task.ProgressCh)
|
||
// M2 note: 不在這裡 Remove — 讓 handler 讀完 progressCh 後呼叫 CleanupTask
|
||
}()
|
||
|
||
return taskID, task.ProgressCh, nil
|
||
}
|