event.go 2.5 KB

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