1. 操作历史增删改查
2. 定义设备开关接口(只有定义)
This commit is contained in:
91
internal/controller/device/device.go
Normal file
91
internal/controller/device/device.go
Normal file
@@ -0,0 +1,91 @@
|
||||
// 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
|
||||
logger *logs.Logger
|
||||
}
|
||||
|
||||
// NewController 创建设备控制控制器实例
|
||||
func NewController(deviceControlRepo repository.DeviceControlRepo) *Controller {
|
||||
return &Controller{
|
||||
deviceControlRepo: deviceControlRepo,
|
||||
logger: logs.NewLogger(),
|
||||
}
|
||||
}
|
||||
|
||||
// ControlRequest 设备控制请求结构体
|
||||
type ControlRequest struct {
|
||||
PigPenID string `json:"pig_pen_id" binding:"required"`
|
||||
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
|
||||
}
|
||||
|
||||
// TODO: 实际的设备控制逻辑
|
||||
// 这里暂时用TODO代替具体逻辑
|
||||
|
||||
// 创建设备控制记录
|
||||
control := &model.DeviceControl{
|
||||
UserID: user.ID,
|
||||
PigPenID: req.PigPenID,
|
||||
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,
|
||||
"pig_pen_id": control.PigPenID,
|
||||
"device_type": control.DeviceType,
|
||||
"device_id": control.DeviceID,
|
||||
"action": control.Action,
|
||||
"status": control.Status,
|
||||
"result": control.Result,
|
||||
"created_at": control.CreatedAt,
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user