123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- using Communication;
- using Google.Protobuf;
- using Message;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace communicate_module.communication
- {
- class TerminalSimul : Communication.Communicator
- {
- protected TerminalSimul() : base()
- {
- map_lock = new object();
- license_status_map = new Dictionary<string, CAR_STATUS>();
- for (int i = 0; i < 10; i++)
- {
- string str = String.Format("鄂A{0:D5}", i);
- license_status_map.Add(str, CAR_STATUS.eCAR_IDEL);
- //Console.WriteLine(str);
- }
- park_terminal_status_map = new Dictionary<int, int>();
- for (int i = 0; i < 6; i++)
- {
- park_terminal_status_map.Add(i, 0);
- }
- fetch_terminal_status_map = new Dictionary<int, int>();
- for (int i = 0; i < 6; i++)
- {
- fetch_terminal_status_map.Add(i, 0);
- }
- park_license_map = new Dictionary<int, string>();
- fetch_license_map = new Dictionary<int, string>();
- }
- /// <summary>
- /// 单例访问
- /// </summary>
- public new static TerminalSimul GetInstance()
- {
- if (instance == null)
- {
- lock (lockObj)
- {
- if (instance == null)
- {
- instance = new TerminalSimul();
- }
- }
- }
- return (TerminalSimul)instance;
- }
- /// <summary>
- /// 检查消息
- /// </summary>
- /// <param name="header"></param>
- /// <returns></returns>
- public override bool CheckMsg(Base_info header)
- {
- if (header.HasMsgType && header.HasSender
- && ((header.MsgType == Message_type.EStoreCommandResponseMsg && header.Sender == Message.Communicator.EMain)
- || (header.MsgType == Message_type.EPickupCommandResponseMsg && header.Sender == Message.Communicator.EMain)
- || (header.MsgType == Message_type.EStoringProcessStatuMsg && header.Sender == Message.Communicator.EMain)
- || (header.MsgType == Message_type.EPickingProcessStatuMsg && header.Sender == Message.Communicator.EMain)
- )
- )
- return true;
- else
- return false;
- }
- /// <summary>
- /// 检查执行器状态
- /// </summary>
- /// <param name="header"></param>
- /// <param name="receiveTime"></param>
- /// <returns></returns>
- public override CheckExecuterReturn CheckExecuter(Base_info header, DateTime receiveTime)
- {
- if ((DateTime.Now - receiveTime).Milliseconds > header.TimeoutMs)
- return CheckExecuterReturn.MSG_TIMEOUT;
- else
- return CheckExecuterReturn.EXECUTER_READY;
- }
- /// <summary>
- /// 执行消息
- /// </summary>
- /// <param name="msgStamped"></param>
- /// <returns></returns>
- public override bool ExecuteMsg(MsgStamped msgStamped)
- {
- if (msgStamped == null || msgStamped.header==null)
- return false;
- switch(msgStamped.header.MsgType)
- {
- case Message_type.EStoreCommandResponseMsg:
- Store_command_response_msg store_Command_Response_Msg = Store_command_response_msg.Parser.ParseFrom(msgStamped.msg);
- Console.WriteLine("停车终端 " + store_Command_Response_Msg.TerminalId + " 签收, " + (store_Command_Response_Msg.Code.ErrorCode == 0?"成功":"失败"));
- break;
- case Message_type.EPickupCommandResponseMsg:
- Pickup_command_response_msg pickup_Command_Response_Msg = Pickup_command_response_msg.Parser.ParseFrom(msgStamped.msg);
- Console.WriteLine("取车终端 " + pickup_Command_Response_Msg.TerminalId + " 签收, " + (pickup_Command_Response_Msg.Code.ErrorCode == 0 ? "成功" : "失败"));
- break;
- case Message_type.EStoringProcessStatuMsg:
- Storing_process_statu_msg storing_Process_Statu_Msg = Storing_process_statu_msg.Parser.ParseFrom(msgStamped.msg);
- if (storing_Process_Statu_Msg.Completed || storing_Process_Statu_Msg.BackCompleted)
- {
- bool forward = storing_Process_Statu_Msg.Completed;
- lock (map_lock)
- {
- if (park_license_map.ContainsKey(storing_Process_Statu_Msg.TerminalId))
- {
- string lic = park_license_map[storing_Process_Statu_Msg.TerminalId];
- if (license_status_map.ContainsKey(lic))
- {
- string whether_success = "";
- if (forward)
- {
- // 车辆状态修改
- license_status_map[lic] = CAR_STATUS.eCAR_PARKED;
- whether_success = "成功";
- }
- else
- {
- license_status_map[lic] = CAR_STATUS.eCAR_IDEL;
- whether_success = "失败";
- }
- // 停车流程结束,删除记录
- park_license_map.Remove(storing_Process_Statu_Msg.TerminalId);
- // 停车终端复位
- park_terminal_status_map[storing_Process_Statu_Msg.TerminalId] = 0;
- Console.WriteLine("停车 " + lic + " 完成, " + whether_success);
- }
- }
- else
- {
- //Console.WriteLine("未找到与反馈对应停车流程???");
- }
- }
- }
- break;
- case Message_type.EPickingProcessStatuMsg:
- Picking_process_statu_msg picking_Process_Statu_Msg = Picking_process_statu_msg.Parser.ParseFrom(msgStamped.msg);
- if (picking_Process_Statu_Msg.Completed || picking_Process_Statu_Msg.BackCompleted)
- {
- bool forward = picking_Process_Statu_Msg.Completed;
- lock (map_lock)
- {
- if (fetch_license_map.ContainsKey(picking_Process_Statu_Msg.TerminalId))
- {
- string lic = fetch_license_map[picking_Process_Statu_Msg.TerminalId];
- if (license_status_map.ContainsKey(lic))
- {
- string whether_success = "";
- if (forward)
- {
- license_status_map[lic] = CAR_STATUS.eCAR_IDEL;
- whether_success = "成功";
- }
- else
- {
- license_status_map[lic] = CAR_STATUS.eCAR_PARKED;
- whether_success = "失败";
- }
- fetch_license_map.Remove(picking_Process_Statu_Msg.TerminalId);
- fetch_terminal_status_map[picking_Process_Statu_Msg.TerminalId] = 0;
- Console.WriteLine("取车 " + lic + " 完成, " + whether_success);
- }
- }
- else
- {
- //Console.WriteLine("未找到与反馈对应取车流程???");
- }
- }
- }
- break;
- }
- return true;
- }
- /// <summary>
- /// 模拟终端与主控发送消息
- /// </summary>
- public void CreateSimulThread()
- {
- Task.Factory.StartNew(()=> {
- Random rnd = new Random();
- while(!mb_exit)
- {
- int act = rnd.Next(1, 4);
- lock (map_lock) {
- switch (act)
- {
- // 停车
- case 1:
- // 找一辆空闲车发送停车指令
- string park_lic = "";
- KeyValuePair<string, CAR_STATUS> t_park_lic_pair;
- for (int i = 0; i < license_status_map.Count; i++)
- {
- t_park_lic_pair = license_status_map.ElementAt(i);
- if (t_park_lic_pair.Value == CAR_STATUS.eCAR_IDEL)
- {
- park_lic = t_park_lic_pair.Key;
- break;
- }
- }
- if(park_lic != "")
- {
- Store_command_request_msg t_store_req = new Store_command_request_msg();
- t_store_req.BaseInfo = new Base_info();
- t_store_req.BaseInfo.MsgType = Message_type.EStoreCommandRequestMsg;
- t_store_req.BaseInfo.TimeoutMs = 5000;
- t_store_req.BaseInfo.Sender = Message.Communicator.ETerminor;
- t_store_req.BaseInfo.Receiver = Message.Communicator.EMain;
- // 寻找空闲终端
- int termId = GetParkTerminalId();
- if(termId>=0)
- {
- t_store_req.TerminalId = termId;
- t_store_req.LocateInformation = new Locate_information();
- t_store_req.LocateInformation.LocateX = 0.01f;
- t_store_req.LocateInformation.LocateY = 0.01f;
- t_store_req.LocateInformation.LocateAngle = 90.5f;
- t_store_req.LocateInformation.LocateLength = 0f;
- t_store_req.LocateInformation.LocateWidth = 0f;
- t_store_req.LocateInformation.LocateHeight = 0f;
- t_store_req.LocateInformation.LocateWheelBase = 2.64f;
- t_store_req.LocateInformation.LocateWheelWidth = 1.65f;
- t_store_req.LocateInformation.LocateCorrect = true;
- t_store_req.LocateInformation.LocateFrontTheta = 0f;
- t_store_req.CarInfo = new Car_info();
- t_store_req.CarInfo.License = park_lic;
- t_store_req.CarInfo.CarHeight = 1.7f;
- t_store_req.CarInfo.CarWheelWidth = t_store_req.LocateInformation.LocateWheelWidth;
- t_store_req.CarInfo.CarWheelBase = t_store_req.LocateInformation.LocateWheelBase;
- // 发送停车指令
- Console.WriteLine("停车 "+park_lic);
- Send_msg(t_store_req.ToByteString());
- license_status_map[park_lic] = CAR_STATUS.eCAR_PARKING;
- park_license_map.Add(termId, park_lic);
- }
- }
- break;
- // 取车
- case 2:
- // 找一辆占用车发送取车指令
- string fetch_lic = "";
- KeyValuePair<string, CAR_STATUS> t_fetch_lic_pair;
- for (int i = 0; i < license_status_map.Count; i++)
- {
- t_fetch_lic_pair = license_status_map.ElementAt(i);
- if (t_fetch_lic_pair.Value == CAR_STATUS.eCAR_PARKED)
- {
- fetch_lic = t_fetch_lic_pair.Key;
- break;
- }
- }
- if(fetch_lic!="")
- {
- Pickup_command_request_msg pick_req = new Pickup_command_request_msg();
- pick_req.BaseInfo = new Base_info();
- pick_req.BaseInfo.MsgType = Message_type.EPickupCommandRequestMsg;
- pick_req.BaseInfo.TimeoutMs = 5000;
- pick_req.BaseInfo.Sender = Message.Communicator.ETerminor;
- pick_req.BaseInfo.Receiver = Message.Communicator.EMain;
- // 寻找空闲终端
- int termId = GetFetchTerminalId();
- if(termId >=0)
- {
- pick_req.TerminalId = termId;
- pick_req.CarInfo = new Car_info();
- pick_req.CarInfo.License = fetch_lic;
- // 发送取车指令
- Console.WriteLine("取车 " + fetch_lic);
- Send_msg(pick_req.ToByteString());
- license_status_map[fetch_lic] = CAR_STATUS.eCAR_FETCHING;
- fetch_license_map.Add(termId, fetch_lic);
- }
- }
- break;
- // 不处理
- case 3:
- break;
- }
-
- }
- Thread.Sleep(2000);
- }
- });
- }
- protected int GetParkTerminalId()
- {
- int termId = -1;
- KeyValuePair<int, int> t_res;
- for (int i = 0; i < park_terminal_status_map.Count; i++)
- {
- t_res = park_terminal_status_map.ElementAt(i);
- // 停取终端同时空闲
- if (t_res.Value == 0)
- {
- termId = t_res.Key;
- break;
- }
- }
- if(termId>=0)
- park_terminal_status_map[termId] = 1;
- return termId;
- }
- protected int GetFetchTerminalId()
- {
- int termId = -1;
- KeyValuePair<int, int> t_res;
- for (int i = 0; i < fetch_terminal_status_map.Count; i++)
- {
- t_res = fetch_terminal_status_map.ElementAt(i);
- if (t_res.Value == 0)
- {
- termId = t_res.Key;
- break;
- }
- }
- if (termId >= 0)
- fetch_terminal_status_map[termId] = 1;
- return termId;
- }
- public enum CAR_STATUS
- {
- eCAR_IDEL,
- eCAR_PARKED,
- eCAR_PARKING,
- eCAR_FETCHING,
- }
- protected object map_lock;
- // 车辆状态表
- protected Dictionary<string, CAR_STATUS> license_status_map;
- // 记录停车终端当前号牌
- protected Dictionary<int, string> park_license_map;
- // 记录取车终端当前号牌
- protected Dictionary<int, string> fetch_license_map;
- // 0空闲,非零占用
- protected Dictionary<int, int> park_terminal_status_map;
- // 0空闲,非零占用
- protected Dictionary<int, int> fetch_terminal_status_map;
- }
- }
|