校验device和task的配置的地方全部用内置方法
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -57,19 +57,10 @@ func (g *GeneralDeviceService) Switch(device *models.Device, action DeviceAction
|
||||
return fmt.Errorf("解析设备 %v(id=%v) 配置失败: %v", device.Name, device.ID, err)
|
||||
}
|
||||
|
||||
// TODO 这种校验放自检里
|
||||
busNumber, err := strconv.Atoi(fmt.Sprintf("%v", deviceInfo[models.BusNumber]))
|
||||
if err != nil {
|
||||
return fmt.Errorf("无效的总线号: %v", err)
|
||||
}
|
||||
busAddress, err := strconv.Atoi(fmt.Sprintf("%v", deviceInfo[models.BusAddress]))
|
||||
if err != nil {
|
||||
return fmt.Errorf("无效的总线地址: %v", err)
|
||||
}
|
||||
relayChannel, err := strconv.Atoi(fmt.Sprintf("%v", deviceInfo[models.RelayChannel]))
|
||||
if err != nil {
|
||||
return fmt.Errorf("无效的继电器通道: %v", err)
|
||||
}
|
||||
// TODO 这种校验放自检里 (已通过 SelfCheck 保证其为纯数字,此处仅进行类型转换)
|
||||
busNumber, _ := strconv.Atoi(fmt.Sprintf("%v", deviceInfo[models.BusNumber]))
|
||||
busAddress, _ := strconv.Atoi(fmt.Sprintf("%v", deviceInfo[models.BusAddress]))
|
||||
relayChannel, _ := strconv.Atoi(fmt.Sprintf("%v", deviceInfo[models.RelayChannel]))
|
||||
|
||||
data, err := anypb.New(&proto.Switch{
|
||||
DeviceAction: string(action),
|
||||
|
||||
Reference in New Issue
Block a user