forked from barnybug/go-cast
-
Notifications
You must be signed in to change notification settings - Fork 1
/
status.go
98 lines (85 loc) · 2.17 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package chromecast
import (
"fmt"
"strings"
)
type Launcher interface {
Launch(appID string) (Status, error)
Stop() (Status, error)
}
type StatusResponse struct {
Status *Status `json:"status"`
}
type Status struct {
Applications []*ApplicationSession `json:"applications"`
Volume *Volume `json:"volume,omitempty"`
}
func (st Status) String() string {
var str strings.Builder
if st.Applications != nil {
if len(st.Applications) == 0 {
str.WriteString("No application running\n")
} else {
str.WriteString(fmt.Sprintf("Running applications: %d\n", len(st.Applications)))
for _, app := range st.Applications {
str.WriteString(fmt.Sprintf(" - [%s %s] %s\n", *app.DisplayName, *app.AppID, *app.StatusText))
}
}
}
if st.Volume != nil {
str.WriteString(fmt.Sprintf("Volume: %.2f", *st.Volume.Level))
if *st.Volume.Muted {
str.WriteString(" (muted)")
}
}
return str.String()
}
func (st Status) AppSupporting(namespace string) (apps []ApplicationSession) {
for _, app := range st.Applications {
if app == nil {
continue
}
for _, ns := range app.Namespaces {
if ns == nil || ns.Name != namespace {
continue
}
apps = append(apps, *app)
}
}
return apps
}
func (st Status) AppWithID(id string) *ApplicationSession {
for _, app := range st.Applications {
if app == nil {
continue
}
if app.AppID != nil && *app.AppID == id {
return app
}
}
return nil
}
func (st Status) FirstDestinationSupporting(namespace string) (string, error) {
apps := st.AppSupporting(namespace)
for _, app := range apps {
if app.TransportId != nil {
return *app.TransportId, nil
}
}
return "", ErrAppNotFound
}
type ApplicationSession struct {
AppID *string `json:"appId,omitempty"`
DisplayName *string `json:"displayName,omitempty"`
Namespaces []*Namespace `json:"namespaces"`
SessionID *string `json:"sessionId,omitempty"`
StatusText *string `json:"statusText,omitempty"`
TransportId *string `json:"transportId,omitempty"`
}
type Namespace struct {
Name string `json:"name"`
}
type Volume struct {
Level *float64 `json:"level,omitempty"`
Muted *bool `json:"muted,omitempty"`
}