camera_event.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package lc
  2. import (
  3. "encoding/xml"
  4. "github.com/sirupsen/logrus"
  5. "io"
  6. "io/ioutil"
  7. "lc-smartX/lc/model"
  8. "lc-smartX/util"
  9. "lc-smartX/util/gopool"
  10. "log"
  11. "mime/multipart"
  12. "net/http"
  13. "strings"
  14. "time"
  15. )
  16. func NewCameraEventServer() *CameraServer {
  17. server := &CameraServer{Cameras: util.Config.Cameras, Notifiers: make(map[string]Notifier, 4)}
  18. return server
  19. }
  20. type CameraServer struct {
  21. Cameras []model.CameraInfo
  22. Notifiers map[string]Notifier
  23. }
  24. func (s *CameraServer) Start() {
  25. http.HandleFunc(util.Config.HikServer.Path, s.Handler)
  26. logrus.Fatal("事件监听服务启动失败:", util.Config.HikServer.Addr, ", error:", http.ListenAndServe(util.Config.HikServer.Addr, nil))
  27. }
  28. func (s *CameraServer) RegisterCallback(branch byte, notifier Notifier) {
  29. for _, camera := range s.Cameras {
  30. s.Notifiers[camera.IP] = notifier
  31. }
  32. }
  33. func (s *CameraServer) Callback(ip string) {
  34. notifier, ok := s.Notifiers[ip]
  35. if !ok {
  36. logrus.Errorf("回调函数注册表没有该ip:%s", ip)
  37. return
  38. }
  39. notifier.Notify()
  40. logrus.Debugf("camera [%s] Callback", ip)
  41. }
  42. func (s *CameraServer) Handler(w http.ResponseWriter, r *http.Request) {
  43. gopool.Go(func() {
  44. //监听主机应答固定,直接先应答
  45. w.WriteHeader(200)
  46. w.Header().Add("Date", time.Now().String())
  47. w.Header().Add("Connection", "keep-alive")
  48. })
  49. //1.
  50. contentType := r.Header.Get("Content-Type")
  51. if strings.Contains(contentType, "application/xml") {
  52. bytes, err := io.ReadAll(r.Body)
  53. if err != nil {
  54. return
  55. }
  56. var event model.EventNotificationAlert
  57. err = xml.Unmarshal(bytes, &event)
  58. if err != nil {
  59. return
  60. }
  61. logrus.Errorf("EventNotificationAlert = %+v", event)
  62. //发送事件通知
  63. s.Callback(event.IpAddress)
  64. } else if strings.Contains(contentType, "multipart/form-data") {
  65. s.HandleMultipart(r)
  66. }
  67. }
  68. // 处理多文件事件
  69. func (s *CameraServer) HandleMultipart(r *http.Request) {
  70. multipartReader := multipart.NewReader(r.Body, "boundary")
  71. // 循环读取每个 part
  72. var event model.EventNotificationAlert
  73. for {
  74. part, err := multipartReader.NextPart()
  75. //defer part.Close()
  76. if err == io.EOF {
  77. break
  78. }
  79. if err != nil {
  80. log.Println("Failed to read part:", err)
  81. return
  82. }
  83. if part.FormName() != "linedetectionImage" || !strings.Contains(part.FormName(), "Image") {
  84. //不含图片的xml部分数据
  85. xmlData, err := ioutil.ReadAll(part)
  86. if err != nil {
  87. return
  88. }
  89. xml.Unmarshal(xmlData, &event)
  90. continue
  91. }
  92. }
  93. //发送事件通知
  94. s.Callback(event.IpAddress)
  95. }