-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (57 loc) · 1.76 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
package main
import (
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
mqtt_client "go2rtc-mqtt-bridge/mqtt-client"
"go2rtc-mqtt-bridge/utils"
"io"
"net/http"
"path"
"regexp"
"time"
)
func main() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
config := utils.GetConfig()
hostRegex := regexp.MustCompile(fmt.Sprintf(`^%s/(.+?)/`, config.MQTT.BaseTopic))
cli := mqtt_client.NewMQTT(config)
go func() {
for {
cli.PublishAvailability()
time.Sleep(time.Second * 30)
}
}()
cli.WatchTopicPlayOnSpeaker(func(client mqtt.Client, message mqtt.Message) {
matches := hostRegex.FindStringSubmatch(message.Topic())
if matches == nil && len(matches) < 2 {
log.Info().Msgf("failed to extract camera host from topic: %s", message.Topic())
return
}
cameraName := matches[1]
audioFilePath := path.Join(config.AudioFilesPath, string(message.Payload()))
srcArgs := fmt.Sprintf("ffmpeg:%s#audio=pcma#input=file", audioFilePath)
go2rtcUrl := config.Go2rtcURL.JoinPath("api/streams")
queryValues := go2rtcUrl.Query()
queryValues.Add("dst", cameraName)
queryValues.Add("src", srcArgs)
httpCli := &http.Client{
Timeout: time.Second * 30,
}
req, err := http.NewRequest(http.MethodPost, go2rtcUrl.String(), nil)
req.URL.RawQuery = queryValues.Encode()
response, err := httpCli.Do(req)
if err != nil {
log.Error().Msgf("failed to send %s to %s with error %s", message.Payload(), cameraName, err)
return
}
if response.StatusCode != http.StatusOK {
body, _ := io.ReadAll(response.Body)
log.Error().Msgf("failed to send %s to %s with status %d: %s",
message.Payload(), cameraName, response.StatusCode, body)
return
}
log.Info().Msgf("play %s on %s", message.Payload(), cameraName)
})
}