package main import ( "context" "dmxconnect/hardware" genericmidi "dmxconnect/hardware/genericMIDI" "dmxconnect/hardware/genericftdi" "dmxconnect/hardware/os2l" "fmt" "io" "time" "github.com/rs/zerolog/log" "os" "strings" "sync" ) // App struct type App struct { ctx context.Context cancelFunc context.CancelFunc wait sync.WaitGroup hardwareManager *hardware.Manager // 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.NewManager() // Register all the providers to use as hardware scanners hardwareManager.RegisterProvider(genericftdi.NewProvider(3 * time.Second)) hardwareManager.RegisterProvider(os2l.NewProvider()) hardwareManager.RegisterProvider(genericmidi.NewProvider(3 * time.Second)) return &App{ hardwareManager: hardwareManager, projectSave: "", projectInfo: ProjectInfo{ EndpointsInfo: make(map[string]hardware.EndpointInfo), }, } } // 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, a.cancelFunc = context.WithCancel(ctx) // Starting the hardware manager a.wait.Add(1) go func() { defer a.wait.Done() err := a.hardwareManager.Start(a.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 endpoints connected func (a *App) onReady(ctx context.Context) { // log.Debug().Str("file", "endpoints").Msg("getting endpoints...") // err := a.hardwareManager.Scan() // if err != nil { // log.Err(err).Str("file", "app").Msg("unable to get the endpoints") // } return } // onShutdown is called when the app is closing // We stop all the pending processes func (a *App) onShutdown(ctx context.Context) { // Close the application properly log.Trace().Str("file", "app").Msg("app is closing") // Explicitly close the context a.cancelFunc() // Wait for application to close properly a.hardwareManager.WaitStop() // a.cancelFunc() // err := a.hardwareManager.Stop() // if err != nil { // log.Err(err).Str("file", "app").Msg("unable to stop the hardware manager") // } 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 }