]> code.octet-stream.net Git - broadcaster/blob - radio/config.go
315265338859c2ef96b4727e1b0988f1efe58312
[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 + "/radiosync"
70 }