camera_event.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. //关联主路led屏和支路摄像头;关联支路led屏和主路摄像头
  31. if branch == 0 && camera.Branch == 1 || branch == 1 && camera.Branch == 0 {
  32. s.Notifiers[camera.IP] = notifier
  33. }
  34. }
  35. }
  36. func (s *CameraServer) Callback(ip, id string) {
  37. notifier, ok := s.Notifiers[ip]
  38. if !ok {
  39. logrus.Errorf("回调函数注册表没有该ip:%s", ip)
  40. return
  41. }
  42. notifier.Notify(id)
  43. logrus.Debugf("camera [%s] Callback", ip)
  44. }
  45. func (s *CameraServer) Handler(w http.ResponseWriter, r *http.Request) {
  46. gopool.Go(func() {
  47. //监听主机应答固定,直接先应答
  48. w.WriteHeader(200)
  49. w.Header().Add("Date", time.Now().String())
  50. w.Header().Add("Connection", "keep-alive")
  51. })
  52. //1.
  53. contentType := r.Header.Get("Content-Type")
  54. if strings.Contains(contentType, "application/xml") {
  55. bytes, err := io.ReadAll(r.Body)
  56. if err != nil {
  57. return
  58. }
  59. var event model.EventNotificationAlert
  60. err = xml.Unmarshal(bytes, &event)
  61. if err != nil {
  62. return
  63. }
  64. //发送事件通知
  65. for _, v := range event.DetectionRegionList.DetectionRegionEntry {
  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. }