综合平台编程器项目的远程存储
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

254 satır
9.0 KiB

  1. #include "services/runtime_mode_service.h"
  2. #include "services/offline_simulation_service.h"
  3. #include "services/project_service.h"
  4. #include "domain/active_register_repository.h"
  5. #include "domain/project_storage.h"
  6. #include "domain/register_repository.h"
  7. #include <functional>
  8. #include <iostream>
  9. #include <stdexcept>
  10. #include <string>
  11. #include <utility>
  12. namespace {
  13. class TestProjectStorage final : public ProjectStorage
  14. {
  15. public:
  16. ProjectSaveResult save(const Project &, const std::string &) override
  17. {
  18. return {true, ProjectStorageError::None, {}};
  19. }
  20. ProjectLoadResult load(const std::string &) override
  21. {
  22. return {false, {}, ProjectStorageError::FileReadFailed, {}};
  23. }
  24. };
  25. class ReadyPlcGateway final : public PlcCommunicationGateway
  26. {
  27. public:
  28. PlcCommunicationResult connectDevice(const PlcSerialConfiguration &) override
  29. {
  30. connection_state = PlcConnectionState::Connected;
  31. if (state_changed)
  32. {
  33. state_changed();
  34. }
  35. return {true, {}};
  36. }
  37. void disconnectDevice() override
  38. {
  39. connection_state = PlcConnectionState::Disconnected;
  40. initial_read = false;
  41. }
  42. void setPollAddresses(
  43. const std::vector<RegisterAddress> &addresses) override
  44. {
  45. poll_addresses = addresses;
  46. }
  47. PlcConnectionState state() const override { return connection_state; }
  48. bool initialReadCompleted() const override { return initial_read; }
  49. PlcCommunicationError lastErrorType() const override
  50. {
  51. return PlcCommunicationError::None;
  52. }
  53. const std::string &lastError() const override { return last_error; }
  54. void setCallbacks(
  55. std::function<void()> state_callback,
  56. std::function<void(bool)> initial_callback,
  57. std::function<void()> cache_callback,
  58. std::function<void(const std::string &)> error_callback) override
  59. {
  60. state_changed = std::move(state_callback);
  61. initial_read_changed = std::move(initial_callback);
  62. cache_updated = std::move(cache_callback);
  63. error_reported = std::move(error_callback);
  64. }
  65. void completeInitialRead()
  66. {
  67. initial_read = true;
  68. if (initial_read_changed)
  69. {
  70. initial_read_changed(true);
  71. }
  72. }
  73. const std::vector<RegisterAddress> &pollAddresses() const
  74. {
  75. return poll_addresses;
  76. }
  77. private:
  78. PlcConnectionState connection_state = PlcConnectionState::Disconnected;
  79. bool initial_read = false;
  80. std::string last_error;
  81. std::function<void()> state_changed;
  82. std::function<void(bool)> initial_read_changed;
  83. std::function<void()> cache_updated;
  84. std::function<void(const std::string &)> error_reported;
  85. std::vector<RegisterAddress> poll_addresses;
  86. };
  87. void require(bool condition, const std::string &message)
  88. {
  89. if (!condition)
  90. {
  91. throw std::runtime_error(message);
  92. }
  93. }
  94. void testModeTransitions()
  95. {
  96. // 验证服务将 PLC 首读状态与领域模式切换规则正确组合
  97. TestProjectStorage storage;
  98. ProjectService project_service(storage);
  99. VirtualRegisterRepository virtual_repository;
  100. VirtualRegisterRepository plc_repository;
  101. ActiveRegisterRepository active_repository(virtual_repository);
  102. OfflineSimulationService simulation_service(virtual_repository);
  103. ReadyPlcGateway gateway;
  104. RuntimeModeService service(project_service, simulation_service);
  105. service.configurePlc(
  106. gateway, active_repository, virtual_repository, plc_repository);
  107. Project &project = project_service.editProject();
  108. project.alarmDefinitions.push_back(
  109. {"alarm-m", {RegisterArea::M, 12}, AlarmCondition::MOn, 0, "M alarm"});
  110. project.alarmDefinitions.push_back(
  111. {"alarm-d", {RegisterArea::D, 34}, AlarmCondition::DHigh, 100, "D alarm"});
  112. project.registerComments = {
  113. {RegisterAddress{RegisterArea::M, 3999}, "只用于说明"},
  114. {RegisterAddress{RegisterArea::D, 3999}, "只用于说明"}};
  115. ControlLogic logic;
  116. logic.id = "poll-logic";
  117. logic.name = "Poll logic";
  118. LogicNode edge;
  119. edge.id = "poll-edge";
  120. edge.config = EdgeContactNodeConfig{
  121. RegisterAddress{RegisterArea::M, 20}, EdgeMode::Rising};
  122. LogicNode edge_coil;
  123. edge_coil.id = "poll-edge-coil";
  124. edge_coil.config = CoilNodeConfig{
  125. RegisterAddress{RegisterArea::M, 21}, CoilMode::Normal};
  126. LadderRung edge_rung;
  127. edge_rung.id = "poll-edge-rung";
  128. edge_rung.name = "Poll edge";
  129. edge_rung.condition = ConditionExpression::fromNode(edge);
  130. edge_rung.output = edge_coil;
  131. logic.rungs.push_back(edge_rung);
  132. LogicNode comparison;
  133. comparison.id = "poll-comparison";
  134. comparison.config = CompareNodeConfig{
  135. RegisterAddress{RegisterArea::D, 35}, ComparisonOperator::GreaterThan, 0};
  136. LogicNode comparison_coil;
  137. comparison_coil.id = "poll-comparison-coil";
  138. comparison_coil.config = CoilNodeConfig{
  139. RegisterAddress{RegisterArea::M, 22}, CoilMode::Normal};
  140. LadderRung comparison_rung;
  141. comparison_rung.id = "poll-comparison-rung";
  142. comparison_rung.name = "Poll comparison";
  143. comparison_rung.condition = ConditionExpression::fromNode(comparison);
  144. comparison_rung.output = comparison_coil;
  145. logic.rungs.push_back(comparison_rung);
  146. LogicNode timer_input;
  147. timer_input.id = "poll-timer-input";
  148. timer_input.config = ContactNodeConfig{
  149. RegisterAddress{RegisterArea::M, 23}, ContactMode::NormallyOpen};
  150. LogicNode ton;
  151. ton.id = "poll-ton";
  152. ton.config = TonNodeConfig{TimerAddress{4}, 100};
  153. LadderRung ton_rung;
  154. ton_rung.id = "poll-ton-rung";
  155. ton_rung.name = "Poll TON";
  156. ton_rung.condition = ConditionExpression::fromNode(timer_input);
  157. ton_rung.output = ton;
  158. logic.rungs.push_back(ton_rung);
  159. LogicNode timer_contact;
  160. timer_contact.id = "poll-timer-contact";
  161. timer_contact.config = TimerContactNodeConfig{
  162. TimerAddress{4}, ContactMode::NormallyOpen};
  163. LogicNode timer_coil;
  164. timer_coil.id = "poll-timer-coil";
  165. timer_coil.config = CoilNodeConfig{
  166. RegisterAddress{RegisterArea::M, 24}, CoilMode::Normal};
  167. LadderRung timer_rung;
  168. timer_rung.id = "poll-timer-rung";
  169. timer_rung.name = "Poll timer contact";
  170. timer_rung.condition = ConditionExpression::fromNode(timer_contact);
  171. timer_rung.output = timer_coil;
  172. logic.rungs.push_back(timer_rung);
  173. project.controlLogics.push_back(logic);
  174. require(service.mode() == ApplicationMode::Editing,
  175. "service must start in editing mode");
  176. require(service.policy().allowsProjectEditing,
  177. "editing mode must allow project editing");
  178. require(service.enterOfflineRunning().succeeded,
  179. "editing mode must enter offline running");
  180. require(service.simulationState() == SimulationState::Running,
  181. "offline mode must start the software executor");
  182. require(service.policy().usesVirtualRegisters,
  183. "offline running must use virtual registers");
  184. require(service.enterOnlineRunning().error
  185. == ModeTransitionError::MustReturnToEditing,
  186. "running modes must not switch directly");
  187. require(service.enterEditing().succeeded,
  188. "offline running must return to editing");
  189. require(service.simulationState() == SimulationState::Stopped,
  190. "returning to editing must stop the software executor first");
  191. require(service.enterOnlineRunning().error
  192. == ModeTransitionError::InitialPlcReadRequired,
  193. "online running must require an initial PLC read");
  194. require(service.connectPlc(
  195. {"COM9", 1, 9600, 8, 2, 1, 1000, 2, 200}).succeeded,
  196. "PLC connection must be established before its initial read can complete");
  197. require(gateway.pollAddresses()
  198. == std::vector<RegisterAddress>({
  199. {RegisterArea::M, 12}, {RegisterArea::M, 20},
  200. {RegisterArea::M, 21}, {RegisterArea::M, 22},
  201. {RegisterArea::M, 23}, {RegisterArea::M, 24},
  202. {RegisterArea::D, 34}, {RegisterArea::D, 35}}),
  203. "M/D logic and alarm addresses must be polled while T and comments stay offline-only");
  204. gateway.completeInitialRead();
  205. require(service.initialPlcReadCompleted(),
  206. "service must retain the initial PLC read state");
  207. require(service.enterOnlineRunning().succeeded,
  208. "online running must start after an initial PLC read");
  209. require(service.simulationState() == SimulationState::Stopped,
  210. "online running must never start the software executor");
  211. require(service.policy().usesPlcRegisters,
  212. "online running must use PLC registers");
  213. require(!service.policy().runsLogicExecutor,
  214. "online running must keep the software executor stopped");
  215. }
  216. } // namespace
  217. int main()
  218. {
  219. try
  220. {
  221. // 运行模式只有这一组状态机边界测试
  222. testModeTransitions();
  223. }
  224. catch (const std::exception &error)
  225. {
  226. std::cerr << "runtime mode service tests failed: " << error.what() << '\n';
  227. return 1;
  228. }
  229. std::cout << "runtime mode service tests passed\n";
  230. return 0;
  231. }