FormCentralController.cs 34 KB

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