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 }