123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- using DevComponents.DotNetBar.Controls;
- using PLC_Communication;
- using PLCConnector;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Runtime.InteropServices;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Terminal;
- using Excel = Microsoft.Office.Interop.Excel;
- namespace centralController
- {
- public partial class centralController : Form
- {
- /// <summary>
- /// 窗体初始化
- /// </summary>
- public centralController()
- {
- InitializeComponent();
- if (Monitor.Monitor.ins == null)
- {
- Monitor.Monitor.ins = new Monitor.Monitor(flowLayoutPanel1.Handle);
- Monitor.Monitor.ins.Start();
- }
- //进度条测试
- Task.Factory.StartNew(() =>
- {
- int temp = 0;
- while (true)
- {
- if (temp != Monitor.Monitor.initializeState)
- {
- temp = Monitor.Monitor.initializeState;
- progressBar1.Invoke(new Action(() => { progressBar1.Value = temp * 20; }));
- }
- if (temp == 5)
- {
- dataGridViewX1.Invoke(new Action(() => { RefreshRecords(null, null); }));
- break;
- }
- Thread.Sleep(300);
- }
- });
- //定时器,定时更新停车记录
- System.Windows.Forms.Timer recordsTimer = new System.Windows.Forms.Timer();
- recordsTimer.Interval = 5000;
- recordsTimer.Tick += new EventHandler(RefreshRecords);
- recordsTimer.Start();
- //定时器,定时更新提示信息
- System.Windows.Forms.Timer NoteTimer = new System.Windows.Forms.Timer();
- NoteTimer.Interval = 1000;
- NoteTimer.Tick += new EventHandler(UpdateText);
- NoteTimer.Start();
- //定时器,定时更新系统时间与剩余车位数
- System.Windows.Forms.Timer freeSpaceTimer = new System.Windows.Forms.Timer();
- freeSpaceTimer.Interval = 1000;
- freeSpaceTimer.Tick += new EventHandler(UpdateFreeSpace);
- freeSpaceTimer.Start();
- UpdateBasicInfo();
- tbx_notification.Text = "";
- }
- #region 测试
- /// <summary>
- /// 获取号牌测试
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button1_Click(object sender, EventArgs e)
- {
- string ip = textBox1.Text;
- IPAddress temp;
- if (IPAddress.TryParse(ip, out temp))
- {
- string license = "";
- Task.Factory.StartNew(() =>
- {
- license = Monitor.Monitor.numMachineLinker.GetLicensePlate(1);
- if (textBox2.InvokeRequired)
- {
- textBox2.Invoke(new Action(() => { textBox2.Text = license; }));
- }
- else
- {
- textBox2.Text = license;
- }
- });
- }
- }
- #endregion
- #region 系统
- /// <summary>
- /// 结束按钮
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btn_exit_Click(object sender, EventArgs e)
- {
- //DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel);
- //if (result.Equals(DialogResult.OK))
- //{
- // Monitor.Monitor.ins.Stop();
- // Close();
- //}
- Close();
- }
- #endregion
- #region 文件
- /// <summary>
- /// 打开日志
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btn_openFile_Click(object sender, EventArgs e)
- {
- }
- /// <summary>
- /// 刷新停车记录按钮
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btn_refreshRecords_Click(object sender, EventArgs e)
- {
- RefreshRecords(null, null);
- }
- /// <summary>
- /// 导出停车记录按钮
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btn_exportRecords_Click(object sender, EventArgs e)
- {
- Export(dataGridViewX1, Directory.GetCurrentDirectory() + "\\");
- Console.WriteLine(Directory.GetCurrentDirectory() + "\\");
- }
- /// <summary>
- /// 显示与设置收费策略
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btn_setScheme_Click(object sender, EventArgs e)
- {
- Form_paymentScheme form_PaymentScheme = new Form_paymentScheme();
- form_PaymentScheme.ShowDialog();
- }
- #endregion
- #region 视图
- /// <summary>
- /// 车位视图
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btn_parkingSpace_Click(object sender, EventArgs e)
- {
- if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
- if (dci_ParkingSpace.Visible == false) { dci_ParkingSpace.Visible = true; }
- }
- /// <summary>
- /// 显示号牌机视图
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btn_numMachine_Click(object sender, EventArgs e)
- {
- if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
- if (dci_NumMachine.Visible == false) { dci_NumMachine.Visible = true; }
- }
- /// <summary>
- /// 支付视图
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btn_payment_Click(object sender, EventArgs e)
- {
- if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
- if (dci_payment.Visible == false) { dci_payment.Visible = true; }
- }
- /// <summary>
- /// 显示测试选项卡
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btn_test_Click(object sender, EventArgs e)
- {
- if (bar_mainWin.Visible == false) { bar_mainWin.Visible = true; }
- if (dci_Test.Visible == false) { dci_Test.Visible = true; }
- }
- /// <summary>
- /// 终端视图
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btn_terminals_Click(object sender, EventArgs e)
- {
- if (bar_side.Visible == false) { bar_side.Visible = true; }
- if (dci_terminal.Visible == false) { dci_terminal.Visible = true; }
- }
- /// <summary>
- /// 停取记录
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btn_records_Click(object sender, EventArgs e)
- {
- if (bar_bottom.Visible == false) { bar_bottom.Visible = true; }
- if (dci_ParkingRecords.Visible == false) { dci_ParkingRecords.Visible = true; }
- }
- #endregion
- #region 设备
- /// <summary>
- /// 手动连接PLC
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btn_linkPLC_Click(object sender, EventArgs e)
- {
- if (!Monitor.Monitor.PLC.isConnected)
- {
- Monitor.Monitor.PLC.PLCConnect();
- }
- }
- /// <summary>
- /// PLC数据调试工具
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btn_PLCConf_Click(object sender, EventArgs e)
- {
- FormPLCConf formPLCConf = new FormPLCConf();
- formPLCConf.Show();
- }
- /// <summary>
- /// 启动号牌机调试工具
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btn_numMachineConf_Click(object sender, EventArgs e)
- {
- FormModbus form = new FormModbus();
- form.Show();
- }
- #endregion
- /// <summary>
- /// 定时更新提示信息
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void UpdateText(object sender, EventArgs e)
- {
- tbx_notification.Text = Monitor.Monitor.GetNotification();
- tbx_notification.ScrollToCaret();
- }
- /// <summary>
- /// 定时更新时间与空闲车位显示
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void UpdateFreeSpace(object sender, EventArgs e)
- {
- string currentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- string freeSpace = "剩余车位:" + Monitor.Monitor.GetFreeSpaceCount();
- string bookableSpace = "可预约车位:" + Monitor.Monitor.GetBookableSpaceCount();
- lbx_freeSpace.Text = currentTime + "\r\n" + freeSpace + " " + bookableSpace;
- }
- /// <summary>
- /// 刷新停车记录显示
- /// </summary>
- /// <param name="data"></param>
- private void RefreshRecords(object data, EventArgs e)
- {
- dataGridViewX1.Rows.Clear();
- List<object[]> list = Monitor.Monitor.GetParkingRecords();
- List<object[]>.Enumerator listEnumer = list.GetEnumerator();
- while (listEnumer.MoveNext())
- {
- dataGridViewX1.Rows.Add(listEnumer.Current);
- }
- }
- /// <summary>
- /// 导出excel文件
- /// </summary>
- /// <param name="dataGridView1"></param>
- /// <param name="path">绝对路径,"D:\\abc\\xxx.xls"</param>
- private void Export(DataGridViewX dataGridView1, string path)
- {
- try
- {
- if (dataGridView1.Rows.Count == 0)
- {
- return;
- }
- Excel.Application excel = new Excel.Application();
- excel.Visible = false;
- Excel.Workbook workbook = excel.Workbooks.Add(true);
- Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
- for (int i = 0; i < dataGridView1.Columns.Count; i++)
- {
- if (dataGridView1.Columns[i].Visible == true)
- {
- worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
- }
- }
- for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
- {
- System.Windows.Forms.Application.DoEvents();
- for (int j = 0; j < dataGridView1.Columns.Count; j++)
- {
- if (dataGridView1.Columns[j].Visible == true)
- {
- if (dataGridView1[j, i].ValueType == typeof(string))
- {
- worksheet.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value.ToString();
- }
- else
- {
- worksheet.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
- }
- }
- }
- }
- //设置禁止弹出保存和覆盖的询问提示框
- excel.DisplayAlerts = false;
- excel.AlertBeforeOverwriting = false;
- worksheet.Cells.Columns.AutoFit();
- //保存写入的数据,这里还没有保存到磁盘
- workbook.Saved = true;
- if (Directory.Exists(path))
- {
- //设置新建文件路径及名称
- string savePath = path + DateTime.Now.ToString("yyyy-MM-dd") + "停车记录.xls";
- //创建文件
- FileStream file = new FileStream(savePath, FileMode.CreateNew);
- //关闭释放流,不然没办法写入数据
- file.Close();
- file.Dispose();
- //保存到指定的路径
- workbook.SaveCopyAs(savePath);
- }
- workbook.Close(false, Type.Missing, Type.Missing);
- //确保Excel进程关闭
- excel.Quit();
- //释放 COM 对象
- Marshal.ReleaseComObject(worksheet);
- Marshal.ReleaseComObject(workbook);
- Marshal.ReleaseComObject(excel);
- excel = null;
- worksheet = null;
- workbook = null;
- GC.Collect();//如果不使用这条语,excel会无法正常退出
- }
- catch (Exception e) { Console.WriteLine("导出excel文件异常," + e.Message); }
- }
- /// <summary>
- /// 更新基本信息
- /// </summary>
- private void UpdateBasicInfo()
- {
- //string sysInfo = Monitor.Monitor.GetSysInfo();
- //tbx_sysInfo.Text = sysInfo;
- //tbx_sysInfo.ScrollToCaret();
- }
- /// <summary>
- /// 动态调整号牌机界面
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void flowLayoutPanel1_Resize(object sender, EventArgs e)
- {
- Control.ControlCollection flpCC = flowLayoutPanel1.Controls;
- try
- {
- for (int i = 0; i < flpCC.Count; i++)
- {
- flpCC[i].Width = (int)(flowLayoutPanel1.Width * 0.3);
- flpCC[i].Height = (int)(flowLayoutPanel1.Height * 0.49);
- Console.WriteLine("flp::" + flowLayoutPanel1.Width + "," + flowLayoutPanel1.Height);
- //Console.WriteLine("tlp--"+flpCC[i].Width + "," + flpCC[i].Height);
- Control.ControlCollection tlpCC = flpCC[i].Controls;
- if (tlpCC.Count == 2)
- {
- tlpCC[0].Width = flpCC[i].Width - 5;
- tlpCC[0].Height = (int)(flpCC[i].Height * 0.79);
- tlpCC[1].Width = flpCC[i].Width - 5;
- tlpCC[1].Height = (int)(flpCC[i].Height * 0.19);
- //Console.WriteLine(tlpCC[0].Width + "," + tlpCC[0].Height);
- //Console.WriteLine(tlpCC[1].Width + "," + tlpCC[1].Height);
- }
- }
- }
- catch (Exception ex) { Console.WriteLine(ex.Message); }
- }
- /// <summary>
- /// 右上角关闭函数
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void centralController_FormClosing(object sender, FormClosingEventArgs e)
- {
- DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel);
- if (result.Equals(DialogResult.OK))
- {
- Monitor.Monitor.ins.Stop();
- }
- else
- {
- e.Cancel = true;
- }
- }
- private void btn_sysInfo_Click(object sender, EventArgs e)
- {
- FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo());
- sysInfo.ShowDialog();
- }
- private void shortcut_sysInfo_Click(object sender, EventArgs e)
- {
- FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo());
- sysInfo.ShowDialog();
- }
- private void btn_advertUpdate_Click(object sender, EventArgs e)
- {
- if (Monitor.Monitor.advertMgr != null)
- {
- if (Monitor.Monitor.advertMgr.UpdateAdvert())
- MessageBox.Show("更新成功","提示");
- else
- MessageBox.Show("更新失败,请稍后重试", "提示");
- }
-
- }
- private void shortcut_close_Click(object sender, EventArgs e)
- {
- Close();
- }
- }
- }
|