FormCentralController.cs 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. }
  449. /// <summary>
  450. /// 终端id获取测试
  451. /// </summary>
  452. /// <param name="sender"></param>
  453. /// <param name="e"></param>
  454. private void button3_Click(object sender, EventArgs e)
  455. {
  456. string license = textBox1.Text;
  457. int id = Monitor.Monitor.numMachineLinker.GetLicenseID(license);
  458. textBox2.Text = "终端" + id.ToString();
  459. }
  460. /// <summary>
  461. /// tableLayoutPanel测试
  462. /// </summary>
  463. /// <param name="sender"></param>
  464. /// <param name="e"></param>
  465. private void button4_Click(object sender, EventArgs e)
  466. {
  467. Control c = tableLayoutPanel3.GetControlFromPosition(0, 0);
  468. Console.WriteLine(tableLayoutPanel3.Controls.Count);
  469. Control c1 = tableLayoutPanel3.GetControlFromPosition(0, 1);
  470. Console.WriteLine(c1.Controls.Count);
  471. Console.WriteLine("...");
  472. }
  473. #endregion
  474. #region 系统
  475. /// <summary>
  476. /// 结束按钮
  477. /// </summary>
  478. /// <param name="sender"></param>
  479. /// <param name="e"></param>
  480. private void btn_exit_Click(object sender, EventArgs e)
  481. {
  482. //DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel);
  483. //if (result.Equals(DialogResult.OK))
  484. //{
  485. // Monitor.Monitor.ins.Stop();
  486. // Close();
  487. //}
  488. Close();
  489. }
  490. #endregion
  491. #region 文件
  492. /// <summary>
  493. /// 打开日志
  494. /// </summary>
  495. /// <param name="sender"></param>
  496. /// <param name="e"></param>
  497. private void btn_openFile_Click(object sender, EventArgs e)
  498. {
  499. }
  500. /// <summary>
  501. /// 刷新停车记录按钮
  502. /// </summary>
  503. /// <param name="sender"></param>
  504. /// <param name="e"></param>
  505. private void btn_recordManagement_Click(object sender, EventArgs e)
  506. {
  507. FormRecordsManager formRecordsManager = new FormRecordsManager();
  508. formRecordsManager.ShowDialog();
  509. }
  510. /// <summary>
  511. /// 导出停车记录按钮
  512. /// </summary>
  513. /// <param name="sender"></param>
  514. /// <param name="e"></param>
  515. private void btn_exportRecords_Click(object sender, EventArgs e)
  516. {
  517. Export(dgvx_parkingRecords, Directory.GetCurrentDirectory() + "\\");
  518. Console.WriteLine(Directory.GetCurrentDirectory() + "\\");
  519. }
  520. /// <summary>
  521. /// 显示与设置收费策略
  522. /// </summary>
  523. /// <param name="sender"></param>
  524. /// <param name="e"></param>
  525. private void btn_setScheme_Click(object sender, EventArgs e)
  526. {
  527. FormPaymentScheme form_PaymentScheme = new FormPaymentScheme();
  528. form_PaymentScheme.ShowDialog();
  529. }
  530. /// <summary>
  531. /// 手动更新广告
  532. /// </summary>
  533. /// <param name="sender"></param>
  534. /// <param name="e"></param>
  535. private void btn_advertUpdate_Click(object sender, EventArgs e)
  536. {
  537. Task.Factory.StartNew(() =>
  538. {
  539. MessageBox.Show("更新中,请稍候。", "提示");
  540. string adAlert = "";
  541. if (Monitor.Monitor.advertMgr != null)
  542. {
  543. if (Monitor.Monitor.advertMgr.UpdateAdvert(out adAlert))
  544. MessageBox.Show("更新成功\n" + adAlert, "提示");
  545. else
  546. MessageBox.Show("更新失败,请稍后重试", "提示");
  547. }
  548. });
  549. }
  550. /// <summary>
  551. /// 系统配置
  552. /// </summary>
  553. /// <param name="sender"></param>
  554. /// <param name="e"></param>
  555. private void btn_config_Click(object sender, EventArgs e)
  556. {
  557. FormSysConfig formSysConfig = new FormSysConfig();
  558. formSysConfig.ShowDialog();
  559. }
  560. #endregion
  561. #region 视图
  562. /// <summary>
  563. /// 车位视图
  564. /// </summary>
  565. /// <param name="sender"></param>
  566. /// <param name="e"></param>
  567. private void btn_parkingSpace_Click(object sender, EventArgs e)
  568. {
  569. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  570. if (dci_ParkingSpace.Visible == false) { dci_ParkingSpace.Visible = true; }
  571. }
  572. /// <summary>
  573. /// 显示号牌机视图
  574. /// </summary>
  575. /// <param name="sender"></param>
  576. /// <param name="e"></param>
  577. private void btn_numMachine_Click(object sender, EventArgs e)
  578. {
  579. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  580. if (dci_NumMachine.Visible == false) { dci_NumMachine.Visible = true; }
  581. }
  582. /// <summary>
  583. /// 支付视图
  584. /// </summary>
  585. /// <param name="sender"></param>
  586. /// <param name="e"></param>
  587. private void btn_payment_Click(object sender, EventArgs e)
  588. {
  589. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  590. if (dci_payment.Visible == false) { dci_payment.Visible = true; }
  591. }
  592. /// <summary>
  593. /// 显示测试选项卡
  594. /// </summary>
  595. /// <param name="sender"></param>
  596. /// <param name="e"></param>
  597. private void btn_test_Click(object sender, EventArgs e)
  598. {
  599. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  600. if (dci_Test.Visible == false) { dci_Test.Visible = true; }
  601. }
  602. /// <summary>
  603. /// 终端视图
  604. /// </summary>
  605. /// <param name="sender"></param>
  606. /// <param name="e"></param>
  607. private void btn_terminals_Click(object sender, EventArgs e)
  608. {
  609. if (bar_side.Visible == false) { bar_side.Visible = true; }
  610. if (dci_terminal.Visible == false) { dci_terminal.Visible = true; }
  611. }
  612. /// <summary>
  613. /// 停取记录
  614. /// </summary>
  615. /// <param name="sender"></param>
  616. /// <param name="e"></param>
  617. private void btn_records_Click(object sender, EventArgs e)
  618. {
  619. if (bar_bottom.Visible == false) { bar_bottom.Visible = true; }
  620. if (dci_ParkingRecords.Visible == false) { dci_ParkingRecords.Visible = true; }
  621. }
  622. #endregion
  623. #region 设备
  624. /// <summary>
  625. /// 手动连接PLC
  626. /// </summary>
  627. /// <param name="sender"></param>
  628. /// <param name="e"></param>
  629. private void btn_linkPLC_Click(object sender, EventArgs e)
  630. {
  631. if (Monitor.Monitor.PLC != null)
  632. {
  633. if (!Monitor.Monitor.PLC.isConnected)
  634. {
  635. Monitor.Monitor.SetNotification("检测到PLC掉线,尝试重连", TextColor.Info);
  636. Monitor.Monitor.PLC.PLCConnect();
  637. Monitor.Monitor.updateCount = true;
  638. }
  639. else
  640. {
  641. //List<object> received = Monitor.Monitor.PLC.ReadFromPLC(PLCDataType.terminal, 0);
  642. //if (received.Count != Monitor.Monitor.plcTerminalCount)
  643. //{
  644. // Monitor.Monitor.SetNotification("检测到PLC数据异常", TextColor.Warning);
  645. Monitor.Monitor.SetNotification("尝试关闭并重新建立与PLC的连接", TextColor.Info);
  646. Monitor.Monitor.PLC.PLCDisconnect();
  647. Monitor.Monitor.PLC.PLCConnect();
  648. Monitor.Monitor.updateCount = true;
  649. //}
  650. //else
  651. //{
  652. // Monitor.Monitor.SetNotification("PLC连接正常,无需连接", TextColor.Info);
  653. //}
  654. }
  655. }
  656. }
  657. /// <summary>
  658. /// 手动连接Web
  659. /// </summary>
  660. /// <param name="sender"></param>
  661. /// <param name="e"></param>
  662. private void btn_linkWeb_Click(object sender, EventArgs e)
  663. {
  664. Task.Factory.StartNew(() =>
  665. {
  666. Monitor.Monitor.webServer.Connect();
  667. });
  668. }
  669. /// <summary>
  670. /// PLC数据调试工具
  671. /// </summary>
  672. /// <param name="sender"></param>
  673. /// <param name="e"></param>
  674. private void btn_PLCConf_Click(object sender, EventArgs e)
  675. {
  676. FormPLCConf formPLCConf = new FormPLCConf();
  677. formPLCConf.Show();
  678. }
  679. /// <summary>
  680. /// 启动号牌机调试工具
  681. /// </summary>
  682. /// <param name="sender"></param>
  683. /// <param name="e"></param>
  684. private void btn_numMachineConf_Click(object sender, EventArgs e)
  685. {
  686. FormModbus form = new FormModbus();
  687. form.Show();
  688. }
  689. #endregion
  690. /// <summary>
  691. /// 更新提示信息,将被从外部调用
  692. /// </summary>
  693. /// <param name="sender"></param>
  694. /// <param name="e"></param>
  695. public void UpdateText(string str, TextColor tc, int maxlines)
  696. {
  697. if (!rtb_notification.Focused)
  698. rtb_notification.ScrollToCaret();
  699. string[] strs = rtb_notification.Text.Split('\n');
  700. //Console.WriteLine(strs.ToString());
  701. if (strs.Length > maxlines * 2)
  702. {
  703. string[] newStrs = new string[maxlines];
  704. Array.Copy(strs, strs.Length - maxlines, newStrs, 0, maxlines);
  705. string temp = String.Join("\n", newStrs);
  706. rtb_notification.Text = temp;
  707. }
  708. if (str == "clear")
  709. {
  710. rtb_notification.Clear();
  711. }
  712. else
  713. {
  714. //Console.WriteLine("---------------------");
  715. //Console.WriteLine(rtb_notification.SelectionStart);
  716. string appendingText = str;
  717. rtb_notification.AppendText(appendingText);
  718. string currentTxt = rtb_notification.Text;
  719. string temp = currentTxt.Substring(0, currentTxt.Length - appendingText.Length + 2);
  720. int index = temp.LastIndexOf("\n", temp.Length);
  721. //rtb_notification.Select(index < 0 ? 0 : index, appendingText.Length - 1);
  722. rtb_notification.Select(index < 0 ? 0 : index, appendingText.Length - 2);
  723. //Console.WriteLine("******************");
  724. //Console.WriteLine("选中 "+rtb_notification.SelectedText);
  725. //Console.WriteLine("******************");
  726. //rtb_notification.SelectionFont = new Font(rtb_notification.SelectionFont, FontStyle.Bold);
  727. switch (tc)
  728. {
  729. case TextColor.Log:
  730. rtb_notification.SelectionColor = Color.Gray;
  731. break;
  732. case TextColor.Info:
  733. rtb_notification.SelectionColor = Color.Black;
  734. break;
  735. case TextColor.Warning:
  736. rtb_notification.SelectionColor = Color.Orange;
  737. break;
  738. case TextColor.Error:
  739. rtb_notification.SelectionColor = Color.Red;
  740. break;
  741. }
  742. rtb_notification.Select(index + appendingText.Length - 1, 0);
  743. if (!rtb_notification.Focused)
  744. rtb_notification.ScrollToCaret();
  745. }
  746. //rtb_notification.Text = Monitor.Monitor.ins.GetNotification();
  747. }
  748. /// <summary>
  749. /// 定时更新时间与空闲车位显示,以及设备连接状态
  750. /// </summary>
  751. /// <param name="sender"></param>
  752. /// <param name="e"></param>
  753. private void UpdateFreeSpace(object sender, EventArgs e)
  754. {
  755. string currentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  756. string freeSpace = "剩余车位:" + Monitor.Monitor.ins.GetFreeSpaceCount(0);
  757. string bookableSpace = "可预约车位:" + Monitor.Monitor.ins.GetFreeSpaceCount(2);
  758. lbx_freeSpace.Text = currentTime + "\r\n" + freeSpace + " " + bookableSpace;
  759. lbx_PLCStatus.SymbolColor = (Monitor.Monitor.PLC != null && Monitor.Monitor.PLC.isConnected) ? Color.Green : Color.OrangeRed;
  760. lbx_webStatus.SymbolColor = (Monitor.Monitor.webServer != null && Monitor.Monitor.webServer.GetConnectStatus()) ? Color.Green : Color.OrangeRed;
  761. lbx_led1Status.SymbolColor = (Monitor.Monitor.allInOneMachine != null && Monitor.Monitor.allInOneMachine.boardList.Count > 0 && Monitor.Monitor.allInOneMachine.boardList[0].connected) ? Color.Green : Color.OrangeRed;
  762. }
  763. /// <summary>
  764. /// 刷新停车记录显示
  765. /// </summary>
  766. /// <param name="data"></param>
  767. private void RefreshRecords(object data, EventArgs e)
  768. {
  769. dgvx_parkingRecords.Rows.Clear();
  770. List<object[]> list = Monitor.Monitor.GetParkingRecords();
  771. List<object[]>.Enumerator listEnumer = list.GetEnumerator();
  772. while (listEnumer.MoveNext())
  773. {
  774. dgvx_parkingRecords.Rows.Add(listEnumer.Current);
  775. }
  776. }
  777. /// <summary>
  778. /// 导出excel文件
  779. /// </summary>
  780. /// <param name="dataGridView1"></param>
  781. /// <param name="path">绝对路径,"D:\\abc\\xxx.xls"</param>
  782. private void Export(DataGridViewX dataGridView1, string path)
  783. {
  784. try
  785. {
  786. if (dataGridView1.Rows.Count == 0)
  787. {
  788. return;
  789. }
  790. Excel.Application excel = new Excel.Application();
  791. excel.Visible = false;
  792. Excel.Workbook workbook = excel.Workbooks.Add(true);
  793. Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
  794. for (int i = 0; i < dataGridView1.Columns.Count; i++)
  795. {
  796. if (dataGridView1.Columns[i].Visible == true)
  797. {
  798. worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
  799. }
  800. }
  801. for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
  802. {
  803. System.Windows.Forms.Application.DoEvents();
  804. for (int j = 0; j < dataGridView1.Columns.Count; j++)
  805. {
  806. if (dataGridView1.Columns[j].Visible == true)
  807. {
  808. if (dataGridView1[j, i].ValueType == typeof(string))
  809. {
  810. worksheet.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value.ToString();
  811. }
  812. else
  813. {
  814. worksheet.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
  815. }
  816. }
  817. }
  818. }
  819. //设置禁止弹出保存和覆盖的询问提示框
  820. excel.DisplayAlerts = false;
  821. excel.AlertBeforeOverwriting = false;
  822. worksheet.Cells.Columns.AutoFit();
  823. //保存写入的数据,这里还没有保存到磁盘
  824. workbook.Saved = true;
  825. if (Directory.Exists(path))
  826. {
  827. //设置新建文件路径及名称
  828. string savePath = path + DateTime.Now.ToString("yyyy-MM-dd") + "停车记录.xls";
  829. //创建文件
  830. FileStream file = new FileStream(savePath, FileMode.CreateNew);
  831. //关闭释放流,不然没办法写入数据
  832. file.Close();
  833. file.Dispose();
  834. //保存到指定的路径
  835. workbook.SaveCopyAs(savePath);
  836. }
  837. workbook.Close(false, Type.Missing, Type.Missing);
  838. //确保Excel进程关闭
  839. excel.Quit();
  840. //释放 COM 对象
  841. Marshal.ReleaseComObject(worksheet);
  842. Marshal.ReleaseComObject(workbook);
  843. Marshal.ReleaseComObject(excel);
  844. excel = null;
  845. worksheet = null;
  846. workbook = null;
  847. GC.Collect();//如果不使用这条语,excel会无法正常退出
  848. }
  849. catch (Exception e) { Console.WriteLine("导出excel文件异常," + e.Message); }
  850. }
  851. /// <summary>
  852. /// 动态调整号牌机界面
  853. /// </summary>
  854. /// <param name="sender"></param>
  855. /// <param name="e"></param>
  856. private void flowLayoutPanel1_Resize(object sender, EventArgs e)
  857. {
  858. Control.ControlCollection flpCC = flowLayoutPanel1.Controls;
  859. try
  860. {
  861. int rows = (int)Math.Sqrt(flowLayoutPanel1.Controls.Count + 1);
  862. int columns = (int)(flowLayoutPanel1.Controls.Count * 1.0 / rows + 1);
  863. for (int i = 0; i < flpCC.Count; i++)
  864. {
  865. flpCC[i].Width = (int)(flowLayoutPanel1.Width * 1.0 / (columns * 1.1));
  866. flpCC[i].Height = (int)(flowLayoutPanel1.Height * 1.0 / (rows * 1.15));
  867. Console.WriteLine("flp::" + flowLayoutPanel1.Width + "," + flowLayoutPanel1.Height);
  868. //Console.WriteLine("tlp--"+flpCC[i].Width + "," + flpCC[i].Height);
  869. Control.ControlCollection tlpCC = flpCC[i].Controls;
  870. if (tlpCC.Count == 2)
  871. {
  872. tlpCC[0].Width = (int)(flpCC[i].Width * 0.95);
  873. tlpCC[0].Height = (int)(flpCC[i].Height * 0.79);
  874. tlpCC[1].Width = (int)(flpCC[i].Width * 0.95);
  875. tlpCC[1].Height = (int)(flpCC[i].Height * 0.20);
  876. }
  877. }
  878. }
  879. catch (Exception ex) { Console.WriteLine(ex.Message); }
  880. }
  881. /// <summary>
  882. /// 右上角关闭函数
  883. /// </summary>
  884. /// <param name="sender"></param>
  885. /// <param name="e"></param>
  886. private void centralController_FormClosing(object sender, FormClosingEventArgs e)
  887. {
  888. DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel);
  889. if (result.Equals(DialogResult.OK))
  890. {
  891. Monitor.Monitor.ins.Stop();
  892. closing = true;
  893. }
  894. else
  895. {
  896. e.Cancel = true;
  897. }
  898. }
  899. #region 帮助
  900. /// <summary>
  901. /// 系统信息
  902. /// </summary>
  903. /// <param name="sender"></param>
  904. /// <param name="e"></param>
  905. private void btn_sysInfo_Click(object sender, EventArgs e)
  906. {
  907. FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo());
  908. sysInfo.ShowDialog();
  909. }
  910. /// <summary>
  911. /// 关于我们信息
  912. /// </summary>
  913. /// <param name="sender"></param>
  914. /// <param name="e"></param>
  915. private void btn_about_Click(object sender, EventArgs e)
  916. {
  917. FormAboutUs aboutUs = new FormAboutUs(Monitor.Monitor.GetAboutInfo());
  918. aboutUs.ShowDialog();
  919. }
  920. #endregion
  921. #region 快捷菜单栏
  922. /// <summary>
  923. /// 快捷显示系统信息
  924. /// </summary>
  925. /// <param name="sender"></param>
  926. /// <param name="e"></param>
  927. private void shortcut_sysInfo_Click(object sender, EventArgs e)
  928. {
  929. FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo());
  930. sysInfo.ShowDialog();
  931. }
  932. /// <summary>
  933. /// 快捷关闭
  934. /// </summary>
  935. /// <param name="sender"></param>
  936. /// <param name="e"></param>
  937. private void shortcut_close_Click(object sender, EventArgs e)
  938. {
  939. Close();
  940. }
  941. /// <summary>
  942. /// 终止与启动流程
  943. /// </summary>
  944. /// <param name="sender"></param>
  945. /// <param name="e"></param>
  946. private void shortcut_pause_Click(object sender, EventArgs e)
  947. {
  948. if (shortcut_pause.Enabled)
  949. {
  950. shortcut_pause.Enabled = false;
  951. Monitor.Monitor.globalStatus = !Monitor.Monitor.globalStatus;
  952. if (Monitor.Monitor.globalStatus)
  953. {
  954. shortcut_pause.Image = Properties.Resources.circulate;
  955. shortcut_pause.Text = "点击终止";
  956. }
  957. else
  958. {
  959. shortcut_pause.Image = Properties.Resources.circulate2;
  960. shortcut_pause.Text = "点击启动";
  961. }
  962. Monitor.Monitor.SetNotification(Monitor.Monitor.globalStatus ? "工作正常" : "终止状态");
  963. Task.Factory.StartNew(() =>
  964. {
  965. Thread.Sleep(1500);
  966. try
  967. {
  968. this.Invoke(new Action(() =>
  969. {
  970. shortcut_pause.Enabled = true;
  971. }));
  972. }
  973. catch (Exception ex) { Console.WriteLine(ex.Message); }
  974. });
  975. }
  976. }
  977. /// <summary>
  978. /// 关于我们信息框
  979. /// </summary>
  980. /// <param name="sender"></param>
  981. /// <param name="e"></param>
  982. private void shortcut_about_Click(object sender, EventArgs e)
  983. {
  984. FormAboutUs aboutUs = new FormAboutUs(Monitor.Monitor.GetAboutInfo());
  985. aboutUs.ShowDialog();
  986. }
  987. #endregion
  988. /// <summary>
  989. /// 支付信息查询功能
  990. /// </summary>
  991. /// <param name="sender"></param>
  992. /// <param name="e"></param>
  993. private void btnx_paymentSearch_Click(object sender, EventArgs e)
  994. {
  995. DateTime start, end;
  996. string license = "";
  997. try
  998. {
  999. start = DateTime.Parse(dtp_paymentStartDate.Text);
  1000. end = DateTime.Parse(dtp_paymentEndDate.Text);
  1001. license = tb_paymentLicense.Text;
  1002. }
  1003. catch { MessageBox.Show("日期输入异常,请重新输入"); return; }
  1004. dgvx_payment.Rows.Clear();
  1005. List<object[]> list = Monitor.Monitor.GetParkingRecords(license, start.ToString("yyyy-MM-dd"), end.AddDays(1).ToString("yyyy-MM-dd"));
  1006. List<object[]>.Enumerator listEnumer = list.GetEnumerator();
  1007. while (listEnumer.MoveNext())
  1008. {
  1009. dgvx_payment.Rows.Add(listEnumer.Current);
  1010. //Console.WriteLine(listEnumer.Current);
  1011. }
  1012. }
  1013. /// <summary>
  1014. /// 支付信息修改
  1015. /// </summary>
  1016. /// <param name="sender"></param>
  1017. /// <param name="e"></param>
  1018. private void dgvx_payment_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  1019. {
  1020. int columnIndex = e.ColumnIndex;
  1021. int rowIndex = e.RowIndex;
  1022. if (columnIndex == 8)
  1023. {
  1024. DataGridViewRow selectedRow = dgvx_payment.Rows[rowIndex];
  1025. DataGridViewCellCollection cellCollection = selectedRow.Cells;
  1026. string license = cellCollection[2].Value.ToString();
  1027. string receiptNum = cellCollection[6].Value.ToString();
  1028. string parkingFee = cellCollection[7].Value.ToString();
  1029. string paymentStatus = cellCollection[8].Value.ToString();
  1030. string notification = "确认将号牌 " + license + " 凭证号 " + receiptNum + " 的这条记录中,停车费用与支付状态改为 " + parkingFee + " 与 " + paymentStatus + " 吗?";
  1031. DialogResult dResult = MessageBox.Show(notification, "修改确认", MessageBoxButtons.OKCancel);
  1032. if (dResult == DialogResult.OK)
  1033. {
  1034. Monitor.Monitor.UpdateParkingRecords(license, receiptNum, parkingFee, paymentStatus, true);
  1035. Monitor.Monitor.UpdateParkingRecords(license, receiptNum, parkingFee, paymentStatus, false);
  1036. Monitor.Monitor.UpdateTermFeeStatus(receiptNum);
  1037. Monitor.Monitor.SetNotification("支付信息已修改", TextColor.Warning);
  1038. Log.WriteLog(LogType.process, LogFile.INFO, "手动修改支付信息,号牌:" + license + ",凭证号:" + receiptNum + ",停车费用与支付状态改为:\n" + parkingFee + "与" + paymentStatus);
  1039. }
  1040. }
  1041. }
  1042. }
  1043. }