package controller import ( "gat1400Exchange/models" "gat1400Exchange/pkg/logger" "gat1400Exchange/service" "net/http" "strconv" "time" "gat1400Exchange/config" "gat1400Exchange/pkg/auth" "gat1400Exchange/repository" "gat1400Exchange/vo" "github.com/gin-gonic/gin" ) type SystemController struct { Auth *auth.DigestAuth Repository repository.SystemRepository } // 构造函数 func NewSystemController() SystemController { svr := repository.NewSystemRepository() controller := SystemController{Repository: svr} controller.Auth = auth.NewDigestAuthenticator("Basic Realm", func(user, realm string) string { // 需要在这里实现检查用户名和密码是否有效的逻辑, 目前使用固定密码 return config.ServeConf.Password }) controller.Auth.PlainTextSecrets = true return controller } // 设备注册 func (s SystemController) Register(c *gin.Context) { user, _ := s.Auth.CheckAuth(c.Request) if user == "" { s.Auth.RequireAuth(c.Writer, c.Request) c.AbortWithStatus(http.StatusUnauthorized) return } rspMsg := vo.ResponseStatus{ RequestURL: c.FullPath(), StatusCode: vo.StatusSuccess, StatusString: vo.StatusString[vo.StatusSuccess], Id: user, LocalTime: time.Now().Format("20060102150405"), } c.JSON(http.StatusOK, gin.H{"ResponseStatusObject": rspMsg}) } // 设备保活 func (s SystemController) Keepalive(c *gin.Context) { var req vo.RequestKeepalive if err := c.BindJSON(&req); err != nil { c.AbortWithStatus(http.StatusBadRequest) return } // 上报设备信息 var d = models.Device{ Id: req.KeepaliveObject.DeviceID, } err := d.Upsert() if err != nil { logger.Warn("Device db update camera error:%s", err.Error()) } service.KeepDeviceAlive(req.KeepaliveObject.DeviceID) rspMsg := vo.ResponseStatus{ RequestURL: c.FullPath(), StatusCode: vo.StatusSuccess, StatusString: vo.StatusString[vo.StatusSuccess], Id: req.KeepaliveObject.DeviceID, LocalTime: time.Now().Format("20060102150405"), } c.JSON(http.StatusOK, gin.H{"ResponseStatusObject": rspMsg}) } // 设备退出 func (s SystemController) UnRegister(c *gin.Context) { var req vo.RequestUnRegister if err := c.BindJSON(&req); err != nil { c.AbortWithStatus(http.StatusBadRequest) return } // 删库 rspMsg := vo.ResponseStatus{ RequestURL: c.FullPath(), StatusCode: vo.StatusSuccess, StatusString: vo.StatusString[vo.StatusSuccess], Id: req.UnRegisterObject.DeviceID, LocalTime: time.Now().Format("20060102150405"), } c.JSON(http.StatusOK, gin.H{"ResponseStatusObject": rspMsg}) } // 时间校准 func (s SystemController) Time(c *gin.Context) { iTime, _ := strconv.ParseInt(time.Now().Format("20060102150405"), 10, 64) rspMsg := vo.ResponseSystemTime{ VIIDServerID: config.ServeConf.ID, LocalTime: iTime, } c.JSON(http.StatusOK, gin.H{"SystemTimeObject": rspMsg}) }