SilentPumpHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using Edge.Core.Processor;using Edge.Core.IndustryStandardInterface.Pump;
  2. using HengShan_Pump_TQC_IFSF.MessageEntity;
  3. using HengShan_Pump_TQC_IFSF.MessageEntity.Incoming;
  4. using HengShan_Pump_TQC_IFSF.MessageEntity.Outgoing;
  5. using Edge.Core.Parser.BinaryParser.Util;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Globalization;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Xml;
  14. using Wayne.FDCPOSLibrary;
  15. using Timer = System.Timers.Timer;
  16. namespace HengShan_Pump_TQC_IFSF
  17. {
  18. public class SilentPumpHandler : PumpHandler
  19. {
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. /// <param name="pumpId"></param>
  24. /// <param name="ifsfFuelPointId">range from 0x21 to 0x24</param>
  25. /// <param name="recipientSubnet">used to locate the remote tqc pump, must a value different from FC</param>
  26. /// <param name="recipientNode">used to locate the remote tqc pump</param>
  27. /// <param name="nozzlesXmlConfiguration"></param>
  28. public SilentPumpHandler(PumpGroupHandler parent, int pumpId, byte ifsfFuelPointId, byte recipientSubnet, byte recipientNode, string nozzlesXmlConfiguration, Func<byte> msgTokenGenerator)
  29. : base(parent, pumpId, ifsfFuelPointId, recipientSubnet, recipientNode, nozzlesXmlConfiguration, msgTokenGenerator)
  30. {
  31. //sample of nozzlesXmlConfiguration
  32. // <Pump id='3'>
  33. // <Nozzles>
  34. // <Nozzle logicalId='1' physicalId='1' ifsfLogicalNozzleId="0x11"/>
  35. // <Nozzle logicalId='2' physicalId='2' ifsfLogicalNozzleId="0x12"/>
  36. // </Nozzles>
  37. // </Pump>
  38. }
  39. public override Task Process(IContext<byte[], IfsfMessageBase> context)
  40. {
  41. this.context = context;
  42. if (context.Incoming.Message is IfsfMessage ifsfMsg
  43. && ifsfMsg.MessageType == MessageType.IFSF_MESSAGE_TYPE_UNSOLICITED_ACK)
  44. {
  45. //logger.Debug("PumpHandlerSilent with ifsfFpId 0x" + base.ifsfFuelPointId.ToHexLogString() + " will ack the Ack requried event!");
  46. context.Outgoing.Write(
  47. new AcknowledgeMessage(
  48. ifsfMsg.OriginatorSubnet,
  49. ifsfMsg.OriginatorNode,
  50. PumpGroupHandler.originatorSubnet, PumpGroupHandler.originatorNode,
  51. ifsfMsg.MessageToken,
  52. ifsfMsg.DatabaseId,
  53. MessageAcknowledgeStatus.ACK_PositiveAcknowledgeDataReceived
  54. ));
  55. }
  56. switch (context.Incoming.Message)
  57. {
  58. case LogicalNozzleDb_Nozzle_PhyId_Event_ACK usedAsExternalNozzleIdConfigurationEvent:
  59. // physical nozzle id is actually site level nozzle id.
  60. break;
  61. case FuellingPointDb_FpStatus_Event fpStatusEvent:
  62. {
  63. // when trx event piped in, there's no nozzle info linked it within the event(ifsf bad design?), so here always track
  64. // and keep the state of the last calling or fuelling nozzle.
  65. if (fpStatusEvent.FuelPointState == FuellingPointStatus.CALLING || fpStatusEvent.FuelPointState == FuellingPointStatus.FUELLING)
  66. {
  67. var operatingNozzlePhysicalId = (byte)(fpStatusEvent.RemovedNozzleIndexes.First() + 0x10);
  68. if (this.nozzles.Any(n => n.PhysicalId == operatingNozzlePhysicalId))
  69. base.currentCallingNozzleLogicalId = base.nozzles.First(n => n.PhysicalId == operatingNozzlePhysicalId).LogicalId;
  70. else
  71. base.currentCallingNozzleLogicalId = (fpStatusEvent.RemovedNozzleIndexes.Any() ? fpStatusEvent.RemovedNozzleIndexes.First() : (byte)1);
  72. }
  73. logger.Debug("Pump " + this.pumpId + " fpStatusEvent reported: " + fpStatusEvent.FuelPointState);
  74. this.HandleFpStatusChange(fpStatusEvent.FuelPointState,
  75. this.nozzles.FirstOrDefault(n => n.LogicalId == this.currentCallingNozzleLogicalId));
  76. break;
  77. }
  78. case FuellingTrxDb_TransactionBufferStatus_Event_ACK trxBufferStatusEvent:
  79. {
  80. if (trxBufferStatusEvent.Volume != 0 || trxBufferStatusEvent.Amount != 0)
  81. {
  82. logger.Debug("Pump " + this.pumpId
  83. + ", received a Payable trx from TQC with vol(without decimal points): " + trxBufferStatusEvent.Volume
  84. + ", amount(without decimal points): " + trxBufferStatusEvent.Amount
  85. + ", Price(without decimal points): " + trxBufferStatusEvent.Price);
  86. var operatingNozzle = this.nozzles.First(f => f.LogicalId == base.currentCallingNozzleLogicalId);
  87. base.FireOnCurrentFuellingStatusChangeEvent(new FdcTransaction()
  88. {
  89. Nozzle = operatingNozzle,
  90. Amount = trxBufferStatusEvent.Amount,
  91. Volumn = trxBufferStatusEvent.Volume,
  92. Price = int.Parse(trxBufferStatusEvent.Price),
  93. SequenceNumberGeneratedOnPhysicalPump = trxBufferStatusEvent.TransactionSeqNumber,
  94. Finished = true,
  95. });
  96. }
  97. break;
  98. }
  99. case FuellingPointDb_FpRunningTransaction_Event_ACK fpRunningTrxEvent:
  100. {
  101. var operatingNozzle = this.nozzles.First(f => f.LogicalId == base.currentCallingNozzleLogicalId);
  102. base.FireOnCurrentFuellingStatusChangeEvent(new FdcTransaction()
  103. {
  104. Nozzle = operatingNozzle,
  105. Amount = fpRunningTrxEvent.CurrentAmount,
  106. Volumn = fpRunningTrxEvent.CurrentVolume,
  107. Price = int.Parse(fpRunningTrxEvent.CurrentPrice),
  108. Finished = false,
  109. });
  110. break;
  111. }
  112. default:
  113. base.Process(context);
  114. break;
  115. }
  116. return Task.CompletedTask;
  117. }
  118. public override void HandleFpStatusChange(FuellingPointStatus newStatus, LogicalNozzle stateChangedNozzle)
  119. {
  120. switch (newStatus)
  121. {
  122. case FuellingPointStatus.Idle:
  123. {
  124. if (this.lastLogicalDeviceState != LogicalDeviceState.FDC_READY)
  125. {
  126. this.lastLogicalDeviceState = LogicalDeviceState.FDC_READY;
  127. this.FireOnStateChangeEvent(LogicalDeviceState.FDC_READY, stateChangedNozzle);
  128. }
  129. break;
  130. }
  131. case FuellingPointStatus.CALLING:
  132. {
  133. if (this.lastLogicalDeviceState != LogicalDeviceState.FDC_CALLING)
  134. {
  135. this.lastLogicalDeviceState = LogicalDeviceState.FDC_CALLING;
  136. this.FireOnStateChangeEvent(LogicalDeviceState.FDC_CALLING, stateChangedNozzle);
  137. }
  138. break;
  139. }
  140. case FuellingPointStatus.AUTHORISED:
  141. {
  142. if (this.lastLogicalDeviceState != LogicalDeviceState.FDC_AUTHORISED)
  143. {
  144. this.lastLogicalDeviceState = LogicalDeviceState.FDC_AUTHORISED;
  145. this.FireOnStateChangeEvent(LogicalDeviceState.FDC_AUTHORISED, stateChangedNozzle);
  146. }
  147. break;
  148. }
  149. case FuellingPointStatus.STARTED:
  150. {
  151. if (this.lastLogicalDeviceState != LogicalDeviceState.FDC_STARTED)
  152. {
  153. this.lastLogicalDeviceState = LogicalDeviceState.FDC_STARTED;
  154. this.FireOnStateChangeEvent(LogicalDeviceState.FDC_STARTED, stateChangedNozzle);
  155. }
  156. break;
  157. }
  158. case FuellingPointStatus.FUELLING:
  159. {
  160. if (this.lastLogicalDeviceState != LogicalDeviceState.FDC_FUELLING)
  161. {
  162. this.lastLogicalDeviceState = LogicalDeviceState.FDC_FUELLING;
  163. this.FireOnStateChangeEvent(LogicalDeviceState.FDC_FUELLING, stateChangedNozzle);
  164. }
  165. break;
  166. }
  167. case FuellingPointStatus.Inoperative:
  168. {
  169. if (this.lastLogicalDeviceState != LogicalDeviceState.FDC_OFFLINE)
  170. {
  171. this.lastLogicalDeviceState = LogicalDeviceState.FDC_OFFLINE;
  172. this.FireOnStateChangeEvent(LogicalDeviceState.FDC_OFFLINE, stateChangedNozzle);
  173. }
  174. break;
  175. }
  176. case FuellingPointStatus.Closed:
  177. {
  178. if (this.lastLogicalDeviceState != LogicalDeviceState.FDC_CLOSED)
  179. {
  180. this.lastLogicalDeviceState = LogicalDeviceState.FDC_CLOSED;
  181. this.FireOnStateChangeEvent(LogicalDeviceState.FDC_CLOSED, stateChangedNozzle);
  182. }
  183. break;
  184. }
  185. }
  186. }
  187. public override async Task<LogicalDeviceState> QueryStatusAsync()
  188. {
  189. return await Task.FromResult(base.lastLogicalDeviceState);
  190. }
  191. public override async Task<bool> AuthorizeAsync(byte logicalNozzleId)
  192. {
  193. return false;
  194. }
  195. public override async Task<bool> AuthorizeWithAmountAsync(int moneyAmountWithoutDecimalPoint, byte logicalNozzleId)
  196. {
  197. return false;
  198. }
  199. public override async Task<bool> AuthorizeWithVolumeAsync(int volumnWithoutDecimalPoint, byte logicalNozzleId)
  200. {
  201. return false;
  202. }
  203. public override async Task<bool> ChangeFuelPriceAsync(int newPriceWithoutDecimalPoint, byte logicalNozzleId)
  204. {
  205. return false;
  206. }
  207. public override async Task<bool> FuelingRoundUpByAmountAsync(int amount)
  208. {
  209. return false;
  210. }
  211. public override async Task<bool> FuelingRoundUpByVolumeAsync(int volume)
  212. {
  213. return false;
  214. }
  215. public override async Task<System.Tuple<int, int>> QueryTotalizerAsync(byte logicalNozzleId)
  216. {
  217. return new System.Tuple<int, int>(-1, -1);
  218. }
  219. }
  220. }