Eps.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. using Applications.FDC;
  2. using Edge.Core.Processor;using Edge.Core.IndustryStandardInterface.Pump;
  3. using Dfs.WayneChina.SinochemEps;
  4. using NozzleLockConfiguration;
  5. using Edge.Core.Parser.HttpMessageParser;
  6. using SinochemCarplateService.Models;
  7. using SinochemCloudClient.Models;
  8. using SinoChemFC2PosProxy;
  9. using SinochemInternetPlusApp.EpsTrxCleanup;
  10. using SinochemPosClient.Models;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Xml.Serialization;
  18. using Wayne.FDCPOSLibrary;
  19. using Wayne.Lib;
  20. using Wayne.Lib.Log;
  21. using WayneChina_IcCardReader_SinoChem;
  22. using WayneChina_IcCardReader_SinoChem.MessageEntity;
  23. using WayneChina_IcCardReader_SinoChem.MessageEntity.Incoming;
  24. namespace SinochemInternetPlusApp
  25. {
  26. /// <summary>
  27. /// Electronic payment service with WT 30 IC.
  28. /// </summary>
  29. public class Eps : IDisposable
  30. {
  31. static NLog.Logger logger = NLog.LogManager.LoadConfiguration("nlog.config").GetLogger("SinochemEpsApp");
  32. private IEnumerable<GenericDeviceProcessor<System.Byte[], WayneChina_IcCardReader_SinoChem.MessageEntity.IcCardReaderMessageBase>> icCardReaderProcessors;
  33. private List<FuelingPoint> fuelingPoints;
  34. private EpsTrxCleanupManager epsTrxCleanupManager;
  35. private int requestId;
  36. private object requestSyncObj = new object();
  37. #region devices and services
  38. private Sinochem_CarPlateRecognizeCamera_HuLianWangJia.Handler carPlateServer;
  39. private IEnumerable<WayneChina_IcCardReader_SinoChem.TcpHandler> cardReaderHandlers;
  40. private CloudManager cloudManager;
  41. private PosManager posManger;
  42. #endregion
  43. private IEnumerable<IProcessor> processors;
  44. private IEnumerable<IFdcPumpController> pumpControllers;
  45. private FdcServerHostApp fdcServer;
  46. private Dictionary<int, TcpHandler> tcpHandlersDict = new Dictionary<int, TcpHandler>();
  47. #region constants
  48. public int InvalidNozzleId => 9999;
  49. #endregion
  50. ForecourtTrxManager forecourtTrxManager;
  51. #region Constructor
  52. public Eps(IEnumerable<int> fpIds, Dictionary<int, IEnumerable<int>> fpNozzlesDict, IEnumerable<IProcessor> processors)
  53. {
  54. var identifiableEntity = new IdentifiableEntity(0, "EpsMain", "", null);
  55. fuelingPoints = new List<FuelingPoint>(fpIds.Count());
  56. foreach (int nozzleId in fpIds)
  57. {
  58. fuelingPoints.Add(new FuelingPoint(nozzleId, fpNozzlesDict[nozzleId], this));
  59. }
  60. epsTrxCleanupManager = new EpsTrxCleanupManager(this);
  61. cloudManager = new CloudManager();
  62. posManger = new PosManager();
  63. NozzleLockAccessor.FillinNozzles(fpNozzlesDict);
  64. this.processors = processors;
  65. List<IFdcPumpController> pumpControllerList = new List<IFdcPumpController>();
  66. foreach (dynamic processor in processors)
  67. {
  68. if (processor is IAppProcessor)
  69. {
  70. var fdcServer = processor as FdcServerHostApp;
  71. if (fdcServer != null)
  72. {
  73. this.fdcServer = fdcServer;
  74. }
  75. continue;
  76. }
  77. var handler = processor.Context.Handler;
  78. if (handler is TcpHandler)
  79. {
  80. if (!tcpHandlersDict.ContainsKey(handler.DispenserId))
  81. tcpHandlersDict.Add(handler.DispenserId, handler);
  82. }
  83. else if (handler is IDeviceHandler<string, BaseHttpMessage<string>>)
  84. {
  85. if (carPlateServer == null)
  86. {
  87. carPlateServer = handler;
  88. }
  89. }
  90. else if (handler is IFdcPumpController)
  91. {
  92. pumpControllerList.Add(handler);
  93. }
  94. else if (handler is IEnumerable<IFdcPumpController>)
  95. {
  96. pumpControllerList.AddRange(handler);
  97. }
  98. }
  99. pumpControllers = pumpControllerList;
  100. logger.Info($"There are {pumpControllers.Count()} pumps available for EPS");
  101. forecourtTrxManager = new ForecourtTrxManager(pumpControllers, SinochemEpsApp.PumpSideMapping,
  102. SinochemEpsApp.ForceMappingFusionHoseToHuiTianHoseStr, SinochemEpsApp.PosDatabaseConnString,
  103. SinochemEpsApp.RawProductNameToPosProductNameStr);
  104. forecourtTrxManager.Init();
  105. logger.Info("Eps instance has been constructed");
  106. }
  107. internal TrxNotificationResponse NotifySuccessfulTrxToPos(EpsTransactionModel model, DebugLogger debugLogger)
  108. {
  109. return posManger.NotifyPosSuccessfulTrx(model, debugLogger);
  110. }
  111. internal PaymentResponse SendPaymentToCloud(EpsTransactionModel currentEpsTrxModel, DebugLogger debugLogger)
  112. {
  113. return cloudManager.Payment(currentEpsTrxModel, debugLogger);
  114. }
  115. internal BalanceInquiryResponse SendBalanceInquiryToCloud(string cardNo, string encryptedPin, string tid, int nozzleId, DebugLogger debugLogger)
  116. {
  117. return cloudManager.BalanceInquiry(cardNo, encryptedPin, tid, nozzleId, debugLogger);
  118. }
  119. #endregion
  120. public void Run()
  121. {
  122. SetupICCardReaderHandler();
  123. if (fuelingPoints != null)
  124. {
  125. foreach (var fp in fuelingPoints)
  126. {
  127. fp.Start();
  128. }
  129. }
  130. epsTrxCleanupManager.Start();
  131. SetupCarplateServer();
  132. SetupFccClient();
  133. }
  134. private void SetupFccClient()
  135. {
  136. foreach (var pumpController in pumpControllers)
  137. {
  138. pumpController.OnStateChange += (s, a) =>
  139. {
  140. var pump = s as IFdcPumpController;
  141. if (a.NewPumpState == LogicalDeviceState.FDC_CALLING)
  142. {
  143. logger.Info($"Pump {pump.PumpId} at state CALLING");
  144. int sitewiseNozzleId =
  145. SiteConfigUtility.Default.GetSiteLevelNozzleIdByLogicalNozzleId(pump.PumpId,
  146. a.StateChangedNozzles.FirstOrDefault()?.LogicalId ?? 0);
  147. FccClient_NozzleLifted(pump.PumpId, pump);
  148. //FccClient_NozzleLifted(sitewiseNozzleId, pump);
  149. }
  150. else if (a.NewPumpState == LogicalDeviceState.FDC_READY)
  151. {
  152. int sitewiseNozzleId = 0;
  153. if (a.StateChangedNozzles != null)
  154. {
  155. SiteConfigUtility.Default.GetSiteLevelNozzleIdByLogicalNozzleId(pump.PumpId, a.StateChangedNozzles.FirstOrDefault()?.LogicalId ?? 0);
  156. }
  157. else
  158. {
  159. logger.Info("StateChangeNozzles null, use sitewiseNozzleId 0");
  160. }
  161. logger.Info($"Pump {pump.PumpId} at state: FDC_READY, siteWiseNozzleId: {sitewiseNozzleId}");
  162. FccClient_NozzleReplaced(pump.PumpId);
  163. //FccClient_NozzleReplaced(sitewiseNozzleId);
  164. }
  165. else if (a.NewPumpState == LogicalDeviceState.FDC_AUTHORISED)
  166. {
  167. int sitewiseNozzleId =
  168. SiteConfigUtility.Default.GetSiteLevelNozzleIdByLogicalNozzleId(pump.PumpId,
  169. a.StateChangedNozzles.FirstOrDefault()?.LogicalId ?? 0);
  170. logger.Info($"Pump {pump.PumpId} at state FDC_AUTHORISED, siteWiseNozzleId: {sitewiseNozzleId}");
  171. FccClient_AuthOk(pump.PumpId, 1999);
  172. //FccClient_AuthOk(sitewiseNozzleId, 1999);
  173. }
  174. else if (a.NewPumpState == LogicalDeviceState.FDC_FUELLING)
  175. {
  176. logger.Info($"Pump {pump.PumpId} is fueling");
  177. }
  178. };
  179. pumpController.OnCurrentFuellingStatusChange += async (s, a) =>
  180. {
  181. var pump = s as IFdcPumpController;
  182. if (a.Transaction.Finished)
  183. {
  184. int sitewiseNozzleId =
  185. SiteConfigUtility.Default.GetSiteLevelNozzleIdByLogicalNozzleId(pump.PumpId, a.Transaction.Nozzle.LogicalId);
  186. logger.Info($"Pump {pump.PumpId} fueling finished, siteWiseNozzleId: {sitewiseNozzleId}");
  187. FccClient_NozzleReplaced(pump.PumpId);
  188. //Give ForecourtTrxManager some time to insert filling transaction to xiaofei2.
  189. await Task.Delay(500);
  190. //Note: amount and volume are integer, directly from FC instead of Global FC.
  191. FccClient_FuelingDone(pump.PumpId, a.Transaction.SequenceNumberGeneratedOnPhysicalPump, a.Transaction.Amount,
  192. a.Transaction.Volumn, 1999);
  193. }
  194. };
  195. }
  196. }
  197. // find car plate handler and listen to the new car plate event
  198. private void SetupCarplateServer()
  199. {
  200. if (carPlateServer != null)
  201. {
  202. carPlateServer.NewCarPlateScanned += CarPlateServer_NewCarPlateScanned;
  203. carPlateServer.OnMessageReceivedViaFdc += (msg) =>
  204. {
  205. logger.Debug($"fdc message:\n {msg}");
  206. if (msg.Length > 10 && msg.StartsWith("<Tran"))
  207. {
  208. var serializer = new XmlSerializer(typeof(TransactionOperation));
  209. using (var reader = new StringReader(msg))
  210. {
  211. var trxOpRequest = (TransactionOperation)serializer.Deserialize(reader);
  212. if (trxOpRequest != null)
  213. {
  214. foreach (var fp in fuelingPoints)
  215. {
  216. fp.SignalTrxOpRequest(trxOpRequest);
  217. }
  218. }
  219. }
  220. }
  221. else
  222. {
  223. var serializer = new XmlSerializer(typeof(DisplayResponse));
  224. using (var reader = new StringReader(msg))
  225. {
  226. var displayResp = (DisplayResponse)serializer.Deserialize(reader);
  227. logger.Debug($"Display Response, id = {displayResp.RequestId}");
  228. if (displayResp != null && displayResp.OverallResult == ResultType.Success)
  229. {
  230. foreach (var fp in fuelingPoints)
  231. {
  232. if (fp.ContainsRequestId(displayResp.RequestId))
  233. fp.SignalDisplayResponseReceived(displayResp);
  234. }
  235. }
  236. }
  237. }
  238. return new Tuple<string, OverallResult>("DisplayResponse ACK", OverallResult.Success);
  239. };
  240. }
  241. else
  242. {
  243. logger.Info("carPlateServer not found!!!");
  244. }
  245. }
  246. private void CarPlateServer_NewCarPlateScanned(CarPlateTrxRequest request)
  247. {
  248. int sitewiseNozzleId = InvalidNozzleId;
  249. try
  250. {
  251. sitewiseNozzleId = int.Parse(request.gun);
  252. }
  253. catch (Exception ex)
  254. {
  255. logger.Error(ex);
  256. }
  257. if (IsNozzleConfiguredOpen(sitewiseNozzleId))
  258. {
  259. var fp = GetFp(sitewiseNozzleId);
  260. fp?.SignalNewCarplate(request);
  261. }
  262. else
  263. {
  264. logger.Info($"Physical nozzle# {sitewiseNozzleId} is closed, so we ignore the car plate event!!!");
  265. }
  266. }
  267. private void SetupICCardReaderHandler()
  268. {
  269. cardReaderHandlers = tcpHandlersDict.Values;
  270. if (cardReaderHandlers != null && cardReaderHandlers.Count() > 0)
  271. {
  272. foreach (var handler in cardReaderHandlers)
  273. {
  274. handler.OnCardReaderMessageReceived += Handler_OnCardReaderMessageReceived;
  275. }
  276. }
  277. else
  278. {
  279. logger.Info("card reader handlers not found!!");
  280. }
  281. }
  282. private void Handler_OnCardReaderMessageReceived(object sender, CardReaderMessageEventArgs e)
  283. {
  284. logger.Info($"Receiving Card Reader message {e.CardReaderMessage.GetType().ToString()} for FP id = {e.FuelingPointId}");
  285. //Heart beat messages are always from address 0x0F.
  286. if (e.CardReaderMessage.SourceAddress == 0x0F)
  287. {
  288. var currentHandler = sender as TcpHandler;
  289. if (currentHandler != null)
  290. {
  291. logger.Info($"Heart beat from dispenser of id: {currentHandler.DispenserId}");
  292. currentHandler.Write(new EOT { RawMessageSeqNumberAndSourceAddress = 0x0F });
  293. //GetHandler(1).Write(new EOT { RawMessageSeqNumberAndSourceAddress = 0x0F });
  294. return;
  295. }
  296. }
  297. var targetFP = fuelingPoints.First(_ => _.FuelingPointId == e.FuelingPointId);
  298. if (e.CardReaderMessage is ACK)
  299. {
  300. targetFP?.SingalAckReceived((ACK)e.CardReaderMessage);
  301. }
  302. else if (e.CardReaderMessage is SignDataResponse)
  303. {
  304. targetFP?.SignalSignedDataArrived((SignDataResponse)e.CardReaderMessage);
  305. }
  306. else if (e.CardReaderMessage is CardReaderStateEvent)
  307. {
  308. targetFP?.SingalCardReaderStateEvent((CardReaderStateEvent)e.CardReaderMessage);
  309. }
  310. else if (e.CardReaderMessage is CardExternalCheckErrorRequest)
  311. {
  312. targetFP?.SignalCardExternalCheckFailure((CardExternalCheckErrorRequest)e.CardReaderMessage);
  313. }
  314. else if (e.CardReaderMessage is CardOnlineVerificationRequest)
  315. {
  316. targetFP?.SignalCardOnlineVerification((CardOnlineVerificationRequest)e.CardReaderMessage);
  317. }
  318. //else if (e.CardReaderMessage is HeartBeat)
  319. //{
  320. // targetFP?.SignalCardReaderHeartbeat((HeartBeat)e.CardReaderMessage);
  321. //}
  322. }
  323. #region Cloud interactions
  324. internal RefundResponse SendRefundToCloud(EpsTransactionModel trxModel, DebugLogger debugLogger)
  325. {
  326. return cloudManager.Refund(trxModel, debugLogger);
  327. }
  328. internal TrxStatusInquiryResponse SendTrxQueryToCloud(EpsTransactionModel trxModel, DebugLogger debugLogger)
  329. {
  330. return cloudManager.TrxStatusInquiry(trxModel, debugLogger);
  331. }
  332. #endregion
  333. #region FDC client interactions
  334. private void FccClient_NozzleReplaced(int sitewiseNozzleId)
  335. {
  336. var fp = GetFp(sitewiseNozzleId);
  337. fp?.SignalNozzleReplaced(sitewiseNozzleId);
  338. }
  339. private void FccClient_AuthFailed(int sitewiseNozzleId)
  340. {
  341. var fp = GetFp(sitewiseNozzleId);
  342. fp?.SignalAuthFailed();
  343. }
  344. private void FccClient_AuthOk(int sitewiseNozzleId, long? authId)
  345. {
  346. var fp = GetFp(sitewiseNozzleId);
  347. fp?.SignalAuthOk(authId);
  348. }
  349. private void FccClient_NozzleLifted(int sitewiseNozzleId, IFdcPumpController callingPump)
  350. {
  351. if (IsNozzleConfiguredOpen(sitewiseNozzleId))
  352. {
  353. var fp = GetFp(sitewiseNozzleId);
  354. fp?.SignalNozzleLifted(callingPump, sitewiseNozzleId);
  355. }
  356. else
  357. {
  358. logger.Info(string.Format($"Physical nozzle# {sitewiseNozzleId} is closed, so we ignore the nozzle lift event!!!"));
  359. }
  360. }
  361. private bool IsNozzleConfiguredOpen(int sitewiseNozzleId)
  362. {
  363. return !NozzleLockAccessor.IsNozzleClosedInTermsOfSale(sitewiseNozzleId);
  364. }
  365. private void FccClient_FuelingDone(int sitewiseNozzleId, int seqNum, decimal fuelAmount, decimal fuelingQty, long authId)
  366. {
  367. var fp = GetFp(sitewiseNozzleId);
  368. logger.Info($"Fueling point is null? {fp == null}");
  369. fp?.SignalFuelingDone(sitewiseNozzleId, seqNum, Convert.ToDecimal(fuelAmount)/100, Convert.ToDecimal(fuelingQty) / 100, authId);
  370. }
  371. public void AuthorizePumpAsync(IFdcPumpController callingPump, int sitewiseNozzleId, decimal authAmount)
  372. {
  373. //Have to put pump auth in a different thread due to FC reasons, 2019/06/19
  374. Task.Run(() =>
  375. {
  376. callingPump.AuthorizeWithAmountAsync((int)((double)authAmount * Math.Pow(10, callingPump.AmountDecimalDigits)), 1);
  377. });
  378. }
  379. #endregion
  380. private void TeardownFccClient()
  381. {
  382. //if (fccClient != null)
  383. //{
  384. // fccClient.NozzleLifted -= FccClient_NozzleLifted;
  385. // fccClient.NozzleReplaced -= FccClient_NozzleReplaced;
  386. // fccClient.AuthOk -= FccClient_AuthOk;
  387. // fccClient.AuthFailed -= FccClient_AuthFailed;
  388. // fccClient.FuelingDone -= FccClient_FuelingDone;
  389. //}
  390. }
  391. public void Shutdown()
  392. {
  393. TeardownCarplateServer();
  394. TeardownCardReaderHandler();
  395. if (fuelingPoints != null)
  396. {
  397. foreach (var fp in fuelingPoints)
  398. {
  399. fp.SignalShutdown();
  400. }
  401. }
  402. }
  403. public WayneChina_IcCardReader_SinoChem.TcpHandler GetHandler(int nozzleId)
  404. {
  405. logger.Info($"Trying to get card reader handler for fueling point: {nozzleId}");
  406. foreach (var handler in cardReaderHandlers)
  407. {
  408. if (handler.SupportedNozzles.Contains(nozzleId))
  409. return handler;
  410. }
  411. return null;
  412. }
  413. private void TeardownCarplateServer()
  414. {
  415. if (carPlateServer != null)
  416. {
  417. carPlateServer.NewCarPlateScanned -= CarPlateServer_NewCarPlateScanned;
  418. var server = carPlateServer as IDisposable;
  419. if (server != null)
  420. server.Dispose();
  421. }
  422. }
  423. private void TeardownCardReaderHandler()
  424. {
  425. if (cardReaderHandlers != null)
  426. {
  427. foreach (var handler in cardReaderHandlers)
  428. {
  429. handler.OnCardReaderMessageReceived -= Handler_OnCardReaderMessageReceived;
  430. }
  431. }
  432. }
  433. public Sinochem_CarPlateRecognizeCamera_HuLianWangJia.Handler CarPlateHandler => carPlateServer;
  434. public TcpHandler GetCardReader(int nozzleId)
  435. {
  436. return cardReaderHandlers.FirstOrDefault(h => h.SupportedNozzles.Contains(nozzleId));
  437. }
  438. public FuelingPoint GetFp(int nozzleId)
  439. {
  440. foreach (var fp in fuelingPoints)
  441. {
  442. if (fp.AssociatedNozzles != null && fp.AssociatedNozzles.Contains(nozzleId))
  443. return fp;
  444. }
  445. return null;
  446. }
  447. public FuelingPoint GetFuelingPoint(int fpId)
  448. {
  449. return fuelingPoints?.FirstOrDefault(_ => fpId == _.FuelingPointId);
  450. }
  451. public int GetNextRequestId()
  452. {
  453. lock (requestSyncObj)
  454. {
  455. if (requestId == int.MaxValue)
  456. {
  457. requestId = 0;
  458. }
  459. return requestId++;
  460. }
  461. }
  462. public string CreateDisplayTrxCommand(IEnumerable<EpsTransactionModel> epsTrxList, out int requestId, int i)
  463. {
  464. Display display;
  465. string cmdText = "";
  466. XmlSerializer serializer = new XmlSerializer(typeof(Display));
  467. MemoryStream ms;
  468. StreamReader sr;
  469. display = new Display();
  470. display.ScreenType = ScreenType.ShowTrxList;
  471. display.RequestId = GetNextRequestId();
  472. display.TimeoutSpecified = true;
  473. display.Timeout = 300;
  474. requestId = display.RequestId;
  475. display.PumpInfo = new DisplayPumpInfo
  476. {
  477. Id = 1,
  478. NozzleId = 1
  479. };
  480. int count = epsTrxList.Count();
  481. display.TrxList = new DisplayTrx[epsTrxList.Count()];
  482. for (int index = 0; index < count; index++)
  483. {
  484. display.TrxList[index] = ConvertEpsTrxModelToDisplayTrx(epsTrxList.ToArray()[index]);
  485. }
  486. ms = new MemoryStream();
  487. serializer.Serialize(ms, display);
  488. ms.Position = 0;
  489. sr = new StreamReader(ms, true);
  490. cmdText = sr.ReadToEnd();
  491. ms.Close();
  492. sr.Close();
  493. return cmdText;
  494. }
  495. public DisplayTrx ConvertEpsTrxModelToDisplayTrx(EpsTransactionModel epsTrxModel)
  496. {
  497. TrxStatus state = TrxStatus.ReadyForFillingStart;
  498. if (epsTrxModel.trx_status == EpsTrxStatus.BeforeFueling)
  499. state = TrxStatus.ReadyForFillingStart;
  500. else if (epsTrxModel.trx_status == EpsTrxStatus.BeforePayment)
  501. state = TrxStatus.PendingForPayment;
  502. else if (epsTrxModel.trx_status == EpsTrxStatus.Fueling)
  503. state = TrxStatus.FillingOngoing;
  504. else if (epsTrxModel.trx_status == EpsTrxStatus.FuelingDone)
  505. state = TrxStatus.PendingForPayment;
  506. else if (epsTrxModel.trx_status == EpsTrxStatus.PaymentOk)
  507. state = TrxStatus.Success;
  508. else if (epsTrxModel.trx_status == EpsTrxStatus.PaymentFailed)
  509. state = TrxStatus.Failed;
  510. else if (epsTrxModel.trx_status == EpsTrxStatus.PaymentNeedConfirm)
  511. state = TrxStatus.Failed;
  512. else if (epsTrxModel.trx_status == EpsTrxStatus.PaymentOkButNeedRefund)
  513. state = TrxStatus.Failed;
  514. else if (epsTrxModel.trx_status == EpsTrxStatus.PaymentRefunded)
  515. state = TrxStatus.Failed;
  516. var displayTrx = new DisplayTrx
  517. {
  518. Id = epsTrxModel.id.ToString(),
  519. State = state,
  520. TimeStamp = new DisplayTrxTimeStamp
  521. {
  522. StartTime = epsTrxModel.created_time,
  523. FinishTime = string.IsNullOrEmpty(epsTrxModel.xf_time) ?
  524. epsTrxModel.created_time :
  525. Utilities.CombineDateAndTime(epsTrxModel.xf_date, epsTrxModel.xf_time)
  526. },
  527. MemberInfo = new DisplayTrxMemberInfo
  528. {
  529. Id = epsTrxModel.cardNo_masked,
  530. LicensePlateNo = epsTrxModel.car_number
  531. },
  532. FillingInfo = new DisplayTrxFillingInfo
  533. {
  534. PumpId = 1,
  535. NozzleId = epsTrxModel.jihao,
  536. ProductNo = 1,
  537. UnitPrice = (decimal)epsTrxModel.danjia,
  538. ProductType = epsTrxModel.youpin,
  539. ProductDiscription = "汽油",
  540. Amount = (decimal)epsTrxModel.real_pay_amount,
  541. Volume = (decimal)epsTrxModel.qty,
  542. }
  543. };
  544. return displayTrx;
  545. }
  546. public string CreateDisplayCommand(ScreenType screenType)
  547. {
  548. Display display;
  549. string cmdText = "";
  550. XmlSerializer serializer = new XmlSerializer(typeof(Display));
  551. MemoryStream ms;
  552. StreamReader sr;
  553. switch (screenType)
  554. {
  555. case ScreenType.Idle:
  556. display = new Display();
  557. display.ScreenType = screenType;
  558. display.CompanyContactInfo = new DisplayCompanyContactInfo
  559. {
  560. Tel = "010-59569575",
  561. Address = "世界500强企业\n中国第四大国家石油公司"
  562. };
  563. display.StationInfo = new DisplayStationInfo
  564. {
  565. StationNo = "000001",
  566. StationName = "沈阳望花中街加油加气站"
  567. };
  568. display.PumpInfo = new DisplayPumpInfo
  569. {
  570. Id = 1,
  571. NozzleId = 1
  572. };
  573. ms = new MemoryStream();
  574. serializer.Serialize(ms, display);
  575. ms.Position = 0;
  576. sr = new StreamReader(ms, true);
  577. cmdText = sr.ReadToEnd();
  578. break;
  579. case ScreenType.Welcome:
  580. display = new Display();
  581. display.ScreenType = screenType;
  582. display.CompanyContactInfo = new DisplayCompanyContactInfo
  583. {
  584. Tel = "010-59569575",
  585. Address = "世界500强企业\n中国第四大国家石油公司"
  586. };
  587. display.MemberInfo = new DisplayMemberInfo
  588. {
  589. LicensePlateNo = "京A88888",
  590. Id = "1234567"
  591. };
  592. display.PumpInfo = new DisplayPumpInfo
  593. {
  594. Id = 1,
  595. NozzleId = 1
  596. };
  597. ms = new MemoryStream();
  598. serializer.Serialize(ms, display);
  599. ms.Position = 0;
  600. sr = new StreamReader(ms, true);
  601. cmdText = sr.ReadToEnd();
  602. break;
  603. case ScreenType.ShowTrxList:
  604. display = new Display();
  605. display.ScreenType = screenType;
  606. display.PumpInfo = new DisplayPumpInfo
  607. {
  608. Id = 1,
  609. NozzleId = 1
  610. };
  611. display.TrxList = new DisplayTrx[]
  612. {
  613. new DisplayTrx
  614. {
  615. Id = "100",
  616. FillingInfo = new DisplayTrxFillingInfo
  617. {
  618. Amount = 200,
  619. NozzleId = 1,
  620. ProductType = "95#",
  621. UnitPrice = 6.78m,
  622. Volume = 35,
  623. ProductDiscription = "汽油"
  624. },
  625. MemberInfo = new DisplayTrxMemberInfo
  626. {
  627. LicensePlateNo = "京A88888",
  628. Id = "1234567"
  629. },
  630. State = TrxStatus.Success,
  631. TimeStamp = new DisplayTrxTimeStamp
  632. {
  633. StartTime = DateTime.Now,
  634. FinishTime =DateTime.Now
  635. }
  636. },
  637. new DisplayTrx
  638. {
  639. Id = "101",
  640. FillingInfo = new DisplayTrxFillingInfo
  641. {
  642. Amount = 211,
  643. NozzleId = 1,
  644. ProductType = "95#",
  645. UnitPrice = 6.78m,
  646. Volume = 35,
  647. ProductDiscription = "汽油"
  648. },
  649. MemberInfo = new DisplayTrxMemberInfo
  650. {
  651. LicensePlateNo = "京A88888",
  652. Id = "1234567"
  653. },
  654. State = TrxStatus.Success,
  655. TimeStamp = new DisplayTrxTimeStamp
  656. {
  657. StartTime = DateTime.Now,
  658. FinishTime =DateTime.Now
  659. }
  660. },
  661. new DisplayTrx
  662. {
  663. Id = "102",
  664. FillingInfo = new DisplayTrxFillingInfo
  665. {
  666. Amount = 222,
  667. NozzleId = 1,
  668. ProductType = "95#",
  669. UnitPrice = 6.78m,
  670. Volume = 35,
  671. ProductDiscription = "汽油"
  672. },
  673. MemberInfo = new DisplayTrxMemberInfo
  674. {
  675. LicensePlateNo = "京A88888",
  676. Id = "1234567"
  677. },
  678. State = TrxStatus.Success,
  679. TimeStamp = new DisplayTrxTimeStamp
  680. {
  681. StartTime = DateTime.Now,
  682. FinishTime =DateTime.Now
  683. }
  684. },
  685. new DisplayTrx
  686. {
  687. Id = "103",
  688. FillingInfo = new DisplayTrxFillingInfo
  689. {
  690. Amount = 233,
  691. NozzleId = 1,
  692. ProductType = "95#",
  693. UnitPrice = 6.78m,
  694. Volume = 35,
  695. ProductDiscription = "汽油"
  696. },
  697. MemberInfo = new DisplayTrxMemberInfo
  698. {
  699. LicensePlateNo = "京A88888",
  700. Id = "1234567"
  701. },
  702. State = TrxStatus.Success,
  703. TimeStamp = new DisplayTrxTimeStamp
  704. {
  705. StartTime = DateTime.Now,
  706. FinishTime =DateTime.Now
  707. }
  708. },
  709. new DisplayTrx
  710. {
  711. Id = "104",
  712. FillingInfo = new DisplayTrxFillingInfo
  713. {
  714. Amount = 244,
  715. NozzleId = 1,
  716. ProductType = "95#",
  717. UnitPrice = 6.78m,
  718. Volume = 35,
  719. ProductDiscription = "汽油"
  720. },
  721. MemberInfo = new DisplayTrxMemberInfo
  722. {
  723. LicensePlateNo = "京A88888",
  724. Id = "1234567"
  725. },
  726. State = TrxStatus.Success,
  727. TimeStamp = new DisplayTrxTimeStamp
  728. {
  729. StartTime = DateTime.Now,
  730. FinishTime =DateTime.Now
  731. }
  732. }
  733. };
  734. ms = new MemoryStream();
  735. serializer.Serialize(ms, display);
  736. ms.Position = 0;
  737. sr = new StreamReader(ms, true);
  738. cmdText = sr.ReadToEnd();
  739. break;
  740. case ScreenType.TrxResult:
  741. display = new Display();
  742. display.ScreenType = screenType;
  743. display.PumpInfo = new DisplayPumpInfo
  744. {
  745. Id = 1,
  746. NozzleId = 1
  747. };
  748. display.TrxList = new DisplayTrx[]
  749. {
  750. new DisplayTrx
  751. {
  752. Id = "100",
  753. FillingInfo = new DisplayTrxFillingInfo
  754. {
  755. Amount = 200,
  756. NozzleId = 1,
  757. ProductType = "95#",
  758. UnitPrice = 6.78m,
  759. Volume = 35,
  760. ProductDiscription = "汽油"
  761. },
  762. MemberInfo = new DisplayTrxMemberInfo
  763. {
  764. LicensePlateNo = "京A88888",
  765. Id = "1234567"
  766. },
  767. State = TrxStatus.Success,
  768. TimeStamp = new DisplayTrxTimeStamp
  769. {
  770. StartTime = DateTime.Now,
  771. FinishTime =DateTime.Now
  772. }
  773. }
  774. };
  775. ms = new MemoryStream();
  776. serializer.Serialize(ms, display);
  777. ms.Position = 0;
  778. sr = new StreamReader(ms, true);
  779. cmdText = sr.ReadToEnd();
  780. break;
  781. default:
  782. throw new Exception("Unsupported screen type");
  783. }
  784. return cmdText;
  785. }
  786. #region IDisposable Support
  787. private bool disposedValue = false; // To detect redundant calls
  788. protected virtual void Dispose(bool disposing)
  789. {
  790. if (!disposedValue)
  791. {
  792. if (disposing)
  793. {
  794. // TODO: dispose managed state (managed objects).
  795. if (fuelingPoints != null)
  796. {
  797. foreach (var fp in fuelingPoints)
  798. {
  799. fp.Dispose();
  800. }
  801. }
  802. if (epsTrxCleanupManager != null)
  803. {
  804. epsTrxCleanupManager.Dispose();
  805. }
  806. }
  807. // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
  808. // TODO: set large fields to null.
  809. disposedValue = true;
  810. }
  811. }
  812. // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
  813. // ~Eps() {
  814. // // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  815. // Dispose(false);
  816. // }
  817. // This code added to correctly implement the disposable pattern.
  818. public void Dispose()
  819. {
  820. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  821. Dispose(true);
  822. // TODO: uncomment the following line if the finalizer is overridden above.
  823. // GC.SuppressFinalize(this);
  824. }
  825. #endregion
  826. }
  827. }