校验device和task的配置的地方全部用内置方法

This commit is contained in:
2025-09-27 01:03:58 +08:00
parent aed665b6b0
commit 5d1b642cc8
2 changed files with 18 additions and 13 deletions

View File

@@ -143,6 +143,13 @@ func (c *Controller) CreateDevice(ctx *gin.Context) {
Properties: propertiesJSON,
}
// 在创建设备前进行自检
if !device.SelfCheck() {
c.logger.Errorf("创建设备: 设备属性自检失败: %v", device)
controller.SendErrorResponse(ctx, controller.CodeBadRequest, "设备属性不符合要求")
return
}
if err := c.repo.Create(device); err != nil {
c.logger.Errorf("创建设备: 数据库操作失败: %v", err)
controller.SendErrorResponse(ctx, controller.CodeInternalError, "创建设备失败")
@@ -272,6 +279,13 @@ func (c *Controller) UpdateDevice(ctx *gin.Context) {
existingDevice.Location = req.Location
existingDevice.Properties = propertiesJSON
// 在更新设备前进行自检
if !existingDevice.SelfCheck() {
c.logger.Errorf("更新设备: 设备属性自检失败: %v", existingDevice)
controller.SendErrorResponse(ctx, controller.CodeBadRequest, "设备属性不符合要求")
return
}
// 4. 将修改后的 existingDevice 对象保存回数据库
if err := c.repo.Update(existingDevice); err != nil {
c.logger.Errorf("更新设备: 数据库操作失败: %v", err)