FormCentralController.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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) { break; }
  38. Thread.Sleep(200);
  39. }
  40. });
  41. //TimerCallback tc = new TimerCallback(RefreshRecords);
  42. //System.Threading.Timer t = new System.Threading.Timer(tc,null,5000,1000);
  43. System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
  44. timer.Interval = 5000;
  45. timer.Tick += new EventHandler(RefreshRecords);
  46. timer.Start();
  47. UpdateBasicInfo();
  48. }
  49. /// <summary>
  50. /// 结束按钮
  51. /// </summary>
  52. /// <param name="sender"></param>
  53. /// <param name="e"></param>
  54. private void btn_exit_Click(object sender, EventArgs e)
  55. {
  56. DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel);
  57. if (result.Equals(DialogResult.OK))
  58. {
  59. Monitor.Monitor.ins.Stop();
  60. Close();
  61. }
  62. }
  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. /// <summary>
  90. /// 手动连接PLC
  91. /// </summary>
  92. /// <param name="sender"></param>
  93. /// <param name="e"></param>
  94. private void btn_linkPLC_Click(object sender, EventArgs e)
  95. {
  96. if (!Monitor.Monitor.PLC.isConnected)
  97. {
  98. Monitor.Monitor.PLC.PLCConnect();
  99. }
  100. }
  101. /// <summary>
  102. /// 刷新停车记录按钮
  103. /// </summary>
  104. /// <param name="sender"></param>
  105. /// <param name="e"></param>
  106. private void btn_refreshRecords_Click(object sender, EventArgs e)
  107. {
  108. RefreshRecords(null, null);
  109. }
  110. /// <summary>
  111. /// 刷新停车记录显示
  112. /// </summary>
  113. /// <param name="data"></param>
  114. private void RefreshRecords(object data, EventArgs e)
  115. {
  116. dataGridViewX1.Rows.Clear();
  117. List<object[]> list = Monitor.Monitor.GetParkingRecords();
  118. List<object[]>.Enumerator listEnumer = list.GetEnumerator();
  119. while (listEnumer.MoveNext())
  120. {
  121. dataGridViewX1.Rows.Add(listEnumer.Current);
  122. }
  123. }
  124. /// <summary>
  125. /// 导出excel文件
  126. /// </summary>
  127. /// <param name="dataGridView1"></param>
  128. /// <param name="path">绝对路径,"D:\\abc\\xxx.xls"</param>
  129. private void Export(DataGridViewX dataGridView1, string path)
  130. {
  131. try
  132. {
  133. if (dataGridView1.Rows.Count == 0)
  134. {
  135. return;
  136. }
  137. Excel.Application excel = new Excel.Application();
  138. excel.Visible = false;
  139. Excel.Workbook workbook = excel.Workbooks.Add(true);
  140. Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
  141. for (int i = 0; i < dataGridView1.Columns.Count; i++)
  142. {
  143. if (dataGridView1.Columns[i].Visible == true)
  144. {
  145. worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
  146. }
  147. }
  148. for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
  149. {
  150. System.Windows.Forms.Application.DoEvents();
  151. for (int j = 0; j < dataGridView1.Columns.Count; j++)
  152. {
  153. if (dataGridView1.Columns[j].Visible == true)
  154. {
  155. if (dataGridView1[j, i].ValueType == typeof(string))
  156. {
  157. worksheet.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value.ToString();
  158. }
  159. else
  160. {
  161. worksheet.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
  162. }
  163. }
  164. }
  165. }
  166. //设置禁止弹出保存和覆盖的询问提示框
  167. excel.DisplayAlerts = false;
  168. excel.AlertBeforeOverwriting = false;
  169. worksheet.Cells.Columns.AutoFit();
  170. //保存写入的数据,这里还没有保存到磁盘
  171. workbook.Saved = true;
  172. if (Directory.Exists(path))
  173. {
  174. //设置新建文件路径及名称
  175. string savePath = path + DateTime.Now.ToString("yyyy-MM-dd") + "停车记录.xls";
  176. //创建文件
  177. FileStream file = new FileStream(savePath, FileMode.CreateNew);
  178. //关闭释放流,不然没办法写入数据
  179. file.Close();
  180. file.Dispose();
  181. //保存到指定的路径
  182. workbook.SaveCopyAs(savePath);
  183. }
  184. workbook.Close(false, Type.Missing, Type.Missing);
  185. //确保Excel进程关闭
  186. excel.Quit();
  187. //释放 COM 对象
  188. Marshal.ReleaseComObject(worksheet);
  189. Marshal.ReleaseComObject(workbook);
  190. Marshal.ReleaseComObject(excel);
  191. excel = null;
  192. worksheet = null;
  193. workbook = null;
  194. GC.Collect();//如果不使用这条语,excel会无法正常退出
  195. }
  196. catch (Exception e) { Console.WriteLine("导出excel文件异常," + e.Message); }
  197. }
  198. /// <summary>
  199. /// 更新基本信息
  200. /// </summary>
  201. private void UpdateBasicInfo()
  202. {
  203. string sysInfo = Monitor.Monitor.GetSysInfo();
  204. tbx_sysInfo.Text = sysInfo;
  205. }
  206. /// <summary>
  207. /// 导出停车记录按钮
  208. /// </summary>
  209. /// <param name="sender"></param>
  210. /// <param name="e"></param>
  211. private void btn_exportRecords_Click(object sender, EventArgs e)
  212. {
  213. Export(dataGridViewX1, Directory.GetCurrentDirectory()+"\\");
  214. Console.WriteLine(Directory.GetCurrentDirectory()+"\\");
  215. }
  216. private void flowLayoutPanel1_Resize(object sender, EventArgs e)
  217. {
  218. Control.ControlCollection flpCC = flowLayoutPanel1.Controls;
  219. try
  220. {
  221. for(int i = 0; i < flpCC.Count; i++)
  222. {
  223. flpCC[i].Width = (int)(flowLayoutPanel1.Width * 0.3);
  224. flpCC[i].Height = (int)(flowLayoutPanel1.Height * 0.49);
  225. Console.WriteLine("flp::"+flowLayoutPanel1.Width+","+flowLayoutPanel1.Height);
  226. //Console.WriteLine("tlp--"+flpCC[i].Width + "," + flpCC[i].Height);
  227. Control.ControlCollection tlpCC = flpCC[i].Controls;
  228. if (tlpCC.Count == 2)
  229. {
  230. tlpCC[0].Width = flpCC[i].Width - 5;
  231. tlpCC[0].Height = (int)(flpCC[i].Height * 0.79);
  232. tlpCC[1].Width = flpCC[i].Width - 5;
  233. tlpCC[1].Height = (int)(flpCC[i].Height * 0.19);
  234. //Console.WriteLine(tlpCC[0].Width + "," + tlpCC[0].Height);
  235. //Console.WriteLine(tlpCC[1].Width + "," + tlpCC[1].Height);
  236. }
  237. }
  238. }
  239. catch (Exception ex) { Console.WriteLine(ex.Message); }
  240. }
  241. }
  242. }