123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793 |
- using centralController.model;
- using db;
- using nettyCommunication;
- using parkMonitor.LOG;
- using PLCS7;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Terminal;
- namespace centralController.WebServer
- {
- class MyWebServer : IWebServer
- {
- private Queue<MessageUTF8> waitToReserveQueue = null;
- private Queue<MessageUTF8> reservedQueue = null;
- private object waitToReserveLock = new object();
- private object reservedLock = new object();
- private object parkLock = new object();
- private object fetchLock = new object();
- private object connectLock = new object();
- private Communication comm = null;
- private Thread receiveMsg = null;
- private bool isClosing { get; set; }
- /// <summary>
- /// 是否正在调用函数进行连接
- /// </summary>
- private bool connecting { get; set; }
- /// <summary>
- /// 连接状态
- /// </summary>
- private bool connected { get; set; }
- public void BookFetchRecord()
- {
- throw new NotImplementedException();
- }
- public void BookParkRecord()
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// 更新预约车车辆状态
- /// </summary>
- /// <param name="localDB"></param>
- /// <param name="state"></param>
- /// <param name="orderRecordsID"></param>
- /// <param name="license"></param>
- /// <returns></returns>
- private bool UpdateVehicleState(bool localDB, int state, int orderRecordsID, string license)
- {
- string vehicleUpdateSql = "";
- string vehicleInsertSql = "";
- if (orderRecordsID > 0)
- {
- if (state >= 0)
- {
- vehicleUpdateSql = "update vehicle set vehiclepParkState = " + state + " ,orderRecordsID = " + orderRecordsID + " where numberPlate = '" + license + "';";
- vehicleInsertSql = "insert into vehicle (numberPlate,vehiclepParkState,orderRecordsID) values " +
- "('" + license + "'," + state + "," + orderRecordsID + ");";
- }
- else
- {
- vehicleUpdateSql = "update vehicle set orderRecordsID = " + orderRecordsID + " where numberPlate = '" + license + "';";
- vehicleInsertSql = "insert into vehicle (numberPlate,orderRecordsID) values " +
- "('" + license + "'," + orderRecordsID + ");";
- }
- }
- else
- {
- if (state >= 0)
- {
- vehicleUpdateSql = "update vehicle set vehiclepParkState = " + state + " where numberPlate = '" + license + "';";
- vehicleInsertSql = "insert into vehicle (numberPlate,vehiclepParkState) values " +
- "('" + license + "'," + state + ");";
- }
- else
- {
- return false;
- }
- }
- List<string> vehicleUpdateList = new List<string>();
- List<string> vehicleInsertList = new List<string>();
- vehicleUpdateList.Add(vehicleUpdateSql);
- vehicleInsertList.Add(vehicleInsertSql);
- DBOperation dbHandle = null;
- if (localDB)
- dbHandle = Monitor.Monitor.localDBOper;
- else
- dbHandle = Monitor.Monitor.remoteDBOper;
- if (!dbHandle.UpdateTransaction(vehicleUpdateList))
- {
- int id = 0;
- if (!dbHandle.Insert(vehicleInsertList, out id))
- return false;
- else
- return true;
- }
- else return true;
- }
- /// <summary>
- /// 插入预约记录
- /// </summary>
- /// <param name="localDB"></param>
- /// <param name="userID"></param>
- /// <param name="parking"></param>
- /// <param name="license"></param>
- /// <param name="orderTime"></param>
- /// <param name="orderLength"></param>
- /// <returns></returns>
- private bool InsertOrderRecord(bool localDB, string userID, bool parking, string license, string orderTime, int orderLength, out int id)
- {
- bool result = false;
- string orderRecordInsertSql;
- if (parking)
- {
- orderRecordInsertSql = "insert into orderrecords (userID,numberPlate,garageID, bookParkTime, bookHour, bookPrice,bookState) " +
- "VALUES (" + userID + ", '" + license + "', '" + Monitor.Monitor.garageID + "', '" + orderTime + "', " + orderLength + ",NULL, '0');";
- }
- else
- {
- orderRecordInsertSql = "insert into orderrecords (userID,numberPlate,garageID, bookFetchTime, bookHour, bookPrice,bookState) " +
- "VALUES (" + userID + ", '" + license + "', '" + Monitor.Monitor.garageID + "', '" + orderTime + "', " + orderLength + ",NULL, '2');";
- }
- List<string> orderList = new List<string>();
- orderList.Add(orderRecordInsertSql);
- if (localDB)
- result = Monitor.Monitor.localDBOper.Insert(orderList, out id);
- else
- result = Monitor.Monitor.remoteDBOper.Insert(orderList, out id);
- return result;
- }
- /// <summary>
- /// 找到当前预约记录ID,暂未使用
- /// </summary>
- /// <param name="localDB"></param>
- /// <param name="license"></param>
- /// <param name="latestIndex">最近第几条记录,1表示最新一条</param>
- /// <returns></returns>
- private int FindCurrentOrderRecordID(bool localDB, string license, int latestIndex = 1)
- {
- int currentID = 0;
- int count = 10;
- while (count-- > 0 && currentID == 0)
- {
- List<object[]> orderRecords = Monitor.Monitor.GetOrderRecords(localDB, license, DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"));
- if (orderRecords.Count != 0)
- {
- try
- {
- currentID = (int)(UInt32)orderRecords[latestIndex - 1][0];
- }
- catch { }
- }
- }
- return currentID;
- }
- /// <summary>
- /// 预约数据库操作
- /// </summary>
- /// <param name="localDB"></param>
- /// <param name="userID"></param>
- /// <param name="parking"></param>
- /// <param name="license"></param>
- /// <param name="orderTime"></param>
- /// <param name="orderLength"></param>
- /// <returns></returns>
- private bool ReserveDBOperation(bool localDB, string userID, bool parking, string license, string orderTime, int orderLength)
- {
- UpdateVehicleState(localDB, parking ? 4 : 5, 0, license);
- //预约记录插入db
- int currentID = 0;
- InsertOrderRecord(localDB, userID, parking, license, orderTime, orderLength, out currentID);
- ////查询预约记录id号
- //int currentID = FindCurrentOrderRecordID(localDB, license);
- if (currentID == 0) { /*反馈web,预约失败*/ return false; }
- //更新车辆状态
- UpdateVehicleState(localDB, parking ? 4 : 5, currentID, license);
- return true;
- }
- /// <summary>
- /// 检查预约指令是否可行
- /// </summary>
- /// <param name="msg"></param>
- /// <returns></returns>
- private bool ReservationValidate(MessageUTF8 msg)
- {
- int allBookableSpace, count;
- lock (waitToReserveLock)
- {
- //可预约车位总数
- allBookableSpace = Monitor.Monitor.ins.GetFreeSpaceCount(4);
- count = 0;
- DateTime start, end;
- try
- {
- start = DateTime.Parse(msg.bookTime);
- end = start.AddHours(msg.bookLength);
- //Console.WriteLine("--------"+msg.bookTime+","+msg.bookLength);
- }
- catch { Console.WriteLine("时间解析异常1"); return false; }
- Queue<MessageUTF8>.Enumerator enumer = waitToReserveQueue.GetEnumerator();
- while (enumer.MoveNext())
- {
- DateTime tempStart, tempEnd;
- try
- {
- tempStart = DateTime.Parse(enumer.Current.bookTime);
- tempEnd = tempStart.AddHours(enumer.Current.bookLength);
- //Console.WriteLine("#########" + enumer.Current.bookTime + "," + enumer.Current.bookLength);
- }
- catch { Console.WriteLine("时间解析异常2"); return false; }
- //Console.WriteLine("**********" + start.ToString() + "," + end.ToString() + "," + tempStart.ToString() + "," + tempEnd.ToString());
- //Console.WriteLine("**********" + ((tempStart - end).TotalMinutes > 0)+","+ ((start - tempEnd).TotalMinutes > 0));
- if (!((tempStart - end).TotalMinutes > 0 || (start - tempEnd).TotalMinutes > 0)) { count += 1; }
- }
- Queue<MessageUTF8>.Enumerator enumer2 = reservedQueue.GetEnumerator();
- while (enumer2.MoveNext())
- {
- DateTime tempStart, tempEnd;
- try
- {
- tempStart = DateTime.Parse(enumer2.Current.bookTime);
- tempEnd = tempStart.AddHours(enumer2.Current.bookLength);
- //Console.WriteLine("#########" + enumer.Current.bookTime + "," + enumer.Current.bookLength);
- }
- catch { Console.WriteLine("时间解析异常2"); return false; }
- //Console.WriteLine("**********" + start.ToString() + "," + end.ToString() + "," + tempStart.ToString() + "," + tempEnd.ToString());
- //Console.WriteLine("**********" + ((tempStart - end).TotalMinutes > 0)+","+ ((start - tempEnd).TotalMinutes > 0));
- if (!((tempStart - end).TotalMinutes > 0 || (start - tempEnd).TotalMinutes > 0)) { count += 1; }
- }
- }
- Console.WriteLine("----------" + msg.context + ":" + allBookableSpace + "," + count);
- if (allBookableSpace > count)
- return true;
- else
- return false;
- }
- /// <summary>
- /// 根据消息类型分别处理
- /// </summary>
- /// <param name="msg"></param>
- private void MsgHandling(MessageUTF8 msg)
- {
- try
- {
- MessageUTF8 returnMsg = new MessageUTF8();
- switch (msg.cmd)
- {
- //预约停
- case "RESERVE":
- if (msg.userID != "" && msg.bookTime != "" && msg.bookLength != 0)
- {
- if (!ReservationValidate(msg))
- {
- //回复预约失败给web
- returnMsg.cmd = "RESERVEFAILED";
- returnMsg.userID = msg.userID;
- returnMsg.context = msg.context;
- returnMsg.garageID = ClientSettings.GarageID;
- comm.SendMessage(returnMsg);
- Monitor.Monitor.SetNotification("车辆" + msg.context + "预约停车,已无可预约车位", parkMonitor.model.TextColor.Warning);
- }
- else
- {
- bool DBOperResult = true;
- lock (waitToReserveLock)
- {
- waitToReserveQueue.Enqueue(msg);
- }
- //预约记录与车辆状态写入数据库
- DBOperResult = DBOperResult && ReserveDBOperation(true, msg.userID, true, msg.context, msg.bookTime, msg.bookLength);
- DBOperResult = DBOperResult && ReserveDBOperation(false, msg.userID, true, msg.context, msg.bookTime, msg.bookLength);
- //回复成功给web
- if (!DBOperResult)
- Log.WriteLog(LogType.process, LogFile.WARNING, msg.context + "从" + msg.bookTime + "开始预约" + msg.bookLength + "小时,预约指令数据库操作未获得记录ID");
- returnMsg.cmd = "RESERVEOK";
- returnMsg.userID = msg.userID;
- returnMsg.context = msg.context;
- returnMsg.garageID = ClientSettings.GarageID;
- comm.SendMessage(returnMsg);
- Monitor.Monitor.SetNotification("车辆" + msg.context + "预约停车,操作成功", parkMonitor.model.TextColor.Log);
- }
- }
- break;
- case "CANCELRESERVE":
- //IEqualityComparer<MessageUTF8> comparer ;
- //if(waitToReserveQueue.Contains(msg, )
- break;
- //预约取
- case "PREFETCH":
- break;
- //停车
- case "PARK":
- lock (parkLock)
- {
- returnMsg = new MessageUTF8();
- int id = 0;
- int countdown = 2;
- int resultCode = 8;
- //根据号牌寻找对应号牌机编号,找不到则返回失败信息
- if (msg.context != "" && msg.userID != "")
- {
- while (id == 0 && countdown-- > 0)
- {
- try
- {
- id = Terminal.Terminal.GetIdAccordingToLicense(msg.context.Split('.')[2]);
- //id = 1;
- //id = Monitor.Monitor.numMachineLinker.GetLicenseID(msg.context.Split('.')[2]);
- }
- catch { Console.WriteLine("号牌截取异常"); }
- }
- }
- if (id > 0)
- //判断号牌机编号对应PLC数据块是否空闲,空闲则判断按钮状态并发送停车指令与号牌到PLC,否则返回失败信息
- {
- try
- {
- resultCode = TerminalSimul.ParkTermOper(id, msg.context);
- }
- catch { resultCode = 8; }
- }
- else { resultCode = 1; }
- Thread.Sleep(1000);
- switch (resultCode)
- {
- case 0:
- returnMsg.cmd = "PARKOK";
- Monitor.Monitor.SetNotification("车辆" + msg.context.Split('.')[2] + ",终端" + id + "正在进行停车", parkMonitor.model.TextColor.Info); break;
- case 1:
- returnMsg.cmd = "PARKFAILED";
- Monitor.Monitor.SetNotification("未识别到车辆" + msg.context.Split('.')[2] + ",终端" + id + "停放位置,请确认车辆已入场", parkMonitor.model.TextColor.Warning); break;
- case 2:
- returnMsg.cmd = "PARKFAILED";
- Monitor.Monitor.SetNotification("车辆" + msg.context.Split('.')[2] + ",终端" + id + "生成凭证号失败", parkMonitor.model.TextColor.Warning); break;
- case 3:
- returnMsg.cmd = "PARKFAILED";
- Monitor.Monitor.SetNotification("车辆" + msg.context.Split('.')[2] + ",终端" + id + "凭证转换异常", parkMonitor.model.TextColor.Warning); break;
- case 4:
- returnMsg.cmd = "PARKFAILED";
- Monitor.Monitor.SetNotification("车辆" + msg.context.Split('.')[2] + ",终端" + id + "停车码解析异常", parkMonitor.model.TextColor.Warning); break;
- case 5:
- returnMsg.cmd = "PARKFAILED";
- Monitor.Monitor.SetNotification("车辆" + msg.context.Split('.')[2] + ",终端" + id + "地感异常,当前位置无地感", parkMonitor.model.TextColor.Warning); break;
- case 6:
- returnMsg.cmd = "PARKFAILED";
- Monitor.Monitor.SetNotification("车辆" + msg.context.Split('.')[2] + ",终端" + id + "状态异常,非停车终端", parkMonitor.model.TextColor.Warning); break;
- case 7:
- returnMsg.cmd = "PARKFAILED";
- Monitor.Monitor.SetNotification("车辆" + msg.context.Split('.')[2] + ",终端" + id + "状态异常,已有停车指令在处理中", parkMonitor.model.TextColor.Warning); break;
- case 8:
- returnMsg.cmd = "PARKFAILED";
- Monitor.Monitor.SetNotification("车辆" + msg.context.Split('.')[2] + ",终端" + id + "其他异常", parkMonitor.model.TextColor.Warning);
- Log.WriteLog(LogType.process, LogFile.ERROR, "凭证号" + msg.context + "出现未知异常,无法停车"); break;
- case 9:
- returnMsg.cmd = "PARKFAILED";
- Monitor.Monitor.SetNotification("车辆" + msg.context.Split('.')[2] + ",终端" + id + "无空闲车位", parkMonitor.model.TextColor.Warning);break;
- }
- returnMsg.userID = msg.userID;
- returnMsg.garageID = Monitor.Monitor.garageID;
- returnMsg.context = msg.context;
- comm.SendMessage(returnMsg);
- }
- break;
- //取车
- case "FETCH":
- lock (fetchLock)
- {
- returnMsg = new MessageUTF8();
- int resultCode = TerminalSimul.FetchTermOper(msg.context);
- Thread.Sleep(1500);
- switch (resultCode)
- {
- case 0:
- returnMsg.cmd = "FETCHOK";
- Monitor.Monitor.SetNotification("凭证号" + msg.context + "正在取车", parkMonitor.model.TextColor.Info); break;
- case 1:
- returnMsg.cmd = "FETCHFAILED";
- Monitor.Monitor.SetNotification("凭证号" + msg.context + "地感异常,有地感时无法取车", parkMonitor.model.TextColor.Warning); break;
- case 2:
- returnMsg.cmd = "FETCHFAILED";
- Monitor.Monitor.SetNotification("凭证号" + msg.context + "终端状态异常,当前非取车终端", parkMonitor.model.TextColor.Warning); break;
- case 3:
- returnMsg.cmd = "FETCHFAILED";
- Monitor.Monitor.SetNotification("凭证号" + msg.context + "指令占用异常,已有取车指令在处理中", parkMonitor.model.TextColor.Warning); break;
- case 4:
- returnMsg.cmd = "FETCHFAILED";
- Monitor.Monitor.SetNotification("凭证号" + msg.context + "凭证解析异常,无法解析该凭证号", parkMonitor.model.TextColor.Warning); break;
- case 5:
- returnMsg.cmd = "FETCHFAILED";
- Monitor.Monitor.SetNotification("凭证号" + msg.context + "其他异常", parkMonitor.model.TextColor.Warning);
- Log.WriteLog(LogType.process, LogFile.ERROR, "凭证号" + msg.context + "出现未知异常,无法取车"); break;
- }
- returnMsg.userID = msg.userID;
- returnMsg.garageID = Monitor.Monitor.garageID;
- returnMsg.context = msg.context;
- comm.SendMessage(returnMsg);
- }
- break;
- //连接断开消息
- case "DISCONNECT":
- Monitor.Monitor.SetNotification("收到连接断开提示消息", parkMonitor.model.TextColor.Warning);
- break;
- //更新广告
- case "ADVERT":
- string adAlert = "";
- bool result = Monitor.Monitor.advertMgr.UpdateAdvert(out adAlert);
- if (!result)
- {
- Monitor.Monitor.SetNotification("广告更新失败,请尝试手动更新", parkMonitor.model.TextColor.Warning);
- }
- else
- {
- Monitor.Monitor.SetNotification("广告更新成功\n" + adAlert, parkMonitor.model.TextColor.Log);
- }
- break;
- case "RESPONSE":
- if (msg.context == "REGSUCCESS")
- {
- Console.WriteLine("收到web注册指令");
- }
- else if (msg.context == "HEARTSUCCESS")
- {
- Console.WriteLine("收到web心跳指令");
- }
- break;
- default:
- Monitor.Monitor.SetNotification("接收到无法识别的指令", parkMonitor.model.TextColor.Warning);
- break;
- }
- }
- catch (Exception ex) { Console.WriteLine("收消息," + ex.Message + "\n" + ex.StackTrace); }
- }
- private void SendBookCmd(bool parking, int state)
- {
- int countdown = 5;
- while (countdown-- > 0)
- {
- if (Monitor.Monitor.mainBlockInfo.bookParkCmd != 0 && countdown > 1)
- {
- Thread.Sleep(300);
- if (countdown == 2)
- {
- Monitor.Monitor.SetNotification("未能获取预约指令位0状态,尝试手动清除", parkMonitor.model.TextColor.Warning);
- MainBlockStru mbs = new MainBlockStru
- {
- centralHearbeat = -1,
- bookParkCmd = parking ? (short)0 : (short)-1,
- bookFetchCmd = !parking ? (short)0 : (short)-1,
- processCompleted = (short)-1,
- licenseReceived = -1
- };
- Monitor.Monitor.PLC.WriteToPLC(mbs, PLCDataType.central);
- Thread.Sleep(500);
- }
- continue;
- }
- MainBlockStru mb = new MainBlockStru
- {
- centralHearbeat = -1,
- bookParkCmd = parking ? (short)state : (short)-1,
- bookFetchCmd = !parking ? (short)state : (short)-1,
- processCompleted = (short)-1,
- licenseReceived = -1
- };
- Monitor.Monitor.PLC.WriteToPLC(mb, PLCDataType.central);
- Monitor.Monitor.SetNotification(mb.bookParkCmd + "," + mb.bookFetchCmd + "; 预约停车指令写入PLC", parkMonitor.model.TextColor.Log);
- Log.WriteLog(LogType.process, LogFile.INFO, mb.bookParkCmd + "," + mb.bookFetchCmd + "预约停车指令写入PLC");
- break;
- }
- }
- /// <summary>
- /// 根据时间段处理所有准备预约及已预约指令
- /// </summary>
- private void ReserveMsgHandling()
- {
- while (!isClosing)
- {
- //处理准备预约指令队列
- lock (waitToReserveLock)
- {
- for (int i = 0; i < waitToReserveQueue.Count; i++)
- {
- try
- {
- MessageUTF8 msg = waitToReserveQueue.Dequeue();
- DateTime startTime = DateTime.Parse(msg.bookTime);
- TimeSpan ts = DateTime.Now - startTime;
- //达到预约启动时间,放入已预约队列
- Console.WriteLine("当前时间差:" + ts.TotalMinutes + ",指令类型:" + msg.cmd);
- if (ts.TotalMinutes >= 0)
- {
- //如果是预约停车,通知PLC减少一个可预约车位数
- if (msg.cmd == "RESERVE")
- {
- //本地车位更新,2->3
- for (int j = 0; j < Monitor.Monitor.parkingSpaceInfo.Count; j++)
- {
- if (Monitor.Monitor.parkingSpaceInfo[j].spaceStatus == 2)
- {
- ParkingSpaceStru ps = Monitor.Monitor.parkingSpaceInfo[j];
- ps.spaceStatus = 3;
- Monitor.Monitor.parkingSpaceInfo[j] = ps;
- break;
- }
- }
- SendBookCmd(true, 1);
- Monitor.Monitor.SetNotification("通知PLC减少可预约车位", parkMonitor.model.TextColor.Log);
- }
- reservedQueue.Enqueue(msg);
- }
- //还未达到启动时间
- else
- {
- waitToReserveQueue.Enqueue(msg);
- }
- }
- catch { }
- }
- }
- lock (reservedLock)
- {
- for (int i = 0; i < reservedQueue.Count; i++)
- {
- try
- {
- MessageUTF8 msg = reservedQueue.Dequeue();
- DateTime startTime = DateTime.Parse(msg.bookTime);
- TimeSpan ts = DateTime.Now - startTime;
- //预约超时
- if (ts.TotalMinutes > msg.bookLength * 60)
- {
- Monitor.Monitor.SetNotification(msg.context + " 预约已超时", parkMonitor.model.TextColor.Warning);
- Log.WriteLog(LogType.process, LogFile.INFO, msg.context + " 预约已超时");
- //本地车位更新,3->2
- for (int j = 0; j < Monitor.Monitor.parkingSpaceInfo.Count; j++)
- {
- if (Monitor.Monitor.parkingSpaceInfo[j].spaceStatus == 3)
- {
- ParkingSpaceStru ps = Monitor.Monitor.parkingSpaceInfo[j];
- ps.spaceStatus = 2;
- Monitor.Monitor.parkingSpaceInfo[j] = ps;
- break;
- }
- }
- //通知PLC将可预约车位数恢复一个
- SendBookCmd(true, 2);
- //恢复车辆状态
- UpdateVehicleState(true, 0, 0, msg.context);
- UpdateVehicleState(false, 0, 0, msg.context);
- }
- else
- {
- reservedQueue.Enqueue(msg);
- }
- }
- catch { }
- }
- }
- Thread.Sleep(5000);
- }
- }
- /// <summary>
- /// 启动消息接收,启动超时指令处理
- /// </summary>
- /// <param name="port"></param>
- /// <returns></returns>
- public bool Start(int port)
- {
- isClosing = false;
- connecting = false;
- waitToReserveQueue = new Queue<MessageUTF8>();
- reservedQueue = new Queue<MessageUTF8>();
- //MessageUTF8 message = new MessageUTF8();
- //message.context = "sending message test";
- //message.cmd = "S";
- //message.parkingRecordsID = 1;
- //持续进行连接尝试
- Task.Factory.StartNew(() =>
- {
- //初始化后与web持续连接
- try
- {
- Connections.Connection();
- connected = true;
- while (!isClosing)
- {
- if (Connections.isAlive())
- {
- comm = new Communication();
- break;
- }
- else
- {
- Connections.close();
- Connections.Connection();
- }
- Thread.Sleep(1000);
- }
- }
- catch (Exception)
- {
- connected = false;
- Console.WriteLine("初始web服务连接异常");
- }
- Connect();
- });
- //持续接收消息
- receiveMsg = new Thread(() =>
- {
- while (!isClosing)
- {
- try
- {
- if (connected && comm != null)
- {
- //byte[] bytes = new byte[256];
- MessageUTF8 msg = ((MessageUTF8)comm.ReceiveMessage());
- //string str = "";
- //str = Encoding.Default.GetString(bytes);
- //Console.WriteLine(str);
- if (msg != null)
- {
- MsgHandling(msg);
- }
- //Monitor.Monitor.SetNotification(msg.context);
- }
- Thread.Sleep(200);
- }
- catch { Console.WriteLine("线程已中断"); }
- }
- });
- receiveMsg.Start();
- //根据所处时间段处理预约指令
- Task.Factory.StartNew(() =>
- {
- ReserveMsgHandling();
- });
- return true;
- }
- /// <summary>
- /// 停止消息接收模块
- /// </summary>
- public void Stop()
- {
- isClosing = true;
- try
- {
- Connections.close();
- }
- catch { }
- //throw new NotImplementedException();
- }
- /// <summary>
- /// 预约车辆入场检测
- /// </summary>
- /// <param name="license"></param>
- /// <returns></returns>
- public bool ReservedCarCheck(string license)
- {
- //对提前入场车辆,将预约指令丢出
- lock (waitToReserveLock)
- {
- for (int i = 0; i < waitToReserveQueue.Count; i++)
- {
- MessageUTF8 msg = waitToReserveQueue.Dequeue();
- if (msg.context != license)
- {
- waitToReserveQueue.Enqueue(msg);
- }
- }
- }
- //已进入预约状态车辆入场,审核确认后指令丢出
- lock (reservedLock)
- {
- for (int i = 0; i < reservedQueue.Count; i++)
- {
- MessageUTF8 msg = reservedQueue.Dequeue();
- if (msg.context == license)
- {
- return true;
- }
- else
- {
- reservedQueue.Enqueue(msg);
- }
- }
- }
- return false;
- }
- /// <summary>
- /// 主动连接web服务器
- /// </summary>
- public void Connect()
- {
- //持续判断连接状态并重连
- if (!connecting)
- {
- lock (connectLock)
- {
- connecting = true;
- int count = 3;
- while (!isClosing)
- {
- //if (receiveMsg != null)
- // Console.WriteLine(Connections.isAlive() + ", " + receiveMsg.ThreadState.ToString());
- Console.WriteLine(Connections.isAlive() + "," + count + "," + connected);
- if (Connections.isAlive())
- {
- if (!connected)
- {
- comm = new Communication();
- Monitor.Monitor.SetNotification("web已连接上", parkMonitor.model.TextColor.Info);
- }
- count = 3;
- connected = true;
- if (receiveMsg != null && receiveMsg.ThreadState == ThreadState.Aborted)
- {
- try
- {
- receiveMsg.Start();
- }
- catch (Exception ex) { Console.WriteLine(ex.Message); }
- }
- }
- else
- {
- if (count == 3 && connected)
- Monitor.Monitor.SetNotification("web连接已断开", parkMonitor.model.TextColor.Warning);
- else if (count == 0)
- {
- try
- {
- Connections.close();
- //comm = null;
- }
- catch (Exception ex) { Console.WriteLine("网络异常,停止尝试"); }
- break;
- }
- else if (connected)
- {
- try { Connections.close(); }
- catch (Exception) { Console.WriteLine("服务没有开启,请检查服务器"); }
- }
- connected = false;
- count--;
- try
- {
- if (receiveMsg != null && receiveMsg.ThreadState == ThreadState.WaitSleepJoin)
- {
- receiveMsg.Interrupt();
- }
- }
- catch (Exception ex)
- {
- Monitor.Monitor.SetNotification("连接断开,终止消息接收线程", parkMonitor.model.TextColor.Log);
- }
- Console.WriteLine(" 连接关闭,需要重新连接注册");
- try { Connections.Connection(); } catch { Console.WriteLine("尝试连接异常"); }
- }
- Thread.Sleep(1000);
- }
- Monitor.Monitor.SetNotification("重连web服务器超时,请检查网络并手动连接web服务器", parkMonitor.model.TextColor.Error);
- connecting = false;
- }
- }
- else
- {
- if (!connected)
- Monitor.Monitor.SetNotification("正在尝试连接,请勿重复点击", parkMonitor.model.TextColor.Warning);
- else
- Monitor.Monitor.SetNotification("已连接,请勿重复点击", parkMonitor.model.TextColor.Info);
- }
- }
- /// <summary>
- /// 获取连接状态
- /// </summary>
- /// <returns></returns>
- public bool GetConnectStatus()
- {
- return Connections.isAlive();
- }
- }
- }
|