-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
235 lines (197 loc) · 5.2 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"fmt"
"log"
"net"
"reflect"
"time"
"github.com/golang/protobuf/proto"
fsm "github.com/Burmudar/go-fsm"
Device "github.com/Burmudar/soundbawr/server/device"
"github.com/godbus/dbus"
"github.com/sqp/pulseaudio"
)
const (
BarOff fsm.State = 0
BarOn fsm.State = 1
GracePeriod fsm.State = 2
GracePeriodTime time.Duration = 15 * time.Minute
)
type Soundbar struct {
fsm fsm.FSM
gracePeriodTimer *time.Timer
}
func NewSoundbar() *Soundbar {
return &Soundbar{fsm.New(
BarOff,
map[fsm.State][]fsm.State{
BarOff: []fsm.State{BarOn},
BarOn: []fsm.State{GracePeriod},
GracePeriod: []fsm.State{BarOff, BarOn},
},
[]fsm.Callback{fsm.DebugCallback, commandDevice},
), nil}
}
func (s *Soundbar) onAudioDeviceChange(state DeviceState) {
switch state {
case DeviceRunning:
{
s.AudioStarted()
break
}
case DeviceSuspended:
case DeviceIdle:
{
s.AudioStopped()
break
}
default:
{
fmt.Printf("Device State: %s\n", state)
}
}
}
func (s *Soundbar) AudioStarted() {
s.fsm.Transition(BarOn)
if s.gracePeriodTimer != nil {
s.gracePeriodTimer.Stop()
s.gracePeriodTimer = nil
}
}
func (s *Soundbar) AudioStopped() {
if (s.fsm.CurrentState() != GracePeriod) && s.gracePeriodTimer == nil {
s.fsm.Transition(GracePeriod)
s.gracePeriodTimer = time.AfterFunc(GracePeriodTime, func() {
s.fsm.Transition(BarOff)
})
}
}
type DeviceState uint32
const DeviceRunning DeviceState = 0
const DeviceIdle DeviceState = 1
const DeviceSuspended DeviceState = 2
func (s DeviceState) String() string {
switch s {
case DeviceRunning:
return "RUNNING"
case DeviceIdle:
return "IDLE"
case DeviceSuspended:
return "SUSPENDED"
}
return "UNKNOWN"
}
type Client struct {
*pulseaudio.Client
soundbar *Soundbar
}
type OnDeviceStateUpdated interface {
DeviceStateUpdated(dbus.ObjectPath, uint32)
}
func (cl *Client) NewPlaybackStream(path dbus.ObjectPath) {
log.Println("NewPlaybackStream", path)
}
func (cl *Client) PlaybackStreamRemoved(path dbus.ObjectPath) {
log.Println("PlaybackStreamRemoved", path)
}
func (cl *Client) DeviceVolumeUpdated(path dbus.ObjectPath, values []uint32) {
log.Println("device volume", path, values)
}
func (cl *Client) StreamVolumeUpdated(path dbus.ObjectPath, values []uint32) {
log.Println("stream volume", path, values)
}
func (cl *Client) NewSink(path dbus.ObjectPath, values []uint32) {
log.Println("New Sink", path, values)
}
func (cl *Client) SinkRemoved(path dbus.ObjectPath, values []uint32) {
log.Println("Sink Removed", path, values)
}
func (cl *Client) FallbackSinkUpdated(path dbus.ObjectPath, values []uint32) {
log.Println("Fallback Sink updated", path, values)
}
func (cl *Client) FallbackSinkUnset(path dbus.ObjectPath, values []uint32) {
log.Println("Fallback Sink unset", path, values)
}
func (cl *Client) DeviceActivePortUpdated(path dbus.ObjectPath, values []uint32) {
log.Println("Device Active Port", path, values)
}
func (cl *Client) DeviceStateUpdated(path dbus.ObjectPath, state uint32) {
var deviceState = DeviceState(state)
cl.soundbar.onAudioDeviceChange(deviceState)
}
func UnkownSignal(s *dbus.Signal) {
log.Println("Unkown Signal!")
log.Println("Name", s.Name)
log.Println("Path", s.Path)
log.Println("Body", s.Body)
log.Println("Sender", s.Sender)
}
func commandDevice(old, new fsm.State) {
if old != GracePeriod && new == BarOn {
log.Println("Sending command to turn sound bar ON")
err := sendCommand(&Device.Command{
Action: Device.Command_TURN_ON,
Device: Device.Command_SOUND_BAR,
})
if err != nil {
log.Fatalf("Failed to send command: %v\n", err)
} else {
log.Println("Command sent!")
}
}
}
func sendCommand(cmd *Device.Command) error {
conn, err := net.Dial("udp", "192.168.1.134:30000")
defer func() {
if conn != nil {
conn.Close()
}
}()
if err != nil {
return err
}
var command = Device.Command{Action: Device.Command_TURN_ON, Device: Device.Command_SOUND_BAR}
data, err := proto.Marshal(&command)
if err != nil {
return err
}
written, err := conn.Write(data)
if err != nil {
return err
}
log.Printf("Wrote %d bytes", written)
return nil
}
func main() {
fsm.StateToString[BarOff] = "BarOff"
fsm.StateToString[BarOn] = "BarOn"
fsm.StateToString[GracePeriod] = "GracePeriod"
pulseaudio.PulseCalls["Device.StateUpdated"] = func(m pulseaudio.Msg) {
m.O.(OnDeviceStateUpdated).DeviceStateUpdated(m.P, m.D[0].(uint32))
}
pulseaudio.PulseTypes["Device.StateUpdated"] = reflect.TypeOf((*OnDeviceStateUpdated)(nil)).Elem()
pulse, e := pulseaudio.New()
if e != nil {
log.Panicln("connect", e)
}
client := &Client{pulse, NewSoundbar()}
pulse.Register(client)
defer pulse.Unregister(client)
defer pulse.StopListening()
sinks, e := client.Core().ListPath("Sinks")
if len(sinks) == 0 {
fmt.Println("no sinks to test")
return
}
for _, sink := range sinks {
dev := client.Device(sink)
state, _ := dev.Uint32("State")
name, _ := dev.String("Name")
log.Printf("%s current state: %v", name, state)
client.soundbar.onAudioDeviceChange(DeviceState(state))
}
fmt.Printf("Path: %v\n", sinks[1])
client.ListenForSignal("Device.StateUpdated", sinks[1])
client.SetOnUnknownSignal(UnkownSignal)
client.Listen()
}