package main import ( "changeme/hardware" "context" "fmt" "io" "github.com/rs/zerolog/log" "os" "strings" "sync" ) // App struct type App struct { ctx context.Context hardwareManager *hardware.HardwareManager // For managing all the hardware wmiMutex sync.Mutex // Avoid some WMI operations at the same time projectInfo ProjectInfo // The project information structure projectSave string // The file name of the project } // NewApp creates a new App application struct func NewApp() *App { // Create a new hadware manager hardwareManager := hardware.NewHardwareManager() hardwareManager.RegisterDriver(hardware.NewMIDIDriver()) hardwareManager.RegisterDriver(hardware.NewFTDIDriver()) hardwareManager.RegisterDriver(hardware.NewOS2LDriver()) return &App{ hardwareManager: hardwareManager, projectSave: "", projectInfo: ProjectInfo{ PeripheralsInfo: make(map[string]hardware.PeripheralInfo), }, } } // startup is called when the app starts. The context is saved // so we can call the runtime methods func (a *App) onStartup(ctx context.Context) { a.ctx = ctx err := a.hardwareManager.Start(ctx) if err != nil { log.Err(err).Str("file", "app").Msg("unable to start the hardware manager") return } } // onReady is called when the DOM is ready // We get the current peripherals connected func (a *App) onReady(ctx context.Context) { log.Debug().Str("file", "peripherals").Msg("getting peripherals...") err := a.hardwareManager.Scan(a.ctx) if err != nil { log.Err(err).Str("file", "app").Msg("unable to get the peripherals") } return } // onShutdown is called when the app is closing // We stop all the pending processes func (a *App) onShutdown(ctx context.Context) { log.Warn().Str("file", "app").Msg("app is closing") return } func formatString(input string) string { // Convertir en minuscules lowerCaseString := strings.ToLower(input) // Remplacer les espaces par des underscores formattedString := strings.ReplaceAll(lowerCaseString, " ", "_") return formattedString } func copy(src, dst string) (int64, error) { sourceFileStat, err := os.Stat(src) if err != nil { return 0, err } if !sourceFileStat.Mode().IsRegular() { return 0, fmt.Errorf("%s is not a regular file", src) } source, err := os.Open(src) if err != nil { return 0, err } defer source.Close() destination, err := os.Create(dst) if err != nil { return 0, err } defer destination.Close() nBytes, err := io.Copy(destination, source) return nBytes, err }