-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
85 lines (76 loc) · 1.58 KB
/
config.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
package main
import (
"encoding/json"
"io/ioutil"
"os"
)
const configFile = "./config.json"
type config struct {
KeyMap []mapType
Listening map[int]bool
Outputs []sACNconf
}
type sACNconf struct {
Universe uint16
Multicast bool
Destinations []string
}
func writeConfig() (err error) {
//function for reporting an error or success on terminating the function
defer func() {
if err != nil {
setEvent(CONFIG_WROTE, err.Error(), false)
} else {
setEvent(CONFIG_WROTE, "", true)
}
}()
conf := config{
KeyMap: getKeyMapAsMapType(),
Listening: listening,
Outputs: getSacnCurrentSetup(),
}
//Write data to file:
data, err := json.Marshal(conf)
if err != nil {
return
}
f, err := os.OpenFile(configFile, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
return
}
defer f.Close()
f.Truncate(0) //clear existing data in the file
_, err = f.Write(data)
return
}
func readConfig() (conf config) {
conf.KeyMap = make([]mapType, 0)
conf.Listening = make(map[int]bool)
file, err := ioutil.ReadFile(configFile)
if err != nil {
return
}
json.Unmarshal(file, &conf)
return
}
func deleteConfig() (err error) {
err = os.Remove(configFile)
if err != nil {
setEvent(CONFIG_DELETE, err.Error(), false)
return
}
setEvent(CONFIG_DELETE, "", true)
return
}
func getSacnCurrentSetup() []sACNconf {
sACNlist := make([]sACNconf, 0)
for _, univ := range trans.GetActivated() {
sACN := sACNconf{
Universe: univ,
Multicast: trans.IsMulticast(univ),
Destinations: getDestinations(univ),
}
sACNlist = append(sACNlist, sACN)
}
return sACNlist
}