exit_execute.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using chutian_parking_terminal.data_buffer;
  7. using tool;
  8. using chutian_parking_terminal.communication;
  9. using Google.Protobuf;
  10. using System.Threading;
  11. namespace chutian_parking_terminal.motion_executer
  12. {
  13. class Exit_execute : Singleton<Exit_execute>
  14. {
  15. //出口流程状态
  16. public enum Exit_process_statu
  17. {
  18. EXIT_UNKNOW = 0,//未知
  19. EXIT_FREE = 1,//空闲 等待取车指令到来
  20. EXIT_SEND_REQUEST = 2,//发送取车请求
  21. EXIT_WAIT_RESPONSE = 3,//等待签收答复 检擦成功进行下一步
  22. EXIT_MONITOR_PROCESS = 4,//监控流程 检查流程结果
  23. EXIT_OPEN_DOOR = 5,//开门 控制单片机开门
  24. EXIT_CHECK_GOUNDSENSE_GEOMAG = 6,//检查地磁地感 等待车辆离开
  25. EXIT_RESET_PROCESS = 7,//重置流程
  26. };
  27. private Exit_execute()
  28. {
  29. }
  30. //初始化
  31. public void exit_execute_init()
  32. {
  33. m_statu = Exit_process_statu.EXIT_FREE;
  34. //默认循环
  35. m_picking_process_condition = true;
  36. m_picking_process_thread = new Thread(picking_process_thread);
  37. m_car_license = "";
  38. m_event_exit = new EventWaitHandle(false, EventResetMode.ManualReset);
  39. //启动线程
  40. m_picking_process_thread.Start();
  41. }
  42. public void exit_execute_uninit()
  43. {
  44. m_statu = Exit_process_statu.EXIT_UNKNOW;
  45. m_picking_process_condition = false;
  46. m_picking_process_thread.Abort();
  47. }
  48. //开始取车流程
  49. public void exit_process_run()
  50. {
  51. m_statu = Exit_process_statu.EXIT_SEND_REQUEST;
  52. }
  53. //设置取车的反馈--由通信层调用
  54. public void set_picking_response_msg(Message.Pickup_command_response_msg t_pickup_command_response_msg)
  55. {
  56. m_pickup_command_response_msg = t_pickup_command_response_msg;
  57. }
  58. //取车任务流程
  59. protected void picking_process_thread()
  60. {
  61. while (m_picking_process_condition && !m_event_exit.WaitOne(1))
  62. {
  63. Console.WriteLine(DateTime.Now.Second.ToString() + ":" + DateTime.Now.Millisecond);
  64. Console.WriteLine(m_statu);
  65. switch (m_statu)
  66. {
  67. //空闲状态 等待指令
  68. case Exit_process_statu.EXIT_FREE:
  69. {
  70. if (!Singlechip_data_buffer.Instance.has_door_control_msg())
  71. {
  72. //0为不控制 1为控制开门 2表示关门
  73. SingleChip.Door_control_msg t_door_control_state_msg = create_door_control_state_msg(2);
  74. Singlechip_data_buffer.Instance.set_door_control_msg(t_door_control_state_msg);
  75. }
  76. }
  77. break;
  78. //发送取车请求
  79. case Exit_process_statu.EXIT_SEND_REQUEST:
  80. {
  81. if (m_car_license != "" && (m_car_license.Length == 7 || m_car_license.Length == 8))
  82. {
  83. //创建取车请求
  84. m_pickup_command_request_msg = create_pickup_request_msg();
  85. //发送取车请求
  86. Channel_communication.Instance.encapsulate_msg(m_pickup_command_request_msg.ToByteArray());
  87. //记录发送时间
  88. m_send_request_time = DateTime.Now;
  89. m_statu = Exit_process_statu.EXIT_WAIT_RESPONSE;
  90. }
  91. else
  92. {
  93. MessageBoxForm messageBoxForm = new MessageBoxForm();
  94. messageBoxForm.ShowDialog("非正常车牌号!"+ m_car_license.Length.ToString());
  95. m_statu = Exit_process_statu.EXIT_FREE;
  96. }
  97. }
  98. break;
  99. //等待签收答复 查看签收结果
  100. case Exit_process_statu.EXIT_WAIT_RESPONSE:
  101. {
  102. m_recv_response_time = DateTime.Now;
  103. if ((m_recv_response_time.Ticks / 1000 - m_send_request_time.Ticks / 1000) > 5000)
  104. {
  105. MessageBoxForm messageBoxForm = new MessageBoxForm();
  106. messageBoxForm.ShowDialog("等待反馈超时!请联系管理员", true);
  107. m_statu = Exit_process_statu.EXIT_RESET_PROCESS;
  108. //进入故障流程
  109. }
  110. if (m_pickup_command_response_msg != null)
  111. {
  112. //查看反馈结果
  113. if (m_pickup_command_response_msg.Code.ErrorCode == 0)
  114. {
  115. //成功签收就进入下一步
  116. Tip_data_buffer.Instance.set_tip_str(m_car_license + " 取车请求已签收!");
  117. //进入监听状态
  118. m_statu = Exit_process_statu.EXIT_MONITOR_PROCESS;
  119. break;
  120. }
  121. else
  122. {
  123. MessageBoxForm messageBoxForm = new MessageBoxForm();
  124. messageBoxForm.ShowDialog("取车请求有误!", true);
  125. Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, m_pickup_command_response_msg.ToString() + " 停车请求有误!");
  126. m_statu = Exit_process_statu.EXIT_RESET_PROCESS;
  127. }
  128. }
  129. }
  130. break;
  131. //监控流程 检查流程结果
  132. case Exit_process_statu.EXIT_MONITOR_PROCESS:
  133. {
  134. if (Central_control_data_buffer.Instance.has_picking_process_statu_msg())
  135. {
  136. Tip_data_buffer.Instance.set_tip_str(" 正在监听!");
  137. Message.Picking_process_statu_msg t_picking_process_statu_msg = Central_control_data_buffer.Instance.get_picking_process_statu_msg();
  138. if (t_picking_process_statu_msg.Completed && t_picking_process_statu_msg.License == m_car_license)
  139. {
  140. Tip_data_buffer.Instance.set_tip_str(" 取车成功,等待开门!");
  141. m_statu = Exit_process_statu.EXIT_OPEN_DOOR;
  142. Thread.Sleep(3000);
  143. }
  144. else if (t_picking_process_statu_msg.BackCompleted)
  145. {
  146. Tip_data_buffer.Instance.set_tip_str(" 取车失败!");
  147. m_statu = Exit_process_statu.EXIT_RESET_PROCESS;
  148. Thread.Sleep(3000);
  149. }
  150. }
  151. }
  152. break;
  153. //控制开门 通知单片机开门
  154. case Exit_process_statu.EXIT_OPEN_DOOR:
  155. {
  156. //0为不控制 1为控制开门 2表示关门
  157. SingleChip.Door_control_msg t_door_control_state_msg = create_door_control_state_msg(1);
  158. Singlechip_data_buffer.Instance.set_door_control_msg(t_door_control_state_msg);
  159. m_statu = Exit_process_statu.EXIT_CHECK_GOUNDSENSE_GEOMAG;
  160. }
  161. break;
  162. //检查地磁地感 等待车辆离开
  163. case Exit_process_statu.EXIT_CHECK_GOUNDSENSE_GEOMAG:
  164. {
  165. if (Singlechip_data_buffer.Instance.has_door_laser_state_msg())
  166. {
  167. SingleChip.Door_laser_state_msg t_door_laser_state_msg = Singlechip_data_buffer.Instance.get_door_laser_state_msg();
  168. //两个雷达均为检测到异物说明车辆已离开
  169. if (t_door_laser_state_msg.Laser1State == 0 && t_door_laser_state_msg.Laser2State == 0)
  170. {
  171. m_statu = Exit_process_statu.EXIT_RESET_PROCESS;
  172. }
  173. else
  174. {
  175. Tip_data_buffer.Instance.set_tip_str(" 等待车辆离开出口!");
  176. }
  177. }
  178. }
  179. break;
  180. //重置流程
  181. case Exit_process_statu.EXIT_RESET_PROCESS:
  182. {
  183. m_pickup_command_request_msg = null;
  184. m_pickup_command_response_msg = null;
  185. //0为不控制 1为控制开门 2表示关门
  186. SingleChip.Door_control_msg t_door_control_state_msg = create_door_control_state_msg(2);
  187. Singlechip_data_buffer.Instance.set_door_control_msg(t_door_control_state_msg);
  188. Tip_data_buffer.Instance.set_tip_str("");
  189. m_statu = Exit_process_statu.EXIT_FREE;
  190. }
  191. break;
  192. }
  193. }
  194. }
  195. //库门和激光雷达运作状态
  196. public SingleChip.Door_control_msg create_door_control_state_msg(int door_state=0)
  197. {
  198. SingleChip.Door_control_msg t_door_control_state_msg = new SingleChip.Door_control_msg();
  199. SingleChip.Base_info baseInfo = new SingleChip.Base_info();
  200. baseInfo.MsgType = SingleChip.Message_type.EDoorControlMsg;
  201. baseInfo.Sender = SingleChip.Communicator.ETerminal;
  202. baseInfo.Receiver = SingleChip.Communicator.ECollector;
  203. t_door_control_state_msg.BaseInfo = baseInfo;
  204. t_door_control_state_msg.TerminalId = Singlechip_data_buffer.Instance.get_door_laser_state_msg().TerminalId;
  205. t_door_control_state_msg.DoorControl = door_state;
  206. return t_door_control_state_msg;
  207. }
  208. //取车请求
  209. public Message.Pickup_command_request_msg create_pickup_request_msg()
  210. {
  211. //取车请求
  212. Message.Pickup_command_request_msg t_msg = new Message.Pickup_command_request_msg();
  213. //消息头
  214. Message.Base_info baseInfo = new Message.Base_info();
  215. baseInfo.MsgType = Message.Message_type.EPickupCommandRequestMsg;
  216. baseInfo.Sender = Message.Communicator.ETerminor;
  217. baseInfo.Receiver = Message.Communicator.EMain;
  218. //车辆信息--只需填充车牌号
  219. Message.Car_info car_Info = new Message.Car_info();
  220. car_Info.License = m_car_license;
  221. //填充信息
  222. t_msg.BaseInfo = baseInfo;
  223. t_msg.TerminalId = Singlechip_data_buffer.Instance.get_door_laser_state_msg().TerminalId;
  224. t_msg.CarInfo = car_Info;
  225. return t_msg;
  226. }
  227. //获取状态
  228. public Exit_process_statu get_process_statu()
  229. {
  230. return m_statu;
  231. }
  232. //从外部获取取车车牌号
  233. public void set_pickup_license(string license)
  234. {
  235. m_car_license = license;
  236. }
  237. protected Exit_process_statu m_statu; //流程状态
  238. protected Thread m_picking_process_thread; //处理停车流程消息线程
  239. protected bool m_picking_process_condition; //处理停车流程消息条件变量
  240. protected string m_car_license; //当前取车流程的车牌号
  241. protected EventWaitHandle m_event_exit; //防止CPU卡死
  242. protected DateTime m_send_request_time; //发送请求的时间
  243. protected DateTime m_recv_response_time; //接收反馈时间
  244. protected Message.Pickup_command_request_msg m_pickup_command_request_msg;//取车请求
  245. protected Message.Pickup_command_response_msg m_pickup_command_response_msg;//取车反馈
  246. }
  247. }