WebServer.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. processCompleted = (short)-1,
  347. licenseReceived = -1
  348. };
  349. Monitor.Monitor.PLC.WriteToPLC(mbs, PLCDataType.central);
  350. Thread.Sleep(500);
  351. }
  352. continue;
  353. }
  354. MainBlockStru mb = new MainBlockStru
  355. {
  356. centralHearbeat = -1,
  357. bookParkCmd = parking ? (short)state : (short)-1,
  358. bookFetchCmd = !parking ? (short)state : (short)-1,
  359. processCompleted = (short)-1,
  360. licenseReceived = -1
  361. };
  362. Monitor.Monitor.PLC.WriteToPLC(mb, PLCDataType.central);
  363. Monitor.Monitor.SetNotification(mb.bookParkCmd + "," + mb.bookFetchCmd + "; 预约停车指令写入PLC", parkMonitor.model.TextColor.Log);
  364. Log.WriteLog(LogType.process, LogFile.INFO, mb.bookParkCmd + "," + mb.bookFetchCmd + "预约停车指令写入PLC");
  365. break;
  366. }
  367. }
  368. /// <summary>
  369. /// 根据时间段处理所有准备预约及已预约指令
  370. /// </summary>
  371. private void ReserveMsgHandling()
  372. {
  373. while (!isClosing)
  374. {
  375. //处理准备预约指令队列
  376. lock (waitToReserveLock)
  377. {
  378. for (int i = 0; i < waitToReserveQueue.Count; i++)
  379. {
  380. try
  381. {
  382. MessageUTF8 msg = waitToReserveQueue.Dequeue();
  383. DateTime startTime = DateTime.Parse(msg.bookTime);
  384. TimeSpan ts = DateTime.Now - startTime;
  385. //达到预约启动时间,放入已预约队列
  386. Console.WriteLine("当前时间差:" + ts.TotalMinutes + ",指令类型:" + msg.cmd);
  387. if (ts.TotalMinutes >= 0)
  388. {
  389. //如果是预约停车,通知PLC减少一个可预约车位数
  390. if (msg.cmd == "RESERVE")
  391. {
  392. //本地车位更新,2->3
  393. for (int j = 0; j < Monitor.Monitor.parkingSpaceInfo.Count; j++)
  394. {
  395. if(Monitor.Monitor.parkingSpaceInfo[j].spaceStatus == 2)
  396. {
  397. ParkingSpaceStru ps = Monitor.Monitor.parkingSpaceInfo[j];
  398. ps.spaceStatus = 3;
  399. Monitor.Monitor.parkingSpaceInfo[j] = ps;
  400. break;
  401. }
  402. }
  403. SendBookCmd(true, 1);
  404. Monitor.Monitor.SetNotification("通知PLC减少可预约车位", parkMonitor.model.TextColor.Log);
  405. }
  406. reservedQueue.Enqueue(msg);
  407. }
  408. //还未达到启动时间
  409. else
  410. {
  411. waitToReserveQueue.Enqueue(msg);
  412. }
  413. }
  414. catch { }
  415. }
  416. }
  417. lock (reservedLock)
  418. {
  419. for (int i = 0; i < reservedQueue.Count; i++)
  420. {
  421. try
  422. {
  423. MessageUTF8 msg = reservedQueue.Dequeue();
  424. DateTime startTime = DateTime.Parse(msg.bookTime);
  425. TimeSpan ts = DateTime.Now - startTime;
  426. //预约超时
  427. if (ts.TotalMinutes > msg.bookLength * 60)
  428. {
  429. Monitor.Monitor.SetNotification(msg.context + " 预约已超时", parkMonitor.model.TextColor.Warning);
  430. Log.WriteLog(LogType.process, LogFile.INFO, msg.context + " 预约已超时");
  431. //本地车位更新,3->2
  432. for (int j = 0; j < Monitor.Monitor.parkingSpaceInfo.Count; j++)
  433. {
  434. if (Monitor.Monitor.parkingSpaceInfo[j].spaceStatus == 3)
  435. {
  436. ParkingSpaceStru ps = Monitor.Monitor.parkingSpaceInfo[j];
  437. ps.spaceStatus = 2;
  438. Monitor.Monitor.parkingSpaceInfo[j] = ps;
  439. break;
  440. }
  441. }
  442. //通知PLC将可预约车位数恢复一个
  443. SendBookCmd(true, 2);
  444. //恢复车辆状态
  445. UpdateVehicleState(true, 0, 0, msg.context);
  446. UpdateVehicleState(false, 0, 0, msg.context);
  447. }
  448. else
  449. {
  450. reservedQueue.Enqueue(msg);
  451. }
  452. }
  453. catch { }
  454. }
  455. }
  456. Thread.Sleep(5000);
  457. }
  458. }
  459. /// <summary>
  460. /// 启动消息接收,启动超时指令处理
  461. /// </summary>
  462. /// <param name="port"></param>
  463. /// <returns></returns>
  464. public bool Start(int port)
  465. {
  466. isClosing = false;
  467. connecting = false;
  468. waitToReserveQueue = new Queue<MessageUTF8>();
  469. reservedQueue = new Queue<MessageUTF8>();
  470. //MessageUTF8 message = new MessageUTF8();
  471. //message.context = "sending message test";
  472. //message.cmd = "S";
  473. //message.parkingRecordsID = 1;
  474. //持续进行连接尝试
  475. Task.Factory.StartNew(() =>
  476. {
  477. //初始化后与web持续连接
  478. while (!isClosing)
  479. {
  480. try
  481. {
  482. Connections.close();
  483. Connections.Connection();
  484. connected = true;
  485. comm = new Communication();
  486. break;
  487. }
  488. catch (Exception)
  489. {
  490. connected = false;
  491. Console.WriteLine("服务没有开启,请检查服务器");
  492. }
  493. Thread.Sleep(2000);
  494. }
  495. Connect();
  496. });
  497. //持续接收消息
  498. receiveMsg = new Thread(() =>
  499. {
  500. while (!isClosing)
  501. {
  502. try
  503. {
  504. if (connected && comm != null)
  505. {
  506. byte[] bytes = new byte[256];
  507. MessageUTF8 msg = ((MessageUTF8)comm.ReceiveMessage(out bytes));
  508. //string str = "";
  509. //str = Encoding.Default.GetString(bytes);
  510. //Console.WriteLine(str);
  511. if (msg != null)
  512. {
  513. MsgHandling(msg);
  514. }
  515. //Monitor.Monitor.SetNotification(msg.context);
  516. }
  517. }
  518. catch { Console.WriteLine("线程已中断"); }
  519. }
  520. });
  521. receiveMsg.Start();
  522. //根据所处时间段处理预约指令
  523. Task.Factory.StartNew(() =>
  524. {
  525. ReserveMsgHandling();
  526. });
  527. return true;
  528. }
  529. /// <summary>
  530. /// 停止消息接收模块
  531. /// </summary>
  532. public void Stop()
  533. {
  534. isClosing = true;
  535. Connections.close();
  536. //throw new NotImplementedException();
  537. }
  538. /// <summary>
  539. /// 预约车辆入场检测
  540. /// </summary>
  541. /// <param name="license"></param>
  542. /// <returns></returns>
  543. public bool ReservedCarCheck(string license)
  544. {
  545. //对提前入场车辆,将预约指令丢出
  546. lock (waitToReserveLock)
  547. {
  548. for (int i = 0; i < waitToReserveQueue.Count; i++)
  549. {
  550. MessageUTF8 msg = waitToReserveQueue.Dequeue();
  551. if (msg.context != license)
  552. {
  553. waitToReserveQueue.Enqueue(msg);
  554. }
  555. }
  556. }
  557. //已进入预约状态车辆入场,审核确认后指令丢出
  558. lock (reservedLock)
  559. {
  560. for (int i = 0; i < reservedQueue.Count; i++)
  561. {
  562. MessageUTF8 msg = reservedQueue.Dequeue();
  563. if (msg.context == license)
  564. {
  565. return true;
  566. }
  567. else
  568. {
  569. reservedQueue.Enqueue(msg);
  570. }
  571. }
  572. }
  573. return false;
  574. }
  575. /// <summary>
  576. /// 主动连接web服务器
  577. /// </summary>
  578. public void Connect()
  579. {
  580. //持续判断连接状态并重连
  581. if (!connecting)
  582. {
  583. connecting = true;
  584. int count = 12;
  585. while (!isClosing)
  586. {
  587. //if (receiveMsg != null)
  588. // Console.WriteLine(Connections.isAlive() + ", " + receiveMsg.ThreadState.ToString());
  589. if (Connections.isAlive())
  590. {
  591. if (!connected)
  592. {
  593. comm = new Communication();
  594. Monitor.Monitor.SetNotification("web已连接上", parkMonitor.model.TextColor.Info);
  595. }
  596. connected = true;
  597. if (receiveMsg != null && receiveMsg.ThreadState == ThreadState.Aborted)
  598. {
  599. try
  600. {
  601. receiveMsg.Start();
  602. }
  603. catch (Exception ex) { Console.WriteLine(ex.Message); }
  604. }
  605. }
  606. else
  607. {
  608. if (count == 12 && connected)
  609. Monitor.Monitor.SetNotification("web连接已断开", parkMonitor.model.TextColor.Warning);
  610. else if (count == 0)
  611. break;
  612. connected = false;
  613. count--;
  614. try
  615. {
  616. if (receiveMsg != null && receiveMsg.ThreadState == ThreadState.WaitSleepJoin)
  617. {
  618. receiveMsg.Interrupt();
  619. }
  620. }
  621. catch (Exception ex)
  622. {
  623. Monitor.Monitor.SetNotification("连接断开,终止消息接收线程", parkMonitor.model.TextColor.Log);
  624. }
  625. Console.WriteLine(" 连接关闭,需要重新连接注册");
  626. try
  627. {
  628. Connections.close();
  629. Connections.Connection();
  630. }
  631. catch (Exception)
  632. {
  633. Console.WriteLine("服务没有开启,请检查服务器");
  634. }
  635. }
  636. Thread.Sleep(1000);
  637. }
  638. Monitor.Monitor.SetNotification("重连web服务器超时,请检查网络并手动连接web服务器", parkMonitor.model.TextColor.Error);
  639. connecting = false;
  640. }
  641. else
  642. {
  643. Monitor.Monitor.SetNotification("正在尝试连接,请勿重复点击", parkMonitor.model.TextColor.Warning);
  644. }
  645. }
  646. /// <summary>
  647. /// 获取连接状态
  648. /// </summary>
  649. /// <returns></returns>
  650. public bool GetConnectStatus()
  651. {
  652. return connected;
  653. }
  654. }
  655. }