2025-09-07 21:33:08 +08:00
|
|
|
|
// 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
|
2025-09-08 10:58:26 +08:00
|
|
|
|
deviceRepo repository.DeviceRepo
|
2025-09-07 21:33:08 +08:00
|
|
|
|
logger *logs.Logger
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewController 创建设备控制控制器实例
|
2025-09-08 10:58:26 +08:00
|
|
|
|
func NewController(deviceControlRepo repository.DeviceControlRepo, deviceRepo repository.DeviceRepo) *Controller {
|
2025-09-07 21:33:08 +08:00
|
|
|
|
return &Controller{
|
|
|
|
|
|
deviceControlRepo: deviceControlRepo,
|
2025-09-08 10:58:26 +08:00
|
|
|
|
deviceRepo: deviceRepo,
|
2025-09-07 21:33:08 +08:00
|
|
|
|
logger: logs.NewLogger(),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ControlRequest 设备控制请求结构体
|
|
|
|
|
|
type ControlRequest struct {
|
2025-09-08 10:58:26 +08:00
|
|
|
|
ParentID *uint `json:"parent_id"` // 区域主控ID
|
2025-09-07 21:33:08 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-08 10:58:26 +08:00
|
|
|
|
// 获取区域主控设备信息(如果提供了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 = "未知区域"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-07 21:33:08 +08:00
|
|
|
|
// TODO: 实际的设备控制逻辑
|
|
|
|
|
|
// 这里暂时用TODO代替具体逻辑
|
|
|
|
|
|
|
|
|
|
|
|
// 创建设备控制记录
|
|
|
|
|
|
control := &model.DeviceControl{
|
|
|
|
|
|
UserID: user.ID,
|
2025-09-08 10:58:26 +08:00
|
|
|
|
Location: location,
|
2025-09-07 21:33:08 +08:00
|
|
|
|
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,
|
2025-09-08 10:58:26 +08:00
|
|
|
|
"location": control.Location,
|
2025-09-07 21:33:08 +08:00
|
|
|
|
"device_type": control.DeviceType,
|
|
|
|
|
|
"device_id": control.DeviceID,
|
|
|
|
|
|
"action": control.Action,
|
|
|
|
|
|
"status": control.Status,
|
|
|
|
|
|
"result": control.Result,
|
|
|
|
|
|
"created_at": control.CreatedAt,
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|