Skip to content

Commit

Permalink
Add initial concept of playlists (#119)
Browse files Browse the repository at this point in the history
* Add deprecated "random" command as a software change command.

* Add playlist management to service and launcher

* Refactor token imports to new service package.

* Playlist launching working.

* Add playlist navigation commands and token source checks

* Refactor TokenQueue to use channels and remove queue.go
  • Loading branch information
wizzomafizzo authored Nov 27, 2024
1 parent f36f9cd commit 9790f72
Show file tree
Hide file tree
Showing 31 changed files with 259 additions and 78 deletions.
8 changes: 4 additions & 4 deletions pkg/api/methods/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"github.com/wizzomafizzo/tapto/pkg/api/models"
"github.com/wizzomafizzo/tapto/pkg/api/models/requests"
"github.com/wizzomafizzo/tapto/pkg/service/tokens"
"golang.org/x/text/unicode/norm"
"net/http"
"net/url"
Expand All @@ -13,7 +14,6 @@ import (
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog/log"
"github.com/wizzomafizzo/tapto/pkg/service/state"
"github.com/wizzomafizzo/tapto/pkg/tokens"
)

var (
Expand Down Expand Up @@ -83,15 +83,15 @@ func HandleLaunch(env requests.RequestEnv) (any, error) {

// TODO: how do we report back errors? put channel in queue
env.State.SetActiveCard(t)
env.TokenQueue.Enqueue(t)
env.TokenQueue <- t

return nil, nil
}

// TODO: this is still insecure
func HandleLaunchBasic(
st *state.State,
tq *tokens.TokenQueue,
itq chan<- tokens.Token,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Info().Msg("received basic launch request")
Expand All @@ -113,7 +113,7 @@ func HandleLaunchBasic(
}

st.SetActiveCard(t)
tq.Enqueue(t)
itq <- t
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/api/models/requests/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"github.com/wizzomafizzo/tapto/pkg/database"
"github.com/wizzomafizzo/tapto/pkg/platforms"
"github.com/wizzomafizzo/tapto/pkg/service/state"
"github.com/wizzomafizzo/tapto/pkg/tokens"
"github.com/wizzomafizzo/tapto/pkg/service/tokens"
)

type RequestEnv struct {
Platform platforms.Platform
Config *config.UserConfig
State *state.State
Database *database.Database
TokenQueue *tokens.TokenQueue
TokenQueue chan<- tokens.Token
IsLocal bool
Id uuid.UUID
Params []byte
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/wizzomafizzo/tapto/pkg/api/methods"
"github.com/wizzomafizzo/tapto/pkg/api/models"
"github.com/wizzomafizzo/tapto/pkg/api/models/requests"
"github.com/wizzomafizzo/tapto/pkg/service/tokens"
"net"
"net/http"
"strings"
Expand All @@ -22,7 +23,6 @@ import (
"github.com/wizzomafizzo/tapto/pkg/database"
"github.com/wizzomafizzo/tapto/pkg/platforms"
"github.com/wizzomafizzo/tapto/pkg/service/state"
"github.com/wizzomafizzo/tapto/pkg/tokens"
)

// TODO: should there be a TTL for request timestamps? what about offline misters?
Expand Down Expand Up @@ -132,7 +132,7 @@ func Start(
pl platforms.Platform,
cfg *config.UserConfig,
st *state.State,
tq *tokens.TokenQueue,
itq chan<- tokens.Token,
db *database.Database,
ns <-chan models.Notification,
) {
Expand Down Expand Up @@ -229,7 +229,7 @@ func Start(
Config: cfg,
State: st,
Database: db,
TokenQueue: tq,
TokenQueue: itq,
IsLocal: clientIp.IsLoopback(),
}, req)
if err != nil {
Expand Down Expand Up @@ -262,7 +262,7 @@ func Start(
})

// TODO: use allow list
r.Get("/l/*", methods.HandleLaunchBasic(st, tq))
r.Get("/l/*", methods.HandleLaunchBasic(st, itq))

err := http.ListenAndServe(":"+cfg.Api.Port, r)
if err != nil {
Expand Down
30 changes: 29 additions & 1 deletion pkg/launcher/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package launcher

import (
"fmt"
"github.com/wizzomafizzo/tapto/pkg/service/playlists"
"github.com/wizzomafizzo/tapto/pkg/service/tokens"
"net/url"
"os"
"path/filepath"
Expand All @@ -44,6 +46,10 @@ var commandMappings = map[string]func(platforms.Platform, platforms.CmdEnv) erro
"launch.random": cmdRandom,
"launch.search": cmdSearch,

"playlist.play": cmdPlaylistPlay,
"playlist.next": cmdPlaylistNext,
"playlist.previous": cmdPlaylistPrevious,

"shell": cmdShell,
"delay": cmdDelay,

Expand Down Expand Up @@ -72,6 +78,7 @@ var commandMappings = map[string]func(platforms.Platform, platforms.CmdEnv) erro
}

var softwareChangeCommands = []string{
"random", // DEPRECATED
"launch",
"launch.system",
"launch.random",
Expand Down Expand Up @@ -125,6 +132,8 @@ func findFile(pl platforms.Platform, cfg *config.UserConfig, path string) (strin
func LaunchToken(
pl platforms.Platform,
cfg *config.UserConfig,
plsc playlists.PlaylistController,
t tokens.Token,
manual bool,
text string,
totalCommands int,
Expand Down Expand Up @@ -152,6 +161,11 @@ func LaunchToken(

// explicit commands must begin with **
if strings.HasPrefix(text, "**") {
if t.Source == tokens.SourcePlaylist {
log.Debug().Str("text", text).Msgf("playlists cannot run commands, skipping")
return nil, false
}

text = strings.TrimPrefix(text, "**")
ps := strings.SplitN(text, ":", 2)
if len(ps) < 2 {
Expand All @@ -165,19 +179,33 @@ func LaunchToken(
Args: args,
NamedArgs: namedArgs,
Cfg: cfg,
Playlist: plsc,
Manual: manual,
Text: text,
TotalCommands: totalCommands,
CurrentIndex: currentIndex,
}

if f, ok := commandMappings[cmd]; ok {
return f(pl, env), slices.Contains(softwareChangeCommands, cmd)
log.Info().Msgf("launching command: %s", cmd)
softwareChange := slices.Contains(softwareChangeCommands, cmd)
if softwareChange {
// a launch triggered outside a playlist itself
log.Debug().Msg("clearing current playlist")
plsc.Queue <- nil
}
return f(pl, env), softwareChange
} else {
return fmt.Errorf("unknown command: %s", cmd), false
}
}

if t.Source != tokens.SourcePlaylist {
// a launch triggered outside a playlist itself
log.Debug().Msg("clearing current playlist")
plsc.Queue <- nil
}

// if it's not a command, treat it as a generic launch command
return cmdLaunch(pl, platforms.CmdEnv{
Cmd: "launch",
Expand Down
55 changes: 55 additions & 0 deletions pkg/launcher/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package launcher

import (
"fmt"
"github.com/wizzomafizzo/tapto/pkg/service/playlists"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -292,3 +293,57 @@ func cmdSearch(pl platforms.Platform, env platforms.CmdEnv) error {

return launch(res[0].Path)
}

func cmdPlaylistPlay(_ platforms.Platform, env platforms.CmdEnv) error {
if env.Args == "" {
return fmt.Errorf("no playlist path specified")
}

if _, err := os.Stat(env.Args); err != nil {
return err
}

files, err := os.ReadDir(env.Args)
if err != nil {
return err
}

media := make([]string, 0)
for _, file := range files {
if file.IsDir() || filepath.Ext(file.Name()) == "" {
continue
}

media = append(media, filepath.Join(env.Args, file.Name()))
}

if len(media) == 0 {
return fmt.Errorf("no media found in: %s", env.Args)
}

log.Info().Any("media", media).Msgf("new playlist: %s", env.Args)
pls := playlists.NewPlaylist(media)
env.Playlist.Queue <- pls

return nil
}

func cmdPlaylistNext(_ platforms.Platform, env platforms.CmdEnv) error {
if env.Playlist.Active == nil {
return fmt.Errorf("no playlist active")
}

env.Playlist.Queue <- playlists.Next(*env.Playlist.Active)

return nil
}

func cmdPlaylistPrevious(_ platforms.Platform, env platforms.CmdEnv) error {
if env.Playlist.Active == nil {
return fmt.Errorf("no playlist active")
}

env.Playlist.Queue <- playlists.Previous(*env.Playlist.Active)

return nil
}
2 changes: 1 addition & 1 deletion pkg/platforms/batocera/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package batocera
import (
"errors"
"github.com/wizzomafizzo/tapto/pkg/api/models"
"github.com/wizzomafizzo/tapto/pkg/service/tokens"
"os"
"os/exec"
"path/filepath"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/wizzomafizzo/tapto/pkg/readers/file"
"github.com/wizzomafizzo/tapto/pkg/readers/libnfc"
"github.com/wizzomafizzo/tapto/pkg/readers/simple_serial"
"github.com/wizzomafizzo/tapto/pkg/tokens"
)

type Platform struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/platforms/mac/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mac

import (
"github.com/wizzomafizzo/tapto/pkg/api/models"
"github.com/wizzomafizzo/tapto/pkg/service/tokens"
"os"
"os/exec"
"path/filepath"
Expand All @@ -13,7 +14,6 @@ import (
"github.com/wizzomafizzo/tapto/pkg/readers/file"
"github.com/wizzomafizzo/tapto/pkg/readers/pn532_uart"
"github.com/wizzomafizzo/tapto/pkg/readers/simple_serial"
"github.com/wizzomafizzo/tapto/pkg/tokens"
)

type Platform struct {
Expand Down
6 changes: 4 additions & 2 deletions pkg/platforms/mister/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/wizzomafizzo/tapto/pkg/api/models"
"github.com/wizzomafizzo/tapto/pkg/database/gamesdb"
"github.com/wizzomafizzo/tapto/pkg/readers/optical_drive"
"github.com/wizzomafizzo/tapto/pkg/service/tokens"
"os"
"os/exec"
"path/filepath"
Expand All @@ -29,7 +30,6 @@ import (
"github.com/wizzomafizzo/tapto/pkg/readers/file"
"github.com/wizzomafizzo/tapto/pkg/readers/libnfc"
"github.com/wizzomafizzo/tapto/pkg/readers/simple_serial"
"github.com/wizzomafizzo/tapto/pkg/tokens"
)

type Platform struct {
Expand Down Expand Up @@ -178,7 +178,9 @@ func (p *Platform) Stop() error {
}
}

p.stopSocket()
if p.stopSocket != nil {
p.stopSocket()
}

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/platforms/mister/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ package mister

import (
"fmt"
"github.com/wizzomafizzo/tapto/pkg/service/tokens"
"net"
"strings"

"github.com/rs/zerolog/log"
"github.com/wizzomafizzo/tapto/pkg/readers"
"github.com/wizzomafizzo/tapto/pkg/tokens"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion pkg/platforms/mistex/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package mistex
import (
"fmt"
"github.com/wizzomafizzo/tapto/pkg/api/models"
"github.com/wizzomafizzo/tapto/pkg/service/tokens"
"os"
"os/exec"
"path/filepath"
Expand All @@ -23,7 +24,6 @@ import (
"github.com/wizzomafizzo/tapto/pkg/readers/file"
"github.com/wizzomafizzo/tapto/pkg/readers/libnfc"
"github.com/wizzomafizzo/tapto/pkg/readers/simple_serial"
"github.com/wizzomafizzo/tapto/pkg/tokens"
)

type Platform struct {
Expand Down
4 changes: 3 additions & 1 deletion pkg/platforms/platforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ package platforms

import (
"github.com/wizzomafizzo/tapto/pkg/api/models"
"github.com/wizzomafizzo/tapto/pkg/service/playlists"
"github.com/wizzomafizzo/tapto/pkg/service/tokens"
"path/filepath"
"strings"

"github.com/wizzomafizzo/tapto/pkg/config"
"github.com/wizzomafizzo/tapto/pkg/readers"
"github.com/wizzomafizzo/tapto/pkg/tokens"
)

type CmdEnv struct {
Cmd string
Args string
NamedArgs map[string]string
Cfg *config.UserConfig
Playlist playlists.PlaylistController
Manual bool
Text string
TotalCommands int
Expand Down
2 changes: 1 addition & 1 deletion pkg/platforms/windows/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/xml"
"errors"
"fmt"
"github.com/wizzomafizzo/tapto/pkg/service/tokens"
"io"
"os"
"os/exec"
Expand All @@ -22,7 +23,6 @@ import (
"github.com/wizzomafizzo/tapto/pkg/readers/file"
"github.com/wizzomafizzo/tapto/pkg/readers/pn532_uart"
"github.com/wizzomafizzo/tapto/pkg/readers/simple_serial"
"github.com/wizzomafizzo/tapto/pkg/tokens"
)

type Platform struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/readers/acr122_pcsc/acr122_pcsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"bytes"
"encoding/hex"
"errors"
"github.com/wizzomafizzo/tapto/pkg/service/tokens"
"strings"
"time"

"github.com/ebfe/scard"
"github.com/rs/zerolog/log"
"github.com/wizzomafizzo/tapto/pkg/config"
"github.com/wizzomafizzo/tapto/pkg/readers"
"github.com/wizzomafizzo/tapto/pkg/tokens"
"github.com/wizzomafizzo/tapto/pkg/utils"
)

Expand Down
Loading

0 comments on commit 9790f72

Please sign in to comment.