jim800121chen aa324cef42 fix(local-tool): flash 模型檔案找不到 — 相對路徑未解析
根因:models.json 的 filePath 是相對路徑("data/nef/kl520/xxx.nef"),
但 server working directory 是 {app}\bin\(app.go 設 cmd.Dir = binary 目錄),
所以 server 在 {app}\bin\data\nef\ 找 .nef 檔,找不到。

實際位置:{app}\data\nef\(installer 裝的位置),對應 server 的 --data-dir
由 Wails app 傳入的 %APPDATA%\visiona-local(或 fallback 到 <exe>/../data)。

修法:
- flash.NewService 新增 dataDir 參數
- StartFlash 中把相對 filePath 用 dataDir 拼接成絕對路徑:
  "data/nef/..." → 去掉 "data/" 前綴 → dataDir + "/nef/..."
- main.go 傳 dataDir 給 flash.NewService

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 22:03:28 +08:00

158 lines
4.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
dataDir string
tracker *ProgressTracker
}
func NewService(deviceMgr *device.Manager, modelRepo *model.Repository, dataDir string) *Service {
return &Service{
deviceMgr: deviceMgr,
modelRepo: modelRepo,
dataDir: dataDir,
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)
}
// models.json 的 filePath 是相對路徑(例如 "data/nef/kl520/xxx.nef")。
// 如果不是絕對路徑,用 dataDir 解析:
// "data/nef/..." → 去掉 "data/" 前綴 → dataDir + "/nef/..."
// 其他相對路徑 → dataDir + "/" + filePath
if !filepath.IsAbs(modelPath) {
if strings.HasPrefix(modelPath, "data/") || strings.HasPrefix(modelPath, "data\\") {
modelPath = filepath.Join(s.dataDir, modelPath[len("data/"):])
} else {
modelPath = filepath.Join(s.dataDir, modelPath)
}
}
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
}