Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support info (device name and other meta-information) in the status. #202

Merged
merged 7 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/buger/jsonparser"
"github.com/pkg/errors"

"github.com/vishen/go-chromecast/cast"
pb "github.com/vishen/go-chromecast/cast/proto"
"github.com/vishen/go-chromecast/playlists"
Expand Down Expand Up @@ -66,6 +65,7 @@ type App interface {
Close(stopMedia bool) error
LoadApp(appID, contentID string) error
Status() (*cast.Application, *cast.Media, *cast.Volume)
Info() (*cast.DeviceInfo, error)
Update() error
Pause() error
Unpause() error
Expand Down Expand Up @@ -93,6 +93,9 @@ type Application struct {
conn cast.Conn
debug bool

// Device name override (originating e.g. from mdns lookup).
deviceNameOverride string

// Internal mapping of request id to result channel
resultChanMap map[int]chan *pb.CastMessage

Expand All @@ -107,6 +110,7 @@ type Application struct {
// Current values from the chromecast.
application *cast.Application // It is possible that there is no current application, can happen for google home.
media *cast.Media
info *cast.DeviceInfo
// There seems to be two different volumes returned from the chromecast,
// one for the receiver and one for the playing media. It looks we update
// the receiver volume from go-chromecast, so we should use that one. But
Expand Down Expand Up @@ -187,6 +191,12 @@ func WithSkipadRetries(retries int) ApplicationOption {
}
}

func WithDeviceNameOverride(deviceName string) ApplicationOption {
return func(a *Application) {
a.SetDeviceNameOverride(deviceName)
}
}

func NewApplication(opts ...ApplicationOption) *Application {
a := &Application{
conn: cast.NewConnection(),
Expand Down Expand Up @@ -223,6 +233,10 @@ func (a *Application) SetIface(iface *net.Interface) { a.iface = iface }
func (a *Application) SetSkipadSleep(sleep time.Duration) { a.skipadSleep = sleep }
func (a *Application) SetSkipadRetries(retries int) { a.skipadRetries = retries }

func (a *Application) SetDeviceNameOverride(deviceName string) {
a.deviceNameOverride = deviceName
}

func (a *Application) App() *cast.Application { return a.application }
func (a *Application) Media() *cast.Media { return a.media }
func (a *Application) Volume() *cast.Volume { return a.volumeReceiver }
Expand Down Expand Up @@ -437,6 +451,25 @@ func (a *Application) Status() (*cast.Application, *cast.Media, *cast.Volume) {
return a.application, a.media, a.volumeReceiver
}

func (a *Application) Info() (*cast.DeviceInfo, error) {
if a.info != nil {
return a.info, nil
}
addr, err := a.conn.RemoteAddr()
if err != nil {
return nil, err
}
info, err := GetInfo(addr)
if err != nil {
return nil, err
}
if len(a.deviceNameOverride) > 0 {
info.Name = a.deviceNameOverride
}
a.info = info
return info, err
}

func (a *Application) Pause() error {
if a.media == nil {
return ErrNoMediaPause
Expand Down
35 changes: 35 additions & 0 deletions application/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package application

import (
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/vishen/go-chromecast/cast"
)

// getInfo uses the http://<ip>:8008/setup/eureka_endpoint to obtain more
// information about the cast-device.
// OBS: The 8008 seems to be pure http, whereas 8009 is typically the port
// to use for protobuf-communication,

func GetInfo(ip string) (info *cast.DeviceInfo, err error) {
// Note: Services exposed not on 8009 port are "Google Cast Group"s
// The only way to find the true device (group) name, is using mDNS outside of this function.
url := fmt.Sprintf("http://%v:8008/setup/eureka_info", ip)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
info = new(cast.DeviceInfo)
if err := json.Unmarshal(data, info); err != nil {
return nil, err
}
return info, nil
}
Loading
Loading