| | |
| | | package controller |
| | | |
| | | import ( |
| | | "fmt" |
| | | "gat1400Exchange/repository" |
| | | "gat1400Exchange/vo" |
| | | "net/http" |
| | | "strings" |
| | | "time" |
| | | |
| | | "gat1400Exchange/config" |
| | | "gat1400Exchange/pkg/logger" |
| | | "gat1400Exchange/repository" |
| | | "gat1400Exchange/vo" |
| | | |
| | | "github.com/gin-gonic/gin" |
| | | ) |
| | | |
| | | type SubscribeController struct { |
| | | Repository repository.SubscribeRepository |
| | | Capture repository.CaptureRepository |
| | | Ape repository.ApeRepository |
| | | } |
| | | |
| | | // 构造函数 |
| | | func NewSubscribeController() SubscribeController { |
| | | svr := repository.NewSubscribeRepository() |
| | | controller := SubscribeController{svr} |
| | | svr1 := repository.NewCaptureRepository() |
| | | svr2 := repository.NewApeRepository() |
| | | controller := SubscribeController{svr, svr1, svr2} |
| | | |
| | | return controller |
| | | } |
| | | |
| | | func (s SubscribeController) Subscribes(c *gin.Context) { |
| | | func (s SubscribeController) VIIDSubscribesList(c *gin.Context) { |
| | | fromId := c.GetHeader("User-Identify") |
| | | subList, _ := s.Repository.ListByFromId(fromId) |
| | | |
| | | c.Header("Content-Type", "application/VIID+json;charset=UTF-8") |
| | | c.JSON(http.StatusOK, gin.H{"SubscribeList": subList}) |
| | | } |
| | | |
| | | func (s SubscribeController) VIIDSubscribes(c *gin.Context) { |
| | | var req vo.RequestSubscribe |
| | | if err := c.BindJSON(&req); err != nil { |
| | | c.AbortWithStatus(http.StatusBadRequest) |
| | |
| | | |
| | | var rsp vo.ResponseStatusList |
| | | for idx, sub := range req.SubscribeListObject.SubscribeObject { |
| | | if err := s.Repository.CreateSubscribe(fromId, &req.SubscribeListObject.SubscribeObject[idx]); err == nil { |
| | | if err := s.Repository.SaveReceiveSubscribe(fromId, &req.SubscribeListObject.SubscribeObject[idx]); err == nil { |
| | | rsp.ResponseStatusObject = append(rsp.ResponseStatusObject, vo.ResponseStatus{ |
| | | RequestURL: c.FullPath(), |
| | | StatusCode: vo.StatusSuccess, |
| | |
| | | c.JSON(http.StatusOK, gin.H{"ResponseStatusListObject": rsp}) |
| | | } |
| | | |
| | | func (s SubscribeController) DeleteSubscribe(c *gin.Context) { |
| | | func (s SubscribeController) VIIDUpdateSubscribes(c *gin.Context) { |
| | | var req vo.RequestSubscribe |
| | | if err := c.BindJSON(&req); err != nil { |
| | | c.AbortWithStatus(http.StatusBadRequest) |
| | | return |
| | | } |
| | | |
| | | var rsp vo.ResponseStatusList |
| | | for idx, sub := range req.SubscribeListObject.SubscribeObject { |
| | | if err := s.Repository.UpdateReceiveSubscribe(&req.SubscribeListObject.SubscribeObject[idx]); err == nil { |
| | | rsp.ResponseStatusObject = append(rsp.ResponseStatusObject, vo.ResponseStatus{ |
| | | RequestURL: c.FullPath(), |
| | | StatusCode: vo.StatusSuccess, |
| | | StatusString: vo.StatusString[vo.StatusSuccess], |
| | | Id: sub.SubscribeID, |
| | | LocalTime: time.Now().Format("20060102150405"), |
| | | }) |
| | | } else { |
| | | logger.Error("Update receive subscribe failure, %s", err.Error()) |
| | | } |
| | | } |
| | | |
| | | c.Header("Content-Type", "application/VIID+json;charset=UTF-8") |
| | | c.JSON(http.StatusOK, gin.H{"ResponseStatusListObject": rsp}) |
| | | } |
| | | |
| | | func (s SubscribeController) VIIDDeleteSubscribe(c *gin.Context) { |
| | | idList := c.Query("IDList") |
| | | var rsp vo.ResponseStatusList |
| | | |
| | | for _, id := range strings.Split(idList, ",") { |
| | | if err := s.Repository.DeleteSubscribe(id); err == nil { |
| | | if err := s.Repository.DeleteReceiveSubscribe(id); err == nil { |
| | | rsp.ResponseStatusObject = append(rsp.ResponseStatusObject, vo.ResponseStatus{ |
| | | RequestURL: c.FullPath(), |
| | | StatusCode: vo.StatusSuccess, |
| | |
| | | c.JSON(http.StatusOK, gin.H{"ResponseStatusListObject": rsp}) |
| | | } |
| | | |
| | | func (s SubscribeController) Notifications(c *gin.Context) { |
| | | data, err := c.GetRawData() |
| | | if err != nil { |
| | | |
| | | func (s SubscribeController) VIIDNotifications(c *gin.Context) { |
| | | var rsp vo.ResponseStatusList |
| | | var req vo.RequestSubscribeNotificationBind |
| | | if err := c.BindJSON(&req); err != nil { |
| | | c.JSON(http.StatusBadRequest, gin.H{"ResponseStatusListObject": rsp}) |
| | | return |
| | | } |
| | | |
| | | fmt.Println("Notifications:", string(data)) |
| | | c.JSON(http.StatusOK, gin.H{"ResponseStatusListObject": ""}) |
| | | logger.Debug("Receive notifications: %+v", req) |
| | | |
| | | for _, msg := range req.SubscribeNotificationListObject.SubscribeNotificationObject { |
| | | rsp.ResponseStatusObject = append(rsp.ResponseStatusObject, vo.ResponseStatus{ |
| | | RequestURL: c.FullPath(), |
| | | StatusCode: vo.StatusSuccess, |
| | | StatusString: vo.StatusString[vo.StatusSuccess], |
| | | Id: msg.SubscribeID, |
| | | LocalTime: time.Now().Format("20060102150405"), |
| | | }) |
| | | |
| | | // 转发 |
| | | if config.ForwardConf.SyncServer != "" && len(msg.FaceObjectList.FaceObject) > 0 { |
| | | go s.Capture.FaceForward(msg.FaceObjectList.FaceObject) |
| | | } |
| | | |
| | | if config.ForwardConf.SyncServer != "" && len(msg.PersonObjectList.PersonObject) > 0 { |
| | | go s.Capture.PersonForward(msg.PersonObjectList.PersonObject) |
| | | } |
| | | |
| | | if len(msg.DeviceList.APEObject) > 0 { |
| | | fromId := c.GetHeader("User-Identify") |
| | | go s.Ape.HandleNotification(fromId, msg.DeviceList.APEObject) |
| | | } |
| | | } |
| | | |
| | | c.Header("Content-Type", "application/VIID+json;charset=UTF-8") |
| | | c.JSON(http.StatusOK, gin.H{"ResponseStatusListObject": rsp}) |
| | | } |
| | | |
| | | // CreateSubscribes 添加下级平台的订阅消息 |
| | | func (s SubscribeController) CreateSubscribes(c *gin.Context) { |
| | | var req vo.Subscribe |
| | | if err := c.BindJSON(&req); err != nil { |
| | | c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()}) |
| | | return |
| | | } |
| | | |
| | | if c.Param("id") == "" { |
| | | c.JSON(http.StatusBadRequest, gin.H{"msg": "下级id为空"}) |
| | | } |
| | | |
| | | if err := s.Repository.CreateSubscribe(c.Param("id"), &req); err != nil { |
| | | c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()}) |
| | | return |
| | | } |
| | | |
| | | c.JSON(http.StatusOK, gin.H{"msg": "ok"}) |
| | | } |
| | | |
| | | // UpdateSubscribes 更新下级平台的订阅消息 |
| | | func (s SubscribeController) UpdateSubscribes(c *gin.Context) { |
| | | var req vo.Subscribe |
| | | if err := c.BindJSON(&req); err != nil { |
| | | c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()}) |
| | | return |
| | | } |
| | | |
| | | if c.Param("id") == "" { |
| | | c.JSON(http.StatusBadRequest, gin.H{"msg": "下级id为空"}) |
| | | } |
| | | |
| | | if err := s.Repository.UpdateSubscribe(c.Param("id"), &req); err != nil { |
| | | c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()}) |
| | | return |
| | | } |
| | | |
| | | c.JSON(http.StatusOK, gin.H{"msg": "ok"}) |
| | | } |
| | | |
| | | func (s SubscribeController) CancelSubscribes(c *gin.Context) { |
| | | var req vo.Subscribe |
| | | if err := c.BindJSON(&req); err != nil { |
| | | c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()}) |
| | | return |
| | | } |
| | | |
| | | if c.Param("id") == "" { |
| | | c.JSON(http.StatusBadRequest, gin.H{"msg": "下级id为空"}) |
| | | } |
| | | |
| | | if err := s.Repository.CancelSubscribe(c.Param("id"), &req); err != nil { |
| | | c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()}) |
| | | return |
| | | } |
| | | |
| | | c.JSON(http.StatusOK, gin.H{"msg": "ok"}) |
| | | } |
| | | |
| | | func (s SubscribeController) List(c *gin.Context) { |
| | | if c.Param("id") == "" { |
| | | c.JSON(http.StatusBadRequest, gin.H{"msg": "下级id为空"}) |
| | | } |
| | | |
| | | list, err := s.Repository.ListByFromId(c.Param("id")) |
| | | if err != nil { |
| | | c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()}) |
| | | return |
| | | } |
| | | |
| | | c.JSON(http.StatusOK, gin.H{"msg": "ok", "data": list}) |
| | | } |
| | | |
| | | func (s SubscribeController) RemoteList(c *gin.Context) { |
| | | if c.Param("id") == "" { |
| | | c.JSON(http.StatusBadRequest, gin.H{"msg": "下级id为空"}) |
| | | } |
| | | |
| | | data, err := s.Repository.RemoteList(c.Param("id")) |
| | | if err != nil { |
| | | c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()}) |
| | | return |
| | | } |
| | | |
| | | c.JSON(http.StatusOK, gin.H{"msg": "ok", "data": data}) |
| | | } |