综合平台编程器项目的远程存储
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

72 lines
1.9 KiB

  1. #include "alarm_model.h"
  2. #include "project_limits.h"
  3. namespace {
  4. // 在调用方提供错误字符串时保存校验失败原因
  5. void setError(std::string *error, const std::string &message)
  6. {
  7. // 错误字符串是可选输出,不提供时只返回校验结果
  8. if (error != nullptr)
  9. {
  10. *error = message;
  11. }
  12. }
  13. } // namespace
  14. // 校验报警定义的文本、地址和触发条件是否匹配
  15. bool AlarmDefinition::validate(std::string *error) const
  16. {
  17. if (id.empty())
  18. {
  19. setError(error, "报警定义 ID 不能为空");
  20. return false;
  21. }
  22. if (id.size() > ProjectLimits::kMaximumIdBytes)
  23. {
  24. setError(error, "报警定义 ID 不能超过 128 个 UTF-8 字节");
  25. return false;
  26. }
  27. if (message.empty())
  28. {
  29. setError(error, "报警文本不能为空");
  30. return false;
  31. }
  32. if (message.size() > ProjectLimits::kMaximumTextBytes)
  33. {
  34. setError(error, "报警文本不能超过 256 个 UTF-8 字节");
  35. return false;
  36. }
  37. if (!address.isValid())
  38. {
  39. setError(error, "报警定义使用了无效地址");
  40. return false;
  41. }
  42. // M ON/OFF 只能监控 M 区,避免把位条件用于 D 字地址
  43. if (condition == AlarmCondition::MOn
  44. || condition == AlarmCondition::MOff)
  45. {
  46. if (address.area() != RegisterArea::M)
  47. {
  48. setError(error, "M ON/OFF 报警必须使用 M 地址");
  49. return false;
  50. }
  51. return true;
  52. }
  53. // D 高低限只能监控 D 区,阈值字段由运行时服务解释
  54. if (condition == AlarmCondition::DHigh
  55. || condition == AlarmCondition::DLow)
  56. {
  57. if (address.area() != RegisterArea::D)
  58. {
  59. setError(error, "D 高限和低限报警必须使用 D 地址");
  60. return false;
  61. }
  62. return true;
  63. }
  64. setError(error, "不支持的报警触发条件");
  65. return false;
  66. }