]> code.octet-stream.net Git - broadcaster/blob - broadcaster-radio/config.go
6837b7dd10bff49735ddc9e789d5322038136fa4
[broadcaster] / broadcaster-radio / config.go
1 package main
2
3 import (
4 "errors"
5 "log"
6 "os"
7 "strings"
8
9 "github.com/BurntSushi/toml"
10 )
11
12 type RadioConfig struct {
13 GpioDevice string
14 PTTPin int
15 COSPin int
16 ServerURL string
17 Token string
18 CachePath string
19 TimeZone string
20 }
21
22 func NewRadioConfig() RadioConfig {
23 return RadioConfig{
24 GpioDevice: "gpiochip0",
25 PTTPin: -1,
26 COSPin: -1,
27 ServerURL: "",
28 Token: "",
29 CachePath: "",
30 TimeZone: "Australia/Hobart",
31 }
32 }
33
34 func (c *RadioConfig) LoadFromFile(path string) {
35 _, err := toml.DecodeFile(path, &c)
36 if err != nil {
37 log.Fatal("could not read config file for reading at path:", path, err)
38 }
39 err = c.Validate()
40 if err != nil {
41 log.Fatal(err)
42 }
43 c.ApplyDefaults()
44 }
45
46 func (c *RadioConfig) Validate() error {
47 if c.ServerURL == "" {
48 return errors.New("ServerURL must be provided in the configuration")
49 }
50 if c.Token == "" {
51 return errors.New("Token must be provided in the configuration")
52 }
53 return nil
54 }
55
56 func (c *RadioConfig) ApplyDefaults() {
57 if c.CachePath == "" {
58 dir, err := os.MkdirTemp("", "broadcast")
59 if err != nil {
60 log.Fatal(err)
61 }
62 c.CachePath = dir
63 }
64 }
65
66 func (c *RadioConfig) WebsocketURL() string {
67 addr := strings.Replace(c.ServerURL, "https://", "wss://", -1)
68 addr = strings.Replace(addr, "http://", "ws://", -1)
69 return addr + "/radio-ws"
70 }