-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (114 loc) · 2.67 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"bufio"
"context"
"errors"
"fmt"
"go.uber.org/zap"
"golang.org/x/sys/unix"
"os"
"os/signal"
"strings"
"syscall"
"v400_monitor/controller"
"v400_monitor/moonraker"
"v400_monitor/web"
)
func getTerminalInput(input chan string) {
_, err := unix.IoctlGetWinsize(int(os.Stdin.Fd()), unix.TIOCGWINSZ)
if err != nil {
return
}
scanner := bufio.NewScanner(os.Stdin)
for {
scanner.Scan()
input <- scanner.Text()
}
}
func getLogger(isDevMode bool) *zap.Logger {
if isDevMode {
logger, err := zap.NewDevelopment()
if err != nil {
panic(err)
}
return logger
}
logger, err := zap.NewProduction()
if err != nil {
panic(err)
}
return logger
}
func main() {
isDevMode := len(os.Getenv("dev")) != 0
logger := getLogger(isDevMode)
defer func(logger *zap.Logger) {
err := logger.Sync()
if err != nil && !errors.Is(err, syscall.ENOTTY) {
panic(err)
}
}(logger)
sugar := logger.Sugar()
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
termInput := make(chan string)
go getTerminalInput(termInput)
config, err := LoadConfig("./config.yaml")
if err != nil {
panic(err)
}
monitors := make(map[string]*moonraker.Monitor)
ctx := context.Background()
for _, p := range config.Printers {
monConfig := moonraker.MonitorConfig{
NoPauseDuration: config.NoPauseDuration,
WillPauseMessage: config.DisplayMessages.WillPauseMessage,
PauseMessage: config.DisplayMessages.PauseMessage,
}
m, err := moonraker.NewMonitor(p.Name, p.Url, monConfig, sugar.With("PrinterName", p.Name))
if err != nil {
panic(err)
}
m.SetAllowNoRegPrint(p.ControllerFailMode != FailModeNoPrint)
m.Start(ctx)
monitors[p.Key] = m
}
var ctrlConnector *controller.Connector
if config.Controller.Url != nil {
ctrlConnector = controller.NewConnector(config.Controller.Url, config.Controller.HubId,
sugar.Named("controller"), monitors)
ctrlConnector.Connect(ctx)
}
server := web.NewServer(ctx, isDevMode, sugar.Named("web"), monitors)
go server.Run()
for {
select {
case inputStr := <-termInput:
input := strings.Split(inputStr, " ")
if len(input) < 1 {
fmt.Println("Usage: <printer_idx> [<job_id>]")
} else {
key := input[0]
if _, ok := monitors[key]; ok {
if len(input) == 1 {
monitors[key].SetRegisteredJobId("")
} else {
monitors[key].SetRegisteredJobId(input[1])
}
} else {
fmt.Println("Error: Printer not found!!")
}
}
case s := <-interrupt:
go server.Shutdown()
if ctrlConnector != nil {
go ctrlConnector.Close()
}
for _, m := range monitors {
m.Stop()
}
fmt.Println("Got signal:", s)
return
}
}
}