FormCentralController.cs 15 KB

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