zhangqian
2023-12-19 c688f083caf965cd13f640098ad204d8fd8c9e69
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
package v1
 
import (
    "apsClient/conf"
    "apsClient/model/request"
    "apsClient/model/response"
    _ "apsClient/model/response"
    "apsClient/pkg/contextx"
    "apsClient/pkg/ecode"
    "apsClient/pkg/logx"
    "apsClient/service"
    "github.com/gin-gonic/gin"
)
 
type DeviceApi struct{}
 
// SetCurrentDeviceId
// @Tags      设备
// @Summary   设置当前设备id
// @Produce   application/json
// @Param     object  body    request.SetCurrentDevice true  "查询参数"
// @Success   200   {object}  contextx.Response{}  "成功"
// @Router    /v1/device/setCurrentDeviceId [post]
func (slf *DeviceApi) SetCurrentDeviceId(c *gin.Context) {
    var params request.SetCurrentDevice
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    if params.CurrentDeviceID == "" {
        ctx.Fail(ecode.ParamsErr)
        return
    }
    list, err := service.GetDeviceIDList()
    if err != nil {
        logx.Errorf("SetCurrentDeviceId GetDeviceIDList err:%v", err)
        ctx.Fail(ecode.DBErr)
        return
    }
    findFlag := false
    for _, item := range list {
        if item == params.CurrentDeviceID {
            findFlag = true
        }
    }
    if !findFlag {
        ctx.Fail(ecode.ParamsErr)
        return
    }
    service.SetDeviceIDToFile(params.CurrentDeviceID)
    conf.Conf.CurrentDeviceID = params.CurrentDeviceID
    ctx.Ok()
}
 
// Config
// @Tags      设备
// @Summary   设置设备一些配置
// @Produce   application/json
// @Param     object  body    request.DeviceConfig true  "查询参数"
// @Success   200   {object}  contextx.Response{}  "成功"
// @Router    /v1/device/config [post]
func (slf *DeviceApi) Config(c *gin.Context) {
    var params request.DeviceConfig
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    if conf.Conf.CurrentDeviceID == "" {
        ctx.FailWithMsg(ecode.UnknownErr, "当前设备为空,请检查")
        return
    }
    err := service.UpdateDevice(conf.Conf.CurrentDeviceID, params.NeedSetProcessParams)
    if err != nil {
        logx.Errorf("save device config err:%v", err)
        ctx.Fail(ecode.DBErr)
        return
    }
    ctx.Ok()
}
 
// DeviceList
// @Tags      设备
// @Summary   获取当前面板绑定的设备列表
// @Produce   application/json
// @Success   200   {object}  contextx.Response{data=response.DeviceListResponse}  "成功"
// @Router    /v1/device/list [get]
func (slf *DeviceApi) DeviceList(c *gin.Context) {
    ctx, ok := contextx.NewContext(c, nil)
    if !ok {
        return
    }
    list, err := service.GetDeviceList()
    if err != nil {
        ctx.Fail(ecode.DBErr)
        return
    }
    resp := response.DeviceListResponse{
        SystemDeviceID:       conf.Conf.System.DeviceId,
        CurrentDeviceID:      conf.Conf.CurrentDeviceID,
        DeviceList:           list,
        SystemDeviceStatus:   response.SystemDeviceStatusNormal,
        ClusterStatus:        conf.Conf.SerfClusterStatus,
        ClusterNodeQuantity:  conf.Conf.ClusterNodeQuantity,
        SystemDeviceRunSince: conf.Conf.SystemDeviceRunSince,
    }
    ctx.OkWithDetailed(resp)
}