CardMessageBase.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using Edge.Core.Parser.BinaryParser.Attributes;
  2. using Edge.Core.Parser.BinaryParser.Util;
  3. using Edge.Core.Parser.BinaryParser.MessageEntity;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace HengshanPaymentTerminal.MessageEntity
  10. {
  11. public enum Command
  12. {
  13. /// <summary>
  14. /// Regular check command from pump against system.
  15. /// 加油机查询命令。
  16. /// </summary>
  17. Check_cmd = 0x10,
  18. /// <summary>
  19. /// Result of the check command.
  20. /// 查询命令的应答。
  21. /// </summary>
  22. Ver_info = 0x11,
  23. //*************************************************************
  24. /// <summary>
  25. /// Register terminal after power-on.
  26. /// 支付终端上电注册。
  27. /// </summary>
  28. RegisterRequest = 0x13,
  29. /// <summary>
  30. /// 后台回应支付终端上电注册。
  31. /// </summary>
  32. RegisterResult = 0x14,
  33. //*************************************************************
  34. /// <summary>
  35. /// Validate card from terminal to system.
  36. /// 验卡请求。
  37. /// </summary>
  38. ValidateCardRequest = 0x15,
  39. /// <summary>
  40. /// Validate card result from the system.
  41. /// 后台返回验卡结果。
  42. /// </summary>
  43. ValidateCardResult = 0x16,
  44. //*************************************************************
  45. AuthRequest = 0x17,
  46. AuthResult = 0x18,
  47. //*************************************************************
  48. /// <summary>
  49. /// Cancellation of authorization request from terminal to system.
  50. /// 支付终端发起撤销授权请求。
  51. /// </summary>
  52. CancelAuthRequest = 0x19,
  53. /// <summary>
  54. /// The system returns result of cancellation of auth.
  55. /// 后台返回撤销授权结果。
  56. /// </summary>
  57. CancelAuthResult = 0x1A,
  58. //*************************************************************
  59. ChangeAuthMode = 0x1B,
  60. ChangeAuthModeAck = 0x20,
  61. //*************************************************************
  62. FuelingData = 0x1C,
  63. FuelingDataResult = 0x35,
  64. //*************************************************************
  65. /// <summary>
  66. /// Change fuel price from system to pump.
  67. /// 改价命令。
  68. /// </summary>
  69. ChangePrice = 0x1E,
  70. /// <summary>
  71. /// Change fuel price result from pump to system.
  72. /// 油机返回改价结果。
  73. /// </summary>
  74. ChangePriceResult = 0x1F,
  75. //*************************************************************
  76. QueryGrayRecordRequest = 0x21,
  77. GrayRecord = 0x22,
  78. //*************************************************************
  79. ReadVolumeTotalizer = 0x23,
  80. VolumeTotalizerResult = 0x24,
  81. //*************************************************************
  82. DataRequest = 0x25,
  83. DataBytesLength = 0x26,
  84. DataContentRequest = 0x27,
  85. DataContent = 0x28,
  86. //*************************************************************
  87. DispenserInfoRequest = 0x29,
  88. DispenserInfo = 0x2A,
  89. //*************************************************************
  90. SendErrorInfoRequest = 0x2B,
  91. ErrorInfoAck = 0x2C,
  92. //*************************************************************
  93. TransactionData = 0x2D,
  94. TransactionDataAck = 0x32,
  95. //*************************************************************
  96. SystemAuth = 0x2E,
  97. SystemAuthResult = 0x31,
  98. OtherNonCardAuth = 0x2F,
  99. //*************************************************************
  100. /// <summary>
  101. /// Payment request from terminal.
  102. /// 终端发起支付请求,命令字30H。
  103. /// </summary>
  104. PaymentRequest = 0x30,
  105. PaymentData = 0x1D,
  106. //*************************************************************
  107. QueryTransactionInfo = 0x33,
  108. TransactionInfoResult = 0x34,
  109. //*************************************************************
  110. RecordCallback = 0x36,
  111. RecordCallbackAck = 0x37,
  112. //*************************************************************
  113. QueryTransactionData = 0x38,
  114. QueryTransactionDataAck = 0x39,
  115. //*************************************************************
  116. AuthManualCancel = 0x40,
  117. AuthManualCancelAck = 0x41,
  118. //*************************************************************
  119. EnableAuth = 0x42,
  120. EnableAuthAck = 0x43,
  121. //*************************************************************
  122. /// <summary>
  123. /// Lock or unlock a pump.
  124. /// 锁机或解锁命令。
  125. /// </summary>
  126. LockOrUnlockPump = 0x44,
  127. /// <summary>
  128. /// The result of locking or unlocking a pump.
  129. /// 锁机或解锁命令的回复。
  130. /// </summary>
  131. LockOrUnlockPumpAck = 0x45,
  132. //*************************************************************
  133. SystemOperationAck = 0x47,
  134. GetRfTagInfo = 0x46,
  135. VerifyFingerPrint = 0x48,
  136. ReturnTotalizer = 0x49
  137. }
  138. /// <summary>
  139. /// Enumeration of the calling parties, either pump or system
  140. /// 主叫方,加油机或后台
  141. /// </summary>
  142. public enum CallingParty
  143. {
  144. /// <summary>
  145. /// System/后台
  146. /// </summary>
  147. System,
  148. /// <summary>
  149. /// Pump/加油机,实际上是终端
  150. /// </summary>
  151. Pump
  152. }
  153. /// <summary>
  154. /// Base class of IC card terminal messages.
  155. /// IC卡终端与后台交互的消息,基类。
  156. /// </summary>
  157. public abstract class CardMessageBase : MessageTemplateBase
  158. {
  159. public CardMessageBase(Command handle)
  160. {
  161. //Handle = handle;
  162. }
  163. /// <summary>
  164. /// Get the caller (Frame sequence number bit 6 indicates the calling party, 0 = system, 1 = pump)
  165. /// 获取主叫方。命令/数据帧号,第6个bit,0 = 后台, 1 = 加油机
  166. /// </summary>
  167. public CallingParty Caller
  168. {
  169. get
  170. {
  171. if (FrameSqNoByte.GetBit(6) == 1)
  172. return CallingParty.Pump;
  173. return CallingParty.System;
  174. }
  175. }
  176. /// <summary>
  177. /// Static field, 'FA'
  178. /// 数据包头,长度1字节,Hex “FA”
  179. /// </summary>
  180. [Format(1, EncodingType.BIN, -100)]
  181. public virtual byte Prefix { get; set; }
  182. /// <summary>
  183. /// The destination address this message targets.
  184. /// 目标地址,长度1字节
  185. /// </summary>
  186. [Format(1, EncodingType.BIN, -99)]
  187. public virtual byte DestinationAddress { get; set; }
  188. /// <summary>
  189. /// The source address of the message.
  190. /// 源地址,长度1字节
  191. /// </summary>
  192. [Format(1, EncodingType.BIN, -98)]
  193. public virtual byte SourceAddress { get; set; }
  194. /// <summary>
  195. /// Command/Data frame sequence number.
  196. /// 命令/数据帧号
  197. /// 格式:
  198. /// b7=0;b6=0时,后台主叫,b6=1,油机主叫;
  199. /// b5~b0为帧号,主叫方每发送一新帧,此帧号加一,应答方回送此帧号。
  200. /// </summary>
  201. [Format(1, EncodingType.BIN, -97)]
  202. public virtual byte FrameSqNoByte { get; set; }
  203. /// <summary>
  204. /// The frame number is in the FrameSqNoByte bit 5 to bit 0 (6 bits).
  205. /// 帧号为 命令/数据帧号 字节的b5-b0,共6个bit。
  206. /// </summary>
  207. public virtual int FrameNo
  208. {
  209. get { return FrameSqNoByte & 0x3F; }
  210. }
  211. /// <summary>
  212. /// Set the calling party of the message
  213. /// 设置此消息的主叫方
  214. /// </summary>
  215. /// <param name="party"></param>
  216. public void SetCallingParty(CallingParty party)
  217. {
  218. FrameSqNoByte = FrameSqNoByte.SetBit(6, 6, party == CallingParty.Pump ? 1 : 0);
  219. }
  220. public void SetSeqNo(byte sqNo)
  221. {
  222. // sequence number is max 5 bits.
  223. if (sqNo > 63)
  224. throw new ArgumentOutOfRangeException("maximum sequenceNumber is 63(total 6 bits).");
  225. var debug = FrameSqNoByte >> 6 << 6;
  226. FrameSqNoByte = (byte)(debug + sqNo);
  227. }
  228. /// <summary>
  229. /// Length of message body
  230. /// 有效数据长度,2字节,HEX
  231. /// </summary>
  232. [Format(2, "%OnSerializingBytesCount", EncodingType.BIN, -96)]
  233. public virtual int BodyLength { get; set; }
  234. /// <summary>
  235. /// Command type
  236. /// 命令字
  237. /// </summary>
  238. [Format(1, EncodingType.BIN, -95)]
  239. public byte Handle { get; set; }
  240. /// <summary>
  241. /// The meaningful body of the message.
  242. /// 消息有效数据
  243. /// </summary>
  244. //[EnumerableFormat("BodyLength", "-2", 2, EncodingType = EncodingType.BIN)]
  245. //public List<byte> Body { get; set; }
  246. //[Format(1, EncodingType.BIN, -1)]
  247. public byte ETX { get; set; }
  248. /// <summary>
  249. /// CRC of the whole message skipping additional 'FA'
  250. /// 消息的CRC校验,转义'FA'字节不计入
  251. /// </summary>
  252. [EnumerableFormat(2, 1000, EncodingType = EncodingType.BIN)]
  253. public virtual List<byte> CRC { get; set; }
  254. public override string ToLogString()
  255. {
  256. return GetType().Name + " " + base.ToLogString().Replace(
  257. "FrameSqNoByte:", "(Caller: " + Caller + ", FrameNo: " + FrameNo + ")FrameSqNoByte:");
  258. }
  259. }
  260. }