MsgRouterMessageUtility.cs 10 KB

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