1. 增加新类型中继设备

2. 优化代码
This commit is contained in:
2025-09-08 12:27:54 +08:00
parent bab44e7e3a
commit c7e2aaee89
7 changed files with 285 additions and 207 deletions

View File

@@ -6,6 +6,7 @@ import (
"net/http"
"git.huangwc.com/pig/pig-farm-controller/internal/api/middleware"
"git.huangwc.com/pig/pig-farm-controller/internal/controller"
"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"
@@ -28,80 +29,130 @@ func NewController(deviceControlRepo repository.DeviceControlRepo, deviceRepo re
}
}
// ControlRequest 设备控制请求结构体
type ControlRequest struct {
// SwitchRequest 设备控制请求结构体
type SwitchRequest 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"`
}
// SwitchResponseData 设备控制响应数据结构体
type SwitchResponseData struct {
DeviceType string `json:"device_type"`
DeviceID string `json:"device_id"`
Action string `json:"action"`
}
// Switch 设备控制接口
func (c *Controller) Switch(ctx *gin.Context) {
// 从上下文中获取用户信息
userValue, exists := ctx.Get("user")
if !exists {
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "无法获取用户信息"})
controller.SendErrorResponse(ctx, http.StatusUnauthorized, "无法获取用户信息")
return
}
user, ok := userValue.(*middleware.AuthUser)
if !ok {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "用户信息格式错误"})
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "用户信息格式错误")
return
}
var req ControlRequest
var req SwitchRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "请求参数错误"})
controller.SendErrorResponse(ctx, http.StatusBadRequest, "请求参数错误")
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 {
if err := c.createDeviceControlRecord(
user.ID,
req.DeviceID,
req.DeviceType,
req.Action,
"success",
"设备控制成功",
); err != nil {
c.logger.Error("创建设备控制记录失败: " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "设备控制失败"})
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "设备控制失败")
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,
},
})
data := SwitchResponseData{
DeviceType: req.DeviceType,
DeviceID: req.DeviceID,
Action: req.Action,
}
controller.SendSuccessResponse(ctx, "设备控制成功", data)
}
// createDeviceControlRecord 创建设备控制记录
func (c *Controller) createDeviceControlRecord(userID uint, deviceID, deviceType, action, status, result string) error {
// 获取设备信息
device, err := c.deviceRepo.FindByIDString(deviceID)
if err != nil {
return err
}
// 构建位置信息
var location string
switch device.Type {
case model.DeviceTypeRelay:
// 如果设备本身就是中继设备
location = device.Name
case model.DeviceTypePigPenController, model.DeviceTypeFeedMillController:
// 如果设备本身就是区域主控设备
if device.ParentID != nil {
// 获取中继设备
relayDevice, err := c.deviceRepo.FindByID(*device.ParentID)
if err != nil {
location = device.Name
} else {
location = relayDevice.Name + " -> " + device.Name
}
} else {
location = device.Name
}
default:
// 如果是普通设备(风机、水帘等)
if device.ParentID != nil {
// 获取区域主控设备
parentDevice, err := c.deviceRepo.FindByID(*device.ParentID)
if err != nil {
location = "未知区域"
} else {
// 检查区域主控设备是否有上级设备(中继设备)
if parentDevice.ParentID != nil {
// 获取中继设备
relayDevice, err := c.deviceRepo.FindByID(*parentDevice.ParentID)
if err != nil {
location = parentDevice.Name
} else {
location = relayDevice.Name + " -> " + parentDevice.Name
}
} else {
location = parentDevice.Name
}
}
} else {
location = "未知区域"
}
}
control := &model.DeviceControl{
UserID: userID,
Location: location,
DeviceType: model.DeviceType(deviceType),
DeviceID: deviceID,
Action: action,
Status: status,
Result: result,
}
return c.deviceControlRepo.Create(control)
}