Skip to content

Commit

Permalink
Fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
ptabor committed Dec 8, 2024
1 parent 7abee37 commit 84fa5f0
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,25 @@ type Handler struct {
apps map[string]application.App
mux *http.ServeMux

verbose bool
autoconnect bool
verbose bool

autoconnectPeriod time.Duration
autoconnectTicker *time.Ticker

// autoupdatePeriodSec defines how frequently app.Update method is called in the background.
autoupdatePeriod time.Duration
autoupdateTicker *time.Ticker
}

func NewHandler(verbose bool) *Handler {
handler := &Handler{
verbose: verbose,
apps: map[string]application.App{},
mux: http.NewServeMux(),
mu: sync.Mutex{},
autoconnect: false,
verbose: verbose,
apps: map[string]application.App{},
mux: http.NewServeMux(),
mu: sync.Mutex{},

autoconnectPeriod: time.Duration(-1),
autoconnectTicker: nil,

autoupdatePeriod: time.Duration(-1),
autoupdateTicker: nil,
Expand All @@ -43,12 +48,26 @@ func NewHandler(verbose bool) *Handler {
return handler
}

// Autoconnect configures the handler to perform auto-discovery of all the cast devices & groups.
// AutoConnect configures the handler to perform auto-discovery of all the cast devices & groups.
// It's intended to be called just after `NewHandler()`, before the handler is registered in the server.
func (h *Handler) Autoconnect() error {
func (h *Handler) AutoConnect(period time.Duration) error {
// Setting the autoconnect property - to allow (in future) periodic refresh of the connections.
h.autoconnect = true
return h.connectAllInternal("", "3")
h.autoconnectPeriod = period
if err := h.connectAllInternal("", "3"); err != nil {
return err
}
if h.autoconnectPeriod > 0 {
h.autoconnectTicker = time.NewTicker(period)
go func() {
for {
<-h.autoconnectTicker.C
if err := h.connectAllInternal("", "3"); err != nil {
log.Printf("AutoConnect issued connectAllInternal failed: %v", err)
}
}
}()
}
return nil
}

// AutoUpdate configures the handler to perform auto-update of all the cast devices & groups.
Expand All @@ -57,15 +76,17 @@ func (h *Handler) Autoconnect() error {
func (h *Handler) AutoUpdate(period time.Duration) error {
// Setting the autoconnect property - to allow (in future) periodic refresh of the connections.
h.autoupdatePeriod = period
h.autoupdateTicker = time.NewTicker(period)
go func() {
for {
<-h.autoupdateTicker.C
if err := h.UpdateAll(); err != nil {
log.Printf("AutoUpdate issued UpdateAll failed: %v", err)
if h.autoupdatePeriod > 0 {
h.autoupdateTicker = time.NewTicker(period)
go func() {
for {
<-h.autoupdateTicker.C
if err := h.UpdateAll(); err != nil {
log.Printf("AutoUpdate issued UpdateAll failed: %v", err)
}
}
}
}()
}()
}
return nil
}

Expand Down Expand Up @@ -609,7 +630,7 @@ func (h *Handler) rewind(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
seconds := q.Get("seconds")
if seconds == "" {
httpValidationError(w, "missing 'seconds' in query paramater")
httpValidationError(w, "missing 'seconds' in query parameter")
return
}

Expand Down Expand Up @@ -638,7 +659,7 @@ func (h *Handler) seek(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
seconds := q.Get("seconds")
if seconds == "" {
httpValidationError(w, "missing 'seconds' in query paramater")
httpValidationError(w, "missing 'seconds' in query parameter")
return
}

Expand Down

0 comments on commit 84fa5f0

Please sign in to comment.