FormCentralController.cs 32 KB

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