FormCentralController.cs 17 KB

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