entrance_execute.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using chutian_parking_terminal.data_buffer;
  8. using tool;
  9. using chutian_parking_terminal.communication;
  10. using Google.Protobuf;
  11. namespace chutian_parking_terminal.motion_executer
  12. {
  13. class Entrance_execute : Singleton<Entrance_execute>
  14. {
  15. //入口流程
  16. public enum Entrance_process_statu
  17. {
  18. ENTRANCE_UNKNOW = 0,//未知
  19. ENTRANCE_FREE = 1,//空闲 待机, 检查地感
  20. ENTRANCE_OPEN_DOOR = 2,//通知单片机开门
  21. ENTRANCE_MONITOR_CAR = 3,//监控车辆是否停到位
  22. ENTRANCE_WAIT_USER = 4,//准备就绪, 等待用户点击存车按钮
  23. ENTRANCE_CLOSE_DOOR = 5,//通知单片机关门
  24. ENTRANCE_SEND_REQUEST = 6,//发送停车请求
  25. ENTRANCE_WAIT_RESPONSE = 7,//等待签收答复, 检擦成功进行下一步
  26. ENTRANCE_MONITOR_PROCESS = 8,//监听流程状态 检查状态结果
  27. ENTRANCE_RESET_PROCESS = 9,//重置流程
  28. };
  29. private Entrance_execute()
  30. {
  31. }
  32. //初始化
  33. public void entrance_execute_init()
  34. {
  35. m_statu = Entrance_process_statu.ENTRANCE_FREE;
  36. //默认循环
  37. m_storing_process_condition = true;
  38. m_storing_process_thread = new Thread(storing_process_thread);
  39. m_car_license = "";
  40. m_event_exit = new EventWaitHandle(false, EventResetMode.ManualReset);
  41. m_terminal_id = int.Parse(json_file_operation.Instance.read_json_string("ternimalNumber"));
  42. //启动线程
  43. m_storing_process_thread.Start();
  44. }
  45. //启动流程
  46. public void entrance_process_run()
  47. {
  48. //开始关门步骤
  49. m_statu = Entrance_process_statu.ENTRANCE_CLOSE_DOOR;
  50. }
  51. //反初始化
  52. public void entrance_execute_uninit()
  53. {
  54. m_statu = Entrance_process_statu.ENTRANCE_UNKNOW;
  55. m_storing_process_condition = false;
  56. m_storing_process_thread.Abort();
  57. }
  58. ///设置存车的反馈--由通信层调用
  59. public void set_store_response_msg(Message.Store_command_response_msg t_store_command_response_msg)
  60. {
  61. //只处理本终端的消息
  62. if (t_store_command_response_msg.TerminalId == m_terminal_id)
  63. {
  64. m_store_command_response_msg = t_store_command_response_msg;
  65. }
  66. }
  67. //处理停车流程消息线程函数
  68. protected void storing_process_thread()
  69. {
  70. while (m_storing_process_condition && !m_event_exit.WaitOne(1))
  71. {
  72. Console.WriteLine(DateTime.Now.Second.ToString()+":"+DateTime.Now.Millisecond);
  73. Console.WriteLine(m_statu);
  74. switch (m_statu)
  75. {
  76. //空闲状态 实时检测是否有车来
  77. case Entrance_process_statu.ENTRANCE_FREE:
  78. {
  79. //Tip_data_buffer.Instance.set_tip_str("等待车辆到来!");
  80. if (!Singlechip_data_buffer.Instance.has_garage_state_msg())
  81. {
  82. //0为不控制 1为控制开门 2表示关门
  83. SingleChip.Garage_state_msg t_garage_state_msg = create_garage_state_msg(2);
  84. Singlechip_data_buffer.Instance.set_garage_state_msg(t_garage_state_msg);
  85. }
  86. if (Singlechip_data_buffer.Instance.has_sensor_state_msg())
  87. {
  88. SingleChip.Sensor_state_msg t_sensor_state_msg = Singlechip_data_buffer.Instance.get_sensor_state_msg();
  89. //查看是否是本终端来车
  90. if (t_sensor_state_msg.TerminalId == m_terminal_id)
  91. {
  92. //地感地磁都检测到有车
  93. if (t_sensor_state_msg.GroundSense.Outdoor == 1 && t_sensor_state_msg.Geomag.Outdoor == 1)
  94. {
  95. //开始流程
  96. Tip_data_buffer.Instance.set_tip_str("开始流程!");
  97. m_statu = Entrance_process_statu.ENTRANCE_OPEN_DOOR;
  98. }
  99. }
  100. }
  101. }
  102. break;
  103. //控制开门 通知单片机开门
  104. case Entrance_process_statu.ENTRANCE_OPEN_DOOR:
  105. {
  106. //0为不控制 1为控制开门 2表示关门
  107. SingleChip.Garage_state_msg t_garage_state_msg = create_garage_state_msg(1);
  108. Singlechip_data_buffer.Instance.set_garage_state_msg(t_garage_state_msg);
  109. m_statu = Entrance_process_statu.ENTRANCE_MONITOR_CAR;
  110. }
  111. break;
  112. //辅助停车 显示当前车辆位置 停的到不到位
  113. case Entrance_process_statu.ENTRANCE_MONITOR_CAR:
  114. {
  115. SingleChip.Sensor_state_msg t_sensor_state_msg = Singlechip_data_buffer.Instance.get_sensor_state_msg();
  116. //监控本终端的车有没有停好
  117. if (t_sensor_state_msg.TerminalId == m_terminal_id)
  118. {
  119. //0表示门开到位
  120. if (t_sensor_state_msg.DoorState == 0)
  121. {
  122. //门内地磁地感未检测到车辆
  123. if (t_sensor_state_msg.Geomag.Indoor == 0 || t_sensor_state_msg.GroundSense.Indoor == 0)
  124. {
  125. //门外地磁地感检测到车辆
  126. if (t_sensor_state_msg.Geomag.Outdoor == 1 && t_sensor_state_msg.GroundSense.Outdoor == 1)
  127. {
  128. Tip_data_buffer.Instance.set_tip_str("请将车辆驶入!");
  129. }
  130. }
  131. //门内地磁地感均检测到车
  132. else if (t_sensor_state_msg.Geomag.Indoor == 1 && t_sensor_state_msg.GroundSense.Indoor == 1)
  133. {
  134. if (t_sensor_state_msg.OverLimit.Front == 0 && t_sensor_state_msg.OverLimit.Back == 0//车辆未超前和超后
  135. && t_sensor_state_msg.OverLimit.Left == 0 && t_sensor_state_msg.OverLimit.Right == 0//车辆未超左和超右
  136. && t_sensor_state_msg.OverLimit.High == 0 && t_sensor_state_msg.OverLimit.Low == 0)//车辆未超高和底盘未过低)
  137. {
  138. //等待用户进行操作
  139. m_statu = Entrance_process_statu.ENTRANCE_WAIT_USER;
  140. }
  141. else
  142. {
  143. Tip_data_buffer.Instance.set_tip_str("请将车辆挪正!");
  144. }
  145. }
  146. }
  147. //1表示门关到位
  148. else if (t_sensor_state_msg.DoorState == 1)
  149. {
  150. Tip_data_buffer.Instance.set_tip_str("请等待开门!");
  151. }
  152. //2表示门运行中
  153. else if (t_sensor_state_msg.DoorState == 2)
  154. {
  155. Tip_data_buffer.Instance.set_tip_str("库门运行中,请保持距离!");
  156. }
  157. //3表示门报警
  158. else if (t_sensor_state_msg.DoorState == 3)
  159. {
  160. Tip_data_buffer.Instance.set_tip_str("门报警,请联系管理员!");
  161. }
  162. }
  163. }
  164. break;
  165. //待机状态 等待用户点击停车按钮
  166. case Entrance_process_statu.ENTRANCE_WAIT_USER:
  167. {
  168. SingleChip.Sensor_state_msg t_sensor_state_msg = Singlechip_data_buffer.Instance.get_sensor_state_msg();
  169. //监控本终端的车有没有停好
  170. if (t_sensor_state_msg.TerminalId == m_terminal_id)
  171. {
  172. //门内地磁地感均检测到车
  173. if (t_sensor_state_msg.Geomag.Indoor == 1 && t_sensor_state_msg.GroundSense.Indoor == 1)
  174. {
  175. //车已停好 等待用户点击停车
  176. Tip_data_buffer.Instance.set_tip_str("车辆已停好,请点击停车按钮!");
  177. }
  178. else
  179. {
  180. //门内地磁地感没检测到车说明车辆离开
  181. m_statu = Entrance_process_statu.ENTRANCE_RESET_PROCESS;
  182. }
  183. }
  184. }
  185. break;
  186. //控制关门 通知单片机关门
  187. case Entrance_process_statu.ENTRANCE_CLOSE_DOOR:
  188. {
  189. //0为不控制 1为控制开门 2表示关门
  190. SingleChip.Garage_state_msg t_garage_state_msg = create_garage_state_msg(2);
  191. Singlechip_data_buffer.Instance.set_garage_state_msg(t_garage_state_msg);
  192. m_statu = Entrance_process_statu.ENTRANCE_SEND_REQUEST;
  193. }
  194. break;
  195. //发送请求 发送停车请求消息
  196. case Entrance_process_statu.ENTRANCE_SEND_REQUEST:
  197. {
  198. //创建停车请求
  199. m_store_command_request_msg = create_store_request_msg();
  200. //发送停车请求
  201. Channel_communication.Instance.encapsulate_msg(m_store_command_request_msg.ToByteArray());
  202. m_send_request_time = DateTime.Now;
  203. m_statu = Entrance_process_statu.ENTRANCE_WAIT_RESPONSE;
  204. }
  205. break;
  206. //等待反馈 等待停车反馈
  207. case Entrance_process_statu.ENTRANCE_WAIT_RESPONSE:
  208. {
  209. m_recv_response_time = DateTime.Now;
  210. if ((m_recv_response_time.Ticks/1000 - m_send_request_time.Ticks/1000) > 5000)
  211. {
  212. MessageBoxForm messageBoxForm = new MessageBoxForm();
  213. messageBoxForm.ShowDialog("等待反馈超时!请联系管理员",true);
  214. m_statu = Entrance_process_statu.ENTRANCE_RESET_PROCESS;
  215. //进入故障流程
  216. }
  217. if (m_store_command_response_msg != null)
  218. {
  219. //查看反馈结果
  220. if (m_store_command_response_msg.Code.ErrorCode == 0)
  221. {
  222. //成功签收就进入下一步
  223. Tip_data_buffer.Instance.set_tip_str(m_car_license + " 停车请求已签收!");
  224. //进入监听状态
  225. m_statu = Entrance_process_statu.ENTRANCE_MONITOR_PROCESS;
  226. break;
  227. }
  228. else
  229. {
  230. Tip_data_buffer.Instance.set_tip_str(" 停车请求有误!");
  231. Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, m_store_command_request_msg.ToString() + " 停车请求有误!" );
  232. m_statu = Entrance_process_statu.ENTRANCE_RESET_PROCESS;
  233. }
  234. }
  235. }
  236. break;
  237. //监听主控流程状态 检查状态结果
  238. case Entrance_process_statu.ENTRANCE_MONITOR_PROCESS:
  239. {
  240. if ( Central_control_data_buffer.Instance.has_storing_process_statu_msg() &&
  241. Central_control_data_buffer.Instance.get_storing_process_statu_msg().TerminalId == m_terminal_id)
  242. {
  243. Tip_data_buffer.Instance.set_tip_str(" 正在监听!");
  244. Message.Storing_process_statu_msg t_storing_process_statu_msg = Central_control_data_buffer.Instance.get_storing_process_statu_msg();
  245. if (t_storing_process_statu_msg.Completed && t_storing_process_statu_msg.License == m_car_license)
  246. {
  247. Tip_data_buffer.Instance.set_tip_str(" 停车成功!");
  248. m_statu = Entrance_process_statu.ENTRANCE_RESET_PROCESS;
  249. Thread.Sleep(3000);
  250. }
  251. else if (t_storing_process_statu_msg.BackCompleted && t_storing_process_statu_msg.License == m_car_license)
  252. {
  253. Tip_data_buffer.Instance.set_tip_str(" 停车失败!");
  254. m_statu = Entrance_process_statu.ENTRANCE_RESET_PROCESS;
  255. Thread.Sleep(3000);
  256. }
  257. }
  258. }
  259. break;
  260. //重置流程 流程还原
  261. case Entrance_process_statu.ENTRANCE_RESET_PROCESS:
  262. {
  263. m_store_command_request_msg = null;
  264. m_store_command_response_msg = null;
  265. Tip_data_buffer.Instance.set_tip_str("");
  266. m_statu = Entrance_process_statu.ENTRANCE_FREE;
  267. }
  268. break;
  269. }
  270. }
  271. }
  272. //车库运作状态
  273. public SingleChip.Garage_state_msg create_garage_state_msg(int door_control)
  274. {
  275. SingleChip.Garage_state_msg t_garage_state_msg = new SingleChip.Garage_state_msg();
  276. SingleChip.Base_info baseInfo = new SingleChip.Base_info();
  277. baseInfo.MsgType = SingleChip.Message_type.EGarageStateMsg;
  278. baseInfo.Sender = SingleChip.Communicator.ETerminal;
  279. baseInfo.Receiver = SingleChip.Communicator.ECollector;
  280. t_garage_state_msg.BaseInfo = baseInfo;
  281. //0表示空闲
  282. if (m_statu == Entrance_process_statu.ENTRANCE_FREE)
  283. {
  284. t_garage_state_msg.GarageState = 0;
  285. }
  286. //1表示工作中
  287. else
  288. {
  289. t_garage_state_msg.GarageState = 1;
  290. }
  291. t_garage_state_msg.DoorControl = door_control;
  292. return t_garage_state_msg;
  293. }
  294. //生成存车请求
  295. public Message.Store_command_request_msg create_store_request_msg()
  296. {
  297. //存车请求
  298. Message.Store_command_request_msg t_msg = new Message.Store_command_request_msg();
  299. //消息头
  300. Message.Base_info baseInfo = new Message.Base_info();
  301. baseInfo.MsgType = Message.Message_type.EStoreCommandRequestMsg;
  302. baseInfo.Sender = Message.Communicator.ETerminor;
  303. baseInfo.Receiver = Message.Communicator.EMain;
  304. //地面雷达的 实时定位信息
  305. Message.Locate_information locateInformation = new Message.Locate_information();
  306. locateInformation = Ground_radar_data_buffer.Instance.get_ground_status_msg().LocateInformationRealtime;
  307. //车辆信息--长宽高从雷达获取,车牌号从单片机获取
  308. Message.Car_info car_Info = new Message.Car_info();
  309. car_Info.License = Singlechip_data_buffer.Instance.get_sensor_state_msg().LicenseNumber;
  310. car_Info.CarLength = locateInformation.LocateLength;
  311. car_Info.CarWidth = locateInformation.LocateWidth;
  312. car_Info.CarHeight = locateInformation.LocateHeight;
  313. m_car_license = car_Info.License;
  314. //填充信息
  315. t_msg.BaseInfo = baseInfo;
  316. t_msg.TerminalId = m_terminal_id;
  317. t_msg.LocateInformation = locateInformation;
  318. t_msg.CarInfo = car_Info;
  319. return t_msg;
  320. }
  321. //生成存车请求
  322. public Message.Store_command_request_msg create_store_request_msg_test()
  323. {
  324. //存车请求
  325. Message.Store_command_request_msg t_msg = new Message.Store_command_request_msg();
  326. //消息头
  327. Message.Base_info baseInfo = new Message.Base_info();
  328. baseInfo.MsgType = Message.Message_type.EStoreCommandRequestMsg;
  329. baseInfo.Sender = Message.Communicator.ETerminor;
  330. baseInfo.Receiver = Message.Communicator.EMain;
  331. //地面雷达的 实时定位信息
  332. Message.Locate_information locateInformation = new Message.Locate_information();
  333. locateInformation = Ground_radar_data_buffer.Instance.get_ground_status_msg().LocateInformationRealtime;
  334. //车辆信息--长宽高从雷达获取,车牌号从单片机获取
  335. Message.Car_info car_Info = new Message.Car_info();
  336. car_Info.License = "鄂A123WK";
  337. car_Info.CarLength = locateInformation.LocateLength;
  338. car_Info.CarWidth = locateInformation.LocateWidth;
  339. car_Info.CarHeight = 1.6f;
  340. m_car_license = car_Info.License;
  341. //填充信息
  342. t_msg.BaseInfo = baseInfo;
  343. t_msg.TerminalId = int.Parse(json_file_operation.Instance.read_json_string("ternimalNumber"));
  344. t_msg.LocateInformation = locateInformation;
  345. t_msg.CarInfo = car_Info;
  346. return t_msg;
  347. }
  348. //打印二维码
  349. public void print_qr_code()
  350. {
  351. if (m_car_license != "")
  352. {
  353. PrintManual printManual = new PrintManual();//打印二维码对象
  354. printManual.printTicket(m_car_license);//打印二维码
  355. }
  356. }
  357. //获取状态
  358. public Entrance_process_statu get_process_statu()
  359. {
  360. return m_statu;
  361. }
  362. //设置状态
  363. public void set_process_statu(Entrance_process_statu t_statu)
  364. {
  365. m_statu = t_statu;
  366. }
  367. private Entrance_process_statu m_statu; //流程状态
  368. private Thread m_storing_process_thread; //处理停车流程消息线程
  369. protected bool m_storing_process_condition; //处理停车流程消息条件变量
  370. protected string m_car_license; //当前停车流程的车牌号,用于打印二维码
  371. protected EventWaitHandle m_event_exit; //防止CPU卡死
  372. protected DateTime m_send_request_time; //发送请求的时间
  373. protected DateTime m_recv_response_time; //接收反馈时间
  374. protected int m_terminal_id; //本停车终端终端号,从settings.json读取
  375. protected Message.Store_command_request_msg m_store_command_request_msg;//存车请求
  376. protected Message.Store_command_response_msg m_store_command_response_msg;//存车反馈
  377. }
  378. }