-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
178 lines (158 loc) · 4.8 KB
/
server.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"context"
"fmt"
"net"
"os"
"regexp"
"strings"
"time"
"github.com/byuoitav/common/db"
"github.com/byuoitav/common/structs"
"github.com/byuoitav/kramer-driver"
"github.com/byuoitav/via-control/monitor"
"github.com/byuoitav/via-control/viacontrol"
"github.com/spf13/pflag"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
/* global variable declaration */
// Changed: lowercase vars
var name string
var deviceList []structs.Device
func init() {
if len(os.Getenv("ROOM_SYSTEM")) == 0 {
fmt.Printf("System is not tied to a specific room. Will not start via monitoring\n")
return
}
name = os.Getenv("SYSTEM_ID")
var err error
fmt.Printf("Gathering information for %s from database\n", name)
s := strings.Split(name, "-")
sa := s[0:2]
room := strings.Join(sa, "-")
fmt.Printf("Waiting for database . . . .\n")
for {
// Pull room information from db
state, err := db.GetDB().GetStatus()
fmt.Printf("%v\n", state)
//+deploy not_requried
if (err != nil || state != "completed") && !(len(os.Getenv("DEV_ROUTER")) > 0 || len(os.Getenv("STOP_REPLICATION")) > 0) {
fmt.Printf("Database replication in state %v. Retrying in 5 seconds.\n", state)
time.Sleep(5 * time.Second)
continue
}
fmt.Printf("Database replication state: %v\n", state)
devices, err := db.GetDB().GetDevicesByRoomAndRole(room, "EventRouter")
if err != nil {
fmt.Printf("Connecting to the Configuration DB failed, retrying in 5 seconds.\n")
time.Sleep(5 * time.Second)
continue
}
if len(devices) == 0 {
//there's a chance that there ARE routers in the room, but the initial database replication is occuring.
//we're good, keep going
state, err := db.GetDB().GetStatus()
if (err != nil || state != "completed") && !(len(os.Getenv("STOP_REPLICATION")) > 0) {
fmt.Printf("Database replication in state %v. Retrying in 5 seconds.\n", state)
time.Sleep(5 * time.Second)
continue
}
}
fmt.Printf("Connection to the Configuration DB established.\n")
break
}
deviceList, err = db.GetDB().GetDevicesByRoomAndType(room, "via-connect-pro")
if err != nil {
fmt.Printf("Error: %v\n", err)
}
}
func main() {
var (
port int
username string
password string
logLevel int8
)
// flags for cmd input
pflag.IntVarP(&port, "port", "P", 8014, "port to run the microservice on")
pflag.StringVarP(&username, "username", "u", "", "username for device")
pflag.StringVarP(&password, "password", "p", "", "password for device")
pflag.Int8VarP(&logLevel, "log-level", "L", 0, "Level to log at. Provided by zap logger: https://godoc.org/go.uber.org/zap/zapcore")
pflag.Parse()
// Build out the Logger
config := zap.Config{
Level: zap.NewAtomicLevelAt(zapcore.Level(logLevel)),
Development: false,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Encoding: "json",
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "@",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}
plain, err := config.Build()
if err != nil {
fmt.Printf("unable to build logger you foolish mortal: %s", err)
os.Exit(1)
}
sugared := plain.Sugar()
addr := fmt.Sprintf(":%d", port)
lis, err := net.Listen("tcp", addr)
if err != nil {
fmt.Printf("failed to start server: %s\n", err)
os.Exit(1)
}
sugared.Infof("This is the addr value: ", addr)
// import driver library for ViaControl -
createVia := func(ctx context.Context, addr string) (viacontrol.ViaDevice, error) {
return &kramer.Via{
Address: addr,
Username: username,
Password: password,
Logger: sugared,
}, nil
}
// Building ViaStruct for use with the ViaMonitor -
sugared.Debugf("Building Via struct for controller.....")
v := &kramer.Via{
Address: addr,
Username: username,
Password: password,
Logger: sugared,
}
// Execute only against the first CP in a room
var re = regexp.MustCompile(`-CP1$`)
test := re.MatchString(name)
var ctx context.Context
//start the persistent VIA monitoring connection if the Controller is CP1
if test == true && len(os.Getenv("ROOM_SYSTEM")) > 0 {
for _, device := range deviceList {
go monitor.StartMonitoring(ctx, device, v)
}
}
// create server for ViaControl -
server, err := viacontrol.CreateVIAServer(createVia)
if err != nil {
fmt.Printf("Error while trying to create DSP Server: %s\n", err)
os.Exit(1)
}
if err = server.Serve(lis); err != nil {
fmt.Printf("failed to listen: %s\n", err)
os.Exit(1)
}
}