camera_event.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, id string) {
  34. notifier, ok := s.Notifiers[ip]
  35. if !ok {
  36. logrus.Errorf("回调函数注册表没有该ip:%s", ip)
  37. return
  38. }
  39. notifier.Notify(id)
  40. logrus.Debugf("camera [%s] [%s] Callback", ip, id)
  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. //发送事件通知
  62. //s.Callback(event.IpAddress)
  63. logrus.Errorf("Handler event = %+v", event)
  64. for _, v := range event.DetectionRegionList.DetectionRegionEntry {
  65. logrus.Errorf("Handler id = %s", v.RegionID)
  66. s.Callback(event.IpAddress, v.RegionID)
  67. }
  68. } else if strings.Contains(contentType, "multipart/form-data") {
  69. s.HandleMultipart(r)
  70. }
  71. }
  72. // 处理多文件事件
  73. func (s *CameraServer) HandleMultipart(r *http.Request) {
  74. multipartReader := multipart.NewReader(r.Body, "boundary")
  75. // 循环读取每个 part
  76. var event model.EventNotificationAlert
  77. for {
  78. part, err := multipartReader.NextPart()
  79. //defer part.Close()
  80. if err == io.EOF {
  81. break
  82. }
  83. if err != nil {
  84. log.Println("Failed to read part:", err)
  85. return
  86. }
  87. if part.FormName() != "linedetectionImage" || !strings.Contains(part.FormName(), "Image") {
  88. //不含图片的xml部分数据
  89. xmlData, err := ioutil.ReadAll(part)
  90. if err != nil {
  91. return
  92. }
  93. xml.Unmarshal(xmlData, &event)
  94. continue
  95. }
  96. }
  97. //发送事件通知
  98. //s.Callback(event.IpAddress, "")
  99. logrus.Errorf("Handler event = %+v", event)
  100. for _, v := range event.DetectionRegionList.DetectionRegionEntry {
  101. logrus.Errorf("Handler id = %s", v.RegionID)
  102. s.Callback(event.IpAddress, v.RegionID)
  103. }
  104. }