FormCentralController.cs 17 KB

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