FormCentralController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. using DevComponents.DotNetBar.Controls;
  2. using PLC_Communication;
  3. using PLCConnector;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Net;
  8. using System.Runtime.InteropServices;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using Terminal;
  13. using Excel = Microsoft.Office.Interop.Excel;
  14. namespace centralController
  15. {
  16. public partial class centralController : Form
  17. {
  18. /// <summary>
  19. /// 窗体初始化
  20. /// </summary>
  21. public centralController()
  22. {
  23. InitializeComponent();
  24. if (Monitor.Monitor.ins == null)
  25. {
  26. Monitor.Monitor.ins = new Monitor.Monitor(flowLayoutPanel1.Handle);
  27. Monitor.Monitor.ins.Start();
  28. }
  29. //进度条测试
  30. Task.Factory.StartNew(() =>
  31. {
  32. int temp = 0;
  33. while (true)
  34. {
  35. if (temp != Monitor.Monitor.initializeState)
  36. {
  37. temp = Monitor.Monitor.initializeState;
  38. progressBar1.Invoke(new Action(() => { progressBar1.Value = temp * 20; }));
  39. }
  40. if (temp == 5)
  41. {
  42. dataGridViewX1.Invoke(new Action(() => { RefreshRecords(null, null); }));
  43. break;
  44. }
  45. Thread.Sleep(300);
  46. }
  47. });
  48. //定时器,定时更新停车记录
  49. System.Windows.Forms.Timer recordsTimer = new System.Windows.Forms.Timer();
  50. recordsTimer.Interval = 5000;
  51. recordsTimer.Tick += new EventHandler(RefreshRecords);
  52. recordsTimer.Start();
  53. //定时器,定时更新提示信息
  54. System.Windows.Forms.Timer NoteTimer = new System.Windows.Forms.Timer();
  55. NoteTimer.Interval = 1000;
  56. NoteTimer.Tick += new EventHandler(UpdateText);
  57. NoteTimer.Start();
  58. //定时器,定时更新系统时间与剩余车位数
  59. System.Windows.Forms.Timer freeSpaceTimer = new System.Windows.Forms.Timer();
  60. freeSpaceTimer.Interval = 1000;
  61. freeSpaceTimer.Tick += new EventHandler(UpdateFreeSpace);
  62. freeSpaceTimer.Start();
  63. UpdateBasicInfo();
  64. tbx_notification.Text = "";
  65. }
  66. #region 测试
  67. /// <summary>
  68. /// 获取号牌测试
  69. /// </summary>
  70. /// <param name="sender"></param>
  71. /// <param name="e"></param>
  72. private void button1_Click(object sender, EventArgs e)
  73. {
  74. string ip = textBox1.Text;
  75. IPAddress temp;
  76. if (IPAddress.TryParse(ip, out temp))
  77. {
  78. string license = "";
  79. Task.Factory.StartNew(() =>
  80. {
  81. license = Monitor.Monitor.numMachineLinker.GetLicensePlate(1);
  82. if (textBox2.InvokeRequired)
  83. {
  84. textBox2.Invoke(new Action(() => { textBox2.Text = license; }));
  85. }
  86. else
  87. {
  88. textBox2.Text = license;
  89. }
  90. });
  91. }
  92. }
  93. #endregion
  94. #region 系统
  95. /// <summary>
  96. /// 结束按钮
  97. /// </summary>
  98. /// <param name="sender"></param>
  99. /// <param name="e"></param>
  100. private void btn_exit_Click(object sender, EventArgs e)
  101. {
  102. //DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel);
  103. //if (result.Equals(DialogResult.OK))
  104. //{
  105. // Monitor.Monitor.ins.Stop();
  106. // Close();
  107. //}
  108. Close();
  109. }
  110. #endregion
  111. #region 文件
  112. /// <summary>
  113. /// 打开日志
  114. /// </summary>
  115. /// <param name="sender"></param>
  116. /// <param name="e"></param>
  117. private void btn_openFile_Click(object sender, EventArgs e)
  118. {
  119. }
  120. /// <summary>
  121. /// 刷新停车记录按钮
  122. /// </summary>
  123. /// <param name="sender"></param>
  124. /// <param name="e"></param>
  125. private void btn_refreshRecords_Click(object sender, EventArgs e)
  126. {
  127. RefreshRecords(null, null);
  128. }
  129. /// <summary>
  130. /// 导出停车记录按钮
  131. /// </summary>
  132. /// <param name="sender"></param>
  133. /// <param name="e"></param>
  134. private void btn_exportRecords_Click(object sender, EventArgs e)
  135. {
  136. Export(dataGridViewX1, Directory.GetCurrentDirectory() + "\\");
  137. Console.WriteLine(Directory.GetCurrentDirectory() + "\\");
  138. }
  139. /// <summary>
  140. /// 显示与设置收费策略
  141. /// </summary>
  142. /// <param name="sender"></param>
  143. /// <param name="e"></param>
  144. private void btn_setScheme_Click(object sender, EventArgs e)
  145. {
  146. Form_paymentScheme form_PaymentScheme = new Form_paymentScheme();
  147. form_PaymentScheme.ShowDialog();
  148. }
  149. #endregion
  150. #region 视图
  151. /// <summary>
  152. /// 车位视图
  153. /// </summary>
  154. /// <param name="sender"></param>
  155. /// <param name="e"></param>
  156. private void btn_parkingSpace_Click(object sender, EventArgs e)
  157. {
  158. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  159. if (dci_ParkingSpace.Visible == false) { dci_ParkingSpace.Visible = true; }
  160. }
  161. /// <summary>
  162. /// 显示号牌机视图
  163. /// </summary>
  164. /// <param name="sender"></param>
  165. /// <param name="e"></param>
  166. private void btn_numMachine_Click(object sender, EventArgs e)
  167. {
  168. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  169. if (dci_NumMachine.Visible == false) { dci_NumMachine.Visible = true; }
  170. }
  171. /// <summary>
  172. /// 支付视图
  173. /// </summary>
  174. /// <param name="sender"></param>
  175. /// <param name="e"></param>
  176. private void btn_payment_Click(object sender, EventArgs e)
  177. {
  178. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  179. if (dci_payment.Visible == false) { dci_payment.Visible = true; }
  180. }
  181. /// <summary>
  182. /// 显示测试选项卡
  183. /// </summary>
  184. /// <param name="sender"></param>
  185. /// <param name="e"></param>
  186. private void btn_test_Click(object sender, EventArgs e)
  187. {
  188. if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
  189. if (dci_Test.Visible == false) { dci_Test.Visible = true; }
  190. }
  191. /// <summary>
  192. /// 终端视图
  193. /// </summary>
  194. /// <param name="sender"></param>
  195. /// <param name="e"></param>
  196. private void btn_terminals_Click(object sender, EventArgs e)
  197. {
  198. if (bar_side.Visible == false) { bar_side.Visible = true; }
  199. if (dci_terminal.Visible == false) { dci_terminal.Visible = true; }
  200. }
  201. /// <summary>
  202. /// 停取记录
  203. /// </summary>
  204. /// <param name="sender"></param>
  205. /// <param name="e"></param>
  206. private void btn_records_Click(object sender, EventArgs e)
  207. {
  208. if (bar_bottom.Visible == false) { bar_bottom.Visible = true; }
  209. if (dci_ParkingRecords.Visible == false) { dci_ParkingRecords.Visible = true; }
  210. }
  211. #endregion
  212. #region 设备
  213. /// <summary>
  214. /// 手动连接PLC
  215. /// </summary>
  216. /// <param name="sender"></param>
  217. /// <param name="e"></param>
  218. private void btn_linkPLC_Click(object sender, EventArgs e)
  219. {
  220. if (!Monitor.Monitor.PLC.isConnected)
  221. {
  222. Monitor.Monitor.PLC.PLCConnect();
  223. }
  224. }
  225. /// <summary>
  226. /// PLC数据调试工具
  227. /// </summary>
  228. /// <param name="sender"></param>
  229. /// <param name="e"></param>
  230. private void btn_PLCConf_Click(object sender, EventArgs e)
  231. {
  232. FormPLCConf formPLCConf = new FormPLCConf();
  233. formPLCConf.Show();
  234. }
  235. /// <summary>
  236. /// 启动号牌机调试工具
  237. /// </summary>
  238. /// <param name="sender"></param>
  239. /// <param name="e"></param>
  240. private void btn_numMachineConf_Click(object sender, EventArgs e)
  241. {
  242. FormModbus form = new FormModbus();
  243. form.Show();
  244. }
  245. #endregion
  246. /// <summary>
  247. /// 定时更新提示信息
  248. /// </summary>
  249. /// <param name="sender"></param>
  250. /// <param name="e"></param>
  251. private void UpdateText(object sender, EventArgs e)
  252. {
  253. tbx_notification.Text = Monitor.Monitor.GetNotification();
  254. tbx_notification.ScrollToCaret();
  255. }
  256. /// <summary>
  257. /// 定时更新时间与空闲车位显示
  258. /// </summary>
  259. /// <param name="sender"></param>
  260. /// <param name="e"></param>
  261. private void UpdateFreeSpace(object sender, EventArgs e)
  262. {
  263. string currentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  264. string freeSpace = "剩余车位:" + Monitor.Monitor.GetFreeSpaceCount();
  265. string bookableSpace = "可预约车位:" + Monitor.Monitor.GetBookableSpaceCount();
  266. lbx_freeSpace.Text = currentTime + "\r\n" + freeSpace + " " + bookableSpace;
  267. }
  268. /// <summary>
  269. /// 刷新停车记录显示
  270. /// </summary>
  271. /// <param name="data"></param>
  272. private void RefreshRecords(object data, EventArgs e)
  273. {
  274. dataGridViewX1.Rows.Clear();
  275. List<object[]> list = Monitor.Monitor.GetParkingRecords();
  276. List<object[]>.Enumerator listEnumer = list.GetEnumerator();
  277. while (listEnumer.MoveNext())
  278. {
  279. dataGridViewX1.Rows.Add(listEnumer.Current);
  280. }
  281. }
  282. /// <summary>
  283. /// 导出excel文件
  284. /// </summary>
  285. /// <param name="dataGridView1"></param>
  286. /// <param name="path">绝对路径,"D:\\abc\\xxx.xls"</param>
  287. private void Export(DataGridViewX dataGridView1, string path)
  288. {
  289. try
  290. {
  291. if (dataGridView1.Rows.Count == 0)
  292. {
  293. return;
  294. }
  295. Excel.Application excel = new Excel.Application();
  296. excel.Visible = false;
  297. Excel.Workbook workbook = excel.Workbooks.Add(true);
  298. Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
  299. for (int i = 0; i < dataGridView1.Columns.Count; i++)
  300. {
  301. if (dataGridView1.Columns[i].Visible == true)
  302. {
  303. worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
  304. }
  305. }
  306. for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
  307. {
  308. System.Windows.Forms.Application.DoEvents();
  309. for (int j = 0; j < dataGridView1.Columns.Count; j++)
  310. {
  311. if (dataGridView1.Columns[j].Visible == true)
  312. {
  313. if (dataGridView1[j, i].ValueType == typeof(string))
  314. {
  315. worksheet.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value.ToString();
  316. }
  317. else
  318. {
  319. worksheet.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
  320. }
  321. }
  322. }
  323. }
  324. //设置禁止弹出保存和覆盖的询问提示框
  325. excel.DisplayAlerts = false;
  326. excel.AlertBeforeOverwriting = false;
  327. worksheet.Cells.Columns.AutoFit();
  328. //保存写入的数据,这里还没有保存到磁盘
  329. workbook.Saved = true;
  330. if (Directory.Exists(path))
  331. {
  332. //设置新建文件路径及名称
  333. string savePath = path + DateTime.Now.ToString("yyyy-MM-dd") + "停车记录.xls";
  334. //创建文件
  335. FileStream file = new FileStream(savePath, FileMode.CreateNew);
  336. //关闭释放流,不然没办法写入数据
  337. file.Close();
  338. file.Dispose();
  339. //保存到指定的路径
  340. workbook.SaveCopyAs(savePath);
  341. }
  342. workbook.Close(false, Type.Missing, Type.Missing);
  343. //确保Excel进程关闭
  344. excel.Quit();
  345. //释放 COM 对象
  346. Marshal.ReleaseComObject(worksheet);
  347. Marshal.ReleaseComObject(workbook);
  348. Marshal.ReleaseComObject(excel);
  349. excel = null;
  350. worksheet = null;
  351. workbook = null;
  352. GC.Collect();//如果不使用这条语,excel会无法正常退出
  353. }
  354. catch (Exception e) { Console.WriteLine("导出excel文件异常," + e.Message); }
  355. }
  356. /// <summary>
  357. /// 更新基本信息
  358. /// </summary>
  359. private void UpdateBasicInfo()
  360. {
  361. //string sysInfo = Monitor.Monitor.GetSysInfo();
  362. //tbx_sysInfo.Text = sysInfo;
  363. //tbx_sysInfo.ScrollToCaret();
  364. }
  365. /// <summary>
  366. /// 动态调整号牌机界面
  367. /// </summary>
  368. /// <param name="sender"></param>
  369. /// <param name="e"></param>
  370. private void flowLayoutPanel1_Resize(object sender, EventArgs e)
  371. {
  372. Control.ControlCollection flpCC = flowLayoutPanel1.Controls;
  373. try
  374. {
  375. for (int i = 0; i < flpCC.Count; i++)
  376. {
  377. flpCC[i].Width = (int)(flowLayoutPanel1.Width * 0.3);
  378. flpCC[i].Height = (int)(flowLayoutPanel1.Height * 0.49);
  379. Console.WriteLine("flp::" + flowLayoutPanel1.Width + "," + flowLayoutPanel1.Height);
  380. //Console.WriteLine("tlp--"+flpCC[i].Width + "," + flpCC[i].Height);
  381. Control.ControlCollection tlpCC = flpCC[i].Controls;
  382. if (tlpCC.Count == 2)
  383. {
  384. tlpCC[0].Width = flpCC[i].Width - 5;
  385. tlpCC[0].Height = (int)(flpCC[i].Height * 0.79);
  386. tlpCC[1].Width = flpCC[i].Width - 5;
  387. tlpCC[1].Height = (int)(flpCC[i].Height * 0.19);
  388. //Console.WriteLine(tlpCC[0].Width + "," + tlpCC[0].Height);
  389. //Console.WriteLine(tlpCC[1].Width + "," + tlpCC[1].Height);
  390. }
  391. }
  392. }
  393. catch (Exception ex) { Console.WriteLine(ex.Message); }
  394. }
  395. /// <summary>
  396. /// 右上角关闭函数
  397. /// </summary>
  398. /// <param name="sender"></param>
  399. /// <param name="e"></param>
  400. private void centralController_FormClosing(object sender, FormClosingEventArgs e)
  401. {
  402. DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel);
  403. if (result.Equals(DialogResult.OK))
  404. {
  405. Monitor.Monitor.ins.Stop();
  406. }
  407. else
  408. {
  409. e.Cancel = true;
  410. }
  411. }
  412. private void btn_sysInfo_Click(object sender, EventArgs e)
  413. {
  414. FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo());
  415. sysInfo.ShowDialog();
  416. }
  417. private void shortcut_sysInfo_Click(object sender, EventArgs e)
  418. {
  419. FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo());
  420. sysInfo.ShowDialog();
  421. }
  422. private void btn_advertUpdate_Click(object sender, EventArgs e)
  423. {
  424. if (Monitor.Monitor.advertMgr != null)
  425. {
  426. if (Monitor.Monitor.advertMgr.UpdateAdvert())
  427. MessageBox.Show("更新成功","提示");
  428. else
  429. MessageBox.Show("更新失败,请稍后重试", "提示");
  430. }
  431. }
  432. private void shortcut_close_Click(object sender, EventArgs e)
  433. {
  434. Close();
  435. }
  436. }
  437. }