Skip to content

Commit

Permalink
Fix not starting if hisilicon server enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
toxuin committed Mar 4, 2021
1 parent bf78d97 commit 255cd22
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.idea/
config.yaml
/config.yaml
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Supported Delivery 📬:

## Configuration

Create file `config.yml` in the folder where the application binary lives.
Create file `config.yaml` in the folder where the application binary lives.

Also see [sample config](docs/config.yml).
Also see [sample config](docs/config.yaml).

When alarm server is coming online, it will also send a status message to `/camera-alerts` topic with its status.

Expand Down
2 changes: 1 addition & 1 deletion docs/config.yml → docs/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
debug: "false"
debug: false

hikvision:
enabled: true
Expand Down
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ func main() {
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(topic string, data string) {
Expand All @@ -51,6 +57,9 @@ func main() {
MessageHandler: messageHandler,
}
hisiliconServer.Start()
if config.Debug {
fmt.Println("STARTED HISILICON SERVER")
}
}

if config.Hikvision.Enabled {
Expand All @@ -61,6 +70,9 @@ func main() {
MessageHandler: messageHandler,
}
hikvisionServer.Start()
if config.Debug {
fmt.Println("STARTED HIKVISION SERVER")
}
}

if config.Ftp.Enabled {
Expand All @@ -73,5 +85,8 @@ func main() {
MessageHandler: messageHandler,
}
ftpServer.Start()
if config.Debug {
fmt.Println("STARTED FTP SERVER")
}
}
}
2 changes: 1 addition & 1 deletion servers/ftp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Event struct {
Message string `json:"message"`
}

func (serv Server) Start() {
func (serv *Server) Start() {
if serv.MessageHandler == nil {
fmt.Println("FTP: Message handler is not set for FTP server - that's probably not what you want")
serv.MessageHandler = func(topic string, data string) {
Expand Down
4 changes: 2 additions & 2 deletions servers/hikvision/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type HikEventReader interface {
ReadEvents(camera *HikCamera, channel chan<- HikEvent, callback func())
}

func (server Server) addCamera(waitGroup *sync.WaitGroup, camera *HikCamera, eventChannel chan<- HikEvent) {
func (server *Server) addCamera(waitGroup *sync.WaitGroup, camera *HikCamera, eventChannel chan<- HikEvent) {
waitGroup.Add(1)
if !camera.BrokenHttp {
camera.EventReader = &HttpEventReader{Debug: server.Debug}
Expand Down Expand Up @@ -74,7 +74,7 @@ func (server Server) addCamera(waitGroup *sync.WaitGroup, camera *HikCamera, eve
}()
}

func (server Server) Start() {
func (server *Server) Start() {
if server.Cameras == nil || len(*server.Cameras) == 0 {
fmt.Println("HIK: Error: no cameras defined")
return
Expand Down
28 changes: 15 additions & 13 deletions servers/hisilicon/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func hexIpToCIDR(hexAddr string) string {
return ipAddr
}

func (server Server) handleTcpConnection(conn net.Conn) {
func (server *Server) handleTcpConnection(conn net.Conn) {
defer conn.Close()

if server.Debug {
Expand Down Expand Up @@ -90,7 +90,7 @@ type Server struct {
MessageHandler func(topic string, data string)
}

func (server Server) Start() {
func (server *Server) Start() {
if server.Port == "" {
server.Port = "15002" // DEFAULT PORT
}
Expand All @@ -101,18 +101,20 @@ func (server Server) Start() {
}
}

// START TCP SERVER
tcpListener, err := net.Listen("tcp4", ":"+server.Port)
if err != nil {
panic(err)
}
defer tcpListener.Close()

for {
conn, err := tcpListener.Accept()
go func() {
// START TCP SERVER
tcpListener, err := net.Listen("tcp4", ":"+server.Port)
if err != nil {
panic(err)
}
go server.handleTcpConnection(conn)
}
defer tcpListener.Close()

for {
conn, err := tcpListener.Accept()
if err != nil {
panic(err)
}
go server.handleTcpConnection(conn)
}
}()
}

0 comments on commit 255cd22

Please sign in to comment.