FormCentralController.cs 17 KB

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