zhangzengfei
2024-10-11 539a78196da60eb97cf7057c5c85dfaa9b240741
controller/systemCtl.go
@@ -1,13 +1,13 @@
package controller
import (
   "gat1400Exchange/models"
   "gat1400Exchange/pkg/logger"
   "net/http"
   "sync"
   "time"
   "gat1400Exchange/config"
   "gat1400Exchange/pkg/auth"
   "gat1400Exchange/pkg/logger"
   "gat1400Exchange/repository"
   "gat1400Exchange/vo"
@@ -15,21 +15,32 @@
)
type SystemController struct {
   Auth       *auth.DigestAuth
   Repository repository.SystemRepository
   Auth     *auth.DigestAuth
   ApeRepo  repository.ApeRepository
   DevCache *sync.Map // 缓存设备类型, id为key, value为 ape 采集设备, subPlatform 下级, cascade上级
}
// 构造函数
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 {
      // 需要在这里实现检查用户名和密码是否有效的逻辑, 目前使用固定密码
   realm := "Basic Realm"
   if config.ServeConf.Realm != "" {
      realm = config.ServeConf.Realm
   }
   controller.Auth = auth.NewDigestAuthenticator(realm, func(user, realm string) string {
      if user == config.ServeConf.Username {
         return config.ServeConf.Password
      }
      return config.ServeConf.Password
   })
   controller.Auth.PlainTextSecrets = true
   controller.DevCache = new(sync.Map)
   controller.InitDeviceCache()
   return controller
}
@@ -43,12 +54,33 @@
      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 || user == req.RegisterObject.DeviceID {
      s.DevCache.Store(req.RegisterObject.DeviceID, 0)
      if err := s.ApeRepo.Create(req.RegisterObject.DeviceID); err != nil {
         logger.Warn("Create ape failure,%s", err.Error())
         c.AbortWithStatus(http.StatusInternalServerError)
         return
      }
   } else {
      // 未缓存的id, 可能是新添加的上下级, 更新缓存
      if _, ok := s.DevCache.Load(req.RegisterObject.DeviceID); !ok {
         s.InitDeviceCache()
      }
   }
   c.JSON(http.StatusOK, gin.H{"ResponseStatusObject": rspMsg})
@@ -62,13 +94,8 @@
      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())
   if devType, ok := s.DevCache.Load(req.KeepaliveObject.DeviceID); ok {
      s.ApeRepo.Keepalive(req.KeepaliveObject.DeviceID, devType.(int))
   }
   rspMsg := vo.ResponseStatus{
@@ -91,7 +118,6 @@
   }
   // 删库
   rspMsg := vo.ResponseStatus{
      RequestURL:   c.FullPath(),
      StatusCode:   vo.StatusSuccess,
@@ -105,6 +131,7 @@
// 时间校准
func (s SystemController) Time(c *gin.Context) {
   //iTime, _ := strconv.ParseInt(time.Now().Format("20060102150405"), 10, 64)
   rspMsg := vo.ResponseSystemTime{
      VIIDServerID: config.ServeConf.ID,
      LocalTime:    time.Now().Format("20060102150405"),
@@ -112,3 +139,44 @@
   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
   }
   fromId := c.Param("from_id")
   err := s.ApeRepo.Save(fromId, &req)
   if err != nil {
      c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
      return
   }
   c.JSON(http.StatusOK, gin.H{"msg": "ok"})
}
func (s SystemController) InitDeviceCache() {
   // 清空
   s.DevCache.Range(func(key, value interface{}) bool {
      s.DevCache.Delete(key)
      return true
   })
   for k, v := range s.ApeRepo.CollectDeviceType() {
      s.DevCache.Store(k, v)
   }
}