diff --git a/handler/tool.go b/handler/tool.go index 820854a..1654a21 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -52,10 +52,37 @@ func SetUpToolGroup(router *gin.Engine) { toolGroup.POST("/file_del", DelFile) //服务器、设备状态接口 toolGroup.POST("/monitor", SetDeviceStatusV2) + toolGroup.GET("/get_monitor_list", GetMonitorList) //获取设备监控列表 + toolGroup.POST("/update_monitor", UpdateMonitor) //设置设备状态 + toolGroup.POST("/del_monitor", DelMonitor) //删除设备监控 //发送邮件 toolGroup.POST("/send_mail", SendMailTool) } +func GetMonitorList(c *gin.Context) { + id, _ := c.Get("id") + userId := int(id.(float64)) + var resp proto.GeneralResp + monitorDeviceList, err := service.GetMonitorDeviceListWithStatus(userId) + if err != nil { + resp.Code = proto.OperationFailed + resp.Message = err.Error() + } else { + resp.Code = proto.SuccessCode + resp.Message = "success" + resp.Data = monitorDeviceList + } + c.JSON(http.StatusOK, resp) +} +func UpdateMonitor(c *gin.Context) { + id, _ := c.Get("id") + id1 := int(id.(float64)) +} +func DelMonitor(c *gin.Context) { + id, _ := c.Get("id") + id1 := int(id.(float64)) +} + func SetDeviceStatusV2(c *gin.Context) { // TODO var req SetDeviceStatusReq diff --git a/proto/conf.go b/proto/conf.go index 0c6bb27..c79fba0 100644 --- a/proto/conf.go +++ b/proto/conf.go @@ -82,6 +82,7 @@ type ConfigStruct struct { MASTER_SERVER_DOMAIN string `json:"master_server_domain"` // 主服务器域名 USER_SYNC_TIME int `json:"user_sync_time"` // 用户数据同步时间,单位秒 SERVER_NAME string `json:"server_name"` // 服务器名称,用于区分不同服务器 + MONITOR_SERVER_TOKEN string `json:"monitor_server_token"` // 监控服务器token,用于状态监控及邮件通知 } // 读取配置文件 diff --git a/proto/resp.go b/proto/resp.go new file mode 100644 index 0000000..afa2759 --- /dev/null +++ b/proto/resp.go @@ -0,0 +1,6 @@ +package proto + +type GetMonitorDeviceStatus struct { + ID string `json:"id" form:"id"` //设备编码 + Status string `json:"status" form:"status"` //设备状态 +} diff --git a/service/toolService.go b/service/toolService.go index 754daed..d4c0793 100644 --- a/service/toolService.go +++ b/service/toolService.go @@ -1,6 +1,7 @@ package service import ( + "errors" "fmt" "regexp" "time" @@ -113,3 +114,26 @@ func CheckEmail(email string) bool { reg := regexp.MustCompile(pattern) return reg.MatchString(email) } + +// 获取监控设备及状态 +func GetMonitorDeviceListWithStatus(userId int) ([]proto.GetMonitorDeviceStatus, error) { + var deviceStatus []proto.GetMonitorDeviceStatus + user := GetUserByIDFromUserCenter(userId) + if user.Role != "admin" { + return deviceStatus, errors.New("user is not admin, can not get monitor device list") + } else { + devices := worker.GetRedisSetMembers(proto.Config.MONITOR_SERVER_TOKEN) + for _, device := range devices { + status := worker.GetRedis("monitor_" + device) + var deviceInfo proto.GetMonitorDeviceStatus + deviceInfo.ID = device + if status == "" { + deviceInfo.Status = "offline" + } else { + deviceInfo.Status = status + } + deviceStatus = append(deviceStatus, deviceInfo) + } + } + return deviceStatus, nil +}