TerminalSimul.cs 5.5 KB

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