FormCentralController.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. }
  762. /// <summary>
  763. /// 刷新停车记录显示
  764. /// </summary>
  765. /// <param name="data"></param>
  766. private void RefreshRecords(object data, EventArgs e)
  767. {
  768. dgvx_parkingRecords.Rows.Clear();
  769. List<object[]> list = Monitor.Monitor.GetParkingRecords();
  770. List<object[]>.Enumerator listEnumer = list.GetEnumerator();
  771. while (listEnumer.MoveNext())
  772. {
  773. dgvx_parkingRecords.Rows.Add(listEnumer.Current);
  774. }
  775. }
  776. /// <summary>
  777. /// 导出excel文件
  778. /// </summary>
  779. /// <param name="dataGridView1"></param>
  780. /// <param name="path">绝对路径,"D:\\abc\\xxx.xls"</param>
  781. private void Export(DataGridViewX dataGridView1, string path)
  782. {
  783. try
  784. {
  785. if (dataGridView1.Rows.Count == 0)
  786. {
  787. return;
  788. }
  789. Excel.Application excel = new Excel.Application();
  790. excel.Visible = false;
  791. Excel.Workbook workbook = excel.Workbooks.Add(true);
  792. Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
  793. for (int i = 0; i < dataGridView1.Columns.Count; i++)
  794. {
  795. if (dataGridView1.Columns[i].Visible == true)
  796. {
  797. worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
  798. }
  799. }
  800. for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
  801. {
  802. System.Windows.Forms.Application.DoEvents();
  803. for (int j = 0; j < dataGridView1.Columns.Count; j++)
  804. {
  805. if (dataGridView1.Columns[j].Visible == true)
  806. {
  807. if (dataGridView1[j, i].ValueType == typeof(string))
  808. {
  809. worksheet.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value.ToString();
  810. }
  811. else
  812. {
  813. worksheet.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
  814. }
  815. }
  816. }
  817. }
  818. //设置禁止弹出保存和覆盖的询问提示框
  819. excel.DisplayAlerts = false;
  820. excel.AlertBeforeOverwriting = false;
  821. worksheet.Cells.Columns.AutoFit();
  822. //保存写入的数据,这里还没有保存到磁盘
  823. workbook.Saved = true;
  824. if (Directory.Exists(path))
  825. {
  826. //设置新建文件路径及名称
  827. string savePath = path + DateTime.Now.ToString("yyyy-MM-dd") + "停车记录.xls";
  828. //创建文件
  829. FileStream file = new FileStream(savePath, FileMode.CreateNew);
  830. //关闭释放流,不然没办法写入数据
  831. file.Close();
  832. file.Dispose();
  833. //保存到指定的路径
  834. workbook.SaveCopyAs(savePath);
  835. }
  836. workbook.Close(false, Type.Missing, Type.Missing);
  837. //确保Excel进程关闭
  838. excel.Quit();
  839. //释放 COM 对象
  840. Marshal.ReleaseComObject(worksheet);
  841. Marshal.ReleaseComObject(workbook);
  842. Marshal.ReleaseComObject(excel);
  843. excel = null;
  844. worksheet = null;
  845. workbook = null;
  846. GC.Collect();//如果不使用这条语,excel会无法正常退出
  847. }
  848. catch (Exception e) { Console.WriteLine("导出excel文件异常," + e.Message); }
  849. }
  850. /// <summary>
  851. /// 动态调整号牌机界面
  852. /// </summary>
  853. /// <param name="sender"></param>
  854. /// <param name="e"></param>
  855. private void flowLayoutPanel1_Resize(object sender, EventArgs e)
  856. {
  857. Control.ControlCollection flpCC = flowLayoutPanel1.Controls;
  858. try
  859. {
  860. int rows = (int)Math.Sqrt(flowLayoutPanel1.Controls.Count + 1);
  861. int columns = (int)(flowLayoutPanel1.Controls.Count * 1.0 / rows + 1);
  862. for (int i = 0; i < flpCC.Count; i++)
  863. {
  864. flpCC[i].Width = (int)(flowLayoutPanel1.Width * 1.0 / (columns * 1.1));
  865. flpCC[i].Height = (int)(flowLayoutPanel1.Height * 1.0 / (rows * 1.15));
  866. Console.WriteLine("flp::" + flowLayoutPanel1.Width + "," + flowLayoutPanel1.Height);
  867. //Console.WriteLine("tlp--"+flpCC[i].Width + "," + flpCC[i].Height);
  868. Control.ControlCollection tlpCC = flpCC[i].Controls;
  869. if (tlpCC.Count == 2)
  870. {
  871. tlpCC[0].Width = (int)(flpCC[i].Width * 0.95);
  872. tlpCC[0].Height = (int)(flpCC[i].Height * 0.79);
  873. tlpCC[1].Width = (int)(flpCC[i].Width * 0.95);
  874. tlpCC[1].Height = (int)(flpCC[i].Height * 0.20);
  875. }
  876. }
  877. }
  878. catch (Exception ex) { Console.WriteLine(ex.Message); }
  879. }
  880. /// <summary>
  881. /// 右上角关闭函数
  882. /// </summary>
  883. /// <param name="sender"></param>
  884. /// <param name="e"></param>
  885. private void centralController_FormClosing(object sender, FormClosingEventArgs e)
  886. {
  887. DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel);
  888. if (result.Equals(DialogResult.OK))
  889. {
  890. Monitor.Monitor.ins.Stop();
  891. closing = true;
  892. }
  893. else
  894. {
  895. e.Cancel = true;
  896. }
  897. }
  898. #region 帮助
  899. /// <summary>
  900. /// 系统信息
  901. /// </summary>
  902. /// <param name="sender"></param>
  903. /// <param name="e"></param>
  904. private void btn_sysInfo_Click(object sender, EventArgs e)
  905. {
  906. FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo());
  907. sysInfo.ShowDialog();
  908. }
  909. /// <summary>
  910. /// 关于我们信息
  911. /// </summary>
  912. /// <param name="sender"></param>
  913. /// <param name="e"></param>
  914. private void btn_about_Click(object sender, EventArgs e)
  915. {
  916. FormAboutUs aboutUs = new FormAboutUs(Monitor.Monitor.GetAboutInfo());
  917. aboutUs.ShowDialog();
  918. }
  919. #endregion
  920. #region 快捷菜单栏
  921. /// <summary>
  922. /// 快捷显示系统信息
  923. /// </summary>
  924. /// <param name="sender"></param>
  925. /// <param name="e"></param>
  926. private void shortcut_sysInfo_Click(object sender, EventArgs e)
  927. {
  928. FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo());
  929. sysInfo.ShowDialog();
  930. }
  931. /// <summary>
  932. /// 快捷关闭
  933. /// </summary>
  934. /// <param name="sender"></param>
  935. /// <param name="e"></param>
  936. private void shortcut_close_Click(object sender, EventArgs e)
  937. {
  938. Close();
  939. }
  940. /// <summary>
  941. /// 终止与启动流程
  942. /// </summary>
  943. /// <param name="sender"></param>
  944. /// <param name="e"></param>
  945. private void shortcut_pause_Click(object sender, EventArgs e)
  946. {
  947. if (shortcut_pause.Enabled)
  948. {
  949. shortcut_pause.Enabled = false;
  950. Monitor.Monitor.globalStatus = !Monitor.Monitor.globalStatus;
  951. if (Monitor.Monitor.globalStatus)
  952. {
  953. shortcut_pause.Image = Properties.Resources.circulate;
  954. shortcut_pause.Text = "点击终止";
  955. }
  956. else
  957. {
  958. shortcut_pause.Image = Properties.Resources.circulate2;
  959. shortcut_pause.Text = "点击启动";
  960. }
  961. Monitor.Monitor.SetNotification(Monitor.Monitor.globalStatus ? "工作正常" : "终止状态");
  962. Task.Factory.StartNew(() =>
  963. {
  964. Thread.Sleep(1500);
  965. try
  966. {
  967. this.Invoke(new Action(() =>
  968. {
  969. shortcut_pause.Enabled = true;
  970. }));
  971. }
  972. catch (Exception ex) { Console.WriteLine(ex.Message); }
  973. });
  974. }
  975. }
  976. /// <summary>
  977. /// 关于我们信息框
  978. /// </summary>
  979. /// <param name="sender"></param>
  980. /// <param name="e"></param>
  981. private void shortcut_about_Click(object sender, EventArgs e)
  982. {
  983. FormAboutUs aboutUs = new FormAboutUs(Monitor.Monitor.GetAboutInfo());
  984. aboutUs.ShowDialog();
  985. }
  986. #endregion
  987. /// <summary>
  988. /// 支付信息查询功能
  989. /// </summary>
  990. /// <param name="sender"></param>
  991. /// <param name="e"></param>
  992. private void btnx_paymentSearch_Click(object sender, EventArgs e)
  993. {
  994. DateTime start, end;
  995. string license = "";
  996. try
  997. {
  998. start = DateTime.Parse(dtp_paymentStartDate.Text);
  999. end = DateTime.Parse(dtp_paymentEndDate.Text);
  1000. license = tb_paymentLicense.Text;
  1001. }
  1002. catch { MessageBox.Show("日期输入异常,请重新输入"); return; }
  1003. dgvx_payment.Rows.Clear();
  1004. List<object[]> list = Monitor.Monitor.GetParkingRecords(license, start.ToString("yyyy-MM-dd"), end.AddDays(1).ToString("yyyy-MM-dd"));
  1005. List<object[]>.Enumerator listEnumer = list.GetEnumerator();
  1006. while (listEnumer.MoveNext())
  1007. {
  1008. dgvx_payment.Rows.Add(listEnumer.Current);
  1009. //Console.WriteLine(listEnumer.Current);
  1010. }
  1011. }
  1012. /// <summary>
  1013. /// 支付信息修改
  1014. /// </summary>
  1015. /// <param name="sender"></param>
  1016. /// <param name="e"></param>
  1017. private void dgvx_payment_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  1018. {
  1019. int columnIndex = e.ColumnIndex;
  1020. int rowIndex = e.RowIndex;
  1021. if (columnIndex == 8)
  1022. {
  1023. DataGridViewRow selectedRow = dgvx_payment.Rows[rowIndex];
  1024. DataGridViewCellCollection cellCollection = selectedRow.Cells;
  1025. string license = cellCollection[2].Value.ToString();
  1026. string receiptNum = cellCollection[6].Value.ToString();
  1027. string parkingFee = cellCollection[7].Value.ToString();
  1028. string paymentStatus = cellCollection[8].Value.ToString();
  1029. string notification = "确认将号牌 " + license + " 凭证号 " + receiptNum + " 的这条记录中,停车费用与支付状态改为 " + parkingFee + " 与 " + paymentStatus + " 吗?";
  1030. DialogResult dResult = MessageBox.Show(notification, "修改确认", MessageBoxButtons.OKCancel);
  1031. if (dResult == DialogResult.OK)
  1032. {
  1033. Monitor.Monitor.UpdateParkingRecords(license, receiptNum, parkingFee, paymentStatus, true);
  1034. Monitor.Monitor.UpdateParkingRecords(license, receiptNum, parkingFee, paymentStatus, false);
  1035. Monitor.Monitor.UpdateTermFeeStatus(receiptNum);
  1036. Monitor.Monitor.SetNotification("支付信息已修改", TextColor.Warning);
  1037. Log.WriteLog(LogType.process, LogFile.INFO, "手动修改支付信息,号牌:" + license + ",凭证号:" + receiptNum + ",停车费用与支付状态改为:\n" + parkingFee + "与" + paymentStatus);
  1038. }
  1039. }
  1040. }
  1041. }
  1042. }