zhangzengfei
2024-05-17 3e9a1a28b1283e40bc7edb94e2370c74e7fd68e0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package controller
 
import (
    "gat1400Exchange/pkg/logger"
    "gat1400Exchange/service"
    "net/http"
    "time"
 
    "gat1400Exchange/config"
    "gat1400Exchange/pkg/auth"
    "gat1400Exchange/repository"
    "gat1400Exchange/vo"
 
    "github.com/gin-gonic/gin"
)
 
type SystemController struct {
    Auth    *auth.DigestAuth
    ApeRepo repository.ApeRepository
}
 
// 构造函数
func NewSystemController() SystemController {
    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
    })
 
    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
    }
 
    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:           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})
}
 
// 设备保活
func (s SystemController) Keepalive(c *gin.Context) {
    var req vo.RequestKeepalive
    if err := c.BindJSON(&req); err != nil {
        c.AbortWithStatus(http.StatusBadRequest)
        return
    }
 
    service.KeepDeviceAlive(req.KeepaliveObject.DeviceID)
    s.ApeRepo.Keepalive(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:    time.Now().Format("20060102150405"),
    }
 
    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"})
}