FormCentralController.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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. else
  286. {
  287. for (int i = 0; i < psList.Count; i++)
  288. {
  289. try
  290. {
  291. idPssMap[psList[i].parkingSpace] = psList[i];
  292. }
  293. catch { }
  294. }
  295. }
  296. TableLayoutPanel tlp = new TableLayoutPanel();
  297. tlp.Name = "tlp_ParkingSpace";
  298. tlp.ColumnCount = Monitor.Monitor.rows;
  299. tlp.RowCount = 1;
  300. tlp.Dock = DockStyle.Fill;
  301. tlp.AutoScroll = true;
  302. tlp.BackColor = SystemColors.GradientActiveCaption;
  303. this.Invoke(new Action(() =>
  304. {
  305. //List<TableLayoutPanel> tlpList = new List<TableLayoutPanel>();
  306. int blockSize = Monitor.Monitor.spacesInRow * Monitor.Monitor.floors * Monitor.Monitor.rows;
  307. int offset = (Monitor.Monitor.rows + 1 - ((blockSize / Monitor.Monitor.spacesInRow) % Monitor.Monitor.rows)) * Monitor.Monitor.spacesInRow;
  308. //创建控件
  309. if (ParkingSpaceMonitorPanel.Controls.Count == 0 || needUpdate)
  310. {
  311. for (int i = 0; i < Monitor.Monitor.rows; i++)
  312. {
  313. TableLayoutPanel temp = new TableLayoutPanel();
  314. temp.Name = "tlp_ParkingSpaceSection" + (i + 1).ToString();
  315. temp.RowCount = Monitor.Monitor.floors;
  316. temp.ColumnCount = Monitor.Monitor.spacesInRow;
  317. temp.Dock = DockStyle.Fill;
  318. temp.AutoSize = true;
  319. temp.BackColor = SystemColors.GradientActiveCaption;
  320. temp.Margin = new Padding(5, 2, 5, 2);
  321. for (int j = Math.Max(psList.Count, blockSize) - 1; j >= 0; j--)
  322. {
  323. Label lb = new Label();
  324. lb.Anchor = (AnchorStyles.Top | AnchorStyles.Left);
  325. lb.AutoSize = true;
  326. lb.Location = new Point(10, 10);
  327. lb.Margin = new Padding(3, 10, 3, 10);
  328. lb.Padding = new Padding(5);
  329. lb.Name = "lb_parkingSpace" + j;
  330. lb.TextAlign = ContentAlignment.MiddleCenter;
  331. if (((j + offset) / Monitor.Monitor.spacesInRow) % Monitor.Monitor.rows == i)
  332. {
  333. if (j > psList.Count - 1)
  334. {
  335. lb.Text = "#####";
  336. temp.Controls.Add(lb);
  337. lb.BackColor = GetPSColor(-1);
  338. }
  339. else
  340. {
  341. lb.Text = "车位" + psList[j].parkingSpace;
  342. lb.BackColor = GetPSColor(psList[j].spaceStatus);
  343. temp.Controls.Add(lb);
  344. }
  345. }
  346. }
  347. tlp.Controls.Add(temp);
  348. //tlpList.Add(temp);
  349. }
  350. ParkingSpaceMonitorPanel.Controls.Clear();
  351. ParkingSpaceMonitorPanel.Controls.Add(tlp);
  352. needUpdate = false;
  353. }
  354. //更新控件
  355. else
  356. {
  357. Control.ControlCollection psCC = ParkingSpaceMonitorPanel.Controls;
  358. foreach (Control segment in psCC)
  359. {
  360. Control.ControlCollection segmentCC = segment.Controls;
  361. //Console.WriteLine("seg::"+segmentCC.Count);
  362. foreach (Control spaces in segmentCC)
  363. {
  364. Control.ControlCollection spaceCC = spaces.Controls;
  365. //Console.WriteLine("spaces::"+spaceCC.Count);
  366. foreach (Control space in spaceCC)
  367. {
  368. if (space.GetType() == typeof(Label))
  369. {
  370. int index = -1;
  371. bool result = int.TryParse(space.Text.Substring(2), out index);
  372. //Console.WriteLine("text is :" + space.Text+", index:"+index);
  373. if (idPssMap.ContainsKey(index))
  374. {
  375. //Console.WriteLine("index:" + index+", status:"+ idPssMap[index].spaceStatus);
  376. space.BackColor = GetPSColor(idPssMap[index].spaceStatus);
  377. }
  378. else if (result)
  379. {
  380. needUpdate = true;
  381. //Console.WriteLine("need update:"+index);
  382. break;
  383. }
  384. }
  385. }
  386. }
  387. if (needUpdate)
  388. {
  389. break;
  390. }
  391. }
  392. }
  393. }));
  394. Thread.Sleep(5000);
  395. }
  396. }
  397. private Color GetPSColor(int status)
  398. {
  399. switch (status)
  400. {
  401. case 0: return Color.White;
  402. case 1: return Color.Yellow;
  403. case 2: return Color.SkyBlue;
  404. case 3: return Color.Blue;
  405. case 4: return Color.Red;
  406. default: return Color.Violet;
  407. }
  408. }
  409. #region 测试
  410. /// <summary>
  411. /// 获取号牌测试
  412. /// </summary>
  413. /// <param name="sender"></param>
  414. /// <param name="e"></param>
  415. private void button1_Click(object sender, EventArgs e)
  416. {
  417. //string ip = textBox1.Text;
  418. //IPAddress temp;
  419. //if (IPAddress.TryParse(ip, out temp))
  420. //{
  421. string license = "";
  422. int id = 0;
  423. int.TryParse(textBox1.Text, out id);
  424. Task.Factory.StartNew(() =>
  425. {
  426. license = Monitor.Monitor.numMachineLinker.GetLicensePlate(id);
  427. if (textBox2.InvokeRequired)
  428. {
  429. textBox2.Invoke(new Action(() => { textBox2.Text = license; }));
  430. }
  431. else
  432. {
  433. textBox2.Text = license;
  434. }
  435. });
  436. //}
  437. }
  438. /// <summary>
  439. /// 屏显测试
  440. /// </summary>
  441. /// <param name="sender"></param>
  442. /// <param name="e"></param>
  443. private void button2_Click(object sender, EventArgs e)
  444. {
  445. string disp = textBox1.Text;
  446. string audio = textBox2.Text;
  447. //Monitor.Monitor.allInOneMachine.DispForAWhile(0, disp, 10, 1, audio);
  448. //Monitor.Monitor.allInOneMachine.boardList[0].UpdateText(disp, 8);
  449. try
  450. {
  451. Monitor.Monitor.allInOneMachine.boardList[0].UpdateText(disp==""?"智象停车欢迎您!":disp, 30, int.Parse(audio));
  452. }
  453. catch { }
  454. //Monitor.Monitor.allInOneMachine.boardList[0].UpdateText("请将方向盘回正,带好随身携带的物品,关闭车窗与车门,收起后视镜,确认所有人员离开车辆后,操作终端停车", 30, 2);
  455. //Thread.Sleep(1000);
  456. //Monitor.Monitor.allInOneMachine.boardList[0].UpdateArrow(2, 5, 2);
  457. }
  458. /// <summary>
  459. /// 终端id获取测试
  460. /// </summary>
  461. /// <param name="sender"></param>
  462. /// <param name="e"></param>
  463. private void button3_Click(object sender, EventArgs e)
  464. {
  465. string license = textBox1.Text;
  466. int id = Monitor.Monitor.numMachineLinker.GetLicenseID(license);
  467. textBox2.Text = "终端" + id.ToString();
  468. }
  469. /// <summary>
  470. /// tableLayoutPanel测试
  471. /// </summary>
  472. /// <param name="sender"></param>
  473. /// <param name="e"></param>
  474. private void button4_Click(object sender, EventArgs e)
  475. {
  476. Control c = tableLayoutPanel3.GetControlFromPosition(0, 0);
  477. Console.WriteLine(tableLayoutPanel3.Controls.Count);
  478. Control c1 = tableLayoutPanel3.GetControlFromPosition(0, 1);
  479. Console.WriteLine(c1.Controls.Count);
  480. Console.WriteLine("...");
  481. }
  482. #endregion
  483. #region 系统
  484. /// <summary>
  485. /// 结束按钮
  486. /// </summary>
  487. /// <param name="sender"></param>
  488. /// <param name="e"></param>
  489. private void btn_exit_Click(object sender, EventArgs e)
  490. {
  491. //DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel);
  492. //if (result.Equals(DialogResult.OK))
  493. //{
  494. // Monitor.Monitor.ins.Stop();
  495. // Close();
  496. //}
  497. Close();
  498. }
  499. #endregion
  500. #region 文件
  501. /// <summary>
  502. /// 打开日志
  503. /// </summary>
  504. /// <param name="sender"></param>
  505. /// <param name="e"></param>
  506. private void btn_openFile_Click(object sender, EventArgs e)
  507. {
  508. }
  509. /// <summary>
  510. /// 刷新停车记录按钮
  511. /// </summary>
  512. /// <param name="sender"></param>
  513. /// <param name="e"></param>
  514. private void btn_recordManagement_Click(object sender, EventArgs e)
  515. {
  516. FormRecordsManager formRecordsManager = new FormRecordsManager();
  517. formRecordsManager.ShowDialog();
  518. }
  519. /// <summary>
  520. /// 导出停车记录按钮
  521. /// </summary>
  522. /// <param name="sender"></param>
  523. /// <param name="e"></param>
  524. private void btn_exportRecords_Click(object sender, EventArgs e)
  525. {
  526. Export(dgvx_parkingRecords, Directory.GetCurrentDirectory() + "\\");
  527. Console.WriteLine(Directory.GetCurrentDirectory() + "\\");
  528. }
  529. /// <summary>
  530. /// 显示与设置收费策略
  531. /// </summary>
  532. /// <param name="sender"></param>
  533. /// <param name="e"></param>
  534. private void btn_setScheme_Click(object sender, EventArgs e)
  535. {
  536. FormPaymentScheme form_PaymentScheme = new FormPaymentScheme();
  537. form_PaymentScheme.ShowDialog();
  538. }
  539. /// <summary>
  540. /// 手动更新广告
  541. /// </summary>
  542. /// <param name="sender"></param>
  543. /// <param name="e"></param>
  544. private void btn_advertUpdate_Click(object sender, EventArgs e)
  545. {
  546. Task.Factory.StartNew(() =>
  547. {
  548. MessageBox.Show("更新中,请稍候。", "提示");
  549. string adAlert = "";
  550. if (Monitor.Monitor.advertMgr != null)
  551. {
  552. if (Monitor.Monitor.advertMgr.UpdateAdvert(out adAlert))
  553. MessageBox.Show("更新成功\n" + adAlert, "提示");
  554. else
  555. MessageBox.Show("更新失败,请稍后重试", "提示");
  556. }
  557. });
  558. }
  559. /// <summary>
  560. /// 系统配置
  561. /// </summary>
  562. /// <param name="sender"></param>
  563. /// <param name="e"></param>
  564. private void btn_config_Click(object sender, EventArgs e)
  565. {
  566. FormSysConfig formSysConfig = new FormSysConfig();
  567. formSysConfig.ShowDialog();
  568. }
  569. #endregion
  570. #region 视图
  571. /// <summary>
  572. /// 车位视图
  573. /// </summary>
  574. /// <param name="sender"></param>
  575. /// <param name="e"></param>
  576. private void btn_parkingSpace_Click(object sender, EventArgs e)
  577. {
  578. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  579. if (dci_ParkingSpace.Visible == false) { dci_ParkingSpace.Visible = true; }
  580. }
  581. /// <summary>
  582. /// 显示号牌机视图
  583. /// </summary>
  584. /// <param name="sender"></param>
  585. /// <param name="e"></param>
  586. private void btn_numMachine_Click(object sender, EventArgs e)
  587. {
  588. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  589. if (dci_NumMachine.Visible == false) { dci_NumMachine.Visible = true; }
  590. }
  591. /// <summary>
  592. /// 支付视图
  593. /// </summary>
  594. /// <param name="sender"></param>
  595. /// <param name="e"></param>
  596. private void btn_payment_Click(object sender, EventArgs e)
  597. {
  598. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  599. if (dci_payment.Visible == false) { dci_payment.Visible = true; }
  600. }
  601. /// <summary>
  602. /// 显示测试选项卡
  603. /// </summary>
  604. /// <param name="sender"></param>
  605. /// <param name="e"></param>
  606. private void btn_test_Click(object sender, EventArgs e)
  607. {
  608. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  609. if (dci_Test.Visible == false) { dci_Test.Visible = true; }
  610. }
  611. /// <summary>
  612. /// 终端视图
  613. /// </summary>
  614. /// <param name="sender"></param>
  615. /// <param name="e"></param>
  616. private void btn_terminals_Click(object sender, EventArgs e)
  617. {
  618. if (bar_side.Visible == false) { bar_side.Visible = true; }
  619. if (dci_terminal.Visible == false) { dci_terminal.Visible = true; }
  620. }
  621. /// <summary>
  622. /// 停取记录
  623. /// </summary>
  624. /// <param name="sender"></param>
  625. /// <param name="e"></param>
  626. private void btn_records_Click(object sender, EventArgs e)
  627. {
  628. if (bar_bottom.Visible == false) { bar_bottom.Visible = true; }
  629. if (dci_ParkingRecords.Visible == false) { dci_ParkingRecords.Visible = true; }
  630. }
  631. #endregion
  632. #region 设备
  633. /// <summary>
  634. /// 手动连接PLC
  635. /// </summary>
  636. /// <param name="sender"></param>
  637. /// <param name="e"></param>
  638. private void btn_linkPLC_Click(object sender, EventArgs e)
  639. {
  640. if (Monitor.Monitor.PLC != null)
  641. {
  642. if (!Monitor.Monitor.PLC.isConnected)
  643. {
  644. Monitor.Monitor.SetNotification("检测到PLC掉线,尝试重连", TextColor.Info);
  645. Monitor.Monitor.PLC.PLCConnect();
  646. Monitor.Monitor.updateCount = true;
  647. }
  648. else
  649. {
  650. //List<object> received = Monitor.Monitor.PLC.ReadFromPLC(PLCDataType.terminal, 0);
  651. //if (received.Count != Monitor.Monitor.plcTerminalCount)
  652. //{
  653. // Monitor.Monitor.SetNotification("检测到PLC数据异常", TextColor.Warning);
  654. Monitor.Monitor.SetNotification("尝试关闭并重新建立与PLC的连接", TextColor.Info);
  655. Monitor.Monitor.PLC.PLCDisconnect();
  656. Monitor.Monitor.PLC.PLCConnect();
  657. Monitor.Monitor.updateCount = true;
  658. //}
  659. //else
  660. //{
  661. // Monitor.Monitor.SetNotification("PLC连接正常,无需连接", TextColor.Info);
  662. //}
  663. }
  664. }
  665. }
  666. /// <summary>
  667. /// 手动连接Web
  668. /// </summary>
  669. /// <param name="sender"></param>
  670. /// <param name="e"></param>
  671. private void btn_linkWeb_Click(object sender, EventArgs e)
  672. {
  673. Task.Factory.StartNew(() =>
  674. {
  675. Monitor.Monitor.webServer.Connect();
  676. });
  677. }
  678. /// <summary>
  679. /// PLC数据调试工具
  680. /// </summary>
  681. /// <param name="sender"></param>
  682. /// <param name="e"></param>
  683. private void btn_PLCConf_Click(object sender, EventArgs e)
  684. {
  685. FormPLCConf formPLCConf = new FormPLCConf();
  686. formPLCConf.Show();
  687. }
  688. /// <summary>
  689. /// 启动号牌机调试工具
  690. /// </summary>
  691. /// <param name="sender"></param>
  692. /// <param name="e"></param>
  693. private void btn_numMachineConf_Click(object sender, EventArgs e)
  694. {
  695. FormModbus form = new FormModbus();
  696. form.Show();
  697. }
  698. #endregion
  699. /// <summary>
  700. /// 更新提示信息,将被从外部调用
  701. /// </summary>
  702. /// <param name="sender"></param>
  703. /// <param name="e"></param>
  704. public void UpdateText(string str, TextColor tc, int maxlines)
  705. {
  706. if (!rtb_notification.Focused)
  707. rtb_notification.ScrollToCaret();
  708. string[] strs = rtb_notification.Text.Split('\n');
  709. //Console.WriteLine(strs.ToString());
  710. if (strs.Length > maxlines * 2)
  711. {
  712. string[] newStrs = new string[maxlines];
  713. Array.Copy(strs, strs.Length - maxlines, newStrs, 0, maxlines);
  714. string temp = String.Join("\n", newStrs);
  715. rtb_notification.Text = temp;
  716. }
  717. if (str == "clear")
  718. {
  719. rtb_notification.Clear();
  720. }
  721. else
  722. {
  723. //Console.WriteLine("---------------------");
  724. //Console.WriteLine(rtb_notification.SelectionStart);
  725. string appendingText = str;
  726. rtb_notification.AppendText(appendingText);
  727. string currentTxt = rtb_notification.Text;
  728. string temp = currentTxt.Substring(0, currentTxt.Length - appendingText.Length + 2);
  729. int index = temp.LastIndexOf("\n", temp.Length);
  730. //rtb_notification.Select(index < 0 ? 0 : index, appendingText.Length - 1);
  731. rtb_notification.Select(index < 0 ? 0 : index, appendingText.Length - 2);
  732. //Console.WriteLine("******************");
  733. //Console.WriteLine("选中 "+rtb_notification.SelectedText);
  734. //Console.WriteLine("******************");
  735. //rtb_notification.SelectionFont = new Font(rtb_notification.SelectionFont, FontStyle.Bold);
  736. switch (tc)
  737. {
  738. case TextColor.Log:
  739. rtb_notification.SelectionColor = Color.Gray;
  740. break;
  741. case TextColor.Info:
  742. rtb_notification.SelectionColor = Color.Black;
  743. break;
  744. case TextColor.Warning:
  745. rtb_notification.SelectionColor = Color.Orange;
  746. break;
  747. case TextColor.Error:
  748. rtb_notification.SelectionColor = Color.Red;
  749. break;
  750. }
  751. rtb_notification.Select(index + appendingText.Length - 1, 0);
  752. if (!rtb_notification.Focused)
  753. rtb_notification.ScrollToCaret();
  754. }
  755. //rtb_notification.Text = Monitor.Monitor.ins.GetNotification();
  756. }
  757. /// <summary>
  758. /// 定时更新时间与空闲车位显示,以及设备连接状态
  759. /// </summary>
  760. /// <param name="sender"></param>
  761. /// <param name="e"></param>
  762. private void UpdateFreeSpace(object sender, EventArgs e)
  763. {
  764. string currentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  765. string freeSpace = "剩余车位:" + Monitor.Monitor.ins.GetFreeSpaceCount(0);
  766. string bookableSpace = "可预约车位:" + Monitor.Monitor.ins.GetFreeSpaceCount(2);
  767. lbx_freeSpace.Text = currentTime + "\r\n" + freeSpace + " " + bookableSpace;
  768. lbx_PLCStatus.SymbolColor = (Monitor.Monitor.PLC != null && Monitor.Monitor.PLC.isConnected) ? Color.Green : Color.OrangeRed;
  769. lbx_webStatus.SymbolColor = (Monitor.Monitor.webServer != null && Monitor.Monitor.webServer.GetConnectStatus()) ? Color.Green : Color.OrangeRed;
  770. lbx_led1Status.SymbolColor = (Monitor.Monitor.allInOneMachine != null && Monitor.Monitor.allInOneMachine.boardList.Count > 0 && Monitor.Monitor.allInOneMachine.boardList[0].connected) ? Color.Green : Color.OrangeRed;
  771. }
  772. /// <summary>
  773. /// 刷新停车记录显示
  774. /// </summary>
  775. /// <param name="data"></param>
  776. private void RefreshRecords(object data, EventArgs e)
  777. {
  778. dgvx_parkingRecords.Rows.Clear();
  779. List<object[]> list = Monitor.Monitor.GetParkingRecords();
  780. List<object[]>.Enumerator listEnumer = list.GetEnumerator();
  781. while (listEnumer.MoveNext())
  782. {
  783. dgvx_parkingRecords.Rows.Add(listEnumer.Current);
  784. }
  785. }
  786. /// <summary>
  787. /// 导出excel文件
  788. /// </summary>
  789. /// <param name="dataGridView1"></param>
  790. /// <param name="path">绝对路径,"D:\\abc\\xxx.xls"</param>
  791. private void Export(DataGridViewX dataGridView1, string path)
  792. {
  793. try
  794. {
  795. if (dataGridView1.Rows.Count == 0)
  796. {
  797. return;
  798. }
  799. Excel.Application excel = new Excel.Application();
  800. excel.Visible = false;
  801. Excel.Workbook workbook = excel.Workbooks.Add(true);
  802. Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
  803. for (int i = 0; i < dataGridView1.Columns.Count; i++)
  804. {
  805. if (dataGridView1.Columns[i].Visible == true)
  806. {
  807. worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
  808. }
  809. }
  810. for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
  811. {
  812. System.Windows.Forms.Application.DoEvents();
  813. for (int j = 0; j < dataGridView1.Columns.Count; j++)
  814. {
  815. if (dataGridView1.Columns[j].Visible == true)
  816. {
  817. if (dataGridView1[j, i].ValueType == typeof(string))
  818. {
  819. worksheet.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value.ToString();
  820. }
  821. else
  822. {
  823. worksheet.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
  824. }
  825. }
  826. }
  827. }
  828. //设置禁止弹出保存和覆盖的询问提示框
  829. excel.DisplayAlerts = false;
  830. excel.AlertBeforeOverwriting = false;
  831. worksheet.Cells.Columns.AutoFit();
  832. //保存写入的数据,这里还没有保存到磁盘
  833. workbook.Saved = true;
  834. if (Directory.Exists(path))
  835. {
  836. //设置新建文件路径及名称
  837. string savePath = path + DateTime.Now.ToString("yyyy-MM-dd") + "停车记录.xls";
  838. //创建文件
  839. FileStream file = new FileStream(savePath, FileMode.CreateNew);
  840. //关闭释放流,不然没办法写入数据
  841. file.Close();
  842. file.Dispose();
  843. //保存到指定的路径
  844. workbook.SaveCopyAs(savePath);
  845. }
  846. workbook.Close(false, Type.Missing, Type.Missing);
  847. //确保Excel进程关闭
  848. excel.Quit();
  849. //释放 COM 对象
  850. Marshal.ReleaseComObject(worksheet);
  851. Marshal.ReleaseComObject(workbook);
  852. Marshal.ReleaseComObject(excel);
  853. excel = null;
  854. worksheet = null;
  855. workbook = null;
  856. GC.Collect();//如果不使用这条语,excel会无法正常退出
  857. }
  858. catch (Exception e) { Console.WriteLine("导出excel文件异常," + e.Message); }
  859. }
  860. /// <summary>
  861. /// 动态调整号牌机界面
  862. /// </summary>
  863. /// <param name="sender"></param>
  864. /// <param name="e"></param>
  865. private void flowLayoutPanel1_Resize(object sender, EventArgs e)
  866. {
  867. Control.ControlCollection flpCC = flowLayoutPanel1.Controls;
  868. try
  869. {
  870. int rows = (int)Math.Sqrt(flowLayoutPanel1.Controls.Count + 1);
  871. int columns = (int)(flowLayoutPanel1.Controls.Count * 1.0 / rows + 1);
  872. for (int i = 0; i < flpCC.Count; i++)
  873. {
  874. flpCC[i].Width = (int)(flowLayoutPanel1.Width * 1.0 / (columns * 1.1));
  875. flpCC[i].Height = (int)(flowLayoutPanel1.Height * 1.0 / (rows * 1.15));
  876. Console.WriteLine("flp::" + flowLayoutPanel1.Width + "," + flowLayoutPanel1.Height);
  877. //Console.WriteLine("tlp--"+flpCC[i].Width + "," + flpCC[i].Height);
  878. Control.ControlCollection tlpCC = flpCC[i].Controls;
  879. if (tlpCC.Count == 2)
  880. {
  881. tlpCC[0].Width = (int)(flpCC[i].Width * 0.95);
  882. tlpCC[0].Height = (int)(flpCC[i].Height * 0.79);
  883. tlpCC[1].Width = (int)(flpCC[i].Width * 0.95);
  884. tlpCC[1].Height = (int)(flpCC[i].Height * 0.20);
  885. }
  886. }
  887. }
  888. catch (Exception ex) { Console.WriteLine(ex.Message); }
  889. }
  890. /// <summary>
  891. /// 右上角关闭函数
  892. /// </summary>
  893. /// <param name="sender"></param>
  894. /// <param name="e"></param>
  895. private void centralController_FormClosing(object sender, FormClosingEventArgs e)
  896. {
  897. DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel);
  898. if (result.Equals(DialogResult.OK))
  899. {
  900. Monitor.Monitor.ins.Stop();
  901. closing = true;
  902. }
  903. else
  904. {
  905. e.Cancel = true;
  906. }
  907. }
  908. #region 帮助
  909. /// <summary>
  910. /// 系统信息
  911. /// </summary>
  912. /// <param name="sender"></param>
  913. /// <param name="e"></param>
  914. private void btn_sysInfo_Click(object sender, EventArgs e)
  915. {
  916. FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo());
  917. sysInfo.ShowDialog();
  918. }
  919. /// <summary>
  920. /// 关于我们信息
  921. /// </summary>
  922. /// <param name="sender"></param>
  923. /// <param name="e"></param>
  924. private void btn_about_Click(object sender, EventArgs e)
  925. {
  926. FormAboutUs aboutUs = new FormAboutUs(Monitor.Monitor.GetAboutInfo());
  927. aboutUs.ShowDialog();
  928. }
  929. #endregion
  930. #region 快捷菜单栏
  931. /// <summary>
  932. /// 快捷显示系统信息
  933. /// </summary>
  934. /// <param name="sender"></param>
  935. /// <param name="e"></param>
  936. private void shortcut_sysInfo_Click(object sender, EventArgs e)
  937. {
  938. FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo());
  939. sysInfo.ShowDialog();
  940. }
  941. /// <summary>
  942. /// 快捷关闭
  943. /// </summary>
  944. /// <param name="sender"></param>
  945. /// <param name="e"></param>
  946. private void shortcut_close_Click(object sender, EventArgs e)
  947. {
  948. Close();
  949. }
  950. /// <summary>
  951. /// 终止与启动流程
  952. /// </summary>
  953. /// <param name="sender"></param>
  954. /// <param name="e"></param>
  955. private void shortcut_pause_Click(object sender, EventArgs e)
  956. {
  957. if (shortcut_pause.Enabled)
  958. {
  959. shortcut_pause.Enabled = false;
  960. Monitor.Monitor.globalStatus = !Monitor.Monitor.globalStatus;
  961. if (Monitor.Monitor.globalStatus)
  962. {
  963. shortcut_pause.Image = Properties.Resources.circulate;
  964. shortcut_pause.Text = "点击终止";
  965. }
  966. else
  967. {
  968. shortcut_pause.Image = Properties.Resources.circulate2;
  969. shortcut_pause.Text = "点击启动";
  970. }
  971. Monitor.Monitor.SetNotification(Monitor.Monitor.globalStatus ? "工作正常" : "终止状态");
  972. Task.Factory.StartNew(() =>
  973. {
  974. Thread.Sleep(1500);
  975. try
  976. {
  977. this.Invoke(new Action(() =>
  978. {
  979. shortcut_pause.Enabled = true;
  980. }));
  981. }
  982. catch (Exception ex) { Console.WriteLine(ex.Message); }
  983. });
  984. }
  985. }
  986. /// <summary>
  987. /// 关于我们信息框
  988. /// </summary>
  989. /// <param name="sender"></param>
  990. /// <param name="e"></param>
  991. private void shortcut_about_Click(object sender, EventArgs e)
  992. {
  993. FormAboutUs aboutUs = new FormAboutUs(Monitor.Monitor.GetAboutInfo());
  994. aboutUs.ShowDialog();
  995. }
  996. #endregion
  997. /// <summary>
  998. /// 支付信息查询功能
  999. /// </summary>
  1000. /// <param name="sender"></param>
  1001. /// <param name="e"></param>
  1002. private void btnx_paymentSearch_Click(object sender, EventArgs e)
  1003. {
  1004. DateTime start, end;
  1005. string license = "";
  1006. try
  1007. {
  1008. start = DateTime.Parse(dtp_paymentStartDate.Text);
  1009. end = DateTime.Parse(dtp_paymentEndDate.Text);
  1010. license = tb_paymentLicense.Text;
  1011. }
  1012. catch { MessageBox.Show("日期输入异常,请重新输入"); return; }
  1013. dgvx_payment.Rows.Clear();
  1014. List<object[]> list = Monitor.Monitor.GetParkingRecords(license, start.ToString("yyyy-MM-dd"), end.AddDays(1).ToString("yyyy-MM-dd"));
  1015. List<object[]>.Enumerator listEnumer = list.GetEnumerator();
  1016. while (listEnumer.MoveNext())
  1017. {
  1018. dgvx_payment.Rows.Add(listEnumer.Current);
  1019. //Console.WriteLine(listEnumer.Current);
  1020. }
  1021. }
  1022. /// <summary>
  1023. /// 支付信息修改
  1024. /// </summary>
  1025. /// <param name="sender"></param>
  1026. /// <param name="e"></param>
  1027. private void dgvx_payment_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  1028. {
  1029. int columnIndex = e.ColumnIndex;
  1030. int rowIndex = e.RowIndex;
  1031. if (columnIndex == 8)
  1032. {
  1033. DataGridViewRow selectedRow = dgvx_payment.Rows[rowIndex];
  1034. DataGridViewCellCollection cellCollection = selectedRow.Cells;
  1035. string license = cellCollection[2].Value.ToString();
  1036. string receiptNum = cellCollection[6].Value.ToString();
  1037. string parkingFee = cellCollection[7].Value.ToString();
  1038. string paymentStatus = cellCollection[8].Value.ToString();
  1039. string notification = "确认将号牌 " + license + " 凭证号 " + receiptNum + " 的这条记录中,停车费用与支付状态改为 " + parkingFee + " 与 " + paymentStatus + " 吗?";
  1040. DialogResult dResult = MessageBox.Show(notification, "修改确认", MessageBoxButtons.OKCancel);
  1041. if (dResult == DialogResult.OK)
  1042. {
  1043. Monitor.Monitor.UpdateParkingRecords(license, receiptNum, parkingFee, paymentStatus, true);
  1044. Monitor.Monitor.UpdateParkingRecords(license, receiptNum, parkingFee, paymentStatus, false);
  1045. Monitor.Monitor.UpdateTermFeeStatus(receiptNum);
  1046. Monitor.Monitor.SetNotification("支付信息已修改", TextColor.Warning);
  1047. Log.WriteLog(LogType.process, LogFile.INFO, "手动修改支付信息,号牌:" + license + ",凭证号:" + receiptNum + ",停车费用与支付状态改为:\n" + parkingFee + "与" + paymentStatus);
  1048. }
  1049. }
  1050. }
  1051. private void button5_Click(object sender, EventArgs e)
  1052. {
  1053. MessageBox.Show("当前空闲车位数: "+Monitor.Monitor.ins.GetFreeSpaceCount(1)+"\n预约车位总数; "+ Monitor.Monitor.ins.GetFreeSpaceCount(3));
  1054. }
  1055. /// <summary>
  1056. /// 修改一号终端地感获取值
  1057. /// </summary>
  1058. /// <param name="sender"></param>
  1059. /// <param name="e"></param>
  1060. private void button6_Click(object sender, EventArgs e)
  1061. {
  1062. Monitor.Monitor.ChangeGroundStatus1 = !Monitor.Monitor.ChangeGroundStatus1;
  1063. MessageBox.Show("修改1号地感开关当前状态:"+ Monitor.Monitor.ChangeGroundStatus1);
  1064. }
  1065. }
  1066. }