|
| 1 | +package dahua |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "io/ioutil" |
| 7 | + "mime" |
| 8 | + "mime/multipart" |
| 9 | + "net/http" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + "sync" |
| 13 | +) |
| 14 | + |
| 15 | +type DhCamera struct { |
| 16 | + Debug bool |
| 17 | + Name string `json:"name"` |
| 18 | + Url string `json:"url"` |
| 19 | + Username string `json:"username"` |
| 20 | + Password string `json:"password"` |
| 21 | + client *http.Client |
| 22 | +} |
| 23 | + |
| 24 | +type Server struct { |
| 25 | + Debug bool |
| 26 | + WaitGroup *sync.WaitGroup |
| 27 | + Cameras *[]DhCamera |
| 28 | + MessageHandler func(topic string, data string) |
| 29 | +} |
| 30 | + |
| 31 | +type DhEvent struct { |
| 32 | + Camera *DhCamera |
| 33 | + Type string |
| 34 | + Message string |
| 35 | +} |
| 36 | + |
| 37 | +type Event struct { |
| 38 | + Code string |
| 39 | + Action string |
| 40 | + Index int |
| 41 | + Data string |
| 42 | + active bool |
| 43 | +} |
| 44 | + |
| 45 | +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 | + if err != nil { |
| 48 | + fmt.Printf("DAHUA: Error: Could not connect to camera %s\n", camera.Name) |
| 49 | + fmt.Println("DAHUA: Error", err) |
| 50 | + callback() |
| 51 | + return |
| 52 | + } |
| 53 | + request.SetBasicAuth(camera.Username, camera.Password) |
| 54 | + |
| 55 | + response, err := camera.client.Do(request) |
| 56 | + if err != nil { |
| 57 | + fmt.Printf("DAHUA: Error opening HTTP connection to camera %s\n", camera.Name) |
| 58 | + fmt.Println(err) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + if response.StatusCode != 200 { |
| 63 | + fmt.Printf("DAHUA: Warning: Status Code was not 200, but %v\n", response.StatusCode) |
| 64 | + } |
| 65 | + |
| 66 | + // FIGURE OUT MULTIPART BOUNDARY |
| 67 | + mediaType, params, err := mime.ParseMediaType(response.Header.Get("Content-Type")) |
| 68 | + if camera.Debug { |
| 69 | + fmt.Printf("DAHUA: Media type is %s\n", mediaType) |
| 70 | + } |
| 71 | + |
| 72 | + if params["boundary"] == "" { |
| 73 | + fmt.Println("DAHUA: ERROR: Camera " + camera.Name + " does not seem to support event streaming") |
| 74 | + callback() |
| 75 | + return |
| 76 | + } |
| 77 | + multipartBoundary := params["boundary"] |
| 78 | + |
| 79 | + event := Event{} |
| 80 | + |
| 81 | + // READ PART BY PART |
| 82 | + multipartReader := multipart.NewReader(response.Body, multipartBoundary) |
| 83 | + for { |
| 84 | + part, err := multipartReader.NextPart() |
| 85 | + if err == io.EOF { |
| 86 | + return |
| 87 | + } |
| 88 | + if err != nil { |
| 89 | + fmt.Println(err) |
| 90 | + continue |
| 91 | + } |
| 92 | + body, err := ioutil.ReadAll(part) |
| 93 | + if err != nil { |
| 94 | + fmt.Println(err) |
| 95 | + continue |
| 96 | + } |
| 97 | + |
| 98 | + if camera.Debug { |
| 99 | + fmt.Printf("DAHUA: Read event body: %s\n", body) |
| 100 | + } |
| 101 | + |
| 102 | + // EXAMPLE: "Code=VideoMotion; action=Start; index=0\r\n\r\n" |
| 103 | + line := strings.Trim(string(body), " \n\r") |
| 104 | + items := strings.Split(line, "; ") |
| 105 | + keyValues := make(map[string]string, len(items)) |
| 106 | + for _, item := range items { |
| 107 | + parts := strings.Split(item, "=") |
| 108 | + if len(parts) > 0 { |
| 109 | + keyValues[parts[0]] = parts[1] |
| 110 | + } |
| 111 | + } |
| 112 | + // EXAMPLE: { Code: VideoMotion, action: Start, index: 0 } |
| 113 | + index := 0 |
| 114 | + index, _ = strconv.Atoi(keyValues["index"]) |
| 115 | + event.Code = keyValues["Code"] |
| 116 | + event.Action = keyValues["action"] |
| 117 | + event.Index = index |
| 118 | + event.Data = keyValues["data"] |
| 119 | + |
| 120 | + switch event.Action { |
| 121 | + case "Start": |
| 122 | + if !event.active { |
| 123 | + if camera.Debug { |
| 124 | + fmt.Println("DAHUA: SENDING CAMERA EVENT!") |
| 125 | + } |
| 126 | + dahuaEvent := DhEvent{ |
| 127 | + Camera: camera, |
| 128 | + Type: event.Code, |
| 129 | + Message: event.Data, |
| 130 | + } |
| 131 | + if dahuaEvent.Message == "" { |
| 132 | + dahuaEvent.Message = event.Action |
| 133 | + } |
| 134 | + channel <- dahuaEvent |
| 135 | + } |
| 136 | + event.active = true |
| 137 | + case "Stop": |
| 138 | + event.active = false |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func (server *Server) addCamera(waitGroup *sync.WaitGroup, cam *DhCamera, channel chan<- DhEvent) { |
| 144 | + waitGroup.Add(1) |
| 145 | + |
| 146 | + if server.Debug { |
| 147 | + fmt.Printf("DAHUA: Adding camera %s: %s\n", cam.Name, cam.Url) |
| 148 | + } |
| 149 | + |
| 150 | + if cam.client == nil { |
| 151 | + cam.client = &http.Client{} |
| 152 | + } |
| 153 | + |
| 154 | + go func() { |
| 155 | + defer waitGroup.Done() |
| 156 | + done := false |
| 157 | + callback := func() { |
| 158 | + done = true |
| 159 | + } |
| 160 | + |
| 161 | + for { |
| 162 | + if done { |
| 163 | + break |
| 164 | + } |
| 165 | + go cam.readEvents(channel, callback) |
| 166 | + } |
| 167 | + fmt.Printf("DAHUA: Closed connection to camera %s\n", cam.Name) |
| 168 | + }() |
| 169 | +} |
| 170 | + |
| 171 | +func (server *Server) Start() { |
| 172 | + if server.Cameras == nil || len(*server.Cameras) == 0 { |
| 173 | + fmt.Println("DAHUA: Error: no cameras defined") |
| 174 | + return |
| 175 | + } |
| 176 | + |
| 177 | + if server.MessageHandler == nil { |
| 178 | + fmt.Println("DAHUA: Message handler is not set for Dahua cams - that's probably not what you want") |
| 179 | + server.MessageHandler = func(topic string, data string) { |
| 180 | + fmt.Printf("DAHUA: Lost alarm: %s: %s\n", topic, data) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + waitGroup := sync.WaitGroup{} |
| 185 | + eventChannel := make(chan DhEvent, 5) |
| 186 | + |
| 187 | + for _, camera := range *server.Cameras { |
| 188 | + server.addCamera(&waitGroup, &camera, eventChannel) |
| 189 | + } |
| 190 | + |
| 191 | + // START MESSAGE PROCESSOR |
| 192 | + go func(waitGroup *sync.WaitGroup, channel <-chan DhEvent) { |
| 193 | + // WAIT GROUP FOR INDIVIDUAL CAMERAS |
| 194 | + defer waitGroup.Done() |
| 195 | + |
| 196 | + // EXTERNAL WAIT GROUP FOR PROCESSES |
| 197 | + defer server.WaitGroup.Done() |
| 198 | + server.WaitGroup.Add(1) |
| 199 | + |
| 200 | + for { |
| 201 | + event := <-channel |
| 202 | + go server.MessageHandler(event.Camera.Name+"/"+event.Type, event.Message) |
| 203 | + } |
| 204 | + }(&waitGroup, eventChannel) |
| 205 | + |
| 206 | + waitGroup.Wait() |
| 207 | +} |
0 commit comments