|
15 | 15 | package cmd |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "encoding/json" |
18 | 19 | "fmt" |
| 20 | + "os" |
| 21 | + "strings" |
19 | 22 | "time" |
20 | 23 |
|
21 | 24 | "github.com/buger/jsonparser" |
22 | 25 | "github.com/spf13/cobra" |
23 | 26 |
|
| 27 | + "github.com/vishen/go-chromecast/application" |
24 | 28 | pb "github.com/vishen/go-chromecast/cast/proto" |
25 | 29 | ) |
26 | 30 |
|
27 | | -var interval float32 |
28 | | - |
29 | 31 | // watchCmd represents the watch command |
30 | 32 | var watchCmd = &cobra.Command{ |
31 | 33 | Use: "watch", |
32 | 34 | Short: "Watch all events sent from a chromecast device", |
33 | 35 | Run: func(cmd *cobra.Command, args []string) { |
34 | | - app, err := castApplication(cmd, args) |
35 | | - if err != nil { |
36 | | - fmt.Printf("unable to get cast application: %v\n", err) |
37 | | - return |
| 36 | + interval, _ := cmd.Flags().GetInt("interval") |
| 37 | + retries, _ := cmd.Flags().GetInt("retries") |
| 38 | + output, _ := cmd.Flags().GetString("output") |
| 39 | + |
| 40 | + o := outputNormal |
| 41 | + if strings.ToLower(output) == "json" { |
| 42 | + o = outputJSON |
38 | 43 | } |
39 | | - go func() { |
40 | | - for { |
41 | | - if err := app.Update(); err != nil { |
42 | | - fmt.Printf("unable to update cast application: %v\n", err) |
| 44 | + |
| 45 | + for i := 0; i < retries; i++ { |
| 46 | + retry := false |
| 47 | + app, err := castApplication(cmd, args) |
| 48 | + if err != nil { |
| 49 | + fmt.Printf("unable to get cast application: %v\n", err) |
| 50 | + time.Sleep(time.Second * 10) |
| 51 | + continue |
| 52 | + } |
| 53 | + done := make(chan struct{}, 1) |
| 54 | + go func() { |
| 55 | + for { |
| 56 | + if err := app.Update(); err != nil { |
| 57 | + fmt.Printf("unable to update cast application: %v\n", err) |
| 58 | + retry = true |
| 59 | + close(done) |
| 60 | + return |
| 61 | + } |
| 62 | + outputStatus(app, o) |
| 63 | + time.Sleep(time.Second * time.Duration(interval)) |
| 64 | + } |
| 65 | + }() |
| 66 | + |
| 67 | + app.AddMessageFunc(func(msg *pb.CastMessage) { |
| 68 | + protocolVersion := msg.GetProtocolVersion() |
| 69 | + sourceID := msg.GetSourceId() |
| 70 | + destID := msg.GetDestinationId() |
| 71 | + namespace := msg.GetNamespace() |
| 72 | + |
| 73 | + payload := msg.GetPayloadUtf8() |
| 74 | + payloadBytes := []byte(payload) |
| 75 | + requestID, _ := jsonparser.GetInt(payloadBytes, "requestId") |
| 76 | + messageType, _ := jsonparser.GetString(payloadBytes, "type") |
| 77 | + // Only log requests that are broadcasted from the chromecast. |
| 78 | + if requestID != 0 { |
43 | 79 | return |
44 | 80 | } |
45 | | - castApplication, castMedia, castVolume := app.Status() |
46 | | - if castApplication == nil { |
47 | | - fmt.Printf("Idle, volume=%0.2f muted=%t\n", castVolume.Level, castVolume.Muted) |
48 | | - } else if castApplication.IsIdleScreen { |
49 | | - fmt.Printf("Idle (%s), volume=%0.2f muted=%t\n", castApplication.DisplayName, castVolume.Level, castVolume.Muted) |
50 | | - } else if castMedia == nil { |
51 | | - fmt.Printf("Idle (%s), volume=%0.2f muted=%t\n", castApplication.DisplayName, castVolume.Level, castVolume.Muted) |
52 | | - } else { |
53 | | - metadata := "unknown" |
54 | | - if castMedia.Media.Metadata.Title != "" { |
55 | | - md := castMedia.Media.Metadata |
56 | | - metadata = fmt.Sprintf("title=%q, artist=%q", md.Title, md.Artist) |
57 | | - } |
58 | | - switch castMedia.Media.ContentType { |
59 | | - case "x-youtube/video": |
60 | | - metadata = fmt.Sprintf("id=\"%s\", %s", castMedia.Media.ContentId, metadata) |
61 | | - } |
62 | | - fmt.Printf(">> %s (%s), %s, time remaining=%.2fs/%.2fs, volume=%0.2f, muted=%t\n", castApplication.DisplayName, castMedia.PlayerState, metadata, castMedia.CurrentTime, castMedia.Media.Duration, castVolume.Level, castVolume.Muted) |
| 81 | + |
| 82 | + switch o { |
| 83 | + case outputJSON: |
| 84 | + json.NewEncoder(os.Stdout).Encode(map[string]interface{}{ |
| 85 | + "type": messageType, |
| 86 | + "proto_version": protocolVersion, |
| 87 | + "namespace": namespace, |
| 88 | + "source_id": sourceID, |
| 89 | + "destination_id": destID, |
| 90 | + "payload": payload, |
| 91 | + }) |
| 92 | + case outputNormal: |
| 93 | + fmt.Printf("CHROMECAST BROADCAST MESSAGE: type=%s proto=%s (namespace=%s) %s -> %s | %s\n", messageType, protocolVersion, namespace, sourceID, destID, payload) |
63 | 94 | } |
64 | | - time.Sleep(time.Millisecond * time.Duration(interval * 1000)) |
| 95 | + }) |
| 96 | + <-done |
| 97 | + if retry { |
| 98 | + // Sleep a little bit in-between retries |
| 99 | + fmt.Println("attempting a retry...") |
| 100 | + time.Sleep(time.Second * 10) |
65 | 101 | } |
66 | | - }() |
| 102 | + } |
| 103 | + return |
| 104 | + }, |
| 105 | +} |
67 | 106 |
|
68 | | - app.AddMessageFunc(func(msg *pb.CastMessage) { |
69 | | - protocolVersion := msg.GetProtocolVersion() |
70 | | - sourceID := msg.GetSourceId() |
71 | | - destID := msg.GetDestinationId() |
72 | | - namespace := msg.GetNamespace() |
| 107 | +type outputType int |
73 | 108 |
|
74 | | - payload := msg.GetPayloadUtf8() |
75 | | - payloadBytes := []byte(payload) |
76 | | - requestID, _ := jsonparser.GetInt(payloadBytes, "requestId") |
77 | | - messageType, _ := jsonparser.GetString(payloadBytes, "type") |
78 | | - // Only log requests that are broadcasted from the chromecast. |
79 | | - if requestID != 0 { |
80 | | - return |
81 | | - } |
| 109 | +const ( |
| 110 | + outputNormal outputType = iota |
| 111 | + outputJSON |
| 112 | +) |
| 113 | + |
| 114 | +func outputStatus(app *application.Application, outputType outputType) { |
| 115 | + castApplication, castMedia, castVolume := app.Status() |
82 | 116 |
|
83 | | - fmt.Printf("CHROMECAST BROADCAST MESSAGE: type=%s proto=%s (namespace=%s) %s -> %s | %s\n", messageType, protocolVersion, namespace, sourceID, destID, payload) |
| 117 | + switch outputType { |
| 118 | + case outputJSON: |
| 119 | + json.NewEncoder(os.Stdout).Encode(map[string]interface{}{ |
| 120 | + "application": castApplication, |
| 121 | + "media": castMedia, |
| 122 | + "volume": castVolume, |
84 | 123 | }) |
85 | | - // Wait forever |
86 | | - c := make(chan bool, 1) |
87 | | - <-c |
88 | | - return |
89 | | - }, |
| 124 | + case outputNormal: |
| 125 | + if castApplication == nil { |
| 126 | + fmt.Printf("Idle, volume=%0.2f muted=%t\n", castVolume.Level, castVolume.Muted) |
| 127 | + } else if castApplication.IsIdleScreen { |
| 128 | + fmt.Printf("Idle (%s), volume=%0.2f muted=%t\n", castApplication.DisplayName, castVolume.Level, castVolume.Muted) |
| 129 | + } else if castMedia == nil { |
| 130 | + fmt.Printf("Idle (%s), volume=%0.2f muted=%t\n", castApplication.DisplayName, castVolume.Level, castVolume.Muted) |
| 131 | + } else { |
| 132 | + metadata := "unknown" |
| 133 | + if castMedia.Media.Metadata.Title != "" { |
| 134 | + md := castMedia.Media.Metadata |
| 135 | + metadata = fmt.Sprintf("title=%q, artist=%q", md.Title, md.Artist) |
| 136 | + } |
| 137 | + switch castMedia.Media.ContentType { |
| 138 | + case "x-youtube/video": |
| 139 | + metadata = fmt.Sprintf("id=\"%s\", %s", castMedia.Media.ContentId, metadata) |
| 140 | + } |
| 141 | + fmt.Printf(">> %s (%s), %s, time remaining=%.2fs/%.2fs, volume=%0.2f, muted=%t\n", castApplication.DisplayName, castMedia.PlayerState, metadata, castMedia.CurrentTime, castMedia.Media.Duration, castVolume.Level, castVolume.Muted) |
| 142 | + } |
| 143 | + } |
90 | 144 | } |
91 | 145 |
|
92 | 146 | func init() { |
93 | | - watchCmd.Flags().Float32Var(&interval, "interval", 10, "interval between status poll in seconds") |
| 147 | + watchCmd.Flags().Int("interval", 10, "interval between status poll in seconds") |
| 148 | + watchCmd.Flags().Int("retries", 10, "times to retry when losing chromecast connection") |
| 149 | + watchCmd.Flags().String("output", "normal", "output format: normal or json") |
94 | 150 | rootCmd.AddCommand(watchCmd) |
95 | 151 | } |
0 commit comments