-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.go
124 lines (110 loc) · 2.85 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
package main
import (
"fmt"
"github.com/toxuin/alarmserver/buses/mqtt"
"github.com/toxuin/alarmserver/buses/webhooks"
conf "github.com/toxuin/alarmserver/config"
"github.com/toxuin/alarmserver/servers/dahua"
"github.com/toxuin/alarmserver/servers/ftp"
"github.com/toxuin/alarmserver/servers/hikvision"
"github.com/toxuin/alarmserver/servers/hisilicon"
"os"
"os/signal"
"sync"
"syscall"
)
var config *conf.Config
func init() {
config.SetDefaults()
}
func main() {
config = config.Load()
fmt.Println("STARTING...")
if config.Debug {
config.Printout()
}
processesWaitGroup := sync.WaitGroup{}
// INIT BUSES
mqttBus := mqtt.Bus{Debug: config.Debug}
if config.Mqtt.Enabled {
mqttBus.Initialize(config.Mqtt)
if config.Debug {
fmt.Println("MQTT BUS INITIALIZED")
}
}
webhookBus := webhooks.Bus{Debug: config.Debug}
if config.Webhooks.Enabled {
webhookBus.Initialize(config.Webhooks)
if config.Debug {
fmt.Println("WEBHOOK BUS INITIALIZED")
}
}
messageHandler := func(cameraName string, eventType string, extra string) {
if config.Mqtt.Enabled {
mqttBus.SendMessage(config.Mqtt.TopicRoot+"/"+cameraName+"/"+eventType, extra)
}
if config.Webhooks.Enabled {
webhookBus.SendMessage(cameraName, eventType, extra)
}
}
if config.Hisilicon.Enabled {
// START HISILICON ALARM SERVER
hisiliconServer := hisilicon.Server{
Debug: config.Debug,
WaitGroup: &processesWaitGroup,
Port: config.Hisilicon.Port,
MessageHandler: messageHandler,
}
hisiliconServer.Start()
if config.Debug {
fmt.Println("STARTED HISILICON SERVER")
}
}
if config.Hikvision.Enabled {
// START HIKVISION ALARM SERVER
hikvisionServer := hikvision.Server{
Debug: config.Debug,
WaitGroup: &processesWaitGroup,
Cameras: &config.Hikvision.Cams,
MessageHandler: messageHandler,
}
hikvisionServer.Start()
if config.Debug {
fmt.Println("STARTED HIKVISION SERVER")
}
}
if config.Dahua.Enabled {
// START DAHUA SERVER
dhServer := dahua.Server{
Debug: config.Debug,
WaitGroup: &processesWaitGroup,
Cameras: &config.Dahua.Cams,
MessageHandler: messageHandler,
}
dhServer.Start()
if config.Debug {
fmt.Println("STARTED DAHUA SERVER")
}
}
if config.Ftp.Enabled {
// START FTP SERVER
ftpServer := ftp.Server{
Debug: config.Debug,
WaitGroup: &processesWaitGroup,
Port: config.Ftp.Port,
AllowFiles: config.Ftp.AllowFiles,
RootPath: config.Ftp.RootPath,
Password: config.Ftp.Password,
MessageHandler: messageHandler,
}
ftpServer.Start()
if config.Debug {
fmt.Println("STARTED FTP SERVER")
}
}
processesWaitGroup.Wait()
// START INFINITE LOOP WAITING FOR SERVERS
exitSignal := make(chan os.Signal)
signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM)
<-exitSignal
}