-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (67 loc) · 1.52 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
// Package main
package main
import (
"flag"
"log/slog"
"net"
"os"
"switcherctl/connections"
"switcherctl/consts"
)
func main() {
ip := flag.String("ip", consts.DefaultIP.String(), "The local Switcher device's IP address")
port := flag.Int("port", consts.UDPPortType1New, "The local Switcher device's port")
shouldGetSchedule := flag.Bool("schedule", false, "Get you Switcher device's work schedule")
flag.Parse()
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
slog.SetDefault(logger)
parsedIP := net.ParseIP(*ip)
if parsedIP == nil {
panic(consts.ErrInvalidIP)
}
if port == nil || *port < 100 || *port >= 65_000 {
panic(consts.ErrInvalidPort)
}
listener, err := connections.TryNewListener(parsedIP, *port)
if err != nil {
panic(err)
}
defer func() {
if closeErr := listener.Close(); closeErr != nil {
panic(err)
}
}()
data, err := listener.Read()
if err != nil {
panic(err)
}
baseDeviceData, err := data.ToJSON()
if err != nil {
panic(err)
}
slog.Debug(
"switcher device data",
"value", baseDeviceData,
)
if *shouldGetSchedule {
slog.Debug(
"connection data",
"ip", parsedIP,
"port", port,
"device ID", baseDeviceData.ID,
)
biConn, err := connections.TryNewBidirectionalConn(parsedIP, *port, baseDeviceData.ID)
if err != nil {
panic(err)
}
defer func() {
if err := biConn.Close(); err != nil {
panic(err)
}
}()
slog.Debug(
"switcher device schedule",
"value", biConn.GetSchedules(),
)
}
}