mirror of
https://github.com/Xevion/HATray.git
synced 2025-12-05 23:15:09 -06:00
refactor: improve logging, no error on null state transitions
This commit is contained in:
@@ -37,7 +37,7 @@ func main() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Create service layer
|
// Create service layer
|
||||||
svc := service.NewService(mainLogger)
|
svc := service.NewService(rootLogger)
|
||||||
|
|
||||||
mainLogger.Info("service initialized")
|
mainLogger.Info("service initialized")
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,8 @@ func (app *App) Pause() error {
|
|||||||
|
|
||||||
switch app.state {
|
switch app.state {
|
||||||
case StatePaused:
|
case StatePaused:
|
||||||
return fmt.Errorf("application is already paused")
|
app.logger.Warn("application is already paused")
|
||||||
|
return nil
|
||||||
case StateRunning:
|
case StateRunning:
|
||||||
// valid state to pause from, do nothing
|
// valid state to pause from, do nothing
|
||||||
default:
|
default:
|
||||||
@@ -96,13 +97,15 @@ func (app *App) Pause() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Resume connects to the server and initiates background tasks
|
// Resume connects to the server and initiates background tasks
|
||||||
|
// This function does not block permanently, it will return very quickly with an error if anything goes wrong.
|
||||||
func (app *App) Resume() error {
|
func (app *App) Resume() error {
|
||||||
app.mu.Lock()
|
app.mu.Lock()
|
||||||
defer app.mu.Unlock()
|
defer app.mu.Unlock()
|
||||||
|
|
||||||
switch app.state {
|
switch app.state {
|
||||||
case StateRunning:
|
case StateRunning:
|
||||||
return fmt.Errorf("application is already running")
|
app.logger.Warn("application is already running")
|
||||||
|
return nil
|
||||||
case StatePaused:
|
case StatePaused:
|
||||||
// valid state to resume from, do nothing
|
// valid state to resume from, do nothing
|
||||||
default:
|
default:
|
||||||
@@ -128,8 +131,13 @@ func (app *App) Resume() error {
|
|||||||
|
|
||||||
app.config = DefaultConfig()
|
app.config = DefaultConfig()
|
||||||
|
|
||||||
|
if err := app.config.Validate(); err != nil {
|
||||||
|
app.logger.Error("invalid configuration", "error", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
app.ha, err = ga.NewApp(ga.NewAppRequest{
|
app.ha, err = ga.NewApp(ga.NewAppRequest{
|
||||||
URL: app.config.Server,
|
URL: *app.config.Server,
|
||||||
HAAuthToken: app.config.APIKey,
|
HAAuthToken: app.config.APIKey,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -60,7 +60,8 @@ func (t *Tray) SetIcon(icon IconReference) error {
|
|||||||
|
|
||||||
func (t *Tray) Start(title string) error {
|
func (t *Tray) Start(title string) error {
|
||||||
if t.active {
|
if t.active {
|
||||||
return fmt.Errorf("tray is already active")
|
t.logger.Warn("tray is already active")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
t.logger.Info("attempting to start systray", "title", title)
|
t.logger.Info("attempting to start systray", "title", title)
|
||||||
|
|||||||
@@ -15,8 +15,9 @@ import (
|
|||||||
"golang.org/x/sys/windows/registry"
|
"golang.org/x/sys/windows/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WindowsTrayService implements the Service interface for Windows as a user application
|
// windowsService implements the Service interface for Windows
|
||||||
type WindowsTrayService struct {
|
// Note that this is a user application, not a true SCM service
|
||||||
|
type windowsService struct {
|
||||||
app *app.App
|
app *app.App
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
restartCount int
|
restartCount int
|
||||||
@@ -28,7 +29,7 @@ type WindowsTrayService struct {
|
|||||||
|
|
||||||
// NewService creates a new Windows tray service instance
|
// NewService creates a new Windows tray service instance
|
||||||
func NewService(logger *slog.Logger) Service {
|
func NewService(logger *slog.Logger) Service {
|
||||||
return &WindowsTrayService{
|
return &windowsService{
|
||||||
logger: logger.With("type", "service", "variant", "windows"),
|
logger: logger.With("type", "service", "variant", "windows"),
|
||||||
app: app.NewApp(logger),
|
app: app.NewApp(logger),
|
||||||
maxRestarts: 3,
|
maxRestarts: 3,
|
||||||
@@ -39,7 +40,7 @@ func NewService(logger *slog.Logger) Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Run implements the Service interface for Windows
|
// Run implements the Service interface for Windows
|
||||||
func (svc *WindowsTrayService) Run() error {
|
func (svc *windowsService) Run() error {
|
||||||
svc.logger.Info("starting Windows tray service")
|
svc.logger.Info("starting Windows tray service")
|
||||||
|
|
||||||
// Setup auto-start if not already configured
|
// Setup auto-start if not already configured
|
||||||
@@ -79,11 +80,12 @@ func (svc *WindowsTrayService) Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// runServiceLoop runs the main service loop
|
// runServiceLoop runs the main service loop
|
||||||
func (svc *WindowsTrayService) runServiceLoop(sigs chan os.Signal) error {
|
func (svc *windowsService) runServiceLoop(sigs chan os.Signal) error {
|
||||||
// Start the application in background
|
// Start the application in background
|
||||||
go func() {
|
go func() {
|
||||||
if err := svc.app.Resume(); err != nil {
|
if err := svc.app.Resume(); err != nil {
|
||||||
svc.logger.Error("failed to start app layer", "error", err)
|
svc.logger.Error("failed to start app layer", "error", err)
|
||||||
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -128,7 +130,7 @@ func (svc *WindowsTrayService) runServiceLoop(sigs chan os.Signal) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// setupAutoStart configures the application to start automatically on login
|
// setupAutoStart configures the application to start automatically on login
|
||||||
func (svc *WindowsTrayService) setupAutoStart() error {
|
func (svc *windowsService) setupAutoStart() error {
|
||||||
exePath, err := os.Executable()
|
exePath, err := os.Executable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get executable path: %v", err)
|
return fmt.Errorf("failed to get executable path: %v", err)
|
||||||
@@ -152,7 +154,7 @@ func (svc *WindowsTrayService) setupAutoStart() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// removeAutoStart removes the auto-start configuration
|
// removeAutoStart removes the auto-start configuration
|
||||||
func (svc *WindowsTrayService) removeAutoStart() error {
|
func (svc *windowsService) removeAutoStart() error {
|
||||||
key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Run`, registry.SET_VALUE)
|
key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Run`, registry.SET_VALUE)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to open registry key: %v", err)
|
return fmt.Errorf("failed to open registry key: %v", err)
|
||||||
@@ -168,7 +170,7 @@ func (svc *WindowsTrayService) removeAutoStart() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// setupPowerManagement handles sleep/wake events
|
// setupPowerManagement handles sleep/wake events
|
||||||
func (svc *WindowsTrayService) setupPowerManagement() {
|
func (svc *windowsService) setupPowerManagement() {
|
||||||
// TODO: Implement Windows power management
|
// TODO: Implement Windows power management
|
||||||
// - Listen for WM_POWERBROADCAST messages
|
// - Listen for WM_POWERBROADCAST messages
|
||||||
// - Handle system sleep/wake events
|
// - Handle system sleep/wake events
|
||||||
@@ -177,7 +179,7 @@ func (svc *WindowsTrayService) setupPowerManagement() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// isAppHealthy checks if the application is running properly
|
// isAppHealthy checks if the application is running properly
|
||||||
func (svc *WindowsTrayService) isAppHealthy() bool {
|
func (svc *windowsService) isAppHealthy() bool {
|
||||||
// TODO: Implement health checks
|
// TODO: Implement health checks
|
||||||
// - Check if Home Assistant connection is alive
|
// - Check if Home Assistant connection is alive
|
||||||
// - Check if systray is responsive
|
// - Check if systray is responsive
|
||||||
|
|||||||
Reference in New Issue
Block a user