FormCentralController.cs 15 KB

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