FdcPumpControllerUnitTest.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Edge.Core.Processor;
  4. using Edge.Core.IndustryStandardInterface.Pump;
  5. using System.Threading;
  6. using Wayne.FDCPOSLibrary;
  7. using Mocks;
  8. using LanTian_Pump_664_Or_886.MessageEntity;
  9. using LanTian_Pump_664_Or_886;
  10. using LanTian_Pump_664_Or_886.MessageEntity.Outgoing;
  11. using System.Threading.Tasks;
  12. using LanTian_Pump_664_Or_886.MessageEntity.Incoming;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using Microsoft.Extensions.DependencyInjection;
  16. using static LanTian_Pump_664_Or_886.PumpGroupParameter;
  17. namespace LanTian_Pump_664_Or_886_Test
  18. {
  19. [TestClass]
  20. public class FdcPumpControllerUnitTest
  21. {
  22. private HalfDuplexActivePollingDeviceProcessor<byte[], MessageBase> processor;
  23. private ComPortCommunicatorMock<MessageBase> mockCommunicator;
  24. private IFdcPumpController fdcPumpController;
  25. [TestInitialize()]
  26. public void InitProcessor()
  27. {
  28. var services = new ServiceCollection();
  29. services.AddLogging();
  30. this.mockCommunicator = new ComPortCommunicatorMock<MessageBase>();
  31. mockCommunicator.OnRawDataWriting += (s, a) => { };
  32. this.processor = new HalfDuplexActivePollingDeviceProcessor<byte[], MessageBase>(
  33. new PumpGroupHandler(new PumpGroupParameter()
  34. {
  35. PumpParameters = new List<PumpParameter>()
  36. {
  37. new PumpParameter()
  38. {
  39. PumpModel = PumpModelEnum.Model_664,
  40. PumpAuthorizeMode = PumpAuthorizeModeEnum.FC_Authorize,
  41. PumpId = 1,
  42. Address = 1,
  43. }
  44. }
  45. }, services.BuildServiceProvider()),
  46. mockCommunicator, 100, null);
  47. this.fdcPumpController = (processor.Context.Handler as IEnumerable<IFdcPumpController>).First();
  48. }
  49. [TestMethod]
  50. public async Task Should_Send_Polling_Pump_Message_Test()
  51. {
  52. int pollingMessageSendTimes = 0;
  53. this.processor.Context.Outgoing.OnWriting += (s, a) =>
  54. {
  55. if (a.Message is ReadPumpStateRequest)
  56. pollingMessageSendTimes++;
  57. };
  58. await this.processor.Start();
  59. await Task.Delay(1000);
  60. Assert.AreEqual(true, pollingMessageSendTimes >= 1, "polling message should send more than 1 times");
  61. }
  62. [TestMethod]
  63. public async Task FDC_OFFLINE_State_Test()
  64. {
  65. int onRawDataWriting = 0;
  66. var onStateChangeEventArgs = new List<FdcPumpControllerOnStateChangeEventArg>();
  67. this.fdcPumpController.OnStateChange += (s, a) =>
  68. {
  69. onStateChangeEventArgs.Add(a);
  70. };
  71. this.mockCommunicator.OnRawDataWriting += (a, b) =>
  72. {
  73. onRawDataWriting++;
  74. };
  75. Assert.AreEqual(LogicalDeviceState.FDC_OFFLINE, fdcPumpController.QueryStatusAsync().Result,
  76. "status should change to: FDC_OFFLINE, but now is: " + fdcPumpController.QueryStatusAsync().Result);
  77. Assert.AreEqual(0, onRawDataWriting);
  78. await this.processor.Start();
  79. Assert.AreEqual(LogicalDeviceState.FDC_OFFLINE, this.fdcPumpController.QueryStatusAsync().Result,
  80. "status should be: FDC_OFFLINE, but now is: " + this.fdcPumpController.QueryStatusAsync().Result);
  81. this.fdcPumpController.OnFdcServerInit(new Dictionary<string, object>());
  82. await Task.Delay(500);
  83. Assert.AreEqual(true, onRawDataWriting > 0);
  84. Assert.AreEqual(0, onStateChangeEventArgs.Count);
  85. Assert.AreEqual(LogicalDeviceState.FDC_OFFLINE, this.fdcPumpController.QueryStatusAsync().Result,
  86. "status should be: FDC_OFFLINE, but now is: " + this.fdcPumpController.QueryStatusAsync().Result);
  87. }
  88. [TestMethod]
  89. public async Task FDC_READY_State_Test()
  90. {
  91. int onRawDataWriting = 0;
  92. var onStateChangeEventArgs = new List<FdcPumpControllerOnStateChangeEventArg>();
  93. this.fdcPumpController.OnStateChange += (s, a) =>
  94. {
  95. onStateChangeEventArgs.Add(a);
  96. };
  97. this.mockCommunicator.OnRawDataWriting += (a, b) =>
  98. {
  99. onRawDataWriting++;
  100. var readPumpStateResponse = new ReadPumpStateResponse
  101. {
  102. NozzleState = ReadPumpStateResponse.NozzleStateEnum.挂枪,
  103. StartOrStopState = ReadPumpStateResponse.StartOrStopStateEnum.停机,
  104. ControlState = ReadPumpStateResponse.ControlStateEnum.受控
  105. };
  106. this.mockCommunicator.FireOnDataReceived(readPumpStateResponse);
  107. };
  108. Assert.AreEqual(LogicalDeviceState.FDC_OFFLINE, this.fdcPumpController.QueryStatusAsync().Result,
  109. "status should change to: FDC_OFFLINE, but now is: " + fdcPumpController.QueryStatusAsync().Result);
  110. Assert.AreEqual(0, onRawDataWriting);
  111. await processor.Start();
  112. this.fdcPumpController.OnFdcServerInit(new Dictionary<string, object>());
  113. await Task.Delay(500);
  114. Assert.AreEqual(true, onRawDataWriting > 0);
  115. Assert.AreEqual(1, onStateChangeEventArgs.Count);
  116. Assert.AreEqual(LogicalDeviceState.FDC_READY, fdcPumpController.QueryStatusAsync().Result,
  117. "status should be: FDC_READY, but now is: " + fdcPumpController.QueryStatusAsync().Result);
  118. }
  119. [TestMethod]
  120. public async Task CallingState_Test()
  121. {
  122. int onRawDataWriting = 0;
  123. var onStateChangeEventArgs = new List<FdcPumpControllerOnStateChangeEventArg>();
  124. this.fdcPumpController.OnStateChange += (s, a) =>
  125. {
  126. onStateChangeEventArgs.Add(a);
  127. };
  128. this.mockCommunicator.OnRawDataWriting += (a, b) =>
  129. {
  130. onRawDataWriting++;
  131. var readPumpStateResponse = new ReadPumpStateResponse
  132. {
  133. NozzleState = ReadPumpStateResponse.NozzleStateEnum.提枪,
  134. StartOrStopState = ReadPumpStateResponse.StartOrStopStateEnum.停机,
  135. ControlState = ReadPumpStateResponse.ControlStateEnum.受控
  136. };
  137. this.mockCommunicator.FireOnDataReceived(readPumpStateResponse);
  138. };
  139. await this.processor.Start();
  140. this.fdcPumpController.OnFdcServerInit(new Dictionary<string, object>());
  141. await Task.Delay(500);
  142. Assert.AreEqual(1, onStateChangeEventArgs.Count, "OnStateChange should be called 1 times: FdcCalling");
  143. Assert.AreEqual(LogicalDeviceState.FDC_CALLING, onStateChangeEventArgs[0].NewPumpState);
  144. Assert.AreEqual(LogicalDeviceState.FDC_CALLING, this.fdcPumpController.QueryStatusAsync().Result,
  145. "status should change to: FDC_CALLING, but now is: " + this.fdcPumpController.QueryStatusAsync().Result);
  146. }
  147. [TestMethod]
  148. public async Task CallingState_Test2()
  149. {
  150. int onRawDataWriting = 0;
  151. var onStateChangeEventArgs = new List<FdcPumpControllerOnStateChangeEventArg>();
  152. this.fdcPumpController.OnStateChange += (s, a) =>
  153. {
  154. onStateChangeEventArgs.Add(a);
  155. };
  156. this.mockCommunicator.OnRawDataWriting += (a, b) =>
  157. {
  158. onRawDataWriting++;
  159. if (onRawDataWriting >= 2) return;
  160. var readPumpStateResponse_NozzleLifted = new ReadPumpStateResponse
  161. {
  162. NozzleState = ReadPumpStateResponse.NozzleStateEnum.提枪,
  163. StartOrStopState = ReadPumpStateResponse.StartOrStopStateEnum.停机,
  164. ControlState = ReadPumpStateResponse.ControlStateEnum.受控
  165. };
  166. this.mockCommunicator.FireOnDataReceived(readPumpStateResponse_NozzleLifted);
  167. Assert.AreEqual(LogicalDeviceState.FDC_CALLING, this.fdcPumpController.QueryStatusAsync().Result,
  168. "status should change to: FDC_CALLING, but now is: " + this.fdcPumpController.QueryStatusAsync().Result);
  169. var readPumpStateResponse_NozzlePlaceBack = new ReadPumpStateResponse
  170. {
  171. NozzleState = ReadPumpStateResponse.NozzleStateEnum.挂枪,
  172. StartOrStopState = ReadPumpStateResponse.StartOrStopStateEnum.停机,
  173. ControlState = ReadPumpStateResponse.ControlStateEnum.受控
  174. };
  175. this.mockCommunicator.FireOnDataReceived(readPumpStateResponse_NozzlePlaceBack);
  176. Assert.AreEqual(LogicalDeviceState.FDC_READY, this.fdcPumpController.QueryStatusAsync().Result,
  177. "status should change to: FDC_READY, but now is: " + this.fdcPumpController.QueryStatusAsync().Result);
  178. };
  179. await this.processor.Start();
  180. this.fdcPumpController.OnFdcServerInit(new Dictionary<string, object>());
  181. await Task.Delay(500);
  182. Assert.AreEqual(2, onStateChangeEventArgs.Count, "OnStateChange should be called 3 times: FdcCalling, FdcReady");
  183. Assert.AreEqual(LogicalDeviceState.FDC_CALLING, onStateChangeEventArgs[0].NewPumpState);
  184. Assert.AreEqual(LogicalDeviceState.FDC_READY, onStateChangeEventArgs[1].NewPumpState);
  185. Assert.AreEqual(LogicalDeviceState.FDC_READY, this.fdcPumpController.QueryStatusAsync().Result,
  186. "status should change to: FDC_CALLING, but now is: " + this.fdcPumpController.QueryStatusAsync().Result);
  187. }
  188. [TestMethod]
  189. public async Task AuthorizedState_Test()
  190. {
  191. int onRawDataWriting = 0;
  192. var onStateChangeEventArgs = new List<FdcPumpControllerOnStateChangeEventArg>();
  193. this.fdcPumpController.OnStateChange += (s, a) =>
  194. {
  195. onStateChangeEventArgs.Add(a);
  196. };
  197. this.mockCommunicator.OnRawDataWriting += (a, b) =>
  198. {
  199. onRawDataWriting++;
  200. if (onRawDataWriting >= 2) return;
  201. var readPumpStateResponse_NozzleLifted = new ReadPumpStateResponse
  202. {
  203. NozzleState = ReadPumpStateResponse.NozzleStateEnum.提枪,
  204. StartOrStopState = ReadPumpStateResponse.StartOrStopStateEnum.停机,
  205. ControlState = ReadPumpStateResponse.ControlStateEnum.受控
  206. };
  207. this.mockCommunicator.FireOnDataReceived(readPumpStateResponse_NozzleLifted);
  208. Assert.AreEqual(LogicalDeviceState.FDC_CALLING, this.fdcPumpController.QueryStatusAsync().Result,
  209. "status should change to: FDC_CALLING, but now is: " + this.fdcPumpController.QueryStatusAsync().Result);
  210. var readPumpStateResponse_NozzlePlaceBack = new ReadPumpStateResponse
  211. {
  212. NozzleState = ReadPumpStateResponse.NozzleStateEnum.提枪,
  213. StartOrStopState = ReadPumpStateResponse.StartOrStopStateEnum.开机,
  214. ControlState = ReadPumpStateResponse.ControlStateEnum.受控
  215. };
  216. this.mockCommunicator.FireOnDataReceived(readPumpStateResponse_NozzlePlaceBack);
  217. Assert.AreEqual(LogicalDeviceState.FDC_AUTHORISED, this.fdcPumpController.QueryStatusAsync().Result,
  218. "status should change to: FDC_AUTHORISED, but now is: " + this.fdcPumpController.QueryStatusAsync().Result);
  219. };
  220. await this.processor.Start();
  221. this.fdcPumpController.OnFdcServerInit(new Dictionary<string, object>());
  222. await Task.Delay(500);
  223. Assert.AreEqual(2, onStateChangeEventArgs.Count, "OnStateChange should be called 3 times: FdcCalling, FDC_AUTHORISED");
  224. Assert.AreEqual(LogicalDeviceState.FDC_CALLING, onStateChangeEventArgs[0].NewPumpState);
  225. Assert.AreEqual(LogicalDeviceState.FDC_AUTHORISED, onStateChangeEventArgs[1].NewPumpState);
  226. Assert.AreEqual(LogicalDeviceState.FDC_AUTHORISED, this.fdcPumpController.QueryStatusAsync().Result,
  227. "status should change to: FDC_AUTHORISED, but now is: " + this.fdcPumpController.QueryStatusAsync().Result);
  228. }
  229. public TestContext TestContext { get; set; }
  230. [TestMethod]
  231. public async Task FuelingState_Test()
  232. {
  233. int onRawDataWriting = 0;
  234. int fuelingStatusReportingTimes = 0;
  235. var onStateChangeEventArgs = new List<FdcPumpControllerOnStateChangeEventArg>();
  236. var onCurrentFuellingStatusChangeEventArgs = new List<FdcTransactionDoneEventArg>();
  237. this.fdcPumpController.OnStateChange += (s, a) =>
  238. {
  239. this.TestContext.WriteLine("Received a OnStateChange: " + a.NewPumpState);
  240. onStateChangeEventArgs.Add(a);
  241. };
  242. this.fdcPumpController.OnCurrentFuellingStatusChange += (s, a) =>
  243. {
  244. this.TestContext.WriteLine("Received a OnCurrentFuellingStatusChange, amt: " + a.Transaction.Amount + ", vol: " + a.Transaction.Volumn);
  245. onCurrentFuellingStatusChangeEventArgs.Add(a);
  246. };
  247. this.mockCommunicator.OnRawDataWriting += (a, b) =>
  248. {
  249. onRawDataWriting++;
  250. if (onRawDataWriting == 1)
  251. {
  252. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  253. var readPumpStateResponse_NozzleLifted = new ReadPumpStateResponse
  254. {
  255. NozzleState = ReadPumpStateResponse.NozzleStateEnum.提枪,
  256. StartOrStopState = ReadPumpStateResponse.StartOrStopStateEnum.停机,
  257. ControlState = ReadPumpStateResponse.ControlStateEnum.受控
  258. };
  259. this.TestContext.WriteLine("Fire NozzleLifted");
  260. this.mockCommunicator.FireOnDataReceived(readPumpStateResponse_NozzleLifted);
  261. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  262. var readPumpStateResponse_Nozzle开机 = new ReadPumpStateResponse
  263. {
  264. NozzleState = ReadPumpStateResponse.NozzleStateEnum.提枪,
  265. StartOrStopState = ReadPumpStateResponse.StartOrStopStateEnum.开机,
  266. ControlState = ReadPumpStateResponse.ControlStateEnum.受控
  267. };
  268. this.TestContext.WriteLine("Fire Nozzle开机");
  269. this.mockCommunicator.FireOnDataReceived(readPumpStateResponse_Nozzle开机);
  270. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  271. }
  272. else if (b.Message is ReadFuelDataRequest fuelDataRequest)
  273. {
  274. var readFuelDataResponse = new ReadFuelDataResponse();
  275. readFuelDataResponse.SetAmountAndVolume(100 + onRawDataWriting, 20 + onRawDataWriting);
  276. this.TestContext.WriteLine("Fire FuelDataResponse");
  277. this.mockCommunicator.FireOnDataReceived(readFuelDataResponse);
  278. fuelingStatusReportingTimes++;
  279. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  280. }
  281. };
  282. await this.processor.Start();
  283. this.fdcPumpController.OnFdcServerInit(new Dictionary<string, object>());
  284. await Task.Delay(1000);
  285. Assert.AreEqual(3, onStateChangeEventArgs.Count, "OnStateChange should be called 3 times: FdcCalling, FDC_AUTHORISED, FDC_FUELLING");
  286. Assert.AreEqual(fuelingStatusReportingTimes, onCurrentFuellingStatusChangeEventArgs.Count, "onCurrentFuellingStatusChangeEventArgs should have many");
  287. Assert.AreEqual(LogicalDeviceState.FDC_CALLING, onStateChangeEventArgs[0].NewPumpState);
  288. Assert.AreEqual(LogicalDeviceState.FDC_AUTHORISED, onStateChangeEventArgs[1].NewPumpState);
  289. Assert.AreEqual(LogicalDeviceState.FDC_FUELLING, onStateChangeEventArgs[2].NewPumpState);
  290. Assert.AreEqual(LogicalDeviceState.FDC_FUELLING, this.fdcPumpController.QueryStatusAsync().Result,
  291. "status should change to: FDC_FUELLING, but now is: " + this.fdcPumpController.QueryStatusAsync().Result);
  292. }
  293. [TestMethod]
  294. public async Task FuelingState_Test1()
  295. {
  296. /* use FdcPumpController.AuthorizeAsync to auth pump rather than full simulation*/
  297. int onRawDataWriting = 0;
  298. int fuelingStatusReportingTimes = 0;
  299. int authorizingCalledTimes = 0;
  300. var onStateChangeEventArgs = new List<FdcPumpControllerOnStateChangeEventArg>();
  301. var onCurrentFuellingStatusChangeEventArgs = new List<FdcTransactionDoneEventArg>();
  302. this.fdcPumpController.OnStateChange += (s, a) =>
  303. {
  304. this.TestContext.WriteLine("Received a OnStateChange: " + a.NewPumpState);
  305. onStateChangeEventArgs.Add(a);
  306. };
  307. this.fdcPumpController.OnCurrentFuellingStatusChange += (s, a) =>
  308. {
  309. this.TestContext.WriteLine("Received a OnCurrentFuellingStatusChange, amt: " + a.Transaction.Amount + ", vol: " + a.Transaction.Volumn);
  310. onCurrentFuellingStatusChangeEventArgs.Add(a);
  311. };
  312. // default is for simulate calling
  313. var readPumpStateResponse = new ReadPumpStateResponse
  314. {
  315. NozzleState = ReadPumpStateResponse.NozzleStateEnum.提枪,
  316. StartOrStopState = ReadPumpStateResponse.StartOrStopStateEnum.停机,
  317. ControlState = ReadPumpStateResponse.ControlStateEnum.受控
  318. };
  319. this.mockCommunicator.OnRawDataWriting += async (a, b) =>
  320. {
  321. onRawDataWriting++;
  322. if (b.Message is ReadPumpStateRequest readPumpStateRequest)
  323. {
  324. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  325. this.TestContext.WriteLine("Fire Nozzle state: " + readPumpStateResponse.StartOrStopState);
  326. this.mockCommunicator.FireOnDataReceived(readPumpStateResponse);
  327. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  328. if (authorizingCalledTimes == 0)
  329. {
  330. this.TestContext.WriteLine("AuthorizeAsync...");
  331. var authResult = await this.fdcPumpController.AuthorizeWithAmountAsync(1000, 1);
  332. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  333. }
  334. }
  335. else if (b.Message is PresetAmountRequest presetAmountRequest)
  336. {
  337. this.mockCommunicator.FireOnDataReceived(new PresetAmountResponse());
  338. }
  339. else if (b.Message is OpenRequest openRequest)
  340. {
  341. authorizingCalledTimes++;
  342. // to authorized state.
  343. readPumpStateResponse = new ReadPumpStateResponse
  344. {
  345. NozzleState = ReadPumpStateResponse.NozzleStateEnum.提枪,
  346. StartOrStopState = ReadPumpStateResponse.StartOrStopStateEnum.开机,
  347. ControlState = ReadPumpStateResponse.ControlStateEnum.受控
  348. };
  349. }
  350. else if (b.Message is ReadFuelDataRequest fuelDataRequest)
  351. {
  352. var readFuelDataResponse = new ReadFuelDataResponse();
  353. readFuelDataResponse.SetAmountAndVolume(100 + onRawDataWriting, 20 + onRawDataWriting);
  354. this.TestContext.WriteLine("Fire FuelDataResponse");
  355. this.mockCommunicator.FireOnDataReceived(readFuelDataResponse);
  356. fuelingStatusReportingTimes++;
  357. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  358. }
  359. };
  360. await this.processor.Start();
  361. this.fdcPumpController.OnFdcServerInit(new Dictionary<string, object>());
  362. await Task.Delay(1000);
  363. Assert.AreEqual(3, onStateChangeEventArgs.Count, "OnStateChange should be called 3 times: FdcCalling, FDC_AUTHORISED, FDC_FUELLING");
  364. Assert.AreEqual(1, authorizingCalledTimes);
  365. Assert.AreEqual(fuelingStatusReportingTimes, onCurrentFuellingStatusChangeEventArgs.Count, "onCurrentFuellingStatusChangeEventArgs should have many");
  366. Assert.AreEqual(LogicalDeviceState.FDC_CALLING, onStateChangeEventArgs[0].NewPumpState);
  367. Assert.AreEqual(LogicalDeviceState.FDC_AUTHORISED, onStateChangeEventArgs[1].NewPumpState);
  368. Assert.AreEqual(LogicalDeviceState.FDC_FUELLING, onStateChangeEventArgs[2].NewPumpState);
  369. Assert.AreEqual(LogicalDeviceState.FDC_FUELLING, this.fdcPumpController.QueryStatusAsync().Result,
  370. "status should change to: FDC_FUELLING, but now is: " + this.fdcPumpController.QueryStatusAsync().Result);
  371. }
  372. [TestMethod]
  373. public async Task FuelTrxDone_Test()
  374. {
  375. int onRawDataWriting = 0;
  376. int fuelingStatusReportingTimes = 0;
  377. var onStateChangeEventArgs = new List<FdcPumpControllerOnStateChangeEventArg>();
  378. var onCurrentFuellingStatusChangeEventArgs = new List<FdcTransactionDoneEventArg>();
  379. this.fdcPumpController.OnStateChange += (s, a) =>
  380. {
  381. this.TestContext.WriteLine("Received a OnStateChange: " + a.NewPumpState);
  382. onStateChangeEventArgs.Add(a);
  383. };
  384. this.fdcPumpController.OnCurrentFuellingStatusChange += (s, a) =>
  385. {
  386. this.TestContext.WriteLine("Received a OnCurrentFuellingStatusChange, Finished: " + a.Transaction.Finished + ", amt: " + a.Transaction.Amount + ", vol: " + a.Transaction.Volumn);
  387. onCurrentFuellingStatusChangeEventArgs.Add(a);
  388. };
  389. this.mockCommunicator.OnRawDataWriting += (a, b) =>
  390. {
  391. onRawDataWriting++;
  392. if (onRawDataWriting == 1)
  393. {
  394. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  395. var readPumpStateResponse_NozzleLifted = new ReadPumpStateResponse
  396. {
  397. NozzleState = ReadPumpStateResponse.NozzleStateEnum.提枪,
  398. StartOrStopState = ReadPumpStateResponse.StartOrStopStateEnum.停机,
  399. ControlState = ReadPumpStateResponse.ControlStateEnum.受控
  400. };
  401. this.TestContext.WriteLine("Fire NozzleLifted");
  402. this.mockCommunicator.FireOnDataReceived(readPumpStateResponse_NozzleLifted);
  403. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  404. var readPumpStateResponse_Nozzle开机 = new ReadPumpStateResponse
  405. {
  406. NozzleState = ReadPumpStateResponse.NozzleStateEnum.提枪,
  407. StartOrStopState = ReadPumpStateResponse.StartOrStopStateEnum.开机,
  408. ControlState = ReadPumpStateResponse.ControlStateEnum.受控
  409. };
  410. this.TestContext.WriteLine("Fire Nozzle开机");
  411. this.mockCommunicator.FireOnDataReceived(readPumpStateResponse_Nozzle开机);
  412. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  413. }
  414. else if (b.Message is ReadFuelDataRequest fuelDataRequest)
  415. {
  416. fuelingStatusReportingTimes++;
  417. var readFuelDataResponse = new ReadFuelDataResponse();
  418. readFuelDataResponse.SetAmountAndVolume(100 + fuelingStatusReportingTimes, 20 + fuelingStatusReportingTimes);
  419. this.TestContext.WriteLine("Fire FuelDataResponse");
  420. this.mockCommunicator.FireOnDataReceived(readFuelDataResponse);
  421. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  422. if (onRawDataWriting >= 5)
  423. {
  424. var readPumpStateResponse_NozzleReplaced_And_停机 = new ReadPumpStateResponse
  425. {
  426. NozzleState = ReadPumpStateResponse.NozzleStateEnum.挂枪,
  427. StartOrStopState = ReadPumpStateResponse.StartOrStopStateEnum.停机,
  428. ControlState = ReadPumpStateResponse.ControlStateEnum.受控
  429. };
  430. this.TestContext.WriteLine("Fire NozzleReplaced_And_停机");
  431. this.mockCommunicator.FireOnDataReceived(readPumpStateResponse_NozzleReplaced_And_停机);
  432. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  433. }
  434. }
  435. };
  436. await this.processor.Start();
  437. this.fdcPumpController.OnFdcServerInit(new Dictionary<string, object>());
  438. await Task.Delay(1800);
  439. Assert.AreEqual(4, onStateChangeEventArgs.Count, "OnStateChange should be called 3 times: FdcCalling, FDC_AUTHORISED, FDC_FUELLING, FdcReady");
  440. // 1 extra requesting is to generate the final fuel trx, and will not fire event.
  441. Assert.AreEqual(fuelingStatusReportingTimes, onCurrentFuellingStatusChangeEventArgs.Count + 1, "onCurrentFuellingStatusChangeEventArgs should have many");
  442. Assert.AreEqual(1, onCurrentFuellingStatusChangeEventArgs.Where(a => a.Transaction.Finished).Count());
  443. Assert.AreEqual(100 + fuelingStatusReportingTimes,
  444. onCurrentFuellingStatusChangeEventArgs.First(a => a.Transaction.Finished).Transaction.Amount);
  445. Assert.AreEqual(20 + fuelingStatusReportingTimes,
  446. onCurrentFuellingStatusChangeEventArgs.First(a => a.Transaction.Finished).Transaction.Volumn);
  447. Assert.AreEqual(LogicalDeviceState.FDC_CALLING, onStateChangeEventArgs[0].NewPumpState);
  448. Assert.AreEqual(LogicalDeviceState.FDC_AUTHORISED, onStateChangeEventArgs[1].NewPumpState);
  449. Assert.AreEqual(LogicalDeviceState.FDC_FUELLING, onStateChangeEventArgs[2].NewPumpState);
  450. Assert.AreEqual(LogicalDeviceState.FDC_READY, onStateChangeEventArgs[3].NewPumpState);
  451. Assert.AreEqual(LogicalDeviceState.FDC_READY, this.fdcPumpController.QueryStatusAsync().Result,
  452. "status should change to: FDC_READY, but now is: " + this.fdcPumpController.QueryStatusAsync().Result);
  453. }
  454. [TestMethod]
  455. public async Task FuelTrxDone_Test1()
  456. {
  457. /* use FdcPumpController.AuthorizeAsync to auth pump rather than full simulation*/
  458. int onRawDataWriting = 0;
  459. int fuelingStatusReportingTimes = 0;
  460. int authorizingCalledTimes = 0;
  461. var onStateChangeEventArgs = new List<FdcPumpControllerOnStateChangeEventArg>();
  462. var onCurrentFuellingStatusChangeEventArgs = new List<FdcTransactionDoneEventArg>();
  463. this.fdcPumpController.OnStateChange += async (s, a) =>
  464. {
  465. this.TestContext.WriteLine("Received a OnStateChange: " + a.NewPumpState);
  466. onStateChangeEventArgs.Add(a);
  467. if (a.NewPumpState == LogicalDeviceState.FDC_CALLING)
  468. {
  469. this.TestContext.WriteLine("AuthorizeWithAmountAsync");
  470. await this.fdcPumpController.AuthorizeWithAmountAsync(1000, 1);
  471. }
  472. };
  473. this.fdcPumpController.OnCurrentFuellingStatusChange += (s, a) =>
  474. {
  475. this.TestContext.WriteLine("Received a OnCurrentFuellingStatusChange, Finished: " + a.Transaction.Finished + ", amt: " + a.Transaction.Amount + ", vol: " + a.Transaction.Volumn);
  476. onCurrentFuellingStatusChangeEventArgs.Add(a);
  477. };
  478. // default is for simulate calling
  479. var readPumpStateResponse = new ReadPumpStateResponse
  480. {
  481. NozzleState = ReadPumpStateResponse.NozzleStateEnum.提枪,
  482. StartOrStopState = ReadPumpStateResponse.StartOrStopStateEnum.停机,
  483. ControlState = ReadPumpStateResponse.ControlStateEnum.受控
  484. };
  485. this.mockCommunicator.OnRawDataWriting += (a, b) =>
  486. {
  487. onRawDataWriting++;
  488. if (b.Message is ReadPumpStateRequest _)
  489. {
  490. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  491. this.TestContext.WriteLine("Fire " + readPumpStateResponse.NozzleState + ", " + readPumpStateResponse.StartOrStopState);
  492. this.mockCommunicator.FireOnDataReceived(readPumpStateResponse);
  493. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  494. }
  495. else if (b.Message is PresetAmountRequest presetAmountRequest)
  496. {
  497. this.TestContext.WriteLine("Fire PresetAmountResponse");
  498. this.mockCommunicator.FireOnDataReceived(new PresetAmountResponse());
  499. }
  500. else if (b.Message is OpenRequest openRequest)
  501. {
  502. authorizingCalledTimes++;
  503. // to authorized state.
  504. readPumpStateResponse = new ReadPumpStateResponse
  505. {
  506. NozzleState = ReadPumpStateResponse.NozzleStateEnum.提枪,
  507. StartOrStopState = ReadPumpStateResponse.StartOrStopStateEnum.开机,
  508. ControlState = ReadPumpStateResponse.ControlStateEnum.受控
  509. };
  510. }
  511. else if (b.Message is ReadFuelDataRequest fuelDataRequest)
  512. {
  513. fuelingStatusReportingTimes++;
  514. var readFuelDataResponse = new ReadFuelDataResponse();
  515. readFuelDataResponse.SetAmountAndVolume(100 + fuelingStatusReportingTimes, 20 + fuelingStatusReportingTimes);
  516. this.TestContext.WriteLine("Fire FuelDataResponse");
  517. this.mockCommunicator.FireOnDataReceived(readFuelDataResponse);
  518. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  519. if (onRawDataWriting >= 5)
  520. {
  521. readPumpStateResponse = new ReadPumpStateResponse
  522. {
  523. NozzleState = ReadPumpStateResponse.NozzleStateEnum.挂枪,
  524. StartOrStopState = ReadPumpStateResponse.StartOrStopStateEnum.停机,
  525. ControlState = ReadPumpStateResponse.ControlStateEnum.受控
  526. };
  527. this.TestContext.WriteLine("Fire " + readPumpStateResponse.NozzleState + ", " + readPumpStateResponse.StartOrStopState);
  528. this.TestContext.WriteLine("Pump is in state: " + this.fdcPumpController.QueryStatusAsync().Result);
  529. }
  530. }
  531. };
  532. await this.processor.Start();
  533. this.fdcPumpController.OnFdcServerInit(new Dictionary<string, object>());
  534. await Task.Delay(1800);
  535. Assert.AreEqual(4, onStateChangeEventArgs.Count, "OnStateChange should be called 3 times: FdcCalling, FDC_AUTHORISED, FDC_FUELLING, FdcReady");
  536. // 1 extra requesting is to generate the final fuel trx, and will not fire event.
  537. Assert.AreEqual(fuelingStatusReportingTimes, onCurrentFuellingStatusChangeEventArgs.Count + 1, "onCurrentFuellingStatusChangeEventArgs should have many");
  538. Assert.AreEqual(1, onCurrentFuellingStatusChangeEventArgs.Where(a => a.Transaction.Finished).Count());
  539. Assert.AreEqual(100 + fuelingStatusReportingTimes,
  540. onCurrentFuellingStatusChangeEventArgs.First(a => a.Transaction.Finished).Transaction.Amount);
  541. Assert.AreEqual(20 + fuelingStatusReportingTimes,
  542. onCurrentFuellingStatusChangeEventArgs.First(a => a.Transaction.Finished).Transaction.Volumn);
  543. Assert.AreEqual(LogicalDeviceState.FDC_CALLING, onStateChangeEventArgs[0].NewPumpState);
  544. Assert.AreEqual(LogicalDeviceState.FDC_AUTHORISED, onStateChangeEventArgs[1].NewPumpState);
  545. Assert.AreEqual(LogicalDeviceState.FDC_FUELLING, onStateChangeEventArgs[2].NewPumpState);
  546. Assert.AreEqual(LogicalDeviceState.FDC_READY, onStateChangeEventArgs[3].NewPumpState);
  547. Assert.AreEqual(LogicalDeviceState.FDC_READY, this.fdcPumpController.QueryStatusAsync().Result,
  548. "status should change to: FDC_READY, but now is: " + this.fdcPumpController.QueryStatusAsync().Result);
  549. }
  550. }
  551. }