issue_18 #19
| @@ -143,6 +143,13 @@ func (c *Controller) CreateDevice(ctx *gin.Context) { | |||||||
| 		Properties: propertiesJSON, | 		Properties: propertiesJSON, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	// 在创建设备前进行自检 | ||||||
|  | 	if !device.SelfCheck() { | ||||||
|  | 		c.logger.Errorf("创建设备: 设备属性自检失败: %v", device) | ||||||
|  | 		controller.SendErrorResponse(ctx, controller.CodeBadRequest, "设备属性不符合要求") | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	if err := c.repo.Create(device); err != nil { | 	if err := c.repo.Create(device); err != nil { | ||||||
| 		c.logger.Errorf("创建设备: 数据库操作失败: %v", err) | 		c.logger.Errorf("创建设备: 数据库操作失败: %v", err) | ||||||
| 		controller.SendErrorResponse(ctx, controller.CodeInternalError, "创建设备失败") | 		controller.SendErrorResponse(ctx, controller.CodeInternalError, "创建设备失败") | ||||||
| @@ -272,6 +279,13 @@ func (c *Controller) UpdateDevice(ctx *gin.Context) { | |||||||
| 	existingDevice.Location = req.Location | 	existingDevice.Location = req.Location | ||||||
| 	existingDevice.Properties = propertiesJSON | 	existingDevice.Properties = propertiesJSON | ||||||
|  |  | ||||||
|  | 	// 在更新设备前进行自检 | ||||||
|  | 	if !existingDevice.SelfCheck() { | ||||||
|  | 		c.logger.Errorf("更新设备: 设备属性自检失败: %v", existingDevice) | ||||||
|  | 		controller.SendErrorResponse(ctx, controller.CodeBadRequest, "设备属性不符合要求") | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	// 4. 将修改后的 existingDevice 对象保存回数据库 | 	// 4. 将修改后的 existingDevice 对象保存回数据库 | ||||||
| 	if err := c.repo.Update(existingDevice); err != nil { | 	if err := c.repo.Update(existingDevice); err != nil { | ||||||
| 		c.logger.Errorf("更新设备: 数据库操作失败: %v", err) | 		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) | 		return fmt.Errorf("解析设备 %v(id=%v) 配置失败: %v", device.Name, device.ID, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// TODO 这种校验放自检里 | 	// TODO 这种校验放自检里 (已通过 SelfCheck 保证其为纯数字,此处仅进行类型转换) | ||||||
| 	busNumber, err := strconv.Atoi(fmt.Sprintf("%v", deviceInfo[models.BusNumber])) | 	busNumber, _ := strconv.Atoi(fmt.Sprintf("%v", deviceInfo[models.BusNumber])) | ||||||
| 	if err != nil { | 	busAddress, _ := strconv.Atoi(fmt.Sprintf("%v", deviceInfo[models.BusAddress])) | ||||||
| 		return fmt.Errorf("无效的总线号: %v", err) | 	relayChannel, _ := strconv.Atoi(fmt.Sprintf("%v", deviceInfo[models.RelayChannel])) | ||||||
| 	} |  | ||||||
| 	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) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	data, err := anypb.New(&proto.Switch{ | 	data, err := anypb.New(&proto.Switch{ | ||||||
| 		DeviceAction: string(action), | 		DeviceAction: string(action), | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user