Files
dmxconnect/app.go

111 lines
2.9 KiB
Go
Raw Normal View History

2023-08-25 20:33:11 +00:00
package main
import (
"context"
2025-01-04 00:36:29 +01:00
"dmxconnect/hardware"
2023-08-25 20:33:11 +00:00
"fmt"
"io"
2025-01-04 00:36:29 +01:00
"time"
"github.com/rs/zerolog/log"
"os"
"strings"
2024-12-15 13:45:46 +01:00
"sync"
2023-08-25 20:33:11 +00:00
)
// App struct
type App struct {
2024-12-15 13:45:46 +01:00
ctx context.Context
2025-01-04 00:36:29 +01:00
cancelFunc context.CancelFunc
2024-12-15 13:45:46 +01:00
hardwareManager *hardware.HardwareManager // For managing all the hardware
wmiMutex sync.Mutex // Avoid some WMI operations at the same time
2024-12-20 17:18:57 +01:00
projectInfo ProjectInfo // The project information structure
projectSave string // The file name of the project
2023-08-25 20:33:11 +00:00
}
// NewApp creates a new App application struct
func NewApp() *App {
2024-12-15 13:45:46 +01:00
// Create a new hadware manager
hardwareManager := hardware.NewHardwareManager()
2025-01-04 12:10:25 +01:00
hardwareManager.RegisterFinder(hardware.NewMIDIFinder(5 * time.Second))
hardwareManager.RegisterFinder(hardware.NewFTDIFinder(5 * time.Second))
hardwareManager.RegisterFinder(hardware.NewOS2LFinder())
2024-12-15 13:45:46 +01:00
return &App{
hardwareManager: hardwareManager,
2024-12-20 17:18:57 +01:00
projectSave: "",
2024-12-21 13:24:00 +01:00
projectInfo: ProjectInfo{
PeripheralsInfo: make(map[string]hardware.PeripheralInfo),
},
2024-12-15 13:45:46 +01:00
}
2023-08-25 20:33:11 +00:00
}
// 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) {
2025-01-04 00:36:29 +01:00
a.ctx, a.cancelFunc = context.WithCancel(ctx)
err := a.hardwareManager.Start(a.ctx)
2024-12-15 13:45:46 +01:00
if err != nil {
log.Err(err).Str("file", "app").Msg("unable to start the hardware manager")
2024-12-15 13:45:46 +01:00
return
}
}
// onReady is called when the DOM is ready
// We get the current peripherals connected
func (a *App) onReady(ctx context.Context) {
2025-01-04 00:36:29 +01:00
// log.Debug().Str("file", "peripherals").Msg("getting peripherals...")
// err := a.hardwareManager.Scan()
// 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) {
2025-01-04 00:36:29 +01:00
// Close the application properly
log.Trace().Str("file", "app").Msg("app is closing")
// Explicitly close the context
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
2023-08-25 20:33:11 +00:00
}