FormCentralController.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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.SetNotification("检测到PLC掉线,尝试重连", TextColor.Info);
  445. Monitor.Monitor.PLC.PLCConnect();
  446. }
  447. else
  448. {
  449. //List<object> received = Monitor.Monitor.PLC.ReadFromPLC(PLCDataType.terminal, 0);
  450. //if (received.Count != Monitor.Monitor.plcTerminalCount)
  451. //{
  452. // Monitor.Monitor.SetNotification("检测到PLC数据异常", TextColor.Warning);
  453. Monitor.Monitor.SetNotification("尝试关闭并重新建立与PLC的连接", TextColor.Info);
  454. Monitor.Monitor.PLC.PLCDisconnect();
  455. Monitor.Monitor.PLC.PLCConnect();
  456. //}
  457. //else
  458. //{
  459. // Monitor.Monitor.SetNotification("PLC连接正常,无需连接", TextColor.Info);
  460. //}
  461. }
  462. }
  463. }
  464. /// <summary>
  465. /// 手动连接Web
  466. /// </summary>
  467. /// <param name="sender"></param>
  468. /// <param name="e"></param>
  469. private void btn_linkWeb_Click(object sender, EventArgs e)
  470. {
  471. Task.Factory.StartNew(() =>
  472. {
  473. Monitor.Monitor.webServer.Connect();
  474. });
  475. }
  476. /// <summary>
  477. /// PLC数据调试工具
  478. /// </summary>
  479. /// <param name="sender"></param>
  480. /// <param name="e"></param>
  481. private void btn_PLCConf_Click(object sender, EventArgs e)
  482. {
  483. FormPLCConf formPLCConf = new FormPLCConf();
  484. formPLCConf.Show();
  485. }
  486. /// <summary>
  487. /// 启动号牌机调试工具
  488. /// </summary>
  489. /// <param name="sender"></param>
  490. /// <param name="e"></param>
  491. private void btn_numMachineConf_Click(object sender, EventArgs e)
  492. {
  493. FormModbus form = new FormModbus();
  494. form.Show();
  495. }
  496. #endregion
  497. /// <summary>
  498. /// 更新提示信息,将被从外部调用
  499. /// </summary>
  500. /// <param name="sender"></param>
  501. /// <param name="e"></param>
  502. public void UpdateText(string str, TextColor tc, int maxlines)
  503. {
  504. if (!rtb_notification.Focused)
  505. rtb_notification.ScrollToCaret();
  506. string[] strs = rtb_notification.Text.Split('\n');
  507. //Console.WriteLine(strs.ToString());
  508. if (strs.Length > maxlines * 2)
  509. {
  510. string[] newStrs = new string[maxlines];
  511. Array.Copy(strs, strs.Length - maxlines, newStrs, 0, maxlines);
  512. string temp = String.Join("\n", newStrs);
  513. rtb_notification.Text = temp;
  514. }
  515. if (str == "clear")
  516. {
  517. rtb_notification.Clear();
  518. }
  519. else
  520. {
  521. //Console.WriteLine("---------------------");
  522. //Console.WriteLine(rtb_notification.SelectionStart);
  523. string appendingText = str;
  524. rtb_notification.AppendText(appendingText);
  525. string currentTxt = rtb_notification.Text;
  526. string temp = currentTxt.Substring(0, currentTxt.Length - appendingText.Length + 2);
  527. int index = temp.LastIndexOf("\n", temp.Length);
  528. //rtb_notification.Select(index < 0 ? 0 : index, appendingText.Length - 1);
  529. rtb_notification.Select(index < 0 ? 0 : index, appendingText.Length - 2);
  530. //Console.WriteLine("******************");
  531. //Console.WriteLine("选中 "+rtb_notification.SelectedText);
  532. //Console.WriteLine("******************");
  533. //rtb_notification.SelectionFont = new Font(rtb_notification.SelectionFont, FontStyle.Bold);
  534. switch (tc)
  535. {
  536. case TextColor.Log:
  537. rtb_notification.SelectionColor = Color.Gray;
  538. break;
  539. case TextColor.Info:
  540. rtb_notification.SelectionColor = Color.Black;
  541. break;
  542. case TextColor.Warning:
  543. rtb_notification.SelectionColor = Color.Orange;
  544. break;
  545. case TextColor.Error:
  546. rtb_notification.SelectionColor = Color.Red;
  547. break;
  548. }
  549. //rtb_notification.Select(index + appendingText.Length - 1, 0);
  550. if (!rtb_notification.Focused)
  551. rtb_notification.ScrollToCaret();
  552. }
  553. //rtb_notification.Text = Monitor.Monitor.ins.GetNotification();
  554. }
  555. /// <summary>
  556. /// 定时更新时间与空闲车位显示,以及设备连接状态
  557. /// </summary>
  558. /// <param name="sender"></param>
  559. /// <param name="e"></param>
  560. private void UpdateFreeSpace(object sender, EventArgs e)
  561. {
  562. string currentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  563. string freeSpace = "剩余车位:" + Monitor.Monitor.ins.GetFreeSpaceCount(0);
  564. string bookableSpace = "可预约车位:" + Monitor.Monitor.ins.GetFreeSpaceCount(2);
  565. lbx_freeSpace.Text = currentTime + "\r\n" + freeSpace + " " + bookableSpace;
  566. lbx_PLCStatus.SymbolColor = (Monitor.Monitor.PLC != null && Monitor.Monitor.PLC.isConnected) ? Color.Green : Color.OrangeRed;
  567. lbx_webStatus.SymbolColor = (Monitor.Monitor.webServer != null && Monitor.Monitor.webServer.GetConnectStatus()) ? Color.Green : Color.OrangeRed;
  568. }
  569. /// <summary>
  570. /// 刷新停车记录显示
  571. /// </summary>
  572. /// <param name="data"></param>
  573. private void RefreshRecords(object data, EventArgs e)
  574. {
  575. dgvx_parkingRecords.Rows.Clear();
  576. List<object[]> list = Monitor.Monitor.GetParkingRecords();
  577. List<object[]>.Enumerator listEnumer = list.GetEnumerator();
  578. while (listEnumer.MoveNext())
  579. {
  580. dgvx_parkingRecords.Rows.Add(listEnumer.Current);
  581. }
  582. }
  583. /// <summary>
  584. /// 导出excel文件
  585. /// </summary>
  586. /// <param name="dataGridView1"></param>
  587. /// <param name="path">绝对路径,"D:\\abc\\xxx.xls"</param>
  588. private void Export(DataGridViewX dataGridView1, string path)
  589. {
  590. try
  591. {
  592. if (dataGridView1.Rows.Count == 0)
  593. {
  594. return;
  595. }
  596. Excel.Application excel = new Excel.Application();
  597. excel.Visible = false;
  598. Excel.Workbook workbook = excel.Workbooks.Add(true);
  599. Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
  600. for (int i = 0; i < dataGridView1.Columns.Count; i++)
  601. {
  602. if (dataGridView1.Columns[i].Visible == true)
  603. {
  604. worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
  605. }
  606. }
  607. for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
  608. {
  609. System.Windows.Forms.Application.DoEvents();
  610. for (int j = 0; j < dataGridView1.Columns.Count; j++)
  611. {
  612. if (dataGridView1.Columns[j].Visible == true)
  613. {
  614. if (dataGridView1[j, i].ValueType == typeof(string))
  615. {
  616. worksheet.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value.ToString();
  617. }
  618. else
  619. {
  620. worksheet.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
  621. }
  622. }
  623. }
  624. }
  625. //设置禁止弹出保存和覆盖的询问提示框
  626. excel.DisplayAlerts = false;
  627. excel.AlertBeforeOverwriting = false;
  628. worksheet.Cells.Columns.AutoFit();
  629. //保存写入的数据,这里还没有保存到磁盘
  630. workbook.Saved = true;
  631. if (Directory.Exists(path))
  632. {
  633. //设置新建文件路径及名称
  634. string savePath = path + DateTime.Now.ToString("yyyy-MM-dd") + "停车记录.xls";
  635. //创建文件
  636. FileStream file = new FileStream(savePath, FileMode.CreateNew);
  637. //关闭释放流,不然没办法写入数据
  638. file.Close();
  639. file.Dispose();
  640. //保存到指定的路径
  641. workbook.SaveCopyAs(savePath);
  642. }
  643. workbook.Close(false, Type.Missing, Type.Missing);
  644. //确保Excel进程关闭
  645. excel.Quit();
  646. //释放 COM 对象
  647. Marshal.ReleaseComObject(worksheet);
  648. Marshal.ReleaseComObject(workbook);
  649. Marshal.ReleaseComObject(excel);
  650. excel = null;
  651. worksheet = null;
  652. workbook = null;
  653. GC.Collect();//如果不使用这条语,excel会无法正常退出
  654. }
  655. catch (Exception e) { Console.WriteLine("导出excel文件异常," + e.Message); }
  656. }
  657. /// <summary>
  658. /// 动态调整号牌机界面
  659. /// </summary>
  660. /// <param name="sender"></param>
  661. /// <param name="e"></param>
  662. private void flowLayoutPanel1_Resize(object sender, EventArgs e)
  663. {
  664. Control.ControlCollection flpCC = flowLayoutPanel1.Controls;
  665. try
  666. {
  667. int rows = (int)Math.Sqrt(flowLayoutPanel1.Controls.Count + 1);
  668. int columns = (int)(flowLayoutPanel1.Controls.Count * 1.0 / rows + 1);
  669. for (int i = 0; i < flpCC.Count; i++)
  670. {
  671. flpCC[i].Width = (int)(flowLayoutPanel1.Width * 1.0 / (columns * 1.1));
  672. flpCC[i].Height = (int)(flowLayoutPanel1.Height * 1.0 / (rows * 1.15));
  673. Console.WriteLine("flp::" + flowLayoutPanel1.Width + "," + flowLayoutPanel1.Height);
  674. //Console.WriteLine("tlp--"+flpCC[i].Width + "," + flpCC[i].Height);
  675. Control.ControlCollection tlpCC = flpCC[i].Controls;
  676. if (tlpCC.Count == 2)
  677. {
  678. tlpCC[0].Width = (int)(flpCC[i].Width * 0.95);
  679. tlpCC[0].Height = (int)(flpCC[i].Height * 0.79);
  680. tlpCC[1].Width = (int)(flpCC[i].Width * 0.95);
  681. tlpCC[1].Height = (int)(flpCC[i].Height * 0.20);
  682. }
  683. }
  684. }
  685. catch (Exception ex) { Console.WriteLine(ex.Message); }
  686. }
  687. /// <summary>
  688. /// 右上角关闭函数
  689. /// </summary>
  690. /// <param name="sender"></param>
  691. /// <param name="e"></param>
  692. private void centralController_FormClosing(object sender, FormClosingEventArgs e)
  693. {
  694. DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel);
  695. if (result.Equals(DialogResult.OK))
  696. {
  697. Monitor.Monitor.ins.Stop();
  698. closing = true;
  699. }
  700. else
  701. {
  702. e.Cancel = true;
  703. }
  704. }
  705. #region 帮助
  706. /// <summary>
  707. /// 系统信息
  708. /// </summary>
  709. /// <param name="sender"></param>
  710. /// <param name="e"></param>
  711. private void btn_sysInfo_Click(object sender, EventArgs e)
  712. {
  713. FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo());
  714. sysInfo.ShowDialog();
  715. }
  716. #endregion
  717. #region 快捷菜单栏
  718. /// <summary>
  719. /// 快捷显示系统信息
  720. /// </summary>
  721. /// <param name="sender"></param>
  722. /// <param name="e"></param>
  723. private void shortcut_sysInfo_Click(object sender, EventArgs e)
  724. {
  725. FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo());
  726. sysInfo.ShowDialog();
  727. }
  728. /// <summary>
  729. /// 快捷关闭
  730. /// </summary>
  731. /// <param name="sender"></param>
  732. /// <param name="e"></param>
  733. private void shortcut_close_Click(object sender, EventArgs e)
  734. {
  735. Close();
  736. }
  737. /// <summary>
  738. /// 终止与启动流程
  739. /// </summary>
  740. /// <param name="sender"></param>
  741. /// <param name="e"></param>
  742. private void shortcut_pause_Click(object sender, EventArgs e)
  743. {
  744. if (shortcut_pause.Enabled)
  745. {
  746. shortcut_pause.Enabled = false;
  747. Monitor.Monitor.globalStatus = !Monitor.Monitor.globalStatus;
  748. if (Monitor.Monitor.globalStatus)
  749. {
  750. shortcut_pause.Image = Properties.Resources.circulate;
  751. shortcut_pause.Text = "点击终止";
  752. }
  753. else
  754. {
  755. shortcut_pause.Image = Properties.Resources.circulate2;
  756. shortcut_pause.Text = "点击启动";
  757. }
  758. Monitor.Monitor.SetNotification(Monitor.Monitor.globalStatus ? "工作正常" : "终止状态");
  759. Task.Factory.StartNew(() =>
  760. {
  761. Thread.Sleep(3000);
  762. try
  763. {
  764. this.Invoke(new Action(() =>
  765. {
  766. shortcut_pause.Enabled = true;
  767. }));
  768. }
  769. catch (Exception ex) { Console.WriteLine(ex.Message); }
  770. });
  771. }
  772. }
  773. #endregion
  774. }
  775. }