PumpHandler.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using Edge.Core.Database.Models;
  2. using Edge.Core.Processor;using Edge.Core.IndustryStandardInterface.Pump;
  3. using LanTian_Pump_664_Or_886.MessageEntity;
  4. using LanTian_Pump_664_Or_886.MessageEntity.Incoming;
  5. using LanTian_Pump_664_Or_886.MessageEntity.Outgoing;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.Extensions.Logging.Abstractions;
  8. using Edge.Core.Parser.BinaryParser.Util;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Xml;
  16. using Wayne.FDCPOSLibrary;
  17. using Edge.Core.IndustryStandardInterface.Pump;
  18. namespace LanTian_Pump_664_Or_886
  19. {
  20. public class PumpHandler : IFdcPumpController, IDeviceHandler<byte[], MessageBase>
  21. {
  22. public enum PumpModel
  23. {
  24. // for all cases, like totalizer, amount, price data fields and etc., has smaller value range
  25. Model_664 = 0,
  26. // for all cases, like totalizer, amount, price data fields and etc., has wider value range
  27. Model_886 = 1
  28. }
  29. public enum PumpAuthorizeMode
  30. {
  31. FC_Authorize = 0,
  32. Pump_Self_Authorize = 1,
  33. }
  34. static ILogger logger = NullLogger.Instance;
  35. private LogicalDeviceState lastLogicalDeviceState = LogicalDeviceState.FDC_OFFLINE;
  36. private DateTime lastLogicalDeviceStateReceivedTime;
  37. // by seconds, change this value need change the correlated deviceOfflineCountdownTimer's interval as well
  38. public const int lastLogicalDeviceStateExpiredTime = 9;
  39. private List<LogicalNozzle> nozzles = new List<LogicalNozzle>();
  40. private System.Timers.Timer deviceOfflineCountdownTimer;
  41. private IContext<byte[], MessageBase> context;
  42. private bool isOnFdcServerInitCalled = false;
  43. private int pumpId;
  44. private PumpModel pumpModel;
  45. private PumpAuthorizeMode pumpAuthorizeMode;
  46. private int amountDecimalDigits;
  47. private int volumeDecimalDigits;
  48. private int priceDecimalDigits;
  49. private int volumeTotalizerDecimalDigits;
  50. /// <summary>
  51. /// for avoid a case that FC may miss a pump status change event(pump side issue? or wire issue?),
  52. /// we timely actively request the pump state.
  53. /// </summary>
  54. //private System.Timers.Timer pollingPumpStatus;
  55. //private int pollingPumpStatusInterval = 30000;
  56. #region
  57. public string Name => "LanTian_Pump_664_Or_886";
  58. public int PumpId => this.pumpId;
  59. public IEnumerable<LogicalNozzle> Nozzles => this.nozzles;
  60. public int AmountDecimalDigits => this.amountDecimalDigits;
  61. public int VolumeDecimalDigits => this.volumeDecimalDigits;
  62. public int PriceDecimalDigits => this.priceDecimalDigits;
  63. public int VolumeTotalizerDecimalDigits => this.volumeDecimalDigits;
  64. public Guid Id => Guid.NewGuid();
  65. public int PumpPhysicalId => throw new NotImplementedException();
  66. public event EventHandler<FdcPumpControllerOnStateChangeEventArg> OnStateChange;
  67. public event EventHandler<FdcTransactionDoneEventArg> OnCurrentFuellingStatusChange;
  68. public async Task<global::Wayne.FDCPOSLibrary.LogicalDeviceState> QueryStatusAsync()
  69. {
  70. return this.lastLogicalDeviceState;
  71. }
  72. public async Task<bool> LockNozzleAsync(byte logicalNozzleId)
  73. {
  74. return false;
  75. }
  76. public async Task<bool> UnlockNozzleAsync(byte logicalNozzleId)
  77. {
  78. return false;
  79. }
  80. #endregion
  81. public PumpHandler(int pumpId) : this(pumpId, PumpModel.Model_664, PumpAuthorizeMode.FC_Authorize, 2, 2, 2, 2)
  82. {
  83. }
  84. public PumpHandler(int pumpId, PumpModel pumpModel, PumpAuthorizeMode pumpAuthorizeMode,
  85. int amountDecimalDigits, int volumeDecimalDigits,
  86. int priceDecimalDigits, int volumeTotalizerDecimalDigits)
  87. {
  88. this.pumpId = pumpId;
  89. this.pumpModel = pumpModel;
  90. this.pumpAuthorizeMode = pumpAuthorizeMode;
  91. this.amountDecimalDigits = amountDecimalDigits;
  92. this.volumeDecimalDigits = volumeDecimalDigits;
  93. this.priceDecimalDigits = priceDecimalDigits;
  94. this.volumeTotalizerDecimalDigits = volumeTotalizerDecimalDigits;
  95. this.pumpId = pumpId;
  96. // real nozzle Id put hardcode 1 here since HengShan_pump_nonIC only have 1 nozzle per pump.
  97. // price is not dectected yet, put Null.
  98. this.nozzles.Add(new LogicalNozzle(pumpId, 1, 1, null));
  99. this.deviceOfflineCountdownTimer = new System.Timers.Timer(3000);
  100. this.deviceOfflineCountdownTimer.Elapsed += (_, __) =>
  101. {
  102. if (DateTime.Now.Subtract(this.lastLogicalDeviceStateReceivedTime).TotalSeconds
  103. >= lastLogicalDeviceStateExpiredTime)
  104. {
  105. if (this.lastLogicalDeviceState != LogicalDeviceState.FDC_OFFLINE)
  106. {
  107. this.lastLogicalDeviceState = LogicalDeviceState.FDC_OFFLINE;
  108. logger.LogInformation("Pump: " + this.pumpId + ", " + " State switched to FDC_OFFLINE due to long time no see pump data incoming");
  109. var safe0 = this.OnStateChange;
  110. safe0?.Invoke(this, new FdcPumpControllerOnStateChangeEventArg(LogicalDeviceState.FDC_OFFLINE, null));
  111. logger.LogTrace("Pump: " + this.pumpId + ", " + " OnStateChange event fired and back");
  112. }
  113. }
  114. };
  115. this.deviceOfflineCountdownTimer.Start();
  116. }
  117. public void OnFdcServerInit(Dictionary<string, object> parameters)
  118. {
  119. if (parameters.ContainsKey("LastPriceChange"))
  120. {
  121. // nozzle logical id:rawPrice
  122. var lastPriceChanges = parameters["LastPriceChange"] as Dictionary<byte, int>;
  123. foreach (var priceChange in lastPriceChanges)
  124. {
  125. logger.LogInformation("Pump: " + this.pumpId + ", " + "Pump " + this.pumpId + " OnFdcServerInit, load last price change " +
  126. "on logical nozzle: " + priceChange.Key + " with price: " + priceChange.Value);
  127. this.nozzles.First(n => n.LogicalId == priceChange.Key).ExpectingPriceOnFcSide = priceChange.Value;
  128. }
  129. }
  130. /* Load Last sale trx(from db) for void the case of FC accidently disconnect from Pump in fueling,
  131. and may cause a fueling trx gone from FC control */
  132. if (parameters.ContainsKey("LastFuelSaleTrx"))
  133. {
  134. // nozzle logical id:LastSale
  135. var lastFuelSaleTrxes = parameters["LastFuelSaleTrx"] as Dictionary<byte, FuelSaleTransaction>;
  136. foreach (var lastFuelSaleTrx in lastFuelSaleTrxes)
  137. {
  138. logger.LogInformation("Pump: " + this.pumpId + ", OnFdcServerInit, load last volume Totalizer " +
  139. "on logical nozzle: " + lastFuelSaleTrx.Key + " with volume value: " + lastFuelSaleTrx.Value.VolumeTotalizer);
  140. this.nozzles.First(n => n.LogicalId == lastFuelSaleTrx.Key).VolumeTotalizer = lastFuelSaleTrx.Value.VolumeTotalizer;
  141. }
  142. }
  143. this.isOnFdcServerInitCalled = true;
  144. }
  145. public void Init(IContext<byte[], MessageBase> context)
  146. {
  147. this.context = context;
  148. var timeWindowWithActivePollingOutgoing =
  149. this.context.Outgoing as TimeWindowWithActivePollingOutgoing<byte[], MessageBase>;
  150. timeWindowWithActivePollingOutgoing.PollingMsgProducer = () => new ReadPumpStateRequest();
  151. }
  152. public async Task Process(IContext<byte[], MessageBase> context)
  153. {
  154. if (!isOnFdcServerInitCalled) return;
  155. this.lastLogicalDeviceStateReceivedTime = DateTime.Now;
  156. if (this.lastLogicalDeviceState == LogicalDeviceState.FDC_OFFLINE)
  157. {
  158. logger.LogInformation("Pump: " + this.pumpId + ", " + "Recevied an Pump Msg in FDC_OFFLINE state, " +
  159. "indicates the underlying connection is established, switch to FDC_READY");
  160. this.lastLogicalDeviceState = LogicalDeviceState.FDC_READY;
  161. this.OnStateChange?.Invoke(this, new FdcPumpControllerOnStateChangeEventArg(LogicalDeviceState.FDC_READY));
  162. logger.LogTrace("Pump: " + this.pumpId + ", " + " OnStateChange event fired and back");
  163. #region Start the Initial pump change price process
  164. var readPriceResponse = await this.context.Outgoing.WriteAsync(new ReadPriceRequest(),
  165. (request, response) => response is ReadPriceResponse, 3000).ConfigureAwait(false) as ReadPriceResponse;
  166. if (readPriceResponse == null)
  167. {
  168. logger.LogInformation("Pump: " + this.pumpId + ", " + " Reading initial pump side price timedout, will ignore this error and won't send a ChangePriceRequest here but may need a manual Price change if find price discrepancy");
  169. return;
  170. }
  171. this.nozzles.First().RealPriceOnPhysicalPump = readPriceResponse.Price;
  172. if (this.nozzles.First().ExpectingPriceOnFcSide.HasValue
  173. && readPriceResponse.Price != this.nozzles.First().ExpectingPriceOnFcSide.Value)
  174. {
  175. logger.LogInformation("Pump: " + this.pumpId + ", " + " Read the initial pump side price(without decimal points): " + readPriceResponse.Price + " which NOT Equals with the price as FC expect, will start a change price internally...");
  176. var changePriceResponse = await this.context.Outgoing.WriteAsync(
  177. new ChangePriceRequest(this.nozzles.First().ExpectingPriceOnFcSide.Value, this.pumpModel == PumpModel.Model_664 ? (byte)2 : (byte)3),
  178. (request, response) => response is ChangePriceResponse, 3000).ConfigureAwait(false) as ChangePriceResponse;
  179. if (changePriceResponse == null || !changePriceResponse.Succeed)
  180. {
  181. logger.LogInformation("Pump: " + this.pumpId + ", " + " Initial change price timedout or failed, may need a manual price change later if find price discrepancy");
  182. return;
  183. }
  184. var doubleConfirm_readPriceResponse = await this.context.Outgoing.WriteAsync(new ReadPriceRequest(),
  185. (request, response) => response is ReadPriceResponse, 3000).ConfigureAwait(false) as ReadPriceResponse;
  186. if (doubleConfirm_readPriceResponse == null)
  187. logger.LogInformation("Pump: " + this.pumpId + ", " + " Double confirm Initial change price timedout, so FC can't sure if the new price applied, or a manual Price change is needed if find price discrepancy");
  188. else if (doubleConfirm_readPriceResponse.Price == this.nozzles.First().ExpectingPriceOnFcSide.Value)
  189. logger.LogInformation("Pump: " + this.pumpId + ", " + " Double confirm succeed for new FC price(without decimal points): " + this.nozzles.First().ExpectingPriceOnFcSide.Value + " have been applied to pump.");
  190. else
  191. logger.LogInformation("Pump: " + this.pumpId + ", " + " Double confirm failed for new FC price(without decimal points): " + this.nozzles.First().ExpectingPriceOnFcSide.Value + " failed applying to pump as pump side still report its price(without decimal point): " + doubleConfirm_readPriceResponse.Price + ", a manual Change Price is needed");
  192. }
  193. else
  194. logger.LogInformation("Pump: " + this.pumpId + ", " + " Read the initial pump side price(without decimal points): " + readPriceResponse.Price + ", no need to align with FC(FC has no expecting price)");
  195. #endregion
  196. }
  197. if (context.Incoming.Message is ReadPumpStateResponse readPumpStateResponse)
  198. {
  199. #region AcquireControl if pump authorize mode is FC_Authorize
  200. if (readPumpStateResponse.ControlState == ReadPumpStateResponse.ControlStateEnum.自控
  201. && this.pumpAuthorizeMode == PumpAuthorizeMode.FC_Authorize)
  202. {
  203. var acquireControlResponse = await this.context.Outgoing.WriteAsync(
  204. new AcquireControlRequest(),
  205. (request, response) => response is AcquireControlResponse, 3000).ConfigureAwait(false) as AcquireControlResponse;
  206. if (acquireControlResponse == null)
  207. {
  208. logger.LogInformation("Pump: " + this.pumpId + ", " + " AcquireControlResponse first time timedout, will send 2nd time...");
  209. acquireControlResponse = await this.context.Outgoing.WriteAsync(
  210. new AcquireControlRequest(),
  211. (request, response) => response is AcquireControlResponse, 3000).ConfigureAwait(false) as AcquireControlResponse;
  212. if (acquireControlResponse == null)
  213. logger.LogInformation("Pump: " + this.pumpId + ", " + " AcquireControlResponse 2nd time timedout, will ignore this error, then the pump may have wrong authorize mode");
  214. }
  215. }
  216. #endregion
  217. if (this.lastLogicalDeviceState != LogicalDeviceState.FDC_FUELLING
  218. && readPumpStateResponse.NozzleState == ReadPumpStateResponse.NozzleStateEnum.提枪)
  219. {
  220. logger.LogDebug("Pump: " + this.pumpId + ", " + " Nozzle state report as NozzleStateEnum.提枪, State switched to FDC_CALLING");
  221. this.lastLogicalDeviceState = LogicalDeviceState.FDC_CALLING;
  222. this.OnStateChange?.Invoke(this, new FdcPumpControllerOnStateChangeEventArg(LogicalDeviceState.FDC_CALLING, this.nozzles.First()));
  223. }
  224. if (this.lastLogicalDeviceState == LogicalDeviceState.FDC_AUTHORISED
  225. && readPumpStateResponse.NozzleState == ReadPumpStateResponse.NozzleStateEnum.提枪)
  226. {
  227. logger.LogDebug("Pump: " + this.pumpId + ", " + " internal Fdc State: " + this.lastLogicalDeviceState + ", Nozzle State switched to FDC_FUELLING");
  228. this.lastLogicalDeviceState = LogicalDeviceState.FDC_FUELLING;
  229. this.OnStateChange?.Invoke(this, new FdcPumpControllerOnStateChangeEventArg(LogicalDeviceState.FDC_FUELLING, this.nozzles.First()));
  230. while (true)
  231. {
  232. var readFuelDataResponse = await this.context.Outgoing.WriteAsync(
  233. new ReadFuelDataRequest(),
  234. (request, response) => response is ReadFuelDataResponse, 3000).ConfigureAwait(false) as ReadFuelDataResponse;
  235. if (readFuelDataResponse == null)
  236. {
  237. }
  238. else
  239. {
  240. logger.LogDebug("Pump: " + this.pumpId + ", " + " fueling in progress with amt: " + readFuelDataResponse.Amount
  241. + ", vol: " + readFuelDataResponse.Volume);
  242. // at least within 65 years, exception will not throw here
  243. //int newTrxSeqNumber = (int)(DateTime.Now.Subtract(new DateTime(2018, 5, 25)).TotalSeconds);
  244. //fire fuelling progress.
  245. this.OnCurrentFuellingStatusChange?.Invoke(this, new FdcTransactionDoneEventArg(new FdcTransaction()
  246. {
  247. // 恒山油机只有一把枪
  248. Nozzle = this.nozzles.First(),
  249. Amount = readFuelDataResponse.Amount,
  250. Volumn = readFuelDataResponse.Volume,
  251. Price = this.nozzles.First().RealPriceOnPhysicalPump ?? -1,
  252. Finished = false,
  253. }));
  254. }
  255. }
  256. }
  257. if (this.lastLogicalDeviceState == LogicalDeviceState.FDC_FUELLING
  258. && readPumpStateResponse.NozzleState == ReadPumpStateResponse.NozzleStateEnum.挂枪)
  259. {
  260. logger.LogDebug("Pump: " + this.pumpId + ", " + "internal Fdc State: " + this.lastLogicalDeviceState + ", Nozzle state report as NozzleStateEnum.挂枪 ,State switched to FDC_READY");
  261. this.lastLogicalDeviceState = LogicalDeviceState.FDC_READY;
  262. this.OnStateChange?.Invoke(this, new FdcPumpControllerOnStateChangeEventArg(LogicalDeviceState.FDC_READY, this.nozzles.First()));
  263. }
  264. }
  265. }
  266. public Task<System.Tuple<int, int>> QueryTotalizerAsync(byte logicalNozzleId)
  267. {
  268. throw new NotImplementedException();
  269. }
  270. public Task<bool> SuspendFuellingAsync()
  271. {
  272. throw new NotImplementedException();
  273. }
  274. public Task<bool> ResumeFuellingAsync()
  275. {
  276. throw new NotImplementedException();
  277. }
  278. public Task<bool> ChangeFuelPriceAsync(int newPriceWithoutDecimalPoint, byte logicalNozzleId)
  279. {
  280. throw new NotImplementedException();
  281. }
  282. public Task<bool> AuthorizeAsync(byte logicalNozzleId)
  283. {
  284. throw new NotImplementedException();
  285. }
  286. public Task<bool> UnAuthorizeAsync(byte logicalNozzleId)
  287. {
  288. throw new NotImplementedException();
  289. }
  290. public Task<bool> AuthorizeWithAmountAsync(int moneyAmountWithoutDecimalPoint, byte logicalNozzleId)
  291. {
  292. throw new NotImplementedException();
  293. }
  294. public Task<bool> AuthorizeWithVolumeAsync(int volumnWithoutDecimalPoint, byte logicalNozzleId)
  295. {
  296. throw new NotImplementedException();
  297. }
  298. public Task<bool> FuelingRoundUpByAmountAsync(int amount)
  299. {
  300. throw new NotImplementedException();
  301. }
  302. public Task<bool> FuelingRoundUpByVolumeAsync(int volume)
  303. {
  304. throw new NotImplementedException();
  305. }
  306. }
  307. }