WebServer.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. using centralController.model;
  2. using db;
  3. using nettyCommunication;
  4. using parkMonitor.LOG;
  5. using PLCS7;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace centralController.WebServer
  13. {
  14. class MyWebServer : IWebServer
  15. {
  16. private Queue<MessageUTF8> waitToReserveQueue = null;
  17. private Queue<MessageUTF8> reservedQueue = null;
  18. private object waitToReserveLock = new object();
  19. private object reservedLock = new object();
  20. private Communication comm = null;
  21. private Thread receiveMsg = null;
  22. private bool isClosing { get; set; }
  23. /// <summary>
  24. /// 是否正在调用函数进行连接
  25. /// </summary>
  26. private bool connecting { get; set; }
  27. /// <summary>
  28. /// 连接状态
  29. /// </summary>
  30. private bool connected { get; set; }
  31. public void BookFetchRecord()
  32. {
  33. throw new NotImplementedException();
  34. }
  35. public void BookParkRecord()
  36. {
  37. throw new NotImplementedException();
  38. }
  39. /// <summary>
  40. /// 更新预约车车辆状态
  41. /// </summary>
  42. /// <param name="localDB"></param>
  43. /// <param name="state"></param>
  44. /// <param name="orderRecordsID"></param>
  45. /// <param name="license"></param>
  46. /// <returns></returns>
  47. private bool UpdateVehicleState(bool localDB, int state, int orderRecordsID, string license)
  48. {
  49. string vehicleUpdateSql = "";
  50. string vehicleInsertSql = "";
  51. if (orderRecordsID > 0)
  52. {
  53. if (state >= 0)
  54. {
  55. vehicleUpdateSql = "update vehicle set vehiclepParkState = " + state + " ,orderRecordsID = " + orderRecordsID + " where numberPlate = '" + license + "';";
  56. vehicleInsertSql = "insert into vehicle (numberPlate,vehiclepParkState,orderRecordsID) values " +
  57. "('" + license + "'," + state + "," + orderRecordsID + ");";
  58. }
  59. else
  60. {
  61. vehicleUpdateSql = "update vehicle set orderRecordsID = " + orderRecordsID + " where numberPlate = '" + license + "';";
  62. vehicleInsertSql = "insert into vehicle (numberPlate,orderRecordsID) values " +
  63. "('" + license + "'," + orderRecordsID + ");";
  64. }
  65. }
  66. else
  67. {
  68. if (state >= 0)
  69. {
  70. vehicleUpdateSql = "update vehicle set vehiclepParkState = " + state + " where numberPlate = '" + license + "';";
  71. vehicleInsertSql = "insert into vehicle (numberPlate,vehiclepParkState) values " +
  72. "('" + license + "'," + state + ");";
  73. }
  74. else
  75. {
  76. return false;
  77. }
  78. }
  79. List<string> vehicleUpdateList = new List<string>();
  80. List<string> vehicleInsertList = new List<string>();
  81. vehicleUpdateList.Add(vehicleUpdateSql);
  82. vehicleInsertList.Add(vehicleInsertSql);
  83. DBOperation dbHandle = null;
  84. if (localDB)
  85. dbHandle = Monitor.Monitor.localDBOper;
  86. else
  87. dbHandle = Monitor.Monitor.remoteDBOper;
  88. if (!dbHandle.UpdateTransaction(vehicleUpdateList))
  89. {
  90. int id =0;
  91. if (!dbHandle.Insert(vehicleInsertList, out id))
  92. return false;
  93. else
  94. return true;
  95. }
  96. else return true;
  97. }
  98. /// <summary>
  99. /// 插入预约记录
  100. /// </summary>
  101. /// <param name="localDB"></param>
  102. /// <param name="userID"></param>
  103. /// <param name="parking"></param>
  104. /// <param name="license"></param>
  105. /// <param name="orderTime"></param>
  106. /// <param name="orderLength"></param>
  107. /// <returns></returns>
  108. private bool InsertOrderRecord(bool localDB, string userID, bool parking, string license, string orderTime, int orderLength, out int id)
  109. {
  110. bool result = false;
  111. string orderRecordInsertSql;
  112. if (parking)
  113. {
  114. orderRecordInsertSql = "insert into orderrecords (userID,numberPlate,garageID, bookParkTime, bookHour, bookPrice,bookState) " +
  115. "VALUES (" + userID + ", '" + license + "', '" + Monitor.Monitor.garageID + "', '" + orderTime + "', " + orderLength + ",NULL, '0');";
  116. }
  117. else
  118. {
  119. orderRecordInsertSql = "insert into orderrecords (userID,numberPlate,garageID, bookFetchTime, bookHour, bookPrice,bookState) " +
  120. "VALUES (" + userID + ", '" + license + "', '" + Monitor.Monitor.garageID + "', '" + orderTime + "', " + orderLength + ",NULL, '2');";
  121. }
  122. List<string> orderList = new List<string>();
  123. orderList.Add(orderRecordInsertSql);
  124. if (localDB)
  125. result = Monitor.Monitor.localDBOper.Insert(orderList, out id);
  126. else
  127. result = Monitor.Monitor.remoteDBOper.Insert(orderList, out id);
  128. return result;
  129. }
  130. /// <summary>
  131. /// 找到当前预约记录ID,暂未使用
  132. /// </summary>
  133. /// <param name="localDB"></param>
  134. /// <param name="license"></param>
  135. /// <param name="latestIndex">最近第几条记录,1表示最新一条</param>
  136. /// <returns></returns>
  137. private int FindCurrentOrderRecordID(bool localDB, string license, int latestIndex = 1)
  138. {
  139. int currentID = 0;
  140. int count = 10;
  141. while (count-- > 0 && currentID == 0)
  142. {
  143. List<object[]> orderRecords = Monitor.Monitor.GetOrderRecords(localDB, license, DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"));
  144. if (orderRecords.Count != 0)
  145. {
  146. try
  147. {
  148. currentID = (int)(UInt32)orderRecords[latestIndex - 1][0];
  149. }
  150. catch { }
  151. }
  152. }
  153. return currentID;
  154. }
  155. /// <summary>
  156. /// 预约数据库操作
  157. /// </summary>
  158. /// <param name="localDB"></param>
  159. /// <param name="userID"></param>
  160. /// <param name="parking"></param>
  161. /// <param name="license"></param>
  162. /// <param name="orderTime"></param>
  163. /// <param name="orderLength"></param>
  164. /// <returns></returns>
  165. private bool ReserveDBOperation(bool localDB, string userID, bool parking, string license, string orderTime, int orderLength)
  166. {
  167. UpdateVehicleState(localDB, parking ? 4 : 5, 0, license);
  168. //预约记录插入db
  169. int currentID = 0;
  170. InsertOrderRecord(localDB, userID, parking, license, orderTime, orderLength, out currentID);
  171. ////查询预约记录id号
  172. //int currentID = FindCurrentOrderRecordID(localDB, license);
  173. if (currentID == 0) { /*反馈web,预约失败*/ return false; }
  174. //更新车辆状态
  175. UpdateVehicleState(localDB, parking ? 4 : 5, currentID, license);
  176. return true;
  177. }
  178. /// <summary>
  179. /// 检查预约指令是否可行
  180. /// </summary>
  181. /// <param name="msg"></param>
  182. /// <returns></returns>
  183. private bool ReservationValidate(MessageUTF8 msg)
  184. {
  185. int allBookableSpace, count;
  186. lock (waitToReserveLock)
  187. {
  188. //可预约车位总数
  189. allBookableSpace = Monitor.Monitor.ins.GetFreeSpaceCount(4);
  190. count = 0;
  191. DateTime start, end;
  192. try
  193. {
  194. start = DateTime.Parse(msg.bookTime);
  195. end = start.AddHours(msg.bookLength);
  196. //Console.WriteLine("--------"+msg.bookTime+","+msg.bookLength);
  197. }
  198. catch { Console.WriteLine("时间解析异常1"); return false; }
  199. Queue<MessageUTF8>.Enumerator enumer = waitToReserveQueue.GetEnumerator();
  200. while (enumer.MoveNext())
  201. {
  202. DateTime tempStart, tempEnd;
  203. try
  204. {
  205. tempStart = DateTime.Parse(enumer.Current.bookTime);
  206. tempEnd = tempStart.AddHours(enumer.Current.bookLength);
  207. //Console.WriteLine("#########" + enumer.Current.bookTime + "," + enumer.Current.bookLength);
  208. }
  209. catch { Console.WriteLine("时间解析异常2"); return false; }
  210. //Console.WriteLine("**********" + start.ToString() + "," + end.ToString() + "," + tempStart.ToString() + "," + tempEnd.ToString());
  211. //Console.WriteLine("**********" + ((tempStart - end).TotalMinutes > 0)+","+ ((start - tempEnd).TotalMinutes > 0));
  212. if (!((tempStart - end).TotalMinutes > 0 || (start - tempEnd).TotalMinutes > 0)) { count += 1; }
  213. }
  214. Queue<MessageUTF8>.Enumerator enumer2 = reservedQueue.GetEnumerator();
  215. while (enumer2.MoveNext())
  216. {
  217. DateTime tempStart, tempEnd;
  218. try
  219. {
  220. tempStart = DateTime.Parse(enumer2.Current.bookTime);
  221. tempEnd = tempStart.AddHours(enumer2.Current.bookLength);
  222. //Console.WriteLine("#########" + enumer.Current.bookTime + "," + enumer.Current.bookLength);
  223. }
  224. catch { Console.WriteLine("时间解析异常2"); return false; }
  225. //Console.WriteLine("**********" + start.ToString() + "," + end.ToString() + "," + tempStart.ToString() + "," + tempEnd.ToString());
  226. //Console.WriteLine("**********" + ((tempStart - end).TotalMinutes > 0)+","+ ((start - tempEnd).TotalMinutes > 0));
  227. if (!((tempStart - end).TotalMinutes > 0 || (start - tempEnd).TotalMinutes > 0)) { count += 1; }
  228. }
  229. }
  230. Console.WriteLine("----------" + msg.context + ":" + allBookableSpace + "," + count);
  231. if (allBookableSpace > count)
  232. return true;
  233. else
  234. return false;
  235. }
  236. /// <summary>
  237. /// 根据消息类型分别处理
  238. /// </summary>
  239. /// <param name="msg"></param>
  240. private void MsgHandling(MessageUTF8 msg)
  241. {
  242. try
  243. {
  244. switch (msg.cmd)
  245. {
  246. //预约停
  247. case "RESERVE":
  248. if (msg.sender != "" && msg.bookTime != "" && msg.bookLength != 0)
  249. {
  250. MessageUTF8 returnMsg = new MessageUTF8();
  251. if (!ReservationValidate(msg))
  252. {
  253. //回复预约失败给web
  254. returnMsg.cmd = "FAILED";
  255. returnMsg.garageID = Monitor.Monitor.garageID;
  256. returnMsg.context = msg.context;
  257. comm.SendMessage(returnMsg);
  258. Monitor.Monitor.SetNotification("车辆" + msg.context + "预约停车,已无可预约车位", parkMonitor.model.TextColor.Warning);
  259. }
  260. else
  261. {
  262. bool DBOperResult = true;
  263. lock (waitToReserveLock)
  264. {
  265. waitToReserveQueue.Enqueue(msg);
  266. }
  267. //预约记录与车辆状态写入数据库
  268. DBOperResult = DBOperResult && ReserveDBOperation(true, msg.sender, true, msg.context, msg.bookTime, msg.bookLength);
  269. DBOperResult = DBOperResult && ReserveDBOperation(false, msg.sender, true, msg.context, msg.bookTime, msg.bookLength);
  270. //回复成功给web
  271. if (!DBOperResult)
  272. Log.WriteLog(LogType.process, LogFile.WARNING, msg.context+"从"+msg.bookTime+"开始预约"+msg.bookLength+"小时,预约指令数据库操作未获得记录ID");
  273. returnMsg.cmd = "OK";
  274. returnMsg.garageID = Monitor.Monitor.garageID;
  275. returnMsg.context = msg.context;
  276. comm.SendMessage(returnMsg);
  277. Monitor.Monitor.SetNotification("车辆" + msg.context + "预约停车,操作成功", parkMonitor.model.TextColor.Log);
  278. }
  279. }
  280. break;
  281. case "CANCELRESERVE":
  282. //IEqualityComparer<MessageUTF8> comparer ;
  283. //if(waitToReserveQueue.Contains(msg, )
  284. break;
  285. //预约取
  286. case "PREFETCH":
  287. break;
  288. //停车
  289. case "PARK":
  290. //根据号牌寻找对应号牌机编号,找不到则返回失败信息
  291. //判断号牌机编号对应PLC数据块是否空闲,空闲则判断按钮状态并发送停车指令到PLC,否则返回失败信息
  292. break;
  293. //取车
  294. case "FETCH":
  295. break;
  296. //连接断开消息
  297. case "DISCONNECT":
  298. Monitor.Monitor.SetNotification("收到连接断开提示消息", parkMonitor.model.TextColor.Warning);
  299. break;
  300. //更新广告
  301. case "ADVERT":
  302. string adAlert = "";
  303. bool result = Monitor.Monitor.advertMgr.UpdateAdvert(out adAlert);
  304. if (!result)
  305. {
  306. Monitor.Monitor.SetNotification("广告更新失败,请尝试手动更新", parkMonitor.model.TextColor.Warning);
  307. }
  308. else
  309. {
  310. Monitor.Monitor.SetNotification("广告更新成功\n" + adAlert, parkMonitor.model.TextColor.Log);
  311. }
  312. break;
  313. case "RESPONSE":
  314. if (msg.context == "REGSUCCESS")
  315. {
  316. Console.WriteLine("收到web注册指令");
  317. }
  318. else if (msg.context == "HEARTSUCCESS")
  319. {
  320. Console.WriteLine("收到web心跳指令");
  321. }
  322. break;
  323. default:
  324. Monitor.Monitor.SetNotification("接收到无法识别的指令", parkMonitor.model.TextColor.Warning);
  325. break;
  326. }
  327. }
  328. catch (Exception ex) { Console.WriteLine("收消息," + ex.Message + "\n" + ex.StackTrace); }
  329. }
  330. private void SendBookCmd(bool parking, int state)
  331. {
  332. int countdown = 5;
  333. while (countdown-- > 0)
  334. {
  335. if (Monitor.Monitor.mainBlockInfo.bookParkCmd != 0 && countdown>1)
  336. {
  337. Thread.Sleep(300);
  338. if (countdown == 2)
  339. {
  340. Monitor.Monitor.SetNotification("未能获取预约指令位0状态,尝试手动清除", parkMonitor.model.TextColor.Warning);
  341. MainBlockStru mbs = new MainBlockStru
  342. {
  343. centralHearbeat = -1,
  344. bookParkCmd = parking ? (short)0 : (short)-1,
  345. bookFetchCmd = !parking ? (short)0 : (short)-1,
  346. licenseReceived = -1
  347. };
  348. Monitor.Monitor.PLC.WriteToPLC(mbs, PLCDataType.central);
  349. Thread.Sleep(500);
  350. }
  351. continue;
  352. }
  353. MainBlockStru mb = new MainBlockStru
  354. {
  355. centralHearbeat = -1,
  356. bookParkCmd = parking ? (short)state : (short)-1,
  357. bookFetchCmd = !parking ? (short)state : (short)-1,
  358. licenseReceived = -1
  359. };
  360. Monitor.Monitor.PLC.WriteToPLC(mb, PLCDataType.central);
  361. Monitor.Monitor.SetNotification(mb.bookParkCmd + "," + mb.bookFetchCmd + "; 预约停车指令写入PLC", parkMonitor.model.TextColor.Log);
  362. Log.WriteLog(LogType.process, LogFile.INFO, mb.bookParkCmd + "," + mb.bookFetchCmd + "预约停车指令写入PLC");
  363. break;
  364. }
  365. }
  366. /// <summary>
  367. /// 根据时间段处理所有准备预约及已预约指令
  368. /// </summary>
  369. private void ReserveMsgHandling()
  370. {
  371. while (!isClosing)
  372. {
  373. //处理准备预约指令队列
  374. lock (waitToReserveLock)
  375. {
  376. for (int i = 0; i < waitToReserveQueue.Count; i++)
  377. {
  378. try
  379. {
  380. MessageUTF8 msg = waitToReserveQueue.Dequeue();
  381. DateTime startTime = DateTime.Parse(msg.bookTime);
  382. TimeSpan ts = DateTime.Now - startTime;
  383. //达到预约启动时间,放入已预约队列
  384. Console.WriteLine("当前时间差:" + ts.TotalMinutes + ",指令类型:" + msg.cmd);
  385. if (ts.TotalMinutes >= 0)
  386. {
  387. //如果是预约停车,通知PLC减少一个可预约车位数
  388. if (msg.cmd == "RESERVE")
  389. {
  390. SendBookCmd(true, 1);
  391. Monitor.Monitor.SetNotification("通知PLC减少可预约车位", parkMonitor.model.TextColor.Log);
  392. }
  393. reservedQueue.Enqueue(msg);
  394. }
  395. //还未达到启动时间
  396. else
  397. {
  398. waitToReserveQueue.Enqueue(msg);
  399. }
  400. }
  401. catch { }
  402. }
  403. }
  404. lock (reservedLock)
  405. {
  406. for (int i = 0; i < reservedQueue.Count; i++)
  407. {
  408. try
  409. {
  410. MessageUTF8 msg = reservedQueue.Dequeue();
  411. DateTime startTime = DateTime.Parse(msg.bookTime);
  412. TimeSpan ts = DateTime.Now - startTime;
  413. //预约超时
  414. if (ts.TotalMinutes > msg.bookLength * 60)
  415. {
  416. Monitor.Monitor.SetNotification(msg.context + " 预约已超时", parkMonitor.model.TextColor.Warning);
  417. Log.WriteLog(LogType.process, LogFile.INFO, msg.context + " 预约已超时");
  418. //通知PLC将可预约车位数恢复一个
  419. SendBookCmd(true, 2);
  420. //恢复车辆状态
  421. UpdateVehicleState(true, 0, 0, msg.context);
  422. UpdateVehicleState(false, 0, 0, msg.context);
  423. }
  424. else
  425. {
  426. reservedQueue.Enqueue(msg);
  427. }
  428. }
  429. catch { }
  430. }
  431. }
  432. Thread.Sleep(5000);
  433. }
  434. }
  435. /// <summary>
  436. /// 启动消息接收,启动超时指令处理
  437. /// </summary>
  438. /// <param name="port"></param>
  439. /// <returns></returns>
  440. public bool Start(int port)
  441. {
  442. isClosing = false;
  443. connecting = false;
  444. waitToReserveQueue = new Queue<MessageUTF8>();
  445. reservedQueue = new Queue<MessageUTF8>();
  446. //MessageUTF8 message = new MessageUTF8();
  447. //message.context = "sending message test";
  448. //message.cmd = "S";
  449. //message.parkingRecordsID = 1;
  450. //持续进行连接尝试
  451. Task.Factory.StartNew(() =>
  452. {
  453. //初始化后与web持续连接
  454. while (!isClosing)
  455. {
  456. try
  457. {
  458. Connections.close();
  459. Connections.Connection();
  460. connected = true;
  461. comm = new Communication();
  462. break;
  463. }
  464. catch (Exception)
  465. {
  466. connected = false;
  467. Console.WriteLine("服务没有开启,请检查服务器");
  468. }
  469. Thread.Sleep(2000);
  470. }
  471. Connect();
  472. });
  473. //持续接收消息
  474. receiveMsg = new Thread(() =>
  475. {
  476. while (!isClosing)
  477. {
  478. try
  479. {
  480. if (connected && comm != null)
  481. {
  482. byte[] bytes = new byte[256];
  483. MessageUTF8 msg = ((MessageUTF8)comm.ReceiveMessage(out bytes));
  484. //string str = "";
  485. //str = Encoding.Default.GetString(bytes);
  486. //Console.WriteLine(str);
  487. if (msg != null)
  488. {
  489. MsgHandling(msg);
  490. }
  491. //Monitor.Monitor.SetNotification(msg.context);
  492. }
  493. }
  494. catch { Console.WriteLine("线程已中断"); }
  495. }
  496. });
  497. receiveMsg.Start();
  498. //根据所处时间段处理预约指令
  499. Task.Factory.StartNew(() =>
  500. {
  501. ReserveMsgHandling();
  502. });
  503. return true;
  504. }
  505. /// <summary>
  506. /// 停止消息接收模块
  507. /// </summary>
  508. public void Stop()
  509. {
  510. isClosing = true;
  511. Connections.close();
  512. //throw new NotImplementedException();
  513. }
  514. /// <summary>
  515. /// 预约车辆入场检测
  516. /// </summary>
  517. /// <param name="license"></param>
  518. /// <returns></returns>
  519. public bool ReservedCarCheck(string license)
  520. {
  521. //对提前入场车辆,将预约指令丢出
  522. lock (waitToReserveLock)
  523. {
  524. for (int i = 0; i < waitToReserveQueue.Count; i++)
  525. {
  526. MessageUTF8 msg = waitToReserveQueue.Dequeue();
  527. if (msg.context != license)
  528. {
  529. waitToReserveQueue.Enqueue(msg);
  530. }
  531. }
  532. }
  533. //已进入预约状态车辆入场,审核确认后指令丢出
  534. lock (reservedLock)
  535. {
  536. for (int i = 0; i < reservedQueue.Count; i++)
  537. {
  538. MessageUTF8 msg = reservedQueue.Dequeue();
  539. if (msg.context == license)
  540. {
  541. return true;
  542. }
  543. else
  544. {
  545. reservedQueue.Enqueue(msg);
  546. }
  547. }
  548. }
  549. return false;
  550. }
  551. /// <summary>
  552. /// 主动连接web服务器
  553. /// </summary>
  554. public void Connect()
  555. {
  556. //持续判断连接状态并重连
  557. if (!connecting)
  558. {
  559. connecting = true;
  560. int count = 12;
  561. while (!isClosing)
  562. {
  563. //if (receiveMsg != null)
  564. // Console.WriteLine(Connections.isAlive() + ", " + receiveMsg.ThreadState.ToString());
  565. if (Connections.isAlive())
  566. {
  567. if (!connected)
  568. {
  569. comm = new Communication();
  570. Monitor.Monitor.SetNotification("web已连接上", parkMonitor.model.TextColor.Info);
  571. }
  572. connected = true;
  573. if (receiveMsg != null && receiveMsg.ThreadState == ThreadState.Aborted)
  574. {
  575. try
  576. {
  577. receiveMsg.Start();
  578. }
  579. catch (Exception ex) { Console.WriteLine(ex.Message); }
  580. }
  581. }
  582. else
  583. {
  584. if (count == 12 && connected)
  585. Monitor.Monitor.SetNotification("web连接已断开", parkMonitor.model.TextColor.Warning);
  586. else if (count == 0)
  587. break;
  588. connected = false;
  589. count--;
  590. try
  591. {
  592. if (receiveMsg != null && receiveMsg.ThreadState == ThreadState.WaitSleepJoin)
  593. {
  594. receiveMsg.Interrupt();
  595. }
  596. }
  597. catch (Exception ex)
  598. {
  599. Monitor.Monitor.SetNotification("连接断开,终止消息接收线程", parkMonitor.model.TextColor.Log);
  600. }
  601. Console.WriteLine(" 连接关闭,需要重新连接注册");
  602. try
  603. {
  604. Connections.close();
  605. Connections.Connection();
  606. }
  607. catch (Exception)
  608. {
  609. Console.WriteLine("服务没有开启,请检查服务器");
  610. }
  611. }
  612. Thread.Sleep(1000);
  613. }
  614. Monitor.Monitor.SetNotification("重连web服务器超时,请检查网络并手动连接web服务器", parkMonitor.model.TextColor.Error);
  615. connecting = false;
  616. }
  617. else
  618. {
  619. Monitor.Monitor.SetNotification("正在尝试连接,请勿重复点击", parkMonitor.model.TextColor.Warning);
  620. }
  621. }
  622. /// <summary>
  623. /// 获取连接状态
  624. /// </summary>
  625. /// <returns></returns>
  626. public bool GetConnectStatus()
  627. {
  628. return connected;
  629. }
  630. }
  631. }