FormCentralController.cs 15 KB

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