-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpzbc.go
220 lines (193 loc) · 5.19 KB
/
mpzbc.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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"strconv"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/fhs/gompd/mpd"
)
const volumedelta int = 5
type mpzbc struct {
mqttClient mqtt.Client
mqttServer string
mqttTopic string
mpdClient *mpd.Client
mpdServer string
mpdStatus string
mpdVolume int
mpdVolumeDelta int
update bool
}
type control struct {
Action string
Battery int
Linkquality int
}
func (m *mpzbc) mqttMessage(client mqtt.Client, msg mqtt.Message) {
m.printStatus()
ctrl := control{}
if err := json.Unmarshal(msg.Payload(), &ctrl); err != nil {
log.Printf("Unmarshal error: %v", err)
}
switch ctrl.Action {
case "play_pause":
if m.mpdStatus == "play" {
if err := m.mpdClient.Pause(true); err != nil {
log.Printf("error during mpdClient.Pause(): %v", err)
}
} else {
if err := m.mpdClient.Play(-1); err != nil {
log.Printf("error during mpdclient.Play(): %v", err)
}
}
case "rotate_left":
m.updateMPD()
if m.mpdVolume != -1 {
if err := m.mpdClient.SetVolume(m.mpdVolume - m.mpdVolumeDelta); err != nil {
log.Printf("error during mpdClient.SetVolume(%d): %v", m.mpdVolume-m.mpdVolumeDelta, err)
}
}
case "rotate_right":
m.updateMPD()
if m.mpdVolume != -1 {
if err := m.mpdClient.SetVolume(m.mpdVolume + m.mpdVolumeDelta); err != nil {
log.Printf("error during mpdClient.SetVolume(%d): %v", m.mpdVolume+m.mpdVolumeDelta, err)
}
}
case "skip_backward":
if err := m.mpdClient.Previous(); err != nil {
log.Printf("error during mpdClient.Previous(): %v", err)
}
case "skip_forward":
if err := m.mpdClient.Next(); err != nil {
log.Printf("error during mpdClient.Next(): %v", err)
}
}
m.printStatus()
}
func (m *mpzbc) connectMQTT() error {
log.Println("Build MQTT Client")
co := mqtt.NewClientOptions()
co.AddBroker("tcp://" + m.mqttServer)
co.SetClientID(fmt.Sprintf("mpzbc_%x", os.Getpid()))
co.SetAutoReconnect(true)
co.SetCleanSession(true)
co.OnConnect = func(c mqtt.Client) {
if token := c.Subscribe(m.mqttTopic, 0, m.mqttMessage); token.Wait() && token.Error() != nil {
log.Fatalf("error during mqtt subscribe: %v\n", token.Error())
}
}
client := mqtt.NewClient(co)
log.Println("Connect to MQTT")
if token := client.Connect(); token.Wait() && token.Error() != nil {
return fmt.Errorf("error during mqtt connect: %v", token.Error())
}
return nil
}
func (m *mpzbc) updateMPD() (bool, error) {
update := false
status, err := m.mpdClient.Status()
if err != nil {
return false, fmt.Errorf("couldn't get MPD status: %v", err)
}
state, ok := status["state"]
if !ok {
return false, fmt.Errorf("no state in MPD status")
}
if m.mpdStatus != state {
update = true
}
m.mpdStatus = state
volume, ok := status["volume"]
if !ok {
m.mpdVolume = -1
} else {
newVolume, err := strconv.Atoi(volume)
if err != nil {
return false, fmt.Errorf("couldn't convert %s to integer: %v", volume, err)
}
if m.mpdVolume != newVolume {
update = true
}
m.mpdVolume = newVolume
}
return update, nil
}
func (m *mpzbc) connectMPD() error {
mpdClient, err := mpd.Dial("tcp", m.mpdServer)
if err != nil {
return fmt.Errorf("error while connecting to %s: %v", m.mpdServer, err)
}
m.mpdClient = mpdClient
return nil
}
func (m *mpzbc) getEnv() error {
m.mqttServer = os.Getenv("MQTTSERVER")
if m.mqttServer == "" {
return fmt.Errorf("MQTTSERVER is empty")
}
log.Printf("MQTT Server\t%s\n", m.mqttServer)
m.mqttTopic = os.Getenv("MQTTTOPIC")
if m.mqttTopic == "" {
return fmt.Errorf("MQTTTOPIC is empty")
}
log.Printf("MQTT Topic\t%s\n", m.mqttTopic)
m.mpdServer = os.Getenv("MPDSERVER")
if m.mpdServer == "" {
return fmt.Errorf("MPDSERVER is empty")
}
log.Printf("MPD Server\t%s\n", m.mpdServer)
volumestepenv := os.Getenv("VOLUMESTEP")
if volumestepenv == "" {
log.Printf("Volume step\t%d%% (default, as VOLUMESTEP was empty)\n", volumedelta)
m.mpdVolumeDelta = volumedelta
} else {
volumestep, err := strconv.Atoi(volumestepenv)
if err != nil {
log.Printf("Volume step\t%d%% (default, as \"%s\" was not parseable as int: %v)", volumedelta, volumestepenv, err)
m.mpdVolumeDelta = volumedelta
} else {
log.Printf("Volume step\t%d%%\n", volumestep)
m.mpdVolumeDelta = volumestep
}
}
return nil
}
func (m *mpzbc) printStatus() error {
update, err := m.updateMPD()
if err != nil {
return fmt.Errorf("couldn't update MPD state: %v", err)
}
if update {
log.Printf("state: %s\tvolume: %d\n", m.mpdStatus, m.mpdVolume)
}
return nil
}
func (m *mpzbc) run() error {
if err := m.getEnv(); err != nil {
return fmt.Errorf("couldn't get settings: %v", err)
}
if err := m.connectMPD(); err != nil {
return fmt.Errorf("couldn't connect to MPD: %v", err)
}
if err := m.connectMQTT(); err != nil {
return fmt.Errorf("couldn't connect to MQTT: %v", err)
}
for {
if err := m.printStatus(); err != nil {
return err
}
time.Sleep(1 * time.Second)
}
}
func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile | log.LUTC | log.Lmsgprefix)
log.Println("mpzbc startup")
client := mpzbc{}
if err := client.run(); err != nil {
log.Fatalf("error during client.run(): %v\n", err)
}
}