| | |
| | | package controller |
| | | |
| | | import ( |
| | | "gat1400Exchange/pkg/logger" |
| | | "gat1400Exchange/service" |
| | | "net/http" |
| | | "time" |
| | |
| | | |
| | | type SystemController struct { |
| | | Auth *auth.DigestAuth |
| | | Repository repository.SystemRepository |
| | | ApeRepo repository.ApeRepository |
| | | } |
| | | |
| | | // 构造函数 |
| | | func NewSystemController() SystemController { |
| | | svr := repository.NewSystemRepository() |
| | | controller := SystemController{Repository: svr} |
| | | svr := repository.NewApeRepository() |
| | | controller := SystemController{ApeRepo: svr} |
| | | |
| | | controller.Auth = auth.NewDigestAuthenticator("Basic Realm", func(user, realm string) string { |
| | | // 需要在这里实现检查用户名和密码是否有效的逻辑, 目前使用固定密码 |
| | | if user == config.ServeConf.Username { |
| | | return config.ServeConf.Password |
| | | } |
| | | |
| | | return config.ServeConf.Password |
| | | }) |
| | | |
| | |
| | | return |
| | | } |
| | | |
| | | var req vo.RequestRegister |
| | | 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: user, |
| | | Id: req.RegisterObject.DeviceID, |
| | | LocalTime: time.Now().Format("20060102150405"), |
| | | } |
| | | |
| | | if user == config.ServeConf.Username { |
| | | if err := s.ApeRepo.Create(req.RegisterObject.DeviceID); err != nil { |
| | | logger.Warn("Create ape failure,%s", err.Error()) |
| | | c.AbortWithStatus(http.StatusInternalServerError) |
| | | return |
| | | } |
| | | } |
| | | |
| | | c.JSON(http.StatusOK, gin.H{"ResponseStatusObject": rspMsg}) |
| | |
| | | } |
| | | |
| | | service.KeepDeviceAlive(req.KeepaliveObject.DeviceID) |
| | | s.ApeRepo.Keepalive(req.KeepaliveObject.DeviceID) |
| | | |
| | | rspMsg := vo.ResponseStatus{ |
| | | RequestURL: c.FullPath(), |
| | |
| | | |
| | | c.JSON(http.StatusOK, gin.H{"SystemTimeObject": rspMsg}) |
| | | } |
| | | |
| | | // 设备列表 |
| | | func (s SystemController) ApeList(c *gin.Context) { |
| | | apeList, err := s.ApeRepo.List() |
| | | if err != nil { |
| | | logger.Error(err.Error()) |
| | | } |
| | | |
| | | c.JSON(http.StatusOK, gin.H{"ApeList": apeList}) |
| | | } |
| | | |
| | | // 修改设备 |
| | | func (s SystemController) ApeUpdate(c *gin.Context) { |
| | | var req vo.Ape |
| | | if err := c.BindJSON(&req); err != nil { |
| | | c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()}) |
| | | return |
| | | } |
| | | |
| | | err := s.ApeRepo.Update(&req) |
| | | if err != nil { |
| | | c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()}) |
| | | return |
| | | } |
| | | |
| | | c.JSON(http.StatusOK, gin.H{"msg": "ok"}) |
| | | } |