-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathops.go
227 lines (193 loc) · 4.86 KB
/
ops.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
package main
import (
"errors"
"fmt"
"math"
"strconv"
"strings"
"sync"
"time"
"github.com/rs/zerolog/log"
"github.com/vincent-petithory/mpdclient"
)
func listEvents(mpc *mpdclient.MPDClient, config *Config, events []*Event) ([]*Event, error) {
log.Info().Msgf("listing %d events", len(events))
channels, err := mpc.Channels()
if err != nil {
return nil, err
}
scheduledChannelFound := false
for _, channel := range channels {
if channel == "scheduled" {
scheduledChannelFound = true
}
}
if !scheduledChannelFound {
// Noone listens for the events list, so we don't have to say anything
return events, nil
}
err = mpc.SendMessage("scheduled", "Scheduler queue:")
if err != nil {
return nil, err
}
for index, event := range events {
msg := fmt.Sprintf("%d %v %s", index, event.Time, event.EventType)
err = mpc.SendMessage("scheduled", msg)
if err != nil {
return nil, err
}
}
return events, nil
}
func cancelEvent(mpc *mpdclient.MPDClient, config *Config, events []*Event, index int) ([]*Event, error) {
if index >= len(events) {
return nil, errors.New(fmt.Sprintf("invalid index: %d", index))
}
event := events[index]
log.Info().Msgf("canceling event #%d: %s at %v", index, event.EventType, event.Time)
event.Cancel()
return append(events[:index], events[index+1:]...), nil
}
func getVol(mpc *mpdclient.MPDClient) (uint, error) {
resp := mpc.Cmd("status")
if resp.Err != nil {
return 0, resp.Err
}
for _, data := range resp.Data {
split := strings.Split(data, ":")
if len(split) != 2 {
log.Warn().Msgf("could not parse mpd response: %s", data)
}
key := strings.Trim(split[0], " \t")
value := strings.Trim(split[1], " \t")
if key == "volume" {
vol, err := strconv.Atoi(value)
if err != nil {
return 0, err
}
return uint(vol), nil
}
}
return 0, errors.New("no volume given")
}
func setVol(mpc *mpdclient.MPDClient, vol uint) error {
if vol > 100 {
return errors.New(fmt.Sprintf("invalid volume: %d", vol))
}
resp := mpc.Cmd(fmt.Sprintf("setvol %d", vol))
return resp.Err
}
var (
fadeMutex sync.Mutex
)
func fade(mpc *mpdclient.MPDClient, duration time.Duration, volStart, volEnd uint) error {
fadeMutex.Lock()
defer fadeMutex.Unlock()
if volEnd > 100 {
return errors.New(fmt.Sprintf("invalid volume: %d", volEnd))
}
if volEnd > volStart {
log.Debug().Msgf("fading in from %d to %d, duration %.0fs", volStart, volEnd, math.Ceil(duration.Seconds()))
} else {
log.Debug().Msgf("fading out from %d to %d, duration %.0fs", volStart, volEnd, math.Ceil(duration.Seconds()))
}
err := setVol(mpc, volStart)
if err != nil {
return err
}
tickFn := func(v uint) uint {
return v + 1
}
if volStart > volEnd {
tickFn = func(v uint) uint {
return v - 1
}
}
steps := int(volEnd) - int(volStart)
if steps < 0 {
steps = -steps
}
if steps == 0 {
return nil
}
interval := duration.Milliseconds() / int64(steps)
if interval == 0 {
err = setVol(mpc, volEnd)
return err
}
ticker := time.NewTicker(time.Duration(interval) * time.Millisecond)
for vol := uint(volStart); vol != uint(volEnd); vol = tickFn(vol) {
<-ticker.C
err = setVol(mpc, vol)
}
ticker.Stop()
err = setVol(mpc, volEnd)
if err != nil {
return err
}
log.Debug().Msg("fade finished")
return nil
}
func scheduleSleep(mpc *mpdclient.MPDClient, config *Config, events []*Event, t *time.Time) ([]*Event, error) {
log.Info().Msgf("scheduling sleep for %v", t)
sleepEvent := Schedule(
func() error {
log.Info().Msg("going to sleep")
vol, err := getVol(mpc)
if err == nil {
err := fade(mpc, config.fadeTime, vol, 0)
if err != nil {
log.Error().Err(err).Msg("failed to fade")
return err
}
} else {
log.Warn().Err(err).Msg("failed to get volume, stopping playback")
}
log.Debug().Msg("pausing playback")
resp := mpc.Cmd("pause")
if resp.Err != nil {
log.Error().Err(resp.Err).Msg("failed to pause")
return resp.Err
}
if vol > 0 {
log.Debug().Msgf("restoring volume to %d", vol)
err = setVol(mpc, vol)
if err != nil {
log.Error().Err(err).Msg("failed to set volume")
return err
}
}
return nil
},
t,
"sleep",
)
return append(events, sleepEvent), nil
}
func scheduleAlarm(mpc *mpdclient.MPDClient, config *Config, events []*Event, t *time.Time) ([]*Event, error) {
log.Info().Msgf("scheduling alarm for %v", t)
sleepEvent := Schedule(
func() error {
log.Info().Msg("alarm")
err := setVol(mpc, 0)
if err != nil {
log.Error().Err(err).Msg("failed to set volume")
return err
}
resp := mpc.Cmd("play")
if resp.Err != nil {
log.Error().Err(resp.Err).Msg("failed to play")
return resp.Err
}
err = fade(mpc, config.fadeTime, 0, config.maxVol)
if err != nil {
log.Error().Err(err).Msg("failed to fade")
return err
}
return nil
},
t,
"alarm",
)
return append(events, sleepEvent), nil
}