TerminalSimul.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using nettyCommunication;
  2. using parkMonitor.LOG;
  3. using PLCS7;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace Terminal
  11. {
  12. public class TerminalSimul
  13. {
  14. public static bool stopSimul = false;
  15. /// <summary>
  16. /// 停车终端模拟操作
  17. /// </summary>
  18. /// <returns>0成功 1寻找车辆停放位置失败 2生成凭证号失败 3凭证转换异常 4停车码解析异常 5地感异常 6终端状态异常 7指令占用异常 8其他异常</returns>
  19. public static int ParkTermOper(int id, string carInfo)
  20. {
  21. int countdown = 10;
  22. int index = -1;
  23. for (int i = 0; i < Terminal.terminalInfo.Count; i++)
  24. {
  25. if (Terminal.terminalInfo[i].terminalID == id)
  26. {
  27. index = i;
  28. break;
  29. }
  30. }
  31. if (index == -1)
  32. return 1;
  33. while (!stopSimul && countdown-- > 0)
  34. {
  35. //判断终端块是否有地感、为停车终端、空闲中,之后发送停车指令
  36. if (Terminal.terminalInfo[index].groundStatus == 1
  37. && Terminal.terminalInfo[index].terminalStatus == 1
  38. && Terminal.terminalInfo[index].cmd == 0)
  39. {
  40. string receiptNum = Credence.GetOneCredence();
  41. if (receiptNum == "")
  42. {
  43. Log.WriteLog(LogType.process, LogFile.ERROR, "未能获取凭证号");
  44. return 2;
  45. }
  46. TerminalStru ts = new TerminalStru();
  47. ts.terminalID = (short)id;
  48. ts.paymentStatus = (short)-1;
  49. ts.btnStatus = Terminal.terminalInfo[index].btnStatus == 3 ? (short)3 : (short)2;
  50. ts.cmd = 1;
  51. if (!int.TryParse(receiptNum, out ts.receiptNum))
  52. {
  53. Log.WriteLog(LogType.process, LogFile.ERROR, "凭证转换错误");
  54. return 3;
  55. }
  56. if (!licenseCodeEncoding(carInfo, ref ts))
  57. return 4;
  58. Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.terminal);
  59. return 0;
  60. }
  61. Thread.Sleep(300);
  62. }
  63. if (Terminal.terminalInfo[1].groundStatus != 1)
  64. {
  65. return 5;
  66. }
  67. else if (Terminal.terminalInfo[1].terminalStatus != 1)
  68. {
  69. return 6;
  70. }
  71. else if (Terminal.terminalInfo[1].cmd != 0)
  72. {
  73. return 7;
  74. }
  75. return 8;
  76. }
  77. /// <summary>
  78. /// 用户与车辆信息编码写入PLC
  79. /// </summary>
  80. /// <param name="ts"></param>
  81. private static bool licenseCodeEncoding(string msg, ref TerminalStru ts)
  82. {
  83. int a = 0, b = 0, c = 0, d = 0;
  84. //字符串分割
  85. try
  86. {
  87. string[] array = msg.Split('.');
  88. string str1 = array[0];
  89. string str2 = array[1];
  90. string s1 = str2.Substring(0, 4);
  91. string s2 = str2.Substring(4, 4);
  92. string s3 = str2.Substring(8, str2.Length - 8);
  93. a = BitConverter.ToInt32(Encoding.ASCII.GetBytes(s1), 0);
  94. b = BitConverter.ToInt32(Encoding.ASCII.GetBytes(s2), 0);
  95. if (s3 != null && s3 != "" && s3 != string.Empty)
  96. {
  97. c = BitConverter.ToInt32(Encoding.ASCII.GetBytes(s3), 0);
  98. }
  99. else
  100. {
  101. c = 0;
  102. }
  103. d = Convert.ToInt32(str1);
  104. ts.licenseCodeA = d;
  105. ts.licenseCodeB = a;
  106. ts.licenseCodeC = b;
  107. ts.licenseCodeD = c;
  108. return true;
  109. }
  110. catch (Exception ex)
  111. {
  112. Log.WriteLog(LogType.process, LogFile.ERROR, "错误码7:停车码异常,解析失败。" + ex.StackTrace);
  113. return false;
  114. }
  115. }
  116. /// <summary>
  117. /// 取车终端模拟操作
  118. /// </summary>
  119. /// <param name="receiptNum"></param>
  120. /// <returns>0成功 1地感异常 2终端状态异常 3指令占用异常 4凭证解析异常 5其他异常</returns>
  121. public static int FetchTermOper(string receiptNum)
  122. {
  123. ////定位2号终端
  124. //int index = 0;
  125. //for (int i = 0; i < Terminal.terminalInfo.Count; i++)
  126. //{
  127. // if (Terminal.terminalInfo[i].terminalID == 2)
  128. // {
  129. // index = i;
  130. // break;
  131. // }
  132. //}
  133. int countdown = 10;
  134. while (!stopSimul && countdown-- > 0)
  135. {
  136. //判断终端块是否有地感、为停车终端、空闲中,之后发送停车指令
  137. if (Terminal.terminalInfo[1].groundStatus == 0
  138. && Terminal.terminalInfo[1].terminalStatus == 2
  139. && Terminal.terminalInfo[1].cmd == 0)
  140. {
  141. TerminalStru ts = new TerminalStru();
  142. ts.terminalID = 2;
  143. try
  144. {
  145. ts.receiptNum = int.Parse(receiptNum);
  146. }
  147. catch { return 4; }
  148. ts.paymentStatus = 2;//强制支付完成
  149. ts.btnStatus = (short)-1;
  150. ts.cmd = 2;
  151. ts.licenseCodeA = -1;
  152. ts.licenseCodeB = -1;
  153. ts.licenseCodeC = -1;
  154. ts.licenseCodeD = -1;
  155. Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.terminal);
  156. return 0;
  157. }
  158. Thread.Sleep(300);
  159. }
  160. if (Terminal.terminalInfo[1].groundStatus != 0)
  161. {
  162. return 1;
  163. }else if(Terminal.terminalInfo[1].terminalStatus != 2)
  164. {
  165. return 2;
  166. }else if(Terminal.terminalInfo[1].cmd != 0)
  167. {
  168. return 3;
  169. }
  170. return 5;
  171. }
  172. }
  173. }