添加添加及修改monitor设备接口
This commit is contained in:
parent
510b725ade
commit
3967d68ce0
|
|
@ -74,9 +74,30 @@ func GetMonitorList(c *gin.Context) {
|
|||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
type UpdateMonitorDeviceStatusReq struct {
|
||||
Devices []proto.GetMonitorDeviceStatus `json:"devices" form:"devices"` //设备状态列表
|
||||
}
|
||||
|
||||
func UpdateMonitor(c *gin.Context) {
|
||||
id, _ := c.Get("id")
|
||||
id1 := int(id.(float64))
|
||||
var req UpdateMonitorDeviceStatusReq
|
||||
var resp proto.GeneralResp
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
err2 := service.UpdateMonitorDeviceListWithStatus(id1, req.Devices)
|
||||
if err2 != nil {
|
||||
resp.Code = proto.OperationFailed
|
||||
resp.Message = "更新设备状态失败:" + err2.Error()
|
||||
} else {
|
||||
resp.Code = proto.SuccessCode
|
||||
resp.Message = "更新设备状态成功"
|
||||
}
|
||||
} else {
|
||||
resp.Code = proto.ParameterError
|
||||
resp.Message = "参数解析失败:" + err.Error()
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
func DelMonitor(c *gin.Context) {
|
||||
id, _ := c.Get("id")
|
||||
|
|
|
|||
|
|
@ -3,4 +3,5 @@ package proto
|
|||
type GetMonitorDeviceStatus struct {
|
||||
ID string `json:"id" form:"id"` //设备编码
|
||||
Status string `json:"status" form:"status"` //设备状态
|
||||
Expire int `json:"expire" form:"expire"` //设备过期时间,及更新时间
|
||||
}
|
||||
|
|
|
|||
|
|
@ -124,9 +124,10 @@ func GetMonitorDeviceListWithStatus(userId int) ([]proto.GetMonitorDeviceStatus,
|
|||
} else {
|
||||
devices := worker.GetRedisSetMembers(proto.Config.MONITOR_SERVER_TOKEN)
|
||||
for _, device := range devices {
|
||||
status := worker.GetRedis("monitor_" + device)
|
||||
status, expireIn := worker.GetRedisWithExpire("monitor_" + device)
|
||||
var deviceInfo proto.GetMonitorDeviceStatus
|
||||
deviceInfo.ID = device
|
||||
deviceInfo.Expire = expireIn
|
||||
if status == "" {
|
||||
deviceInfo.Status = "offline"
|
||||
} else {
|
||||
|
|
@ -137,3 +138,29 @@ func GetMonitorDeviceListWithStatus(userId int) ([]proto.GetMonitorDeviceStatus,
|
|||
}
|
||||
return deviceStatus, nil
|
||||
}
|
||||
|
||||
func UpdateMonitorDeviceListWithStatus(userId int, deviceReq []proto.GetMonitorDeviceStatus) error {
|
||||
user := GetUserByIDFromUserCenter(userId)
|
||||
var err error
|
||||
if user.Role != "admin" {
|
||||
err = errors.New("user is not admin, can not update monitor device list")
|
||||
return err
|
||||
} else {
|
||||
// 更新监控设备状态.如果在集合中则添加,不在则添加到集合中
|
||||
devices := worker.GetRedisSetMembers(proto.Config.MONITOR_SERVER_TOKEN)
|
||||
deviceMap := make(map[string]bool, len(devices))
|
||||
for _, device := range devices {
|
||||
deviceMap[device] = true
|
||||
}
|
||||
for _, device := range deviceReq {
|
||||
if _, ok := deviceMap[device.ID]; ok {
|
||||
// 如果设备在集合中,则更新状态
|
||||
worker.SetRedisWithExpire("monitor_"+device.ID, device.Status, time.Duration(device.Expire)*time.Second)
|
||||
} else {
|
||||
worker.SetRedisSetAdd(proto.Config.MONITOR_SERVER_TOKEN, device.ID)
|
||||
worker.SetRedisWithExpire("monitor_"+device.ID, device.Status, time.Duration(device.Expire)*time.Second)
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -176,6 +176,22 @@ func GetRedis(key string) string {
|
|||
return val
|
||||
}
|
||||
|
||||
func GetRedisWithExpire(key string) (string, int) {
|
||||
ctx := context.Background()
|
||||
val, err := RedisClient.Get(ctx, key).Result() // 从 Redis 读取键值, 如果键不存在则返回空字符串, 如果出现错误则返回错误
|
||||
if err != nil {
|
||||
//fmt.Println(key, " Error getting key: %v", err)
|
||||
return "", 0
|
||||
} else {
|
||||
// 获取键的过期时间
|
||||
ttl, err2 := RedisClient.TTL(ctx, key).Result()
|
||||
if err2 != nil {
|
||||
return val, -1
|
||||
}
|
||||
return val, int(ttl.Seconds())
|
||||
}
|
||||
}
|
||||
|
||||
// pop redis list from right,as stack
|
||||
func PopRedisList(key string) string {
|
||||
ctx := context.Background()
|
||||
|
|
|
|||
Loading…
Reference in New Issue