108 lines
3.2 KiB
Go
108 lines
3.2 KiB
Go
// Package device 提供设备控制相关功能的控制器
|
||
// 实现设备控制、查询等操作
|
||
package device
|
||
|
||
import (
|
||
"net/http"
|
||
|
||
"git.huangwc.com/pig/pig-farm-controller/internal/api/middleware"
|
||
"git.huangwc.com/pig/pig-farm-controller/internal/logs"
|
||
"git.huangwc.com/pig/pig-farm-controller/internal/model"
|
||
"git.huangwc.com/pig/pig-farm-controller/internal/storage/repository"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// Controller 设备控制控制器
|
||
type Controller struct {
|
||
deviceControlRepo repository.DeviceControlRepo
|
||
deviceRepo repository.DeviceRepo
|
||
logger *logs.Logger
|
||
}
|
||
|
||
// NewController 创建设备控制控制器实例
|
||
func NewController(deviceControlRepo repository.DeviceControlRepo, deviceRepo repository.DeviceRepo) *Controller {
|
||
return &Controller{
|
||
deviceControlRepo: deviceControlRepo,
|
||
deviceRepo: deviceRepo,
|
||
logger: logs.NewLogger(),
|
||
}
|
||
}
|
||
|
||
// ControlRequest 设备控制请求结构体
|
||
type ControlRequest struct {
|
||
ParentID *uint `json:"parent_id"` // 区域主控ID
|
||
DeviceType string `json:"device_type" binding:"required,oneof=fan water_curtain"`
|
||
DeviceID string `json:"device_id" binding:"required"`
|
||
Action string `json:"action" binding:"required,oneof=on off"`
|
||
}
|
||
|
||
// Switch 设备控制接口
|
||
func (c *Controller) Switch(ctx *gin.Context) {
|
||
// 从上下文中获取用户信息
|
||
userValue, exists := ctx.Get("user")
|
||
if !exists {
|
||
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "无法获取用户信息"})
|
||
return
|
||
}
|
||
|
||
user, ok := userValue.(*middleware.AuthUser)
|
||
if !ok {
|
||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "用户信息格式错误"})
|
||
return
|
||
}
|
||
|
||
var req ControlRequest
|
||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "请求参数错误"})
|
||
return
|
||
}
|
||
|
||
// 获取区域主控设备信息(如果提供了ParentID)
|
||
var location string
|
||
if req.ParentID != nil {
|
||
parentDevice, err := c.deviceRepo.FindByID(*req.ParentID)
|
||
if err != nil {
|
||
c.logger.Error("查找区域主控设备失败: " + err.Error())
|
||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "无效的区域主控ID"})
|
||
return
|
||
}
|
||
location = parentDevice.Name
|
||
} else {
|
||
location = "未知区域"
|
||
}
|
||
|
||
// TODO: 实际的设备控制逻辑
|
||
// 这里暂时用TODO代替具体逻辑
|
||
|
||
// 创建设备控制记录
|
||
control := &model.DeviceControl{
|
||
UserID: user.ID,
|
||
Location: location,
|
||
DeviceType: model.DeviceType(req.DeviceType),
|
||
DeviceID: req.DeviceID,
|
||
Action: req.Action,
|
||
Status: "success", // 假设控制成功
|
||
Result: "设备控制成功",
|
||
}
|
||
|
||
if err := c.deviceControlRepo.Create(control); err != nil {
|
||
c.logger.Error("创建设备控制记录失败: " + err.Error())
|
||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "设备控制失败"})
|
||
return
|
||
}
|
||
|
||
ctx.JSON(http.StatusOK, gin.H{
|
||
"message": "设备控制成功",
|
||
"data": map[string]interface{}{
|
||
"id": control.ID,
|
||
"location": control.Location,
|
||
"device_type": control.DeviceType,
|
||
"device_id": control.DeviceID,
|
||
"action": control.Action,
|
||
"status": control.Status,
|
||
"result": control.Result,
|
||
"created_at": control.CreatedAt,
|
||
},
|
||
})
|
||
}
|