MsgRouterMessageUtility.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Wayne.ForecourtControl;
  6. namespace SinoChemFC2PosProxy
  7. {
  8. class MsgRouterMessageUtility
  9. {
  10. #region Message string generation
  11. /// <summary>
  12. /// Subscribe pump status change for all pumps
  13. /// </summary>
  14. /// <returns></returns>
  15. public static string SubscribePumpStatusChange()
  16. {
  17. return string.Format("2|GUEST|SUBSCRIBE|EVT_PUMP_STATUS_CHANGE_ID_*||||^");
  18. }
  19. /// <summary>
  20. /// Subscribe to pump delivery progress
  21. /// </summary>
  22. /// <returns></returns>
  23. public static string SubscribePumpDeliveryProgress()
  24. {
  25. return string.Format("2|GUEST|SUBSCRIBE|EVT_PUMP_DELIVERY_PROGRESS_ID_*||||^");
  26. }
  27. /// <summary>
  28. /// Generate emergency stop message
  29. /// </summary>
  30. /// <param name="fpId"></param>
  31. /// <returns></returns>
  32. public static string EmergencyStop(int fpId)
  33. {
  34. return string.Format("2|GUEST|POST|REQ_PUMP_STOP_ID_{0, 3:000}|||PA={1}|^", fpId, 1);
  35. }
  36. /// <summary>
  37. /// Clear stop message
  38. /// </summary>
  39. /// <param name="fpId"></param>
  40. public static string ClearStop(int fpId)
  41. {
  42. return string.Format("2|GUEST|POST|REQ_PUMP_CLEAR_STOP_ID_{0, 3:000}||||^", fpId);
  43. }
  44. /// <summary>
  45. /// Login to message rotuer
  46. /// </summary>
  47. /// <param name="userid"></param>
  48. /// <param name="password"></param>
  49. /// <returns></returns>
  50. public static string Login(string userid, string password)
  51. {
  52. return string.Format("2|GUEST|POST|REQ_SECU_LOGIN|||US={0}|PW={1}|^", userid, password);
  53. }
  54. /// <summary>
  55. /// Subscribe starting to apply new price change
  56. /// </summary>
  57. /// <returns></returns>
  58. public static string SubscribeStartingToApplyNewPriceChange()
  59. {
  60. return string.Format("2|GUEST|SUBSCRIBE|EVT_STARTING_TO_APPLY_NEW_PRICE_CHANGE||||^");
  61. }
  62. /// <summary>
  63. /// Subscribe to the event where a preset is entered at the forecourt
  64. /// </summary>
  65. /// <returns></returns>
  66. public static string SubscribeReceivePresetFromForecourt()
  67. {
  68. return string.Format("2|GUEST|SUBSCRIBE|REQ_RECEIVE_PRESET_FROM_FORECOURT_ID_*||||^");
  69. }
  70. /// <summary>
  71. /// Subscribe to the event when an outdoor transaction is denied
  72. /// </summary>
  73. /// <returns></returns>
  74. public static string SubscribeReceiveOutdoorTrxDeniedFromForecourt()
  75. {
  76. return string.Format("2|GUEST|SUBSCRIBE|REQ_RECEIVE_OUTDOOR_TRANSACTION_DENIED_ID_*||||^");
  77. }
  78. /// <summary>
  79. /// Subscribe to the event when an outdoor transaction is approved
  80. /// </summary>
  81. /// <returns></returns>
  82. public static string SubscribeReceiveOutdoorTrxApprovedFromForecourt()
  83. {
  84. return string.Format("2|GUEST|SUBSCRIBE|REQ_RECEIVE_OUTDOOR_TRANSACTION_APPROVED_ID_*||||^");
  85. }
  86. /// <summary>
  87. /// Subscribe to the event when an outdoor transaction is denied
  88. /// </summary>
  89. /// <returns></returns>
  90. public static string SubscribeReceiveOutdoorTrxAirlockFromForecourt()
  91. {
  92. return string.Format("2|GUEST|SUBSCRIBE|REQ_RECEIVE_OUTDOOR_AIRLOCK_ID_*||||^");
  93. }
  94. /// <summary>
  95. /// Subscribe new price change applied
  96. /// </summary>
  97. /// <returns></returns>
  98. public static string SubscribeNewPriceChangeApplied()
  99. {
  100. return string.Format("2|GUEST|SUBSCRIBE|EVT_NEW_PRICE_CHANGE_APPLIED||||^");
  101. }
  102. /// <summary>
  103. /// Subscribe set new price change response
  104. /// </summary>
  105. /// <returns></returns>
  106. public static string SubscribeSetNewPriceChange()
  107. {
  108. return string.Format("2|GUEST|SUBSCRIBE|RES_PRICES_SET_NEW_PRICE_CHANGE||||^");
  109. }
  110. /// <summary>
  111. /// Subscribe refresh price change table
  112. /// </summary>
  113. /// <returns></returns>
  114. public static string SubscribeRefreshPriceChangeTbl()
  115. {
  116. return string.Format("2|GUEST|SUBSCRIBE|RES_PRICES_REFRESH_PRICE_CHANGE_TBL||||^");
  117. }
  118. /// <summary>
  119. /// Force forecourt to load new prices
  120. /// </summary>
  121. /// <returns></returns>
  122. public static string LoadNewPrices()
  123. {
  124. return string.Format("2|GUEST|POST|REQ_LOAD_NEW_PRICES||||^");
  125. }
  126. /// <summary>
  127. /// Set new price for single grade
  128. /// </summary>
  129. /// <param name="da">Date to be processed, YYYYMMDD</param>
  130. /// <param name="ti">Time to be pricessed, HHMMSS</param>
  131. /// <param name="gradeNumber">Grade number</param>
  132. /// <param name="price">Grade price</param>
  133. /// <returns></returns>
  134. public static string SetNewPriceChange(string da, string ti, string gradeNumber, string price)
  135. {
  136. if (da.Length == 0 && ti.Length == 0)
  137. return string.Format("2|GUEST|POST|REQ_PRICES_SET_NEW_PRICE_CHANGE|||QTY=1|G01NR={0}|G01LV=1|G01PR={1}|^",
  138. gradeNumber, price);
  139. else
  140. return string.Format("2|GUEST|POST|REQ_PRICES_SET_NEW_PRICE_CHANGE|||DA={0}|TI={1}|QTY=1|G01NR={2}|G01LV=1|G01PR={3}|^",
  141. da, ti, gradeNumber, price);
  142. }
  143. /// <summary>
  144. /// Refresh price change table
  145. /// </summary>
  146. /// <returns></returns>
  147. public static string RefreshPriceChangeTable()
  148. {
  149. return string.Format("2|GUEST|POST|REQ_PRICES_REFRESH_PRICE_CHANGE_TBL||||^");
  150. }
  151. /// <summary>
  152. /// Refresh pump status
  153. /// </summary>
  154. /// <returns></returns>
  155. public static string RefreshPumpStatus()
  156. {
  157. return string.Format("2|GUEST|POST|REQ_PUMP_STATUS_ID_000||||^");
  158. }
  159. #endregion
  160. #region Helper functions
  161. public static PumpState GetPumpStatus(string state)
  162. {
  163. PumpState ps = PumpState.Unknown;
  164. string stat = state.ToUpper();
  165. if (stat == "AUTHORIZED")
  166. ps = PumpState.Authorized;
  167. else if (stat == "CLOSED")
  168. ps = PumpState.Closed;
  169. else if (stat == "ERROR")
  170. ps = PumpState.Error;
  171. else if (stat == "OFFLINE")
  172. ps = PumpState.Offline;
  173. else if (stat == "CALLING")
  174. ps = PumpState.Calling;
  175. else if (stat == "STARTING")
  176. ps = PumpState.Starting;
  177. else if (stat == "FUELLING")
  178. ps = PumpState.Fuelling;
  179. else if (stat == "PAUSED")
  180. ps = PumpState.Suspended;
  181. else if (stat == "IDLE")
  182. ps = PumpState.Idle;
  183. else if (stat == "STOPPED")
  184. ps = PumpState.Stopped;
  185. return ps;
  186. }
  187. /// <summary>
  188. /// Fusion message router user password encryption
  189. /// </summary>
  190. /// <param name="inp"></param>
  191. /// <param name="inplen"></param>
  192. /// <param name="key"></param>
  193. /// <param name="keylen"></param>
  194. /// <returns></returns>
  195. public static char[] Crypt(char[] inp, int inplen, char[] key, int keylen)
  196. {
  197. //we will consider size of sbox 256 chars
  198. //(extra char are only to prevent any mishep just in case)
  199. char[] Sbox = new char[257];
  200. char[] Sbox2 = new char[257];
  201. int i, j, t, x;
  202. char temp, k = '\0';
  203. i = j = t = x = 0;
  204. temp = '\0';
  205. for (i = 0; i < 256; i++)
  206. {
  207. Sbox[i] = (char)0;
  208. Sbox2[i] = (char)0;
  209. }
  210. //initialize sbox i
  211. for (i = 0; i < 256; i++)
  212. {
  213. Sbox[i] = (char)i;
  214. }
  215. j = 0;
  216. //initialize the sbox2 with user key
  217. for (i = 0; i < 256; i++)
  218. {
  219. if (j == keylen)
  220. {
  221. j = 0;
  222. }
  223. Sbox2[i] = key[j++];
  224. }
  225. j = 0; //Initialize j
  226. //scramble sbox1 with sbox2
  227. for (i = 0; i < 256; i++)
  228. {
  229. j = (j + Sbox[i] + Sbox2[i]) % 256;
  230. temp = Sbox[i];
  231. Sbox[i] = Sbox[j];
  232. Sbox[j] = temp;
  233. }
  234. i = j = 0;
  235. for (x = 0; x < inplen; x++)
  236. {
  237. //increment i
  238. i = (i + 1) % 256;
  239. //increment j
  240. j = (j + Sbox[i]) % 256;
  241. //Scramble SBox #1 further so encryption routine will
  242. //will repeat itself at great interval
  243. temp = Sbox[i];
  244. Sbox[i] = Sbox[j];
  245. Sbox[j] = temp;
  246. //Get ready to create pseudo random char for encryption key
  247. t = (Sbox[i] + Sbox[j]) % 256;
  248. //get the random char
  249. k = Sbox[t];
  250. //xor with the data and done
  251. inp[x] = (char)(inp[x] ^ k);
  252. }
  253. return inp;
  254. }
  255. /// <summary>
  256. /// Binary to hex string conversion
  257. /// </summary>
  258. /// <param name="binStr"></param>
  259. /// <param name="len"></param>
  260. /// <returns></returns>
  261. public static String BinToHexString(char[] binStr, int len)
  262. {
  263. char[] BINTOCHAR = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  264. int i;
  265. String retStr = "";
  266. for (i = 0; i < len; i++)
  267. {
  268. retStr += "" + BINTOCHAR[(binStr[i] & 0xf0) >> 4] + "" + BINTOCHAR[binStr[i] & 0x0f];
  269. }
  270. return retStr;
  271. }
  272. /// <summary>
  273. /// Append space on right
  274. /// </summary>
  275. /// <param name="strIn"></param>
  276. /// <param name="numberofSpace"></param>
  277. /// <returns></returns>
  278. public static string AppendSpaceOnRight(string strIn, int numberofSpace)
  279. {
  280. int strInLength = strIn.Length;
  281. for (int i = 0; i < numberofSpace - strInLength; i++)
  282. {
  283. strIn += " ";
  284. }
  285. return strIn;
  286. }
  287. #endregion
  288. }
  289. }