-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFakeMediaPlayer.go
110 lines (87 loc) · 2.53 KB
/
FakeMediaPlayer.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
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"github.com/ninjasphere/go-ninja/api"
"github.com/ninjasphere/go-ninja/channels"
"github.com/ninjasphere/go-ninja/devices"
"github.com/ninjasphere/go-ninja/model"
)
type fakeMediaPlayer struct {
ninja *devices.MediaPlayerDevice
}
func NewFakeMediaPlayer(driver ninja.Driver, conn *ninja.Connection, id int) (*fakeMediaPlayer, error) {
name := fmt.Sprintf("Fancy Fake Media Player %d", id)
ninja, err := devices.CreateMediaPlayerDevice(driver, &model.Device{
NaturalID: fmt.Sprintf("player-%d", id),
NaturalIDType: "fake",
Name: &name,
Signatures: &map[string]string{
"ninja:manufacturer": "Fake Co.",
"ninja:productName": "fakeMediaPlayer",
"ninja:productType": "MediaPlayer",
"ninja:thingType": "mediaplayer",
},
}, conn)
var fake *fakeMediaPlayer
if err == nil {
fake = &fakeMediaPlayer{
ninja: ninja,
}
err = fake.bindMethods()
if err != nil {
fake = nil
}
} else {
fake = nil
}
return fake, err
}
func (fake *fakeMediaPlayer) bindMethods() error {
fake.ninja.ApplyPlayPause = fake.applyPlayPause
fake.ninja.ApplyStop = fake.applyStop
fake.ninja.ApplyPlaylistJump = fake.applyPlaylistJump
fake.ninja.ApplyVolume = fake.applyVolume
fake.ninja.ApplyPlayURL = fake.applyPlayURL
err := fake.ninja.EnableControlChannel([]string{
"playing",
"paused",
"stopped",
"idle",
})
if err != nil {
return err
}
err = fake.ninja.EnableVolumeChannel(true)
if err != nil {
return err
}
err = fake.ninja.EnableMediaChannel()
if err != nil {
return err
}
return nil
}
func (fake *fakeMediaPlayer) applyPlayPause(playing bool) error {
fake.ninja.Log().Infof("applyPlayPause called, playing: %t", playing)
if playing {
return fake.ninja.UpdateControlState(channels.MediaControlEventPlaying)
} else {
return fake.ninja.UpdateControlState(channels.MediaControlEventPaused)
}
}
func (fake *fakeMediaPlayer) applyStop() error {
fake.ninja.Log().Infof("applyStop called")
return fake.ninja.UpdateControlState(channels.MediaControlEventStopped)
}
func (fake *fakeMediaPlayer) applyPlaylistJump(delta int) error {
fake.ninja.Log().Infof("applyPlaylistJump called, delta : %d", delta)
return nil
}
func (fake *fakeMediaPlayer) applyVolume(state *channels.VolumeState) error {
fake.ninja.Log().Infof("applyVolume called, volume %v", state)
return fake.ninja.UpdateVolumeState(state)
}
func (fake *fakeMediaPlayer) applyPlayURL(url string, queue bool) error {
fake.ninja.Log().Infof("applyPlayURL called, volume %s, %t", url, queue)
return nil
}