Skip to content

Commit

Permalink
play, plause, status, PlayerData
Browse files Browse the repository at this point in the history
  • Loading branch information
hilli committed May 20, 2023
1 parent d6849a7 commit 669b4c4
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 0 deletions.
44 changes: 44 additions & 0 deletions cmd/kefw2/cmd/pause.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"fmt"
"os"

"github.com/hilli/go-kef-w2/kefw2"
"github.com/spf13/cobra"
)

// muteCmd toggles the mute state of the speakers
var pauseCmd = &cobra.Command{
Use: "pause",
Short: "Pause playback when on WiFi source",
Long: `Pause playback when on WiFi source`,
Args: cobra.MaximumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
currentSource, err := currentSpeaker.Source()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if currentSource != kefw2.SourceWiFi {
fmt.Println("Not on WiFi source, not resuming playback")
os.Exit(0)
}
if isPlaying, err := currentSpeaker.IsPlaying(); err != nil {
fmt.Println(err)
os.Exit(1)
} else if !isPlaying {
fmt.Println("Not playing, not pausing")
os.Exit(0)
}
err = currentSpeaker.PlayPause()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

func init() {
rootCmd.AddCommand(pauseCmd)
}
37 changes: 37 additions & 0 deletions cmd/kefw2/cmd/play.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"fmt"
"os"

"github.com/hilli/go-kef-w2/kefw2"
"github.com/spf13/cobra"
)

// muteCmd toggles the mute state of the speakers
var playCmd = &cobra.Command{
Use: "play",
Short: "Resume playback when on WiFi source if paused",
Long: `Resume playback when on WiFi source if paused`,
Args: cobra.MaximumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
currentSource, err := currentSpeaker.Source()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if currentSource != kefw2.SourceWiFi {
fmt.Println("Not on WiFi source, not resuming playback")
os.Exit(0)
}
err = currentSpeaker.PlayPause()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

func init() {
rootCmd.AddCommand(playCmd)
}
50 changes: 50 additions & 0 deletions cmd/kefw2/cmd/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cmd

import (
"fmt"
"os"

"github.com/hilli/go-kef-w2/kefw2"
"github.com/spf13/cobra"
)

// volumeCmd represents the volume command
var statusCmd = &cobra.Command{
Use: "status",
Aliases: []string{"info", "state", "st"},
Short: "Status of the speakers",
Long: `Status of the speakers`,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
status, err := currentSpeaker.SpeakerState()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("State:", status)
source, err := currentSpeaker.Source()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Source:", source)
if source == kefw2.SourceWiFi {
pd, err := currentSpeaker.PlayerData()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Audio Transport:", pd.MediaRoles.Title)
fmt.Println("Artist:", pd.TrackRoles.MediaData.MetaData.Artist)
fmt.Println("Album:", pd.TrackRoles.MediaData.MetaData.Album)
fmt.Println("Track:", pd.TrackRoles.Title)
fmt.Println("Duration:", pd.Status.Duration)
fmt.Println("PlayID:", pd.PlayID.TimeStamp)
fmt.Println("Album Art:", pd.TrackRoles.Icon)
}
},
}

func init() {
rootCmd.AddCommand(statusCmd)
}
87 changes: 87 additions & 0 deletions kefw2/player_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package kefw2

import (
"encoding/json"
"fmt"
)

type PlayerData struct {
State string `json:"state"`
Status PlayerResource `json:"status"`
TrackRoles PlayerTrackRoles `json:"trackRoles"`
Controls PlayerControls `json:"controls"`
MediaRoles PlayerMediaRoles `json:"mediaRoles"`
PlayID PlayerPlayID `json:"playId"`
}

type PlayerResource struct {
Duration int `json:"duration"`
}

type PlayerTrackRoles struct {
Icon string `json:"icon"`
MediaData PlayerMediaData `json:"mediaData"`
Title string `json:"title"`
}

type PlayerMediaData struct {
ActiveResource PlayerResource `json:"activeResource"`
MetaData PlayerMetaData `json:"metaData"`
Resources []PlayerResource `json:"resources"`
}

type PlayerMetaData struct {
Artist string `json:"artist"`
Album string `json:"album"`
}

type PlayerControls struct {
Previous bool `json:"previous"`
Pause bool `json:"pause"`
Next bool `json:"next_"`
}

type PlayerMediaRoles struct {
AudioType string `json:"audioType"`
DoNotTrack bool `json:"doNotTrack"`
Type string `json:"type"`
MediaData PlayerMediaRolesMetaData `json:"mediaData"`
Title string `json:"title"`
}

type PlayerMediaRolesMetaData struct {
MetaData PlayerMediaRolesMedieDataMetaData `json:"metaData"`
Resources []PlayerMimeResource `json:"resources"`
}

type PlayerMediaRolesMedieDataMetaData struct {
ServiceID string `json:"serviceId"`
Live bool `json:"live"`
PlayLogicPath string `json:"playLogicPath"`
}

type PlayerMimeResource struct {
MimeType string `json:"mimeType"`
URI string `json:"uri"`
}

type PlayerPlayID struct {
TimeStamp int `json:"timestamp"`
SystemMemberId string `json:"systemMemberId"`
}

func (s *KEFSpeaker) PlayerData() (PlayerData, error) {
var playersData []PlayerData
var err error
playersJson, err := s.getData("player:player/data")
if err != nil {
return PlayerData{}, fmt.Errorf("error getting player data: %s", err)
}
err = json.Unmarshal(playersJson, &playersData)
if err != nil {
// fmt.Printf("jsonData: %+v\n", string(playersJson))
return PlayerData{}, fmt.Errorf("error unmarshaling player data: %s", err)
}
playerData := playersData[0]
return playerData, nil
}

0 comments on commit 669b4c4

Please sign in to comment.