using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using chutian_parking_terminal.data_buffer; using tool; using chutian_parking_terminal.communication; using Google.Protobuf; using System.Threading; namespace chutian_parking_terminal.motion_executer { class Exit_execute : Singleton { //出口流程状态 public enum Exit_process_statu { EXIT_UNKNOW = 0,//未知 EXIT_FREE = 1,//空闲 等待取车指令到来 EXIT_SEND_REQUEST = 2,//发送取车请求 EXIT_WAIT_RESPONSE = 3,//等待签收答复 检擦成功进行下一步 EXIT_MONITOR_PROCESS = 4,//监控流程 检查流程结果 EXIT_OPEN_DOOR = 5,//开门 控制单片机开门 EXIT_CHECK_GOUNDSENSE_GEOMAG = 6,//检查地磁地感 等待车辆离开 EXIT_RESET_PROCESS = 7,//重置流程 }; private Exit_execute() { } //初始化 public void exit_execute_init() { m_statu = Exit_process_statu.EXIT_FREE; //默认循环 m_picking_process_condition = true; m_picking_process_thread = new Thread(picking_process_thread); m_car_license = ""; m_event_exit = new EventWaitHandle(false, EventResetMode.ManualReset); //启动线程 m_picking_process_thread.Start(); } public void exit_execute_uninit() { m_statu = Exit_process_statu.EXIT_UNKNOW; m_picking_process_condition = false; m_picking_process_thread.Abort(); } //开始取车流程 public void exit_process_run() { m_statu = Exit_process_statu.EXIT_SEND_REQUEST; } //设置取车的反馈--由通信层调用 public void set_picking_response_msg(Message.Pickup_command_response_msg t_pickup_command_response_msg) { m_pickup_command_response_msg = t_pickup_command_response_msg; } //取车任务流程 protected void picking_process_thread() { while (m_picking_process_condition && !m_event_exit.WaitOne(1)) { Console.WriteLine(DateTime.Now.Second.ToString() + ":" + DateTime.Now.Millisecond); Console.WriteLine(m_statu); switch (m_statu) { //空闲状态 等待指令 case Exit_process_statu.EXIT_FREE: { if (!Singlechip_data_buffer.Instance.has_door_control_msg()) { //0为不控制 1为控制开门 2表示关门 SingleChip.Door_control_msg t_door_control_state_msg = create_door_control_state_msg(2); Singlechip_data_buffer.Instance.set_door_control_msg(t_door_control_state_msg); } } break; //发送取车请求 case Exit_process_statu.EXIT_SEND_REQUEST: { if (m_car_license != "" && (m_car_license.Length == 7 || m_car_license.Length == 8)) { //创建取车请求 m_pickup_command_request_msg = create_pickup_request_msg(); //发送取车请求 Channel_communication.Instance.encapsulate_msg(m_pickup_command_request_msg.ToByteArray()); //记录发送时间 m_send_request_time = DateTime.Now; m_statu = Exit_process_statu.EXIT_WAIT_RESPONSE; } else { MessageBoxForm messageBoxForm = new MessageBoxForm(); messageBoxForm.ShowDialog("非正常车牌号!"+ m_car_license.Length.ToString()); m_statu = Exit_process_statu.EXIT_FREE; } } break; //等待签收答复 查看签收结果 case Exit_process_statu.EXIT_WAIT_RESPONSE: { m_recv_response_time = DateTime.Now; if ((m_recv_response_time.Ticks / 1000 - m_send_request_time.Ticks / 1000) > 5000) { MessageBoxForm messageBoxForm = new MessageBoxForm(); messageBoxForm.ShowDialog("等待反馈超时!请联系管理员", true); m_statu = Exit_process_statu.EXIT_RESET_PROCESS; //进入故障流程 } if (m_pickup_command_response_msg != null) { //查看反馈结果 if (m_pickup_command_response_msg.Code.ErrorCode == 0) { //成功签收就进入下一步 Tip_data_buffer.Instance.set_tip_str(m_car_license + " 取车请求已签收!"); //进入监听状态 m_statu = Exit_process_statu.EXIT_MONITOR_PROCESS; break; } else { MessageBoxForm messageBoxForm = new MessageBoxForm(); messageBoxForm.ShowDialog("取车请求有误!", true); Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, m_pickup_command_response_msg.ToString() + " 停车请求有误!"); m_statu = Exit_process_statu.EXIT_RESET_PROCESS; } } } break; //监控流程 检查流程结果 case Exit_process_statu.EXIT_MONITOR_PROCESS: { if (Central_control_data_buffer.Instance.has_picking_process_statu_msg()) { Tip_data_buffer.Instance.set_tip_str(" 正在监听!"); Message.Picking_process_statu_msg t_picking_process_statu_msg = Central_control_data_buffer.Instance.get_picking_process_statu_msg(); if (t_picking_process_statu_msg.Completed && t_picking_process_statu_msg.License == m_car_license) { Tip_data_buffer.Instance.set_tip_str(" 取车成功,等待开门!"); m_statu = Exit_process_statu.EXIT_OPEN_DOOR; Thread.Sleep(3000); } else if (t_picking_process_statu_msg.BackCompleted) { Tip_data_buffer.Instance.set_tip_str(" 取车失败!"); m_statu = Exit_process_statu.EXIT_RESET_PROCESS; Thread.Sleep(3000); } } } break; //控制开门 通知单片机开门 case Exit_process_statu.EXIT_OPEN_DOOR: { //0为不控制 1为控制开门 2表示关门 SingleChip.Door_control_msg t_door_control_state_msg = create_door_control_state_msg(1); Singlechip_data_buffer.Instance.set_door_control_msg(t_door_control_state_msg); m_statu = Exit_process_statu.EXIT_CHECK_GOUNDSENSE_GEOMAG; } break; //检查地磁地感 等待车辆离开 case Exit_process_statu.EXIT_CHECK_GOUNDSENSE_GEOMAG: { if (Singlechip_data_buffer.Instance.has_door_laser_state_msg()) { SingleChip.Door_laser_state_msg t_door_laser_state_msg = Singlechip_data_buffer.Instance.get_door_laser_state_msg(); //两个雷达均为检测到异物说明车辆已离开 if (t_door_laser_state_msg.Laser1State == 0 && t_door_laser_state_msg.Laser2State == 0) { m_statu = Exit_process_statu.EXIT_RESET_PROCESS; } else { Tip_data_buffer.Instance.set_tip_str(" 等待车辆离开出口!"); } } } break; //重置流程 case Exit_process_statu.EXIT_RESET_PROCESS: { m_pickup_command_request_msg = null; m_pickup_command_response_msg = null; //0为不控制 1为控制开门 2表示关门 SingleChip.Door_control_msg t_door_control_state_msg = create_door_control_state_msg(2); Singlechip_data_buffer.Instance.set_door_control_msg(t_door_control_state_msg); Tip_data_buffer.Instance.set_tip_str(""); m_statu = Exit_process_statu.EXIT_FREE; } break; } } } //库门和激光雷达运作状态 public SingleChip.Door_control_msg create_door_control_state_msg(int door_state=0) { SingleChip.Door_control_msg t_door_control_state_msg = new SingleChip.Door_control_msg(); SingleChip.Base_info baseInfo = new SingleChip.Base_info(); baseInfo.MsgType = SingleChip.Message_type.EDoorControlMsg; baseInfo.Sender = SingleChip.Communicator.ETerminal; baseInfo.Receiver = SingleChip.Communicator.ECollector; t_door_control_state_msg.BaseInfo = baseInfo; t_door_control_state_msg.TerminalId = Singlechip_data_buffer.Instance.get_door_laser_state_msg().TerminalId; t_door_control_state_msg.DoorControl = door_state; return t_door_control_state_msg; } //取车请求 public Message.Pickup_command_request_msg create_pickup_request_msg() { //取车请求 Message.Pickup_command_request_msg t_msg = new Message.Pickup_command_request_msg(); //消息头 Message.Base_info baseInfo = new Message.Base_info(); baseInfo.MsgType = Message.Message_type.EPickupCommandRequestMsg; baseInfo.Sender = Message.Communicator.ETerminor; baseInfo.Receiver = Message.Communicator.EMain; //车辆信息--只需填充车牌号 Message.Car_info car_Info = new Message.Car_info(); car_Info.License = m_car_license; //填充信息 t_msg.BaseInfo = baseInfo; t_msg.TerminalId = Singlechip_data_buffer.Instance.get_door_laser_state_msg().TerminalId; t_msg.CarInfo = car_Info; return t_msg; } //获取状态 public Exit_process_statu get_process_statu() { return m_statu; } //从外部获取取车车牌号 public void set_pickup_license(string license) { m_car_license = license; } protected Exit_process_statu m_statu; //流程状态 protected Thread m_picking_process_thread; //处理停车流程消息线程 protected bool m_picking_process_condition; //处理停车流程消息条件变量 protected string m_car_license; //当前取车流程的车牌号 protected EventWaitHandle m_event_exit; //防止CPU卡死 protected DateTime m_send_request_time; //发送请求的时间 protected DateTime m_recv_response_time; //接收反馈时间 protected Message.Pickup_command_request_msg m_pickup_command_request_msg;//取车请求 protected Message.Pickup_command_response_msg m_pickup_command_response_msg;//取车反馈 } }