修改服务器状态的api,以便可以查询设备的状态信息.
This commit is contained in:
@@ -784,8 +784,44 @@ func GetDeviceStatus(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// GetAllDeviceStatus 获取所有设备状态
|
||||
// GetAllDeviceStatus 获取所有设备状态或单个设备状态
|
||||
func GetAllDeviceStatus(c *gin.Context) {
|
||||
// 检查是否有device_id查询参数
|
||||
deviceID := c.Query("device_id")
|
||||
|
||||
// 如果有device_id参数,返回单个设备状态
|
||||
if deviceID != "" {
|
||||
// 从设备存储获取设备信息
|
||||
device, ok := deviceStorage.GetDevice(deviceID)
|
||||
if !ok {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"error": "Device not found",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 查询设备监控数据
|
||||
_, status, err := globalStorage.QueryDeviceStatus(context.Background(), deviceID)
|
||||
if err != nil {
|
||||
// 如果查询监控数据失败,返回设备基本信息和空状态
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"id": device.ID,
|
||||
"name": device.Name,
|
||||
"status": make(map[string]float64),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 返回单个设备状态
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"id": device.ID,
|
||||
"name": device.Name,
|
||||
"status": status,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 没有device_id参数,返回所有设备状态(原有逻辑)
|
||||
// 从设备存储获取所有设备
|
||||
allDevices := deviceStorage.GetDevices()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user