FormCentralController.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. using centralController.model;
  2. using DevComponents.DotNetBar.Controls;
  3. using nettyCommunication;
  4. using parkMonitor.model;
  5. using PLC_Communication;
  6. using PLCConnector;
  7. using PLCS7;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Net;
  13. using System.Runtime.InteropServices;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using System.Windows.Forms;
  17. using Terminal;
  18. using Excel = Microsoft.Office.Interop.Excel;
  19. namespace centralController
  20. {
  21. public partial class centralController : Form
  22. {
  23. private List<TermModel> TermDispList;
  24. private bool closing = false;
  25. /// <summary>
  26. /// 窗体初始化
  27. /// </summary>
  28. public centralController()
  29. {
  30. InitializeComponent();
  31. TermDispList = new List<TermModel>();
  32. if (Monitor.Monitor.ins == null)
  33. {
  34. Monitor.Monitor.ins = new Monitor.Monitor(this, flowLayoutPanel1.Handle);
  35. }
  36. Monitor.Monitor.ins.Start();
  37. //进度条显示
  38. Task.Factory.StartNew(() =>
  39. {
  40. int temp = 0;
  41. while (true)
  42. {
  43. if (temp != Monitor.Monitor.initializeState)
  44. {
  45. temp = Monitor.Monitor.initializeState;
  46. progressBar1.Invoke(new Action(() => { progressBar1.Value = temp * 20; }));
  47. }
  48. if (temp == 5)
  49. {
  50. dgvx_parkingRecords.Invoke(new Action(() => { RefreshRecords(null, null); }));
  51. break;
  52. }
  53. Thread.Sleep(300);
  54. }
  55. });
  56. //定时器,定时更新停车记录
  57. System.Windows.Forms.Timer recordsTimer = new System.Windows.Forms.Timer();
  58. recordsTimer.Interval = 10000;
  59. recordsTimer.Tick += new EventHandler(RefreshRecords);
  60. recordsTimer.Start();
  61. ////定时器,定时更新提示信息
  62. //System.Windows.Forms.Timer NoteTimer = new System.Windows.Forms.Timer();
  63. //NoteTimer.Interval = 1000;
  64. //NoteTimer.Tick += new EventHandler(UpdateText);
  65. //NoteTimer.Start();
  66. //定时器,定时更新系统时间与剩余车位数
  67. System.Windows.Forms.Timer freeSpaceTimer = new System.Windows.Forms.Timer();
  68. freeSpaceTimer.Interval = 1000;
  69. freeSpaceTimer.Tick += new EventHandler(UpdateFreeSpace);
  70. freeSpaceTimer.Start();
  71. rtb_notification.Text = "";
  72. //地面车位显示
  73. Task.Factory.StartNew(() =>
  74. {
  75. UpdateAllTerms();
  76. });
  77. }
  78. /// <summary>
  79. /// 实时更新所有车位显示
  80. /// </summary>
  81. private void UpdateAllTerms()
  82. {
  83. while (!closing)
  84. {
  85. List<TerminalStru> tList = Terminal.Terminal.terminalInfo;
  86. this.Invoke(new Action(() =>
  87. {
  88. int currentDispID = 0;
  89. //出现终端丢失情况,必定是与PLC连接问题导致数据异常,重新初始化显示模块
  90. for (int i = 0; i < TermDispList.Count; i++)
  91. {
  92. bool finded = false;
  93. if (currentDispID < TermDispList[i].id)
  94. {
  95. currentDispID = TermDispList[i].id;
  96. for (int j = 0; j < tList.Count; j++)
  97. {
  98. if (currentDispID == tList[j].terminalID)
  99. {
  100. finded = true; break;
  101. }
  102. }
  103. }
  104. else
  105. {
  106. finded = false;
  107. }
  108. if (!finded)
  109. {
  110. flp_Term.Controls.Clear();
  111. TermDispList.Clear();
  112. Console.WriteLine("******************************");
  113. break;
  114. }
  115. }
  116. for (int i = 0; i < tList.Count; i++)
  117. {
  118. TerminalStru ts = tList[i];
  119. bool updated = false;
  120. for (int j = 0; j < TermDispList.Count; j++)
  121. {
  122. if (ts.terminalID == TermDispList[j].id)
  123. {
  124. UpdateTermGP(TermDispList[j], ts.terminalID,
  125. ts.terminalStatus == (short)1 ? true : false,
  126. ts.cmd == ts.terminalStatus ? true : false,
  127. ts.groundStatus == (short)1 ? true : false,
  128. ts.numMachineLaunch == (short)1 ? true : false);
  129. updated = true;
  130. break;
  131. }
  132. }
  133. if (!updated)
  134. InitTermGP(ts.terminalID,
  135. ts.terminalStatus == (short)1 ? true : false,
  136. ts.cmd == ts.terminalStatus ? true : false,
  137. ts.groundStatus == (short)1 ? true : false,
  138. ts.numMachineLaunch == (short)1 ? true : false);
  139. }
  140. }));
  141. Thread.Sleep(5000);
  142. }
  143. }
  144. /// <summary>
  145. /// 初始化地面车位信息
  146. /// </summary>
  147. /// <param name="id"></param>
  148. /// <param name="park"></param>
  149. /// <param name="launched"></param>
  150. /// <param name="groundSensor"></param>
  151. /// <param name="numMachine"></param>
  152. private void InitTermGP(int id, bool park, bool launched, bool groundSensor, bool numMachine)
  153. {
  154. GroupPanel gpTerm = new GroupPanel();
  155. PictureBox pbCar = new PictureBox();
  156. PictureBox pbNum = new PictureBox();
  157. pbNum.Dock = DockStyle.Top;
  158. //pbNum.Anchor = AnchorStyles.Top;
  159. pbNum.Image = Properties.Resources.numMachine;
  160. pbNum.Location = new Point(27, 1);
  161. pbNum.Name = "term" + id + "pbNum";
  162. pbNum.Size = new Size(50, 29);
  163. pbNum.SizeMode = PictureBoxSizeMode.Zoom;
  164. pbNum.TabStop = false;
  165. pbCar.Dock = DockStyle.Bottom;
  166. //pbCar.Anchor = ((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right);
  167. pbCar.Image = Properties.Resources.car;
  168. pbCar.Location = new Point(23, 45);
  169. pbCar.Name = "term" + id + "pbCar";
  170. pbCar.Size = new Size(56, 43);
  171. pbCar.SizeMode = PictureBoxSizeMode.Zoom;
  172. gpTerm.BackColor = SystemColors.AppWorkspace;
  173. gpTerm.CanvasColor = SystemColors.Control;
  174. gpTerm.ColorSchemeStyle = DevComponents.DotNetBar.eDotNetBarStyle.Office2007;
  175. gpTerm.ColorTable = ePanelColorTable.Red;
  176. gpTerm.Controls.Add(pbCar);
  177. gpTerm.Controls.Add(pbNum);
  178. gpTerm.DisabledBackColor = Color.Empty;
  179. gpTerm.Name = "term" + id + "gp";
  180. gpTerm.Size = new Size(105, 112);
  181. gpTerm.Style.BackColor = Color.FromArgb(((int)(((byte)(229)))), ((int)(((byte)(191)))), ((int)(((byte)(191)))));
  182. gpTerm.Style.BackColor2 = Color.FromArgb(((int)(((byte)(211)))), ((int)(((byte)(150)))), ((int)(((byte)(150)))));
  183. gpTerm.Style.BackColorGradientAngle = 90;
  184. gpTerm.Style.BorderBottom = DevComponents.DotNetBar.eStyleBorderType.Solid;
  185. gpTerm.Style.BorderBottomWidth = 1;
  186. gpTerm.Style.BorderColor = Color.FromArgb(((int)(((byte)(149)))), ((int)(((byte)(55)))), ((int)(((byte)(52)))));
  187. gpTerm.Style.BorderLeft = DevComponents.DotNetBar.eStyleBorderType.Solid;
  188. gpTerm.Style.BorderLeftWidth = 1;
  189. gpTerm.Style.BorderRight = DevComponents.DotNetBar.eStyleBorderType.Solid;
  190. gpTerm.Style.BorderRightWidth = 1;
  191. gpTerm.Style.BorderTop = DevComponents.DotNetBar.eStyleBorderType.Solid;
  192. gpTerm.Style.BorderTopWidth = 1;
  193. gpTerm.Style.CornerDiameter = 4;
  194. gpTerm.Style.CornerType = DevComponents.DotNetBar.eCornerType.Rounded;
  195. gpTerm.Style.TextAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center;
  196. gpTerm.Style.TextColor = Color.FromArgb(((int)(((byte)(99)))), ((int)(((byte)(36)))), ((int)(((byte)(35)))));
  197. gpTerm.Style.TextLineAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Near;
  198. gpTerm.StyleMouseDown.CornerType = DevComponents.DotNetBar.eCornerType.Square;
  199. gpTerm.StyleMouseOver.CornerType = DevComponents.DotNetBar.eCornerType.Square;
  200. gpTerm.Text = "地面车位" + id;
  201. TermModel tm = new TermModel(gpTerm, pbNum, pbCar);
  202. UpdateTermGP(tm, id, park, launched, groundSensor, numMachine);
  203. TermDispList.Add(tm);
  204. flp_Term.Controls.Add(gpTerm);
  205. }
  206. /// <summary>
  207. /// 更新地面车位信息
  208. /// </summary>
  209. /// <param name="tm"></param>
  210. /// <param name="id"></param>
  211. /// <param name="park"></param>
  212. /// <param name="launched"></param>
  213. /// <param name="groundSensor"></param>
  214. /// <param name="numMachine"></param>
  215. private void UpdateTermGP(TermModel tm, int id, bool park, bool launched, bool groundSensor, bool numMachine)
  216. {
  217. tm.UpdateStatus(id, park, launched, groundSensor, numMachine);
  218. if (tm.gp != null)
  219. {
  220. //停取状态
  221. if (park)
  222. tm.gp.ColorTable = ePanelColorTable.Red;
  223. else
  224. tm.gp.ColorTable = ePanelColorTable.Green;
  225. //启动/空闲
  226. if (launched)
  227. tm.gp.BackColor = SystemColors.Highlight;
  228. else
  229. tm.gp.BackColor = SystemColors.AppWorkspace;
  230. //地感
  231. if (groundSensor && tm.pbCar != null)
  232. tm.pbCar.Visible = true;
  233. else
  234. tm.pbCar.Visible = false;
  235. //号牌机
  236. if (numMachine && tm.pbNum != null)
  237. tm.pbNum.Visible = true;
  238. else
  239. tm.pbNum.Visible = false;
  240. }
  241. }
  242. #region 测试
  243. /// <summary>
  244. /// 获取号牌测试
  245. /// </summary>
  246. /// <param name="sender"></param>
  247. /// <param name="e"></param>
  248. private void button1_Click(object sender, EventArgs e)
  249. {
  250. string ip = textBox1.Text;
  251. IPAddress temp;
  252. if (IPAddress.TryParse(ip, out temp))
  253. {
  254. string license = "";
  255. int id = 0;
  256. int.TryParse(textBox1.Text, out id);
  257. Task.Factory.StartNew(() =>
  258. {
  259. license = Monitor.Monitor.numMachineLinker.GetLicensePlate(id);
  260. if (textBox2.InvokeRequired)
  261. {
  262. textBox2.Invoke(new Action(() => { textBox2.Text = license; }));
  263. }
  264. else
  265. {
  266. textBox2.Text = license;
  267. }
  268. });
  269. }
  270. }
  271. /// <summary>
  272. /// 屏显测试
  273. /// </summary>
  274. /// <param name="sender"></param>
  275. /// <param name="e"></param>
  276. private void button2_Click(object sender, EventArgs e)
  277. {
  278. string disp = textBox1.Text;
  279. string audio = textBox2.Text;
  280. Monitor.Monitor.allInOneMachine.DispForAWhile(0, disp, 10, 1, audio);
  281. }
  282. #endregion
  283. #region 系统
  284. /// <summary>
  285. /// 结束按钮
  286. /// </summary>
  287. /// <param name="sender"></param>
  288. /// <param name="e"></param>
  289. private void btn_exit_Click(object sender, EventArgs e)
  290. {
  291. //DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel);
  292. //if (result.Equals(DialogResult.OK))
  293. //{
  294. // Monitor.Monitor.ins.Stop();
  295. // Close();
  296. //}
  297. Close();
  298. }
  299. #endregion
  300. #region 文件
  301. /// <summary>
  302. /// 打开日志
  303. /// </summary>
  304. /// <param name="sender"></param>
  305. /// <param name="e"></param>
  306. private void btn_openFile_Click(object sender, EventArgs e)
  307. {
  308. }
  309. /// <summary>
  310. /// 刷新停车记录按钮
  311. /// </summary>
  312. /// <param name="sender"></param>
  313. /// <param name="e"></param>
  314. private void btn_recordManagement_Click(object sender, EventArgs e)
  315. {
  316. FormRecordsManager formRecordsManager = new FormRecordsManager();
  317. formRecordsManager.ShowDialog();
  318. }
  319. /// <summary>
  320. /// 导出停车记录按钮
  321. /// </summary>
  322. /// <param name="sender"></param>
  323. /// <param name="e"></param>
  324. private void btn_exportRecords_Click(object sender, EventArgs e)
  325. {
  326. Export(dgvx_parkingRecords, Directory.GetCurrentDirectory() + "\\");
  327. Console.WriteLine(Directory.GetCurrentDirectory() + "\\");
  328. }
  329. /// <summary>
  330. /// 显示与设置收费策略
  331. /// </summary>
  332. /// <param name="sender"></param>
  333. /// <param name="e"></param>
  334. private void btn_setScheme_Click(object sender, EventArgs e)
  335. {
  336. FormPaymentScheme form_PaymentScheme = new FormPaymentScheme();
  337. form_PaymentScheme.ShowDialog();
  338. }
  339. /// <summary>
  340. /// 手动更新广告
  341. /// </summary>
  342. /// <param name="sender"></param>
  343. /// <param name="e"></param>
  344. private void btn_advertUpdate_Click(object sender, EventArgs e)
  345. {
  346. Task.Factory.StartNew(() =>
  347. {
  348. MessageBox.Show("更新中,请稍候。", "提示");
  349. string adAlert = "";
  350. if (Monitor.Monitor.advertMgr != null)
  351. {
  352. if (Monitor.Monitor.advertMgr.UpdateAdvert(out adAlert))
  353. MessageBox.Show("更新成功\n" + adAlert, "提示");
  354. else
  355. MessageBox.Show("更新失败,请稍后重试", "提示");
  356. }
  357. });
  358. }
  359. /// <summary>
  360. /// 系统配置
  361. /// </summary>
  362. /// <param name="sender"></param>
  363. /// <param name="e"></param>
  364. private void btn_config_Click(object sender, EventArgs e)
  365. {
  366. FormSysConfig formSysConfig = new FormSysConfig();
  367. formSysConfig.ShowDialog();
  368. }
  369. #endregion
  370. #region 视图
  371. /// <summary>
  372. /// 车位视图
  373. /// </summary>
  374. /// <param name="sender"></param>
  375. /// <param name="e"></param>
  376. private void btn_parkingSpace_Click(object sender, EventArgs e)
  377. {
  378. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  379. if (dci_ParkingSpace.Visible == false) { dci_ParkingSpace.Visible = true; }
  380. }
  381. /// <summary>
  382. /// 显示号牌机视图
  383. /// </summary>
  384. /// <param name="sender"></param>
  385. /// <param name="e"></param>
  386. private void btn_numMachine_Click(object sender, EventArgs e)
  387. {
  388. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  389. if (dci_NumMachine.Visible == false) { dci_NumMachine.Visible = true; }
  390. }
  391. /// <summary>
  392. /// 支付视图
  393. /// </summary>
  394. /// <param name="sender"></param>
  395. /// <param name="e"></param>
  396. private void btn_payment_Click(object sender, EventArgs e)
  397. {
  398. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  399. if (dci_payment.Visible == false) { dci_payment.Visible = true; }
  400. }
  401. /// <summary>
  402. /// 显示测试选项卡
  403. /// </summary>
  404. /// <param name="sender"></param>
  405. /// <param name="e"></param>
  406. private void btn_test_Click(object sender, EventArgs e)
  407. {
  408. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  409. if (dci_Test.Visible == false) { dci_Test.Visible = true; }
  410. }
  411. /// <summary>
  412. /// 终端视图
  413. /// </summary>
  414. /// <param name="sender"></param>
  415. /// <param name="e"></param>
  416. private void btn_terminals_Click(object sender, EventArgs e)
  417. {
  418. if (bar_side.Visible == false) { bar_side.Visible = true; }
  419. if (dci_terminal.Visible == false) { dci_terminal.Visible = true; }
  420. }
  421. /// <summary>
  422. /// 停取记录
  423. /// </summary>
  424. /// <param name="sender"></param>
  425. /// <param name="e"></param>
  426. private void btn_records_Click(object sender, EventArgs e)
  427. {
  428. if (bar_bottom.Visible == false) { bar_bottom.Visible = true; }
  429. if (dci_ParkingRecords.Visible == false) { dci_ParkingRecords.Visible = true; }
  430. }
  431. #endregion
  432. #region 设备
  433. /// <summary>
  434. /// 手动连接PLC
  435. /// </summary>
  436. /// <param name="sender"></param>
  437. /// <param name="e"></param>
  438. private void btn_linkPLC_Click(object sender, EventArgs e)
  439. {
  440. if (Monitor.Monitor.PLC != null)
  441. {
  442. if (!Monitor.Monitor.PLC.isConnected)
  443. {
  444. Monitor.Monitor.PLC.PLCConnect();
  445. }
  446. else
  447. {
  448. List<object> received = Monitor.Monitor.PLC.ReadFromPLC(PLCDataType.terminal, 0);
  449. if (received.Count != Monitor.Monitor.plcTerminalCount)
  450. {
  451. Monitor.Monitor.SetNotification("检测到PLC数据异常", TextColor.Warning);
  452. Monitor.Monitor.PLC.PLCDisconnect();
  453. Monitor.Monitor.PLC.PLCConnect();
  454. }
  455. else
  456. {
  457. Monitor.Monitor.SetNotification("PLC连接正常,无需连接", TextColor.Info);
  458. }
  459. }
  460. }
  461. }
  462. /// <summary>
  463. /// 手动连接Web
  464. /// </summary>
  465. /// <param name="sender"></param>
  466. /// <param name="e"></param>
  467. private void btn_linkWeb_Click(object sender, EventArgs e)
  468. {
  469. Task.Factory.StartNew(() =>
  470. {
  471. Monitor.Monitor.webServer.Connect();
  472. });
  473. }
  474. /// <summary>
  475. /// PLC数据调试工具
  476. /// </summary>
  477. /// <param name="sender"></param>
  478. /// <param name="e"></param>
  479. private void btn_PLCConf_Click(object sender, EventArgs e)
  480. {
  481. FormPLCConf formPLCConf = new FormPLCConf();
  482. formPLCConf.Show();
  483. }
  484. /// <summary>
  485. /// 启动号牌机调试工具
  486. /// </summary>
  487. /// <param name="sender"></param>
  488. /// <param name="e"></param>
  489. private void btn_numMachineConf_Click(object sender, EventArgs e)
  490. {
  491. FormModbus form = new FormModbus();
  492. form.Show();
  493. }
  494. #endregion
  495. /// <summary>
  496. /// 更新提示信息,将被从外部调用
  497. /// </summary>
  498. /// <param name="sender"></param>
  499. /// <param name="e"></param>
  500. public void UpdateText(string str, TextColor tc, int maxlines)
  501. {
  502. if (!rtb_notification.Focused)
  503. rtb_notification.ScrollToCaret();
  504. string[] strs = rtb_notification.Text.Split('\n');
  505. //Console.WriteLine(strs.ToString());
  506. if (strs.Length > maxlines*2)
  507. {
  508. string[] newStrs = new string[maxlines];
  509. Array.Copy(strs, strs.Length - maxlines, newStrs, 0, maxlines);
  510. string temp = String.Join("\n", newStrs);
  511. rtb_notification.Text = temp;
  512. }
  513. if (str == "clear")
  514. {
  515. rtb_notification.Clear();
  516. }
  517. else
  518. {
  519. string appendingText = str;
  520. rtb_notification.AppendText(appendingText);
  521. string currentTxt = rtb_notification.Text;
  522. string temp = currentTxt.Substring(0, currentTxt.Length - appendingText.Length + 2);
  523. int index = temp.LastIndexOf("\n", temp.Length);
  524. //rtb_notification.Select(index < 0 ? 0 : index, appendingText.Length - 1);
  525. rtb_notification.Select(index < 0 ? 0 : index, appendingText.Length - 2);
  526. //Console.WriteLine("******************");
  527. //Console.WriteLine("选中 "+rtb_notification.SelectedText);
  528. //Console.WriteLine("******************");
  529. //rtb_notification.SelectionFont = new Font(rtb_notification.SelectionFont, FontStyle.Bold);
  530. switch (tc)
  531. {
  532. case TextColor.Log:
  533. rtb_notification.SelectionColor = Color.Gray;
  534. break;
  535. case TextColor.Info:
  536. rtb_notification.SelectionColor = Color.Black;
  537. break;
  538. case TextColor.Warning:
  539. rtb_notification.SelectionColor = Color.Orange;
  540. break;
  541. case TextColor.Error:
  542. rtb_notification.SelectionColor = Color.Red;
  543. break;
  544. }
  545. rtb_notification.Select(index + appendingText.Length - 1, 0);
  546. }
  547. //rtb_notification.Text = Monitor.Monitor.ins.GetNotification();
  548. }
  549. /// <summary>
  550. /// 定时更新时间与空闲车位显示,以及设备连接状态
  551. /// </summary>
  552. /// <param name="sender"></param>
  553. /// <param name="e"></param>
  554. private void UpdateFreeSpace(object sender, EventArgs e)
  555. {
  556. string currentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  557. string freeSpace = "剩余车位:" + Monitor.Monitor.ins.GetFreeSpaceCount(0);
  558. string bookableSpace = "可预约车位:" + Monitor.Monitor.ins.GetFreeSpaceCount(2);
  559. lbx_freeSpace.Text = currentTime + "\r\n" + freeSpace + " " + bookableSpace;
  560. lbx_PLCStatus.SymbolColor = (Monitor.Monitor.PLC != null && Monitor.Monitor.PLC.isConnected) ? Color.Green : Color.OrangeRed;
  561. lbx_webStatus.SymbolColor = (Monitor.Monitor.webServer != null && Monitor.Monitor.webServer.GetConnectStatus()) ? Color.Green : Color.OrangeRed;
  562. }
  563. /// <summary>
  564. /// 刷新停车记录显示
  565. /// </summary>
  566. /// <param name="data"></param>
  567. private void RefreshRecords(object data, EventArgs e)
  568. {
  569. dgvx_parkingRecords.Rows.Clear();
  570. List<object[]> list = Monitor.Monitor.GetParkingRecords();
  571. List<object[]>.Enumerator listEnumer = list.GetEnumerator();
  572. while (listEnumer.MoveNext())
  573. {
  574. dgvx_parkingRecords.Rows.Add(listEnumer.Current);
  575. }
  576. }
  577. /// <summary>
  578. /// 导出excel文件
  579. /// </summary>
  580. /// <param name="dataGridView1"></param>
  581. /// <param name="path">绝对路径,"D:\\abc\\xxx.xls"</param>
  582. private void Export(DataGridViewX dataGridView1, string path)
  583. {
  584. try
  585. {
  586. if (dataGridView1.Rows.Count == 0)
  587. {
  588. return;
  589. }
  590. Excel.Application excel = new Excel.Application();
  591. excel.Visible = false;
  592. Excel.Workbook workbook = excel.Workbooks.Add(true);
  593. Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
  594. for (int i = 0; i < dataGridView1.Columns.Count; i++)
  595. {
  596. if (dataGridView1.Columns[i].Visible == true)
  597. {
  598. worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
  599. }
  600. }
  601. for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
  602. {
  603. System.Windows.Forms.Application.DoEvents();
  604. for (int j = 0; j < dataGridView1.Columns.Count; j++)
  605. {
  606. if (dataGridView1.Columns[j].Visible == true)
  607. {
  608. if (dataGridView1[j, i].ValueType == typeof(string))
  609. {
  610. worksheet.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value.ToString();
  611. }
  612. else
  613. {
  614. worksheet.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
  615. }
  616. }
  617. }
  618. }
  619. //设置禁止弹出保存和覆盖的询问提示框
  620. excel.DisplayAlerts = false;
  621. excel.AlertBeforeOverwriting = false;
  622. worksheet.Cells.Columns.AutoFit();
  623. //保存写入的数据,这里还没有保存到磁盘
  624. workbook.Saved = true;
  625. if (Directory.Exists(path))
  626. {
  627. //设置新建文件路径及名称
  628. string savePath = path + DateTime.Now.ToString("yyyy-MM-dd") + "停车记录.xls";
  629. //创建文件
  630. FileStream file = new FileStream(savePath, FileMode.CreateNew);
  631. //关闭释放流,不然没办法写入数据
  632. file.Close();
  633. file.Dispose();
  634. //保存到指定的路径
  635. workbook.SaveCopyAs(savePath);
  636. }
  637. workbook.Close(false, Type.Missing, Type.Missing);
  638. //确保Excel进程关闭
  639. excel.Quit();
  640. //释放 COM 对象
  641. Marshal.ReleaseComObject(worksheet);
  642. Marshal.ReleaseComObject(workbook);
  643. Marshal.ReleaseComObject(excel);
  644. excel = null;
  645. worksheet = null;
  646. workbook = null;
  647. GC.Collect();//如果不使用这条语,excel会无法正常退出
  648. }
  649. catch (Exception e) { Console.WriteLine("导出excel文件异常," + e.Message); }
  650. }
  651. /// <summary>
  652. /// 动态调整号牌机界面
  653. /// </summary>
  654. /// <param name="sender"></param>
  655. /// <param name="e"></param>
  656. private void flowLayoutPanel1_Resize(object sender, EventArgs e)
  657. {
  658. Control.ControlCollection flpCC = flowLayoutPanel1.Controls;
  659. try
  660. {
  661. int rows = (int)Math.Sqrt(flowLayoutPanel1.Controls.Count + 1);
  662. int columns = (int)(flowLayoutPanel1.Controls.Count * 1.0 / rows + 1);
  663. for (int i = 0; i < flpCC.Count; i++)
  664. {
  665. flpCC[i].Width = (int)(flowLayoutPanel1.Width * 1.0 / (columns * 1.1));
  666. flpCC[i].Height = (int)(flowLayoutPanel1.Height * 1.0 / (rows * 1.15));
  667. Console.WriteLine("flp::" + flowLayoutPanel1.Width + "," + flowLayoutPanel1.Height);
  668. //Console.WriteLine("tlp--"+flpCC[i].Width + "," + flpCC[i].Height);
  669. Control.ControlCollection tlpCC = flpCC[i].Controls;
  670. if (tlpCC.Count == 2)
  671. {
  672. tlpCC[0].Width = (int)(flpCC[i].Width * 0.95);
  673. tlpCC[0].Height = (int)(flpCC[i].Height * 0.79);
  674. tlpCC[1].Width = (int)(flpCC[i].Width * 0.95);
  675. tlpCC[1].Height = (int)(flpCC[i].Height * 0.20);
  676. }
  677. }
  678. }
  679. catch (Exception ex) { Console.WriteLine(ex.Message); }
  680. }
  681. /// <summary>
  682. /// 右上角关闭函数
  683. /// </summary>
  684. /// <param name="sender"></param>
  685. /// <param name="e"></param>
  686. private void centralController_FormClosing(object sender, FormClosingEventArgs e)
  687. {
  688. DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel);
  689. if (result.Equals(DialogResult.OK))
  690. {
  691. Monitor.Monitor.ins.Stop();
  692. closing = true;
  693. }
  694. else
  695. {
  696. e.Cancel = true;
  697. }
  698. }
  699. #region 帮助
  700. /// <summary>
  701. /// 系统信息
  702. /// </summary>
  703. /// <param name="sender"></param>
  704. /// <param name="e"></param>
  705. private void btn_sysInfo_Click(object sender, EventArgs e)
  706. {
  707. FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo());
  708. sysInfo.ShowDialog();
  709. }
  710. #endregion
  711. #region 快捷菜单栏
  712. /// <summary>
  713. /// 快捷显示系统信息
  714. /// </summary>
  715. /// <param name="sender"></param>
  716. /// <param name="e"></param>
  717. private void shortcut_sysInfo_Click(object sender, EventArgs e)
  718. {
  719. FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo());
  720. sysInfo.ShowDialog();
  721. }
  722. /// <summary>
  723. /// 快捷关闭
  724. /// </summary>
  725. /// <param name="sender"></param>
  726. /// <param name="e"></param>
  727. private void shortcut_close_Click(object sender, EventArgs e)
  728. {
  729. Close();
  730. }
  731. #endregion
  732. }
  733. }