-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (73 loc) · 2.22 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
package main
import (
"context"
"flag"
"fmt"
"github.com/ChristianGaertner/dmx-controller/database"
"github.com/ChristianGaertner/dmx-controller/dmx"
"github.com/ChristianGaertner/dmx-controller/run"
"github.com/ChristianGaertner/dmx-controller/server"
"github.com/ChristianGaertner/dmx-controller/setup"
"log"
"time"
)
var noDmxOutput = flag.Bool("no-dmx-output", false, "If true, the OLA server will not be used to send dmx values")
var olaRpcEndpoint = flag.String("ola_rpc_endpoint", "localhost:9010", "RPC endpoint of the OLA service")
var addr = flag.String("address", ":8080", "Address of the server to listen on")
var setupFile = flag.String("setup", "", "path to setup json definition")
var databaseFile = flag.String("database", "", "path to the database file, will be created if needed")
var gracefulTimeout = flag.Duration("graceful-timeout", time.Second*15, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
func main() {
flag.Parse()
if *setupFile == "" {
fmt.Println("Please provide a setup file, e.g.: -setup file.json")
return
}
if *databaseFile == "" {
fmt.Println("Please provide a database file, e.g.: -database data.db")
return
}
db, err := database.Open(*databaseFile)
if err != nil {
panic(err)
}
defer func() {
err = db.Close()
if err != nil {
log.Fatal(err)
}
}()
buffer := dmx.NewBuffer()
var renderer dmx.BufferRenderer
if *noDmxOutput {
renderer = &dmx.NilRenderer{}
//renderer := &dmx.StdOutRenderer{NumChannels: 10}
} else {
renderer, err = dmx.NewOlaRPCRenderer(*olaRpcEndpoint)
if err != nil {
panic(err)
}
}
s, err := setup.Load(*setupFile)
if err != nil {
panic(err)
}
log.Printf("initializing %d dmx universes", len(s.GetUniverseIds()))
buffer.Init(s.GetUniverseIds())
deviceMap, err := s.PatchDeviceMap()
if err != nil {
panic(err)
}
engine := run.NewEngine(renderer, s, deviceMap, buffer, db)
onExit := make(chan bool)
ctx, cancel := context.WithCancel(context.Background())
go engine.Boot(ctx, onExit)
err = server.ListenAndServe(ctx, *addr, engine, *gracefulTimeout)
if err != nil {
log.Println(err)
}
log.Println("Shutting down...")
cancel()
<-onExit
log.Println("[SHUTDOWN]")
}