FormCentralController.cs 44 KB

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