123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using chutian_parking_terminal.data_buffer;
- using tool;
- using chutian_parking_terminal.communication;
- using Google.Protobuf;
- namespace chutian_parking_terminal.motion_executer
- {
- class Entrance_execute : Singleton<Entrance_execute>
- {
- //入口流程
- public enum Entrance_process_statu
- {
- ENTRANCE_UNKNOW = 0,//未知
- ENTRANCE_FREE = 1,//空闲 待机, 检查地感
- ENTRANCE_OPEN_DOOR = 2,//通知单片机开门
- ENTRANCE_MONITOR_CAR = 3,//监控车辆是否停到位
- ENTRANCE_WAIT_USER = 4,//准备就绪, 等待用户点击存车按钮
- ENTRANCE_CLOSE_DOOR = 5,//通知单片机关门
- ENTRANCE_SEND_REQUEST = 6,//发送停车请求
- ENTRANCE_WAIT_RESPONSE = 7,//等待签收答复, 检擦成功进行下一步
- ENTRANCE_MONITOR_PROCESS = 8,//监听流程状态 检查状态结果
- ENTRANCE_RESET_PROCESS = 9,//重置流程
- };
-
- private Entrance_execute()
- {
- }
- //初始化
- public void entrance_execute_init()
- {
- m_statu = Entrance_process_statu.ENTRANCE_FREE;
- //默认循环
- m_storing_process_condition = true;
- m_storing_process_thread = new Thread(storing_process_thread);
- m_car_license = "";
- m_event_exit = new EventWaitHandle(false, EventResetMode.ManualReset);
- m_terminal_id = int.Parse(json_file_operation.Instance.read_json_string("ternimalNumber"));
- //启动线程
- m_storing_process_thread.Start();
- }
- //启动流程
- public void entrance_process_run()
- {
- //开始关门步骤
- m_statu = Entrance_process_statu.ENTRANCE_CLOSE_DOOR;
- }
- //反初始化
- public void entrance_execute_uninit()
- {
- m_statu = Entrance_process_statu.ENTRANCE_UNKNOW;
- m_storing_process_condition = false;
- m_storing_process_thread.Abort();
- }
-
- ///设置存车的反馈--由通信层调用
- public void set_store_response_msg(Message.Store_command_response_msg t_store_command_response_msg)
- {
- //只处理本终端的消息
- if (t_store_command_response_msg.TerminalId == m_terminal_id)
- {
- m_store_command_response_msg = t_store_command_response_msg;
- }
- }
- //处理停车流程消息线程函数
- protected void storing_process_thread()
- {
- while (m_storing_process_condition && !m_event_exit.WaitOne(1))
- {
- Console.WriteLine(DateTime.Now.Second.ToString()+":"+DateTime.Now.Millisecond);
- Console.WriteLine(m_statu);
- switch (m_statu)
- {
- //空闲状态 实时检测是否有车来
- case Entrance_process_statu.ENTRANCE_FREE:
- {
- //Tip_data_buffer.Instance.set_tip_str("等待车辆到来!");
- if (!Singlechip_data_buffer.Instance.has_garage_state_msg())
- {
- //0为不控制 1为控制开门 2表示关门
- SingleChip.Garage_state_msg t_garage_state_msg = create_garage_state_msg(2);
- Singlechip_data_buffer.Instance.set_garage_state_msg(t_garage_state_msg);
- }
- if (Singlechip_data_buffer.Instance.has_sensor_state_msg())
- {
- SingleChip.Sensor_state_msg t_sensor_state_msg = Singlechip_data_buffer.Instance.get_sensor_state_msg();
- //查看是否是本终端来车
- if (t_sensor_state_msg.TerminalId == m_terminal_id)
- {
- //地感地磁都检测到有车
- if (t_sensor_state_msg.GroundSense.Outdoor == 1 && t_sensor_state_msg.Geomag.Outdoor == 1)
- {
- //开始流程
- Tip_data_buffer.Instance.set_tip_str("开始流程!");
- m_statu = Entrance_process_statu.ENTRANCE_OPEN_DOOR;
- }
- }
- }
- }
- break;
- //控制开门 通知单片机开门
- case Entrance_process_statu.ENTRANCE_OPEN_DOOR:
- {
- //0为不控制 1为控制开门 2表示关门
- SingleChip.Garage_state_msg t_garage_state_msg = create_garage_state_msg(1);
- Singlechip_data_buffer.Instance.set_garage_state_msg(t_garage_state_msg);
- m_statu = Entrance_process_statu.ENTRANCE_MONITOR_CAR;
- }
- break;
- //辅助停车 显示当前车辆位置 停的到不到位
- case Entrance_process_statu.ENTRANCE_MONITOR_CAR:
- {
- SingleChip.Sensor_state_msg t_sensor_state_msg = Singlechip_data_buffer.Instance.get_sensor_state_msg();
- //监控本终端的车有没有停好
- if (t_sensor_state_msg.TerminalId == m_terminal_id)
- {
- //0表示门开到位
- if (t_sensor_state_msg.DoorState == 0)
- {
- //门内地磁地感未检测到车辆
- if (t_sensor_state_msg.Geomag.Indoor == 0 || t_sensor_state_msg.GroundSense.Indoor == 0)
- {
- //门外地磁地感检测到车辆
- if (t_sensor_state_msg.Geomag.Outdoor == 1 && t_sensor_state_msg.GroundSense.Outdoor == 1)
- {
- Tip_data_buffer.Instance.set_tip_str("请将车辆驶入!");
- }
- }
- //门内地磁地感均检测到车
- else if (t_sensor_state_msg.Geomag.Indoor == 1 && t_sensor_state_msg.GroundSense.Indoor == 1)
- {
- if (t_sensor_state_msg.OverLimit.Front == 0 && t_sensor_state_msg.OverLimit.Back == 0//车辆未超前和超后
- && t_sensor_state_msg.OverLimit.Left == 0 && t_sensor_state_msg.OverLimit.Right == 0//车辆未超左和超右
- && t_sensor_state_msg.OverLimit.High == 0 && t_sensor_state_msg.OverLimit.Low == 0)//车辆未超高和底盘未过低)
- {
- //等待用户进行操作
- m_statu = Entrance_process_statu.ENTRANCE_WAIT_USER;
- }
- else
- {
- Tip_data_buffer.Instance.set_tip_str("请将车辆挪正!");
- }
- }
- }
- //1表示门关到位
- else if (t_sensor_state_msg.DoorState == 1)
- {
- Tip_data_buffer.Instance.set_tip_str("请等待开门!");
- }
- //2表示门运行中
- else if (t_sensor_state_msg.DoorState == 2)
- {
- Tip_data_buffer.Instance.set_tip_str("库门运行中,请保持距离!");
- }
- //3表示门报警
- else if (t_sensor_state_msg.DoorState == 3)
- {
- Tip_data_buffer.Instance.set_tip_str("门报警,请联系管理员!");
- }
- }
- }
- break;
- //待机状态 等待用户点击停车按钮
- case Entrance_process_statu.ENTRANCE_WAIT_USER:
- {
- SingleChip.Sensor_state_msg t_sensor_state_msg = Singlechip_data_buffer.Instance.get_sensor_state_msg();
- //监控本终端的车有没有停好
- if (t_sensor_state_msg.TerminalId == m_terminal_id)
- {
- //门内地磁地感均检测到车
- if (t_sensor_state_msg.Geomag.Indoor == 1 && t_sensor_state_msg.GroundSense.Indoor == 1)
- {
- //车已停好 等待用户点击停车
- Tip_data_buffer.Instance.set_tip_str("车辆已停好,请点击停车按钮!");
- }
- else
- {
- //门内地磁地感没检测到车说明车辆离开
- m_statu = Entrance_process_statu.ENTRANCE_RESET_PROCESS;
- }
- }
-
- }
- break;
- //控制关门 通知单片机关门
- case Entrance_process_statu.ENTRANCE_CLOSE_DOOR:
- {
- //0为不控制 1为控制开门 2表示关门
- SingleChip.Garage_state_msg t_garage_state_msg = create_garage_state_msg(2);
- Singlechip_data_buffer.Instance.set_garage_state_msg(t_garage_state_msg);
- m_statu = Entrance_process_statu.ENTRANCE_SEND_REQUEST;
- }
- break;
- //发送请求 发送停车请求消息
- case Entrance_process_statu.ENTRANCE_SEND_REQUEST:
- {
- //创建停车请求
- m_store_command_request_msg = create_store_request_msg();
- //发送停车请求
- Channel_communication.Instance.encapsulate_msg(m_store_command_request_msg.ToByteArray());
- m_send_request_time = DateTime.Now;
- m_statu = Entrance_process_statu.ENTRANCE_WAIT_RESPONSE;
- }
- break;
- //等待反馈 等待停车反馈
- case Entrance_process_statu.ENTRANCE_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 = Entrance_process_statu.ENTRANCE_RESET_PROCESS;
- //进入故障流程
- }
- if (m_store_command_response_msg != null)
- {
- //查看反馈结果
- if (m_store_command_response_msg.Code.ErrorCode == 0)
- {
- //成功签收就进入下一步
- Tip_data_buffer.Instance.set_tip_str(m_car_license + " 停车请求已签收!");
- //进入监听状态
- m_statu = Entrance_process_statu.ENTRANCE_MONITOR_PROCESS;
- break;
- }
- else
- {
- Tip_data_buffer.Instance.set_tip_str(" 停车请求有误!");
- Log.Instance.WriteLog(LogType.PROCESS, LogFile.LOG, m_store_command_request_msg.ToString() + " 停车请求有误!" );
- m_statu = Entrance_process_statu.ENTRANCE_RESET_PROCESS;
- }
- }
- }
- break;
- //监听主控流程状态 检查状态结果
- case Entrance_process_statu.ENTRANCE_MONITOR_PROCESS:
- {
- if ( Central_control_data_buffer.Instance.has_storing_process_statu_msg() &&
- Central_control_data_buffer.Instance.get_storing_process_statu_msg().TerminalId == m_terminal_id)
- {
- Tip_data_buffer.Instance.set_tip_str(" 正在监听!");
- Message.Storing_process_statu_msg t_storing_process_statu_msg = Central_control_data_buffer.Instance.get_storing_process_statu_msg();
- if (t_storing_process_statu_msg.Completed && t_storing_process_statu_msg.License == m_car_license)
- {
- Tip_data_buffer.Instance.set_tip_str(" 停车成功!");
- m_statu = Entrance_process_statu.ENTRANCE_RESET_PROCESS;
- Thread.Sleep(3000);
- }
- else if (t_storing_process_statu_msg.BackCompleted && t_storing_process_statu_msg.License == m_car_license)
- {
- Tip_data_buffer.Instance.set_tip_str(" 停车失败!");
- m_statu = Entrance_process_statu.ENTRANCE_RESET_PROCESS;
- Thread.Sleep(3000);
- }
- }
- }
- break;
- //重置流程 流程还原
- case Entrance_process_statu.ENTRANCE_RESET_PROCESS:
- {
- m_store_command_request_msg = null;
- m_store_command_response_msg = null;
- Tip_data_buffer.Instance.set_tip_str("");
- m_statu = Entrance_process_statu.ENTRANCE_FREE;
- }
- break;
- }
- }
- }
- //车库运作状态
- public SingleChip.Garage_state_msg create_garage_state_msg(int door_control)
- {
- SingleChip.Garage_state_msg t_garage_state_msg = new SingleChip.Garage_state_msg();
- SingleChip.Base_info baseInfo = new SingleChip.Base_info();
- baseInfo.MsgType = SingleChip.Message_type.EGarageStateMsg;
- baseInfo.Sender = SingleChip.Communicator.ETerminal;
- baseInfo.Receiver = SingleChip.Communicator.ECollector;
- t_garage_state_msg.BaseInfo = baseInfo;
- //0表示空闲
- if (m_statu == Entrance_process_statu.ENTRANCE_FREE)
- {
- t_garage_state_msg.GarageState = 0;
- }
- //1表示工作中
- else
- {
- t_garage_state_msg.GarageState = 1;
- }
- t_garage_state_msg.DoorControl = door_control;
- return t_garage_state_msg;
- }
- //生成存车请求
- public Message.Store_command_request_msg create_store_request_msg()
- {
- //存车请求
- Message.Store_command_request_msg t_msg = new Message.Store_command_request_msg();
- //消息头
- Message.Base_info baseInfo = new Message.Base_info();
- baseInfo.MsgType = Message.Message_type.EStoreCommandRequestMsg;
- baseInfo.Sender = Message.Communicator.ETerminor;
- baseInfo.Receiver = Message.Communicator.EMain;
- //地面雷达的 实时定位信息
- Message.Locate_information locateInformation = new Message.Locate_information();
- locateInformation = Ground_radar_data_buffer.Instance.get_ground_status_msg().LocateInformationRealtime;
- //车辆信息--长宽高从雷达获取,车牌号从单片机获取
- Message.Car_info car_Info = new Message.Car_info();
- car_Info.License = Singlechip_data_buffer.Instance.get_sensor_state_msg().LicenseNumber;
- car_Info.CarLength = locateInformation.LocateLength;
- car_Info.CarWidth = locateInformation.LocateWidth;
- car_Info.CarHeight = locateInformation.LocateHeight;
- m_car_license = car_Info.License;
- //填充信息
- t_msg.BaseInfo = baseInfo;
- t_msg.TerminalId = m_terminal_id;
- t_msg.LocateInformation = locateInformation;
- t_msg.CarInfo = car_Info;
- return t_msg;
- }
- //生成存车请求
- public Message.Store_command_request_msg create_store_request_msg_test()
- {
- //存车请求
- Message.Store_command_request_msg t_msg = new Message.Store_command_request_msg();
- //消息头
- Message.Base_info baseInfo = new Message.Base_info();
- baseInfo.MsgType = Message.Message_type.EStoreCommandRequestMsg;
- baseInfo.Sender = Message.Communicator.ETerminor;
- baseInfo.Receiver = Message.Communicator.EMain;
- //地面雷达的 实时定位信息
- Message.Locate_information locateInformation = new Message.Locate_information();
- locateInformation = Ground_radar_data_buffer.Instance.get_ground_status_msg().LocateInformationRealtime;
- //车辆信息--长宽高从雷达获取,车牌号从单片机获取
- Message.Car_info car_Info = new Message.Car_info();
- car_Info.License = "鄂A123WK";
- car_Info.CarLength = locateInformation.LocateLength;
- car_Info.CarWidth = locateInformation.LocateWidth;
- car_Info.CarHeight = 1.6f;
- m_car_license = car_Info.License;
- //填充信息
- t_msg.BaseInfo = baseInfo;
- t_msg.TerminalId = int.Parse(json_file_operation.Instance.read_json_string("ternimalNumber"));
- t_msg.LocateInformation = locateInformation;
- t_msg.CarInfo = car_Info;
- return t_msg;
- }
- //打印二维码
- public void print_qr_code()
- {
- if (m_car_license != "")
- {
- PrintManual printManual = new PrintManual();//打印二维码对象
- printManual.printTicket(m_car_license);//打印二维码
- }
- }
- //获取状态
- public Entrance_process_statu get_process_statu()
- {
- return m_statu;
- }
- //设置状态
- public void set_process_statu(Entrance_process_statu t_statu)
- {
- m_statu = t_statu;
- }
- private Entrance_process_statu m_statu; //流程状态
- private Thread m_storing_process_thread; //处理停车流程消息线程
- protected bool m_storing_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 int m_terminal_id; //本停车终端终端号,从settings.json读取
- protected Message.Store_command_request_msg m_store_command_request_msg;//存车请求
- protected Message.Store_command_response_msg m_store_command_response_msg;//存车反馈
- }
- }
|