综合平台编程器项目的远程存储
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.
 
 
 
 

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