/
endpoints.go
149 lines (138 loc) · 3.61 KB
/
endpoints.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
package dashboard
import (
"github.com/MrBoombastic/FmRadioStreamer/pkg/condlers"
"github.com/MrBoombastic/FmRadioStreamer/pkg/config"
"github.com/MrBoombastic/FmRadioStreamer/pkg/core"
"github.com/MrBoombastic/FmRadioStreamer/pkg/leds"
"github.com/MrBoombastic/FmRadioStreamer/pkg/logs"
"github.com/MrBoombastic/FmRadioStreamer/pkg/ssd1306"
"github.com/MrBoombastic/FmRadioStreamer/pkg/tools"
"github.com/pbar1/pkill-go"
"os"
)
// dir endpoint returns contents of `dir` directory.
func dir(ctx *RadioContext) {
filesSlice, err := condlers.MusicDir()
if err != nil {
_ = ctx.Fiber.SendStatus(500)
}
err = ctx.Fiber.JSON(filesSlice)
if err != nil {
logs.FmRadStrError(err)
}
}
// stop endpoint plays silence.
func stop(ctx *RadioContext) {
err := core.Play(tools.Params{Type: tools.SilenceType, Cfg: ctx.Cfg})
if err != nil {
logs.PiFmAdvError(err)
_ = ctx.Fiber.SendStatus(500)
}
_ = ctx.Fiber.SendStatus(200)
}
// offair kills PiFmAdv entirely.
func offair(ctx *RadioContext) {
_, err := pkill.Pkill("pi_fm_adv", os.Interrupt)
if err != nil {
logs.FmRadStrError(err)
_ = ctx.Fiber.SendStatus(500)
}
_ = ctx.Fiber.SendStatus(200)
ctx.Cfg.Lock()
if ctx.Cfg.SSD1306 {
ssd1306.MiniMessage("OFF-AIR", ctx.Cfg)
}
ctx.Cfg.Unlock()
}
// yt endpoint performs searching or downloading audio from YouTube.
func yt(ctx *RadioContext) {
search := ctx.Fiber.Query("search")
query := ctx.Fiber.Query("q")
ctx.Cfg.Lock()
key := ctx.Cfg.YouTubeAPIKey
ctx.Cfg.Unlock()
result, err := tools.SearchYouTube(query, key)
if err != nil {
logs.FmRadStrError(err)
_ = ctx.Fiber.SendStatus(500)
return
}
if search == "true" {
err := ctx.Fiber.JSON(result.Items[0])
if err != nil {
logs.FmRadStrError(err)
}
} else {
_ = ctx.Fiber.SendStatus(200)
leds.BlueLedEnabled = true
ctx.Cfg.Lock()
format := ctx.Cfg.Format
ctx.Cfg.Unlock()
err = condlers.Download("https://youtu.be/"+result.Items[0].ID.VideoID, format)
leds.BlueLedEnabled = false
if err != nil {
logs.FmRadStrError(err)
}
}
}
// youtubeDl endpoint performs downloading audio from other sites.
func youtubeDl(ctx *RadioContext) {
query := ctx.Fiber.Query("q")
_ = ctx.Fiber.SendStatus(202)
leds.BlueLedEnabled = true
ctx.Cfg.Lock()
format := ctx.Cfg.Format
ctx.Cfg.Unlock()
err := condlers.Download(query, format)
leds.BlueLedEnabled = false
if err != nil {
logs.FmRadStrError(err)
}
}
// playFile endpoint plays selected file.
func playFile(ctx *RadioContext) {
query := ctx.Fiber.Query("q")
err := core.Play(tools.Params{Type: tools.FileType, Audio: query, Cfg: ctx.Cfg})
if err != nil {
logs.PiFmAdvError(err)
_ = ctx.Fiber.SendStatus(500)
}
_ = ctx.Fiber.SendStatus(200)
}
// playStream endpoint plays remote file via SoX.
func playStream(ctx *RadioContext) {
query := ctx.Fiber.Query("q")
err := core.Play(tools.Params{Type: tools.StreamType, Audio: query, Cfg: ctx.Cfg})
if err != nil {
logs.PiFmAdvError(err)
_ = ctx.Fiber.SendStatus(500)
}
_ = ctx.Fiber.SendStatus(200)
}
// save endpoint updates current config.
func save(ctx *RadioContext) {
newCfg := new(config.Config)
if err := ctx.Fiber.BodyParser(newCfg); err != nil {
logs.PiFmAdvError(err)
_ = ctx.Fiber.SendStatus(500)
}
ctx.Cfg.Lock()
ctx.Cfg.Config = *newCfg
config.Save(newCfg)
if newCfg.SSD1306 {
ssd1306.Refresh(ctx.Cfg)
}
ctx.Cfg.Unlock()
_ = ctx.Fiber.SendStatus(200)
}
// configuration endpoint returns current config.
func configuration(ctx *RadioContext) {
ctx.Cfg.Lock()
configMap := tools.ConfigToMap(ctx.Cfg)
ctx.Cfg.Unlock()
err := ctx.Fiber.JSON(configMap)
if err != nil {
logs.FmRadStrError(err)
_ = ctx.Fiber.SendStatus(500)
}
}