-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
52 lines (45 loc) · 1.82 KB
/
main.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
package main
import (
"log"
"math"
"os"
"github.com/scosman/airplay-music-watcher/actions"
"github.com/scosman/airplay-music-watcher/mdns"
)
const DeviceSupportsRelayBitmask = 0x800
const ReceiverSessionIsActiveBitmask = 0x20000
const DeviceIsPlayingBitmask = DeviceSupportsRelayBitmask | ReceiverSessionIsActiveBitmask
func main() {
args := os.Args
if len(args) != 2 {
// first arg is program path, so 2 == 1...
log.Fatal("command requires exactly 1 arg -- the path to the json config file")
}
jsonFilePath := args[1]
actionRunner, err := actions.NewAirplayMusicActionRunner(jsonFilePath)
if err != nil {
log.Fatalf("Error parsing json config file: %v", err)
}
entriesCh := make(chan *mdns.AirplayFlagsEntry, 4)
defer close(entriesCh)
go func() {
for entry := range entriesCh {
// We're interested if the device is playing or not. From experimentation the 11th bit
// AKA DeviceSupportsRelay is the most relable way of checking this for HomePod and Belkin
// Soundform, while the 17th bit is more reliable for AppleTV.
// It stays on after you pause for about 8 mins, but does flip off eventually.
// If you manually disconnect airplay from a device, you get the off immediately
// https://github.com/openairplay/airplay-spec/blob/master/src/status_flags.md
isPlaying := (DeviceIsPlayingBitmask & entry.Flags) > 0
actionRunner.RunActionForDeviceState(entry.DeviceName, isPlaying)
}
}()
// Publishes a MDNS query for stereo.local.mdns. Not really needed, but not a problem either, and minimizing refactoring
// of the mdns client.go so will just leave this in.
params := mdns.DefaultParams("stereo")
params.Entries = entriesCh
// Timeout set to max time -- will never timeout
params.Timeout = math.MaxInt64
// Runs forever, does term on term signal so we'll call that a win
mdns.Query(params)
}