FormCentralController.cs 41 KB

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