FormCentralController.cs 18 KB

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