From 75f9e07fccd4b83603d8902cab2b599ccdab601f Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Mon, 8 Sep 2025 23:41:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=AE=BE=E5=A4=87=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=8E=A5=E5=8F=A3=E5=A2=9E=E5=8A=A0=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/api/api.go | 1 + internal/controller/device/device.go | 54 ++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/internal/api/api.go b/internal/api/api.go index 12db0e4..e1787b7 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -222,6 +222,7 @@ func (a *API) setupRoutes() { deviceGroup.POST("/create", a.deviceController.Create) deviceGroup.POST("/update", a.deviceController.Update) deviceGroup.POST("/delete", a.deviceController.Delete) + deviceGroup.GET("/status", a.deviceController.GetDeviceStatus) } // 远程控制相关路由 diff --git a/internal/controller/device/device.go b/internal/controller/device/device.go index 36b4480..0892e57 100644 --- a/internal/controller/device/device.go +++ b/internal/controller/device/device.go @@ -17,7 +17,13 @@ import ( // ListResponse 设备列表响应结构体 type ListResponse struct { - Devices []model.Device `json:"devices"` + Devices []DeviceListItem `json:"devices"` +} + +// DeviceListItem 设备列表项结构体 +type DeviceListItem struct { + model.Device + Active bool `json:"active"` } // DeviceRequest 设备创建/更新请求结构体 @@ -140,7 +146,22 @@ func (c *Controller) List(ctx *gin.Context) { 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 创建设备 @@ -290,6 +311,12 @@ type SwitchResponseData struct { Message string `json:"message"` // 添加消息字段 } +// DeviceStatusResponse 设备状态响应结构体 +type DeviceStatusResponse struct { + DeviceID string `json:"device_id"` + Active bool `json:"active"` +} + // RelayControlData 发送给中继设备的控制数据结构体 type RelayControlData struct { DeviceType string `json:"device_type"` @@ -438,3 +465,26 @@ func (c *Controller) createDeviceControlRecord(userID uint, deviceID, deviceType 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) +}