获取设备列表接口增加设备状态
This commit is contained in:
		| @@ -222,6 +222,7 @@ func (a *API) setupRoutes() { | |||||||
| 			deviceGroup.POST("/create", a.deviceController.Create) | 			deviceGroup.POST("/create", a.deviceController.Create) | ||||||
| 			deviceGroup.POST("/update", a.deviceController.Update) | 			deviceGroup.POST("/update", a.deviceController.Update) | ||||||
| 			deviceGroup.POST("/delete", a.deviceController.Delete) | 			deviceGroup.POST("/delete", a.deviceController.Delete) | ||||||
|  | 			deviceGroup.GET("/status", a.deviceController.GetDeviceStatus) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		// 远程控制相关路由 | 		// 远程控制相关路由 | ||||||
|   | |||||||
| @@ -17,7 +17,13 @@ import ( | |||||||
|  |  | ||||||
| // ListResponse 设备列表响应结构体 | // ListResponse 设备列表响应结构体 | ||||||
| type ListResponse struct { | type ListResponse struct { | ||||||
| 	Devices []model.Device `json:"devices"` | 	Devices []DeviceListItem `json:"devices"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // DeviceListItem 设备列表项结构体 | ||||||
|  | type DeviceListItem struct { | ||||||
|  | 	model.Device | ||||||
|  | 	Active bool `json:"active"` | ||||||
| } | } | ||||||
|  |  | ||||||
| // DeviceRequest 设备创建/更新请求结构体 | // DeviceRequest 设备创建/更新请求结构体 | ||||||
| @@ -140,7 +146,22 @@ func (c *Controller) List(ctx *gin.Context) { | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	controller.SendSuccessResponse(ctx, "获取设备列表成功", ListResponse{Devices: devices}) | 	// 构建设备列表项,包含设备状态信息 | ||||||
|  | 	deviceList := make([]DeviceListItem, len(devices)) | ||||||
|  | 	for i, device := range devices { | ||||||
|  | 		// 从设备状态池获取设备状态,默认为非激活状态 | ||||||
|  | 		active := false | ||||||
|  | 		if status, exists := c.deviceStatusPool.GetStatus(strconv.FormatUint(uint64(device.ID), 10)); exists { | ||||||
|  | 			active = status.Active | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		deviceList[i] = DeviceListItem{ | ||||||
|  | 			Device: device, | ||||||
|  | 			Active: active, | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	controller.SendSuccessResponse(ctx, "获取设备列表成功", ListResponse{Devices: deviceList}) | ||||||
| } | } | ||||||
|  |  | ||||||
| // Create 创建设备 | // Create 创建设备 | ||||||
| @@ -290,6 +311,12 @@ type SwitchResponseData struct { | |||||||
| 	Message    string `json:"message"` // 添加消息字段 | 	Message    string `json:"message"` // 添加消息字段 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // DeviceStatusResponse 设备状态响应结构体 | ||||||
|  | type DeviceStatusResponse struct { | ||||||
|  | 	DeviceID string `json:"device_id"` | ||||||
|  | 	Active   bool   `json:"active"` | ||||||
|  | } | ||||||
|  |  | ||||||
| // RelayControlData 发送给中继设备的控制数据结构体 | // RelayControlData 发送给中继设备的控制数据结构体 | ||||||
| type RelayControlData struct { | type RelayControlData struct { | ||||||
| 	DeviceType string `json:"device_type"` | 	DeviceType string `json:"device_type"` | ||||||
| @@ -438,3 +465,26 @@ func (c *Controller) createDeviceControlRecord(userID uint, deviceID, deviceType | |||||||
|  |  | ||||||
| 	return c.deviceControlRepo.Create(control) | 	return c.deviceControlRepo.Create(control) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // GetDeviceStatus 获取设备当前状态 | ||||||
|  | func (c *Controller) GetDeviceStatus(ctx *gin.Context) { | ||||||
|  | 	deviceID := ctx.Query("device_id") | ||||||
|  | 	if deviceID == "" { | ||||||
|  | 		controller.SendErrorResponse(ctx, controller.InvalidParameterCode, "设备ID不能为空") | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// 从设备状态池中获取设备状态 | ||||||
|  | 	status, exists := c.deviceStatusPool.GetStatus(deviceID) | ||||||
|  | 	if !exists { | ||||||
|  | 		controller.SendErrorResponse(ctx, controller.NotFoundCode, "设备状态不存在") | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	response := DeviceStatusResponse{ | ||||||
|  | 		DeviceID: deviceID, | ||||||
|  | 		Active:   status.Active, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	controller.SendSuccessResponse(ctx, "获取设备状态成功", response) | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user