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

321 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 <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. LogicNode counter_input;
  174. counter_input.id = "poll-counter-input";
  175. counter_input.config = ContactNodeConfig{
  176. RegisterAddress{RegisterArea::M, 25}, ContactMode::NormallyOpen};
  177. LogicNode counter_output;
  178. counter_output.id = "poll-counter";
  179. counter_output.config = CounterNodeConfig{
  180. CounterAddress{2},
  181. CounterMode::Up,
  182. RegisterAddress{RegisterArea::D, 36},
  183. WordOperand{
  184. WordOperandKind::Register,
  185. RegisterAddress{RegisterArea::D, 37},
  186. 0},
  187. RegisterAddress{RegisterArea::M, 26}};
  188. LadderRung counter_rung;
  189. counter_rung.id = "poll-counter-rung";
  190. counter_rung.name = "Poll counter";
  191. counter_rung.condition = ConditionExpression::fromNode(counter_input);
  192. counter_rung.output = counter_output;
  193. logic.rungs.push_back(counter_rung);
  194. LogicNode move_input;
  195. move_input.id = "poll-move-input";
  196. move_input.config = ContactNodeConfig{
  197. RegisterAddress{RegisterArea::M, 27}, ContactMode::NormallyOpen};
  198. LogicNode move_output;
  199. move_output.id = "poll-move";
  200. move_output.config = MoveNodeConfig{
  201. WordOperand{
  202. WordOperandKind::Register,
  203. RegisterAddress{RegisterArea::D, 38},
  204. 0},
  205. RegisterAddress{RegisterArea::D, 39}};
  206. LadderRung move_rung;
  207. move_rung.id = "poll-move-rung";
  208. move_rung.name = "Poll MOVE";
  209. move_rung.condition = ConditionExpression::fromNode(move_input);
  210. move_rung.output = move_output;
  211. logic.rungs.push_back(move_rung);
  212. LogicNode add_input;
  213. add_input.id = "poll-add-input";
  214. add_input.config = ContactNodeConfig{
  215. RegisterAddress{RegisterArea::M, 28}, ContactMode::NormallyOpen};
  216. LogicNode add_output;
  217. add_output.id = "poll-add";
  218. add_output.config = ArithmeticNodeConfig{
  219. ArithmeticOperation::Add,
  220. WordOperand{
  221. WordOperandKind::Register,
  222. RegisterAddress{RegisterArea::D, 40},
  223. 0},
  224. WordOperand{
  225. WordOperandKind::Constant,
  226. RegisterAddress{RegisterArea::D, 0},
  227. 1},
  228. RegisterAddress{RegisterArea::D, 41}};
  229. LadderRung add_rung;
  230. add_rung.id = "poll-add-rung";
  231. add_rung.name = "Poll ADD";
  232. add_rung.condition = ConditionExpression::fromNode(add_input);
  233. add_rung.output = add_output;
  234. logic.rungs.push_back(add_rung);
  235. project.controlLogics.push_back(logic);
  236. require(service.mode() == ApplicationMode::Editing,
  237. "service must start in editing mode");
  238. require(service.policy().allowsProjectEditing,
  239. "editing mode must allow project editing");
  240. require(service.enterOfflineRunning().succeeded,
  241. "editing mode must enter offline running");
  242. require(service.simulationState() == SimulationState::Running,
  243. "offline mode must start the software executor");
  244. require(service.policy().usesVirtualRegisters,
  245. "offline running must use virtual registers");
  246. require(service.enterOnlineRunning().error
  247. == ModeTransitionError::MustReturnToEditing,
  248. "running modes must not switch directly");
  249. require(service.enterEditing().succeeded,
  250. "offline running must return to editing");
  251. require(service.simulationState() == SimulationState::Stopped,
  252. "returning to editing must stop the software executor first");
  253. require(service.enterOnlineRunning().error
  254. == ModeTransitionError::InitialPlcReadRequired,
  255. "online running must require an initial PLC read");
  256. require(service.connectPlc(
  257. {"COM9", 1, 9600, 8, 2, 1, 1000, 2, 200}).succeeded,
  258. "PLC connection must be established before its initial read can complete");
  259. require(gateway.pollAddresses()
  260. == std::vector<RegisterAddress>({
  261. {RegisterArea::M, 12}, {RegisterArea::M, 20},
  262. {RegisterArea::M, 21}, {RegisterArea::M, 22},
  263. {RegisterArea::M, 23}, {RegisterArea::M, 24},
  264. {RegisterArea::M, 25}, {RegisterArea::M, 26},
  265. {RegisterArea::M, 27}, {RegisterArea::M, 28},
  266. {RegisterArea::D, 34}, {RegisterArea::D, 35},
  267. {RegisterArea::D, 36}, {RegisterArea::D, 37},
  268. {RegisterArea::D, 38}, {RegisterArea::D, 39},
  269. {RegisterArea::D, 40}, {RegisterArea::D, 41}}),
  270. "all instruction M/D references must be polled while T/C resources and comments stay offline-only");
  271. gateway.completeInitialRead();
  272. require(service.initialPlcReadCompleted(),
  273. "service must retain the initial PLC read state");
  274. require(service.enterOnlineRunning().succeeded,
  275. "online running must start after an initial PLC read");
  276. require(service.simulationState() == SimulationState::Stopped,
  277. "online running must never start the software executor");
  278. require(service.policy().usesPlcRegisters,
  279. "online running must use PLC registers");
  280. require(!service.policy().runsLogicExecutor,
  281. "online running must keep the software executor stopped");
  282. }
  283. } // namespace
  284. int main()
  285. {
  286. try
  287. {
  288. // 运行模式只有这一组状态机边界测试
  289. testModeTransitions();
  290. }
  291. catch (const std::exception &error)
  292. {
  293. std::cerr << "runtime mode service tests failed: " << error.what() << '\n';
  294. return 1;
  295. }
  296. std::cout << "runtime mode service tests passed\n";
  297. return 0;
  298. }