1. 增加程序启动时自动迁移表
2. 增加设备信息model和基础操作函数
This commit is contained in:
@@ -15,20 +15,22 @@ import (
|
||||
// Controller 设备控制控制器
|
||||
type Controller struct {
|
||||
deviceControlRepo repository.DeviceControlRepo
|
||||
deviceRepo repository.DeviceRepo
|
||||
logger *logs.Logger
|
||||
}
|
||||
|
||||
// NewController 创建设备控制控制器实例
|
||||
func NewController(deviceControlRepo repository.DeviceControlRepo) *Controller {
|
||||
func NewController(deviceControlRepo repository.DeviceControlRepo, deviceRepo repository.DeviceRepo) *Controller {
|
||||
return &Controller{
|
||||
deviceControlRepo: deviceControlRepo,
|
||||
deviceRepo: deviceRepo,
|
||||
logger: logs.NewLogger(),
|
||||
}
|
||||
}
|
||||
|
||||
// ControlRequest 设备控制请求结构体
|
||||
type ControlRequest struct {
|
||||
PigPenID string `json:"pig_pen_id" binding:"required"`
|
||||
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"`
|
||||
@@ -55,13 +57,27 @@ func (c *Controller) Switch(ctx *gin.Context) {
|
||||
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,
|
||||
PigPenID: req.PigPenID,
|
||||
Location: location,
|
||||
DeviceType: model.DeviceType(req.DeviceType),
|
||||
DeviceID: req.DeviceID,
|
||||
Action: req.Action,
|
||||
@@ -79,7 +95,7 @@ func (c *Controller) Switch(ctx *gin.Context) {
|
||||
"message": "设备控制成功",
|
||||
"data": map[string]interface{}{
|
||||
"id": control.ID,
|
||||
"pig_pen_id": control.PigPenID,
|
||||
"location": control.Location,
|
||||
"device_type": control.DeviceType,
|
||||
"device_id": control.DeviceID,
|
||||
"action": control.Action,
|
||||
|
||||
Reference in New Issue
Block a user