feat(agent): displaycontroller in app.go verdrahtet

This commit is contained in:
Jesko Anschütz 2026-03-26 23:30:37 +01:00
parent a3255a0ced
commit c91e49dc57
2 changed files with 36 additions and 1 deletions

View file

@ -13,6 +13,7 @@ import (
"time" "time"
"git.az-it.net/az/morz-infoboard/player/agent/internal/config" "git.az-it.net/az/morz-infoboard/player/agent/internal/config"
"git.az-it.net/az/morz-infoboard/player/agent/internal/displaycontroller"
"git.az-it.net/az/morz-infoboard/player/agent/internal/mqttheartbeat" "git.az-it.net/az/morz-infoboard/player/agent/internal/mqttheartbeat"
"git.az-it.net/az/morz-infoboard/player/agent/internal/mqttsubscriber" "git.az-it.net/az/morz-infoboard/player/agent/internal/mqttsubscriber"
"git.az-it.net/az/morz-infoboard/player/agent/internal/playerserver" "git.az-it.net/az/morz-infoboard/player/agent/internal/playerserver"
@ -78,6 +79,8 @@ type App struct {
// screenshotFn is kept so that applyMQTTConfig can pass it to the new subscriber. // screenshotFn is kept so that applyMQTTConfig can pass it to the new subscriber.
screenshotFn func() screenshotFn func()
displayCtrl *displaycontroller.Controller
} }
// mqttCloser is implemented by mqttsubscriber.Subscriber. // mqttCloser is implemented by mqttsubscriber.Subscriber.
@ -222,6 +225,21 @@ func (a *App) Run(ctx context.Context) error {
go ss.TakeAndSendOnce(ctx) go ss.TakeAndSendOnce(ctx)
} }
xDisplay := os.Getenv("DISPLAY")
if xDisplay == "" {
xDisplay = ":0"
}
a.displayCtrl = displaycontroller.New(xDisplay, a.Config.ScreenID, func(slug, state string) {
a.mqttMu.Lock()
pub := a.mqttPub
a.mqttMu.Unlock()
if pub != nil {
if err := pub.SendDisplayState(slug, state); err != nil {
a.logger.Printf("event=display_state_publish_error err=%v", err)
}
}
})
// Subscribe to playlist-changed and screenshot-request MQTT notifications (optional; fallback = polling). // Subscribe to playlist-changed and screenshot-request MQTT notifications (optional; fallback = polling).
sub := mqttsubscriber.New( sub := mqttsubscriber.New(
a.Config.MQTTBroker, a.Config.MQTTBroker,
@ -237,6 +255,10 @@ func (a *App) Run(ctx context.Context) error {
a.logger.Printf("event=mqtt_playlist_notification screen_id=%s", a.Config.ScreenID) a.logger.Printf("event=mqtt_playlist_notification screen_id=%s", a.Config.ScreenID)
}, },
a.screenshotFn, a.screenshotFn,
func(action string) {
a.logger.Printf("event=display_command_received action=%s screen_id=%s", action, a.Config.ScreenID)
a.displayCtrl.Execute(action)
},
) )
a.mqttMu.Lock() a.mqttMu.Lock()
a.mqttSub = sub a.mqttSub = sub
@ -435,6 +457,11 @@ func (a *App) reportStatus(ctx context.Context) {
payloadConnectivity = ConnectivityOnline payloadConnectivity = ConnectivityOnline
} }
var displayState string
if a.displayCtrl != nil {
displayState = a.displayCtrl.State()
}
mqttCfg, err := a.reporter.Send(ctx, statusreporter.Snapshot{ mqttCfg, err := a.reporter.Send(ctx, statusreporter.Snapshot{
Status: string(snapshot.Status), Status: string(snapshot.Status),
ServerConnectivity: string(payloadConnectivity), ServerConnectivity: string(payloadConnectivity),
@ -444,6 +471,7 @@ func (a *App) reportStatus(ctx context.Context) {
HeartbeatEverySeconds: snapshot.HeartbeatEvery, HeartbeatEverySeconds: snapshot.HeartbeatEvery,
StartedAt: snapshot.StartedAt, StartedAt: snapshot.StartedAt,
LastHeartbeatAt: snapshot.LastHeartbeatAt, LastHeartbeatAt: snapshot.LastHeartbeatAt,
DisplayState: displayState,
}) })
if err != nil { if err != nil {
a.mu.Lock() a.mu.Lock()
@ -511,7 +539,12 @@ func (a *App) applyMQTTConfig(broker, username, password string) {
} }
a.logger.Printf("event=mqtt_playlist_notification screen_id=%s", a.Config.ScreenID) a.logger.Printf("event=mqtt_playlist_notification screen_id=%s", a.Config.ScreenID)
} }
sub := mqttsubscriber.New(broker, a.Config.ScreenID, username, password, playlistChangedFn, a.screenshotFn) sub := mqttsubscriber.New(broker, a.Config.ScreenID, username, password, playlistChangedFn, a.screenshotFn,
func(action string) {
a.logger.Printf("event=display_command_received action=%s screen_id=%s", action, a.Config.ScreenID)
a.displayCtrl.Execute(action)
},
)
a.mqttSub = sub a.mqttSub = sub
if sub != nil { if sub != nil {
a.logger.Printf("event=mqtt_subscriber_restarted screen_id=%s broker=%s", a.Config.ScreenID, broker) a.logger.Printf("event=mqtt_subscriber_restarted screen_id=%s broker=%s", a.Config.ScreenID, broker)

View file

@ -320,6 +320,8 @@ func (r *recordingMQTTSender) SendHeartbeat(status, connectivity string, _ time.
return r.err return r.err
} }
func (r *recordingMQTTSender) SendDisplayState(_, _ string) error { return nil }
func (r *recordingMQTTSender) Close() {} func (r *recordingMQTTSender) Close() {}
func TestEmitHeartbeatCallsMQTTPublisher(t *testing.T) { func TestEmitHeartbeatCallsMQTTPublisher(t *testing.T) {