FormCentralController.cs 37 KB

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