fully-functioning music status!
- accessible via /music - only detects strawberry for now
This commit is contained in:
parent
f838edeadb
commit
8a1fabf91a
53 changed files with 923 additions and 51 deletions
|
|
@ -3,7 +3,6 @@ package learning
|
|||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
|
@ -12,7 +11,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"codeberg.org/arimelody/ari-is-learning/broadcast"
|
||||
"codeberg.org/arimelody/ari-stream-tools/broadcast"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
|
@ -29,7 +28,6 @@ type (
|
|||
Service struct {
|
||||
cfg ServiceConfig
|
||||
|
||||
ctx context.Context
|
||||
titleFileMutex sync.Mutex
|
||||
titleLastModTime time.Time
|
||||
titleText string
|
||||
|
|
@ -39,21 +37,25 @@ type (
|
|||
)
|
||||
|
||||
func New(ctx context.Context, cfg ServiceConfig) *Service {
|
||||
titleText := []byte{}
|
||||
titleUpdated := make(chan string)
|
||||
|
||||
if _, err := os.Stat(cfg.TitleFilePath); os.IsNotExist(err) {
|
||||
log.Printf("Learning title file [%s] does not exist, creating one...", cfg.TitleFilePath)
|
||||
os.WriteFile(cfg.TitleFilePath, []byte("Untitled"), 0644)
|
||||
titleText = []byte("Untitled")
|
||||
} else if err != nil {
|
||||
log.Fatalf("Failed to stat title file: %v", err)
|
||||
} else {
|
||||
titleText, err = os.ReadFile(cfg.TitleFilePath)
|
||||
if err != nil { log.Fatalf("Failed to read title file: %v", err) }
|
||||
}
|
||||
|
||||
srv := &Service{
|
||||
cfg: cfg,
|
||||
ctx: ctx,
|
||||
titleFileMutex: sync.Mutex{},
|
||||
titleLastModTime: time.Unix(0, 0),
|
||||
titleText: "",
|
||||
titleText: string(titleText),
|
||||
titleUpdated: titleUpdated,
|
||||
titleUpdatedBroadcast: broadcast.NewBroadcastChannel(ctx, titleUpdated),
|
||||
}
|
||||
|
|
@ -110,6 +112,8 @@ func (srv *Service) BindRoutes(group *gin.RouterGroup) {
|
|||
return
|
||||
}
|
||||
|
||||
srv.titleUpdated<-string(data)
|
||||
|
||||
ctx.String(http.StatusOK, http.StatusText(http.StatusOK))
|
||||
})
|
||||
|
||||
|
|
@ -123,45 +127,3 @@ func (srv *Service) BindRoutes(group *gin.RouterGroup) {
|
|||
http.ServeFileFS(ctx.Writer, ctx.Request, pagesFS, "pages/bye.html")
|
||||
})
|
||||
}
|
||||
|
||||
func (srv *Service) Run(ctx context.Context) {
|
||||
failed := make(chan error)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
srv.titleFileMutex.Lock()
|
||||
stat, err := os.Stat(srv.cfg.TitleFilePath)
|
||||
if err != nil {
|
||||
failed<-fmt.Errorf("Failed to stat title file: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if srv.titleLastModTime.Before(stat.ModTime()) {
|
||||
data, err := os.ReadFile(srv.cfg.TitleFilePath)
|
||||
if err != nil {
|
||||
failed<-fmt.Errorf("Failed to read title file: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf(
|
||||
"title updated: \"%s\" -> \"%s\"",
|
||||
srv.titleText,
|
||||
strings.TrimSpace(string(data)),
|
||||
)
|
||||
|
||||
srv.titleText = strings.TrimSpace(string(data))
|
||||
if !srv.titleLastModTime.IsZero() { srv.titleUpdated<-srv.titleText }
|
||||
srv.titleLastModTime = stat.ModTime()
|
||||
}
|
||||
srv.titleFileMutex.Unlock()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-failed:
|
||||
log.Fatalf("Learning service error: %v", err)
|
||||
case <-ctx.Done():
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue