2023-08-25 20:33:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-01-18 14:53:29 +00:00
|
|
|
"dmxconnect/hardware"
|
2023-08-25 20:33:11 +00:00
|
|
|
"fmt"
|
2024-11-01 20:10:28 +00:00
|
|
|
"io"
|
|
|
|
|
|
2025-01-18 14:53:29 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-11-01 20:10:28 +00:00
|
|
|
|
2025-01-18 14:53:29 +00:00
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
2023-08-25 20:33:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// App struct
|
|
|
|
|
type App struct {
|
2025-11-02 10:57:53 +01:00
|
|
|
ctx context.Context
|
|
|
|
|
cancelFunc context.CancelFunc
|
|
|
|
|
wait sync.WaitGroup
|
|
|
|
|
|
2025-11-24 19:23:50 +01:00
|
|
|
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
|
2023-08-25 20:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewApp creates a new App application struct
|
|
|
|
|
func NewApp() *App {
|
2025-01-18 14:53:29 +00:00
|
|
|
// Create a new hadware manager
|
2025-11-24 19:23:50 +01:00
|
|
|
hardwareManager := hardware.NewManager()
|
2025-01-18 14:53:29 +00:00
|
|
|
return &App{
|
|
|
|
|
hardwareManager: hardwareManager,
|
|
|
|
|
projectSave: "",
|
|
|
|
|
projectInfo: ProjectInfo{
|
|
|
|
|
PeripheralsInfo: make(map[string]hardware.PeripheralInfo),
|
|
|
|
|
},
|
|
|
|
|
}
|
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
|
2025-01-18 14:53:29 +00:00
|
|
|
func (a *App) onStartup(ctx context.Context) {
|
|
|
|
|
a.ctx, a.cancelFunc = context.WithCancel(ctx)
|
2025-11-02 10:57:53 +01:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
}()
|
2024-11-01 20:10:28 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-18 14:53:29 +00:00
|
|
|
// 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()
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// log.Err(err).Str("file", "app").Msg("unable to get the peripherals")
|
|
|
|
|
// }
|
|
|
|
|
return
|
2024-11-01 20:10:28 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-18 14:53:29 +00:00
|
|
|
// 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()
|
2025-11-02 10:57:53 +01:00
|
|
|
// 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")
|
|
|
|
|
// }
|
2025-01-18 14:53:29 +00:00
|
|
|
return
|
2024-11-01 20:10:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|