camera_event.go 2.6 KB

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