TerminalSimul.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. if(Monitor.Monitor.ins.GetFreeSpaceCount(1) == 0)
  59. {
  60. return 9;
  61. }
  62. Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.terminal);
  63. return 0;
  64. }
  65. Thread.Sleep(300);
  66. }
  67. if (Terminal.terminalInfo[1].groundStatus != 1)
  68. {
  69. return 5;
  70. }
  71. else if (Terminal.terminalInfo[1].terminalStatus != 1)
  72. {
  73. return 6;
  74. }
  75. else if (Terminal.terminalInfo[1].cmd != 0)
  76. {
  77. return 7;
  78. }
  79. return 8;
  80. }
  81. /// <summary>
  82. /// 用户与车辆信息编码写入PLC
  83. /// </summary>
  84. /// <param name="ts"></param>
  85. private static bool licenseCodeEncoding(string msg, ref TerminalStru ts)
  86. {
  87. int a = 0, b = 0, c = 0, d = 0;
  88. //字符串分割
  89. try
  90. {
  91. string[] array = msg.Split('.');
  92. string str1 = array[0];
  93. string str2 = array[1];
  94. string s1 = str2.Substring(0, 4);
  95. string s2 = str2.Substring(4, 4);
  96. string s3 = str2.Substring(8, str2.Length - 8);
  97. a = BitConverter.ToInt32(Encoding.ASCII.GetBytes(s1), 0);
  98. b = BitConverter.ToInt32(Encoding.ASCII.GetBytes(s2), 0);
  99. if (s3 != null && s3 != "" && s3 != string.Empty)
  100. {
  101. c = BitConverter.ToInt32(Encoding.ASCII.GetBytes(s3), 0);
  102. }
  103. else
  104. {
  105. c = 0;
  106. }
  107. d = Convert.ToInt32(str1);
  108. ts.licenseCodeA = d;
  109. ts.licenseCodeB = a;
  110. ts.licenseCodeC = b;
  111. ts.licenseCodeD = c;
  112. return true;
  113. }
  114. catch (Exception ex)
  115. {
  116. Log.WriteLog(LogType.process, LogFile.ERROR, "错误码7:停车码异常,解析失败。" + ex.StackTrace);
  117. return false;
  118. }
  119. }
  120. /// <summary>
  121. /// 取车终端模拟操作
  122. /// </summary>
  123. /// <param name="receiptNum"></param>
  124. /// <returns>0成功 1地感异常 2终端状态异常 3指令占用异常 4凭证解析异常 5其他异常</returns>
  125. public static int FetchTermOper(string receiptNum)
  126. {
  127. ////定位2号终端
  128. //int index = 0;
  129. //for (int i = 0; i < Terminal.terminalInfo.Count; i++)
  130. //{
  131. // if (Terminal.terminalInfo[i].terminalID == 2)
  132. // {
  133. // index = i;
  134. // break;
  135. // }
  136. //}
  137. int countdown = 10;
  138. while (!stopSimul && countdown-- > 0)
  139. {
  140. //判断终端块是否有地感、为取车终端、空闲中,之后发送取车指令
  141. if (Terminal.terminalInfo[1].groundStatus == 0
  142. && Terminal.terminalInfo[1].terminalStatus == 2
  143. && Terminal.terminalInfo[1].cmd == 0)
  144. {
  145. TerminalStru ts = new TerminalStru();
  146. ts.terminalID = 2;
  147. try
  148. {
  149. ts.receiptNum = int.Parse(receiptNum);
  150. }
  151. catch { return 4; }
  152. ts.paymentStatus = 2;//强制支付完成
  153. ts.btnStatus = (short)-1;
  154. ts.cmd = 2;
  155. ts.licenseCodeA = -1;
  156. ts.licenseCodeB = -1;
  157. ts.licenseCodeC = -1;
  158. ts.licenseCodeD = -1;
  159. Monitor.Monitor.PLC.WriteToPLC(ts, PLCDataType.terminal);
  160. return 0;
  161. }
  162. Thread.Sleep(300);
  163. }
  164. if (Terminal.terminalInfo[1].groundStatus != 0)
  165. {
  166. return 1;
  167. }else if(Terminal.terminalInfo[1].terminalStatus != 2)
  168. {
  169. return 2;
  170. }else if(Terminal.terminalInfo[1].cmd != 0)
  171. {
  172. return 3;
  173. }
  174. return 5;
  175. }
  176. }
  177. }