mirror of
https://github.com/Xevion/HATray.git
synced 2025-12-06 01:15:11 -06:00
refactor: proper tray logging
This commit is contained in:
@@ -48,7 +48,7 @@ func NewApp(logger *slog.Logger) *App {
|
|||||||
state: StatePaused,
|
state: StatePaused,
|
||||||
config: nil,
|
config: nil,
|
||||||
lastStarted: nil,
|
lastStarted: nil,
|
||||||
tray: &Tray{},
|
tray: NewTray(logger.With("type", "tray")),
|
||||||
ha: nil,
|
ha: nil,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,17 +72,14 @@ func (app *App) Pause() error {
|
|||||||
"previous_state", app.state,
|
"previous_state", app.state,
|
||||||
"new_state", StatePaused)
|
"new_state", StatePaused)
|
||||||
|
|
||||||
// TODO: Implement actual pause logic
|
|
||||||
// - Disconnect from Home Assistant WebSocket
|
// - Disconnect from Home Assistant WebSocket
|
||||||
// - Stop background tasks
|
|
||||||
// - Pause sensor monitoring
|
|
||||||
// - Stop tray icon event loop
|
|
||||||
|
|
||||||
err := app.ha.Close()
|
err := app.ha.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.logger.Error("failed to close home assistant connection", "error", err)
|
app.logger.Error("failed to close home assistant connection", "error", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// - Stop tray icon event loop
|
||||||
err = app.tray.Stop()
|
err = app.tray.Stop()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.logger.Error("failed to stop tray", "error", err)
|
app.logger.Error("failed to stop tray", "error", err)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package app
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"ha-tray/internal"
|
"ha-tray/internal"
|
||||||
|
"log/slog"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/getlantern/systray"
|
"github.com/getlantern/systray"
|
||||||
@@ -30,7 +31,16 @@ func (i IconReference) Path() string {
|
|||||||
|
|
||||||
type Tray struct {
|
type Tray struct {
|
||||||
active bool
|
active bool
|
||||||
currentIcon IconReference
|
currentIcon *IconReference
|
||||||
|
logger *slog.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTray(logger *slog.Logger) *Tray {
|
||||||
|
return &Tray{
|
||||||
|
logger: logger,
|
||||||
|
currentIcon: nil,
|
||||||
|
active: false,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tray) SetIcon(icon IconReference) error {
|
func (t *Tray) SetIcon(icon IconReference) error {
|
||||||
@@ -43,7 +53,7 @@ func (t *Tray) SetIcon(icon IconReference) error {
|
|||||||
return fmt.Errorf("failed to read icon: %w", err)
|
return fmt.Errorf("failed to read icon: %w", err)
|
||||||
}
|
}
|
||||||
systray.SetIcon(iconBytes)
|
systray.SetIcon(iconBytes)
|
||||||
t.currentIcon = icon
|
t.currentIcon = &icon
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -53,11 +63,13 @@ func (t *Tray) Start(title string) error {
|
|||||||
return fmt.Errorf("tray is already active")
|
return fmt.Errorf("tray is already active")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t.logger.Info("attempting to start systray", "title", title)
|
||||||
readyTimeout := make(chan struct{}, 1)
|
readyTimeout := make(chan struct{}, 1)
|
||||||
go systray.Run(func() {
|
go systray.Run(func() {
|
||||||
systray.SetTitle(title)
|
systray.SetTitle(title)
|
||||||
systray.SetTooltip(title)
|
systray.SetTooltip(title)
|
||||||
|
|
||||||
|
t.logger.Info("systray started")
|
||||||
readyTimeout <- struct{}{}
|
readyTimeout <- struct{}{}
|
||||||
close(readyTimeout)
|
close(readyTimeout)
|
||||||
}, func() {
|
}, func() {
|
||||||
@@ -66,11 +78,12 @@ func (t *Tray) Start(title string) error {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-readyTimeout:
|
case <-readyTimeout:
|
||||||
fmt.Println("tray started")
|
t.logger.Info("systray start confirmed")
|
||||||
t.active = true
|
t.active = true
|
||||||
return nil
|
return nil
|
||||||
case <-time.After(5 * time.Second):
|
case <-time.After(5 * time.Second):
|
||||||
close(readyTimeout)
|
close(readyTimeout)
|
||||||
|
t.logger.Error("systray start timed out")
|
||||||
return fmt.Errorf("tray did not start in time")
|
return fmt.Errorf("tray did not start in time")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user