Skip to content

Commit d68f940

Browse files
committed
Dahua: fix event subscription
1 parent 24898e9 commit d68f940

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

servers/dahua/server.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/icholy/digest"
66
"io"
7+
"log"
78
"mime"
89
"mime/multipart"
910
"net/http"
@@ -43,7 +44,7 @@ type Event struct {
4344
}
4445

4546
func (camera *DhCamera) readEvents(channel chan<- DhEvent, callback func()) {
46-
request, err := http.NewRequest("GET", camera.Url+"/cgi-bin/eventManager.cgi?action=attach&codes=All", nil)
47+
request, err := http.NewRequest("GET", camera.Url+"/cgi-bin/eventManager.cgi?action=attach&codes=[All]", nil)
4748
if err != nil {
4849
fmt.Printf("DAHUA: Error: Could not connect to camera %s\n", camera.Name)
4950
fmt.Println("DAHUA: Error", err)
@@ -58,11 +59,20 @@ func (camera *DhCamera) readEvents(channel chan<- DhEvent, callback func()) {
5859
if err != nil {
5960
fmt.Printf("DAHUA: Error opening HTTP connection to camera %s\n", camera.Name)
6061
fmt.Println(err)
62+
callback()
6163
return
6264
}
65+
defer response.Body.Close()
6366

6467
if response.StatusCode != 200 {
6568
fmt.Printf("DAHUA: Warning: Status Code was not 200, but %v\n", response.StatusCode)
69+
if camera.Debug { // DUMP BODY
70+
body, err := io.ReadAll(response.Body)
71+
if err != nil {
72+
log.Fatalln(err)
73+
}
74+
fmt.Println(string(body))
75+
}
6676
}
6777

6878
// FIGURE OUT MULTIPART BOUNDARY
@@ -154,7 +164,7 @@ func (server *Server) addCamera(waitGroup *sync.WaitGroup, cam *DhCamera, channe
154164
}
155165

156166
// PROBE AUTH
157-
request, err := http.NewRequest("GET", cam.Url+"/cgi-bin/eventManager.cgi?action=getConfig&name=General", nil)
167+
request, err := http.NewRequest("GET", cam.Url+"/cgi-bin/configManager.cgi?action=getConfig&name=General", nil)
158168
if err != nil {
159169
fmt.Printf("DAHUA: Error probing auth method for camera %s\n", cam.Name)
160170
fmt.Println(err)
@@ -167,6 +177,7 @@ func (server *Server) addCamera(waitGroup *sync.WaitGroup, cam *DhCamera, channe
167177
fmt.Println(err)
168178
return
169179
}
180+
defer response.Body.Close()
170181
if response.StatusCode == 401 {
171182
if response.Header.Get("WWW-Authenticate") == "" {
172183
// BAD PASSWORD
@@ -213,7 +224,7 @@ func (server *Server) addCamera(waitGroup *sync.WaitGroup, cam *DhCamera, channe
213224
if done {
214225
break
215226
}
216-
go cam.readEvents(channel, callback)
227+
cam.readEvents(channel, callback)
217228
}
218229
fmt.Printf("DAHUA: Closed connection to camera %s\n", cam.Name)
219230
}()

0 commit comments

Comments
 (0)