MessageTemplateLookup.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using HengShan_Pump_TQC_IFSF.MessageEntity;
  2. using HengShan_Pump_TQC_IFSF.MessageEntity.Incoming;
  3. using Edge.Core.Parser.BinaryParser;
  4. using Edge.Core.Parser.BinaryParser.MessageEntity;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace HengShan_Pump_TQC_IFSF
  11. {
  12. public class MessageTemplateLookup : IMessageTemplateLookup
  13. {
  14. public MessageTemplateBase GetMessageTemplateByRawBytes(byte[] bytes)
  15. {
  16. if (bytes.Length == 10
  17. && bytes[0] <= 254 && bytes[0] >= 1
  18. && bytes[1] <= 254 && bytes[1] >= 1
  19. && bytes[2] <= 254 && bytes[2] >= 1
  20. && bytes[3] <= 254 && bytes[3] >= 1
  21. && (((bytes[4] << 8) + bytes[5]) <= 65534))
  22. return new Heartbeat(1, 1, 1, 1, 1, 1, 1);
  23. else if ((byte)(bytes[5] & 0xE0) == 0xE0)
  24. {
  25. return new AcknowledgeMessage();
  26. }
  27. else if ((bytes[5] & 0xE0) == 0x80
  28. && bytes[8] == 0x03
  29. && bytes[9] >= 0x21 && bytes[9] <= 0x24
  30. && bytes[10] == 0x41)
  31. {
  32. return new ErrorCodeDb_Event();
  33. }
  34. else if (
  35. ((bytes[5] & 0xE0) == 0x80 || (bytes[5] & 0xE0) == 0x60)
  36. && bytes[8] == 0x01
  37. && bytes[9] >= 0x21 && bytes[9] <= 0x24
  38. && (bytes[10] == 0x64 || bytes[10] == 0x14))
  39. {
  40. return new FuellingPointDb_FpStatus_Event();
  41. }
  42. else if ((bytes[5] & 0xE0) == 0x20
  43. && bytes[8] == 0x01
  44. && bytes[9] >= 0x21 && bytes[9] <= 0x24
  45. && bytes[10] == 0x14)
  46. {
  47. return new FuellingPointDb_FpStatus_Answer();
  48. }
  49. else if ((bytes[5] & 0xE0) == 0x20
  50. && bytes[8] == 0x01
  51. && bytes[9] >= 0x21 && bytes[9] <= 0x24
  52. && bytes[10] == 0x04)
  53. {
  54. return new FuellingPointDb_Nb_Logical_Nozzle_Answer();
  55. }
  56. else if (
  57. ((bytes[5] & 0xE0) == 0x80)
  58. && bytes[8] == 0x01
  59. && bytes[9] >= 0x21 && bytes[9] <= 0x24
  60. && bytes[10] == 0x66 && bytes[11] == 0x00
  61. && bytes[12] == 0x22 && bytes[19] == 0x23)
  62. {
  63. return new FuellingPointDb_FpRunningTransaction_Event();
  64. }
  65. else if ((bytes[5] & 0xE0) == 0x60
  66. && bytes[8] == 0x01
  67. && bytes[9] >= 0x21 && bytes[9] <= 0x24
  68. && bytes[10] == 0x22
  69. && bytes[17] == 0x23
  70. && bytes[24] == 0x24)
  71. {
  72. return new FuellingPointDb_FpRunningTransaction_Event_ACK();
  73. }
  74. else if (
  75. (bytes[5] & 0xE0) == 0x80
  76. && bytes[8] == 0x04
  77. && bytes[9] >= 0x21 && bytes[9] <= 0x24
  78. && bytes[10] == 0x21
  79. && bytes[11] <= 0x99
  80. && bytes[12] <= 0x99
  81. && bytes[13] == 0x64)
  82. {
  83. return new FuellingTrxDb_TransactionBufferStatus_Event();
  84. }
  85. else if (
  86. (bytes[5] & 0xE0) == 0x60
  87. && bytes[8] == 0x04
  88. && bytes[9] >= 0x21 && bytes[9] <= 0x24
  89. && bytes[10] == 0x21
  90. && bytes[11] <= 0x99
  91. && bytes[12] <= 0x99
  92. && bytes[13] == 0x01
  93. && bytes[17] == 0x05)
  94. {
  95. return new FuellingTrxDb_TransactionBufferStatus_Event_ACK();
  96. }
  97. else if ((bytes[5] & 0xE0) == 0x20
  98. && bytes[8] == 0x04
  99. && bytes[9] >= 0x21 && bytes[9] <= 0x24
  100. && bytes[10] == 0x21)
  101. {
  102. return new FuellingTrxDb_TransactionBufferStatus_Answer();
  103. }
  104. else if ((bytes[5] & 0xE0) == 0x20
  105. && bytes[8] == 0x02
  106. && bytes[9] >= 0x21 && bytes[9] <= 0x24
  107. && bytes[10] >= 0x11 && bytes[10] <= 0x18
  108. && bytes[11] == 0x14
  109. && bytes[20] == 0x15)
  110. {
  111. return new LogicalNozzleDb_NozzleTotalizer_Answer();
  112. }
  113. else if ((bytes[5] & 0xE0) == 0x20
  114. && bytes[8] == 0x01
  115. && bytes[9] >= 0x40 && bytes[9] <= 0x48
  116. // dataid must 0x02
  117. && bytes[10] == 0x02
  118. // length must 4
  119. && bytes[11] == 0x04)
  120. {
  121. return new ProductDb_ProductNo_Answer();
  122. }
  123. else if ((bytes[5] & 0xE0) == 0x20
  124. && bytes[8] == 0x06
  125. && bytes[9] == 0x61
  126. && bytes[14] >= 0x11 && bytes[14] <= 0x18
  127. // dataid must 0x02
  128. && bytes[15] == 0x02
  129. // length must 4
  130. && bytes[16] == 0x04)
  131. {
  132. return new ProductPerFuellingModeDb_ProductPrice_Answer();
  133. }
  134. else if ((bytes[5] & 0xE0) == 0x20
  135. && bytes[8] == 0x02
  136. && bytes[9] >= 0x21 && bytes[9] <= 0x24
  137. && bytes[10] >= 0x11 && bytes[10] <= 0x18
  138. // dataid must 0x01
  139. && bytes[11] == 0x01
  140. && bytes[14] == 0x05
  141. /* the data_id 5, could has its var len to value 0, as I see a sample message from haosheng ifsf dispenser: 0x02 01 01 09 00 23 00 0B 02 21 11 01 01 42 05 00 07 01 00
  142. * and 0 is a valid value from protocol as it said: PCD Comment: As this is an optional data field the PCD can NAK any write requests to this Data_Id with a Data_ACK code of 4 (Data does not exist in this device) or reply to any read request with an answer message with the respective Data_Id’s length set to 0.
  143. * so here adding a strict condition to detetecting it
  144. */
  145. && ((bytes[15] == 0x00 && bytes[16] == 0x07) || (bytes[15] != 0x00 && bytes[17] == 0x07)))
  146. {
  147. return new LogicalNozzleDb_Nozzle_ProductInfo_PhyId_Answer();
  148. }
  149. else if ((bytes[5] & 0xE0) == 0x60
  150. && bytes[8] == 0x02
  151. && bytes[9] >= 0x21 && bytes[9] <= 0x24
  152. && bytes[10] >= 0x11 && bytes[10] <= 0x18
  153. // dataid must 0x05
  154. && bytes[11] == 0x05)
  155. {
  156. return new LogicalNozzleDb_Nozzle_PhyId_Event_ACK();
  157. }
  158. else if ((bytes[5] & 0xE0) == 0x60
  159. && bytes[8] == 0x01
  160. && bytes[9] == 0x00
  161. )
  162. {
  163. return new CommunicationDb_Event_ACK();
  164. }
  165. else if ((bytes[5] & 0xE0) == 0x20
  166. && bytes[8] == 0x01
  167. && bytes[9] == 0x00
  168. && bytes[10] == 0x04
  169. )
  170. {
  171. return new CommunicationDb_HeartbeatInterval_Answer();
  172. }
  173. else return new IfsfMessage();
  174. }
  175. }
  176. }