FormCentralController.cs 18 KB

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