-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
117 lines (102 loc) · 2.66 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
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
package main
import (
"flag"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/Hundemeier/go-sacn/sacn"
"github.com/gorilla/mux"
"gopkg.in/yaml.v2"
)
func main() {
// Flags for the Application
configpath := flag.String("config", "sample-config.yaml", "Path to config file")
flag.Parse()
log.Println("rest2sacn started")
// parsing config file
c := readConfigFile(*configpath)
err := c.Check()
if err != nil {
log.Fatal(err)
}
// set all required sACN Options/Parameters
setupSACN(c)
defer closeSACN()
// rest API setup
router := mux.NewRouter()
router.HandleFunc("/sacn/reset/{universe}", resetHandler)
router.HandleFunc("/sacn/send/{universe}/{channel}/{value}", sacnHandler)
srv := &http.Server{
Handler: router,
Addr: c.Ip,
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 3 * time.Second,
ReadTimeout: 3 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}
// UniverseOutput stores all configured universes and their current data
var UniverseOutput map[int]Universe
// Universe represents a DMX Universe sent out over sACN
type Universe struct {
number uint16
data [512]byte
output chan<- [512]byte
}
// Send outputs the currently stored data in the universe
func (u *Universe) Send() {
u.output <- u.data
}
// setupSACN activates all universes and makes them ready for transmitting
func setupSACN(c Config) {
UniverseOutput = make(map[int]Universe) // global storage to access from rest Handlers
// setup main transmitter.
trans, err := sacn.NewTransmitter("", [16]byte{1, 2, 3}, "rest2sacn")
if err != nil {
log.Fatal(err)
}
// Configuring all universes
for _, univ := range c.Universe {
// Activate and store channel in global var
ch, err := trans.Activate(univ)
if err != nil {
log.Fatal(err)
}
u := Universe{
number: univ,
output: ch,
}
UniverseOutput[int(univ)] = u
// Send universe to be sent out unicast to destination
trans.SetDestinations(u.number, []string{c.Destination})
// finished
log.Println("Setup completed for universe", u.number)
}
}
// closeSACN goes through all open sACN channels and closes them
func closeSACN() {
for _, univ := range UniverseOutput {
close(univ.output)
}
}
// Config represents the YAML Config file of the application
type Config struct {
Universe []uint16
Ip string
Destination string
}
// Check tests if the config is valid
func (c Config) Check() error {
// TODO: Error Check if every required option is set
return nil
}
func readConfigFile(path string) Config {
data, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal("Couldn't read config file. Exiting...")
}
var c Config
yaml.Unmarshal(data, &c)
return c
}