using centralController.model; using DevComponents.DotNetBar.Controls; using nettyCommunication; using parkMonitor.model; using PLC_Communication; using PLCConnector; using PLCS7; using System; using System.Collections.Generic; using System.Drawing; 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 { private List TermDispList; private bool closing = false; /// /// 窗体初始化 /// public centralController() { InitializeComponent(); TermDispList = new List(); if (Monitor.Monitor.ins == null) { Monitor.Monitor.ins = new Monitor.Monitor(this, 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) { dgvx_parkingRecords.Invoke(new Action(() => { RefreshRecords(null, null); })); break; } Thread.Sleep(300); } }); //定时器,定时更新停车记录 System.Windows.Forms.Timer recordsTimer = new System.Windows.Forms.Timer(); recordsTimer.Interval = 10000; 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(); rtb_notification.Text = ""; Task.Factory.StartNew(() => { while (!closing) { List tList = Terminal.Terminal.terminalInfo; for (int i = 0; i < tList.Count; i++) { TerminalStru ts = tList[i]; this.Invoke(new Action(() => { bool updated = false; for (int j = 0; j < TermDispList.Count; j++) { if (ts.terminalID == TermDispList[j].id) { UpdateTermGP(TermDispList[j], ts.terminalID, ts.terminalStatus == (short)1 ? true : false, ts.cmd == ts.terminalStatus ? true : false, ts.groundStatus == (short)1 ? true : false, ts.numMachineLaunch == (short)1 ? true : false); updated = true; break; } } if (!updated) InitTermGP(ts.terminalID, ts.terminalStatus == (short)1 ? true : false, ts.cmd == ts.terminalStatus ? true : false, ts.groundStatus == (short)1 ? true : false, ts.numMachineLaunch == (short)1 ? true : false); })); } Thread.Sleep(5000); } }); } private void InitTermGP(int id, bool park, bool launched, bool groundSensor, bool numMachine) { GroupPanel gpTerm = new GroupPanel(); PictureBox pbCar = new PictureBox(); PictureBox pbNum = new PictureBox(); pbNum.Dock = DockStyle.Top; //pbNum.Anchor = AnchorStyles.Top; pbNum.Image = Properties.Resources.numMachine; pbNum.Location = new Point(27, 1); pbNum.Name = "term" + id + "pbNum"; pbNum.Size = new Size(50, 29); pbNum.SizeMode = PictureBoxSizeMode.Zoom; pbNum.TabStop = false; pbCar.Dock = DockStyle.Bottom; //pbCar.Anchor = ((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right); pbCar.Image = Properties.Resources.car; pbCar.Location = new Point(23, 45); pbCar.Name = "term" + id + "pbCar"; pbCar.Size = new Size(56, 43); pbCar.SizeMode = PictureBoxSizeMode.Zoom; gpTerm.BackColor = SystemColors.AppWorkspace; gpTerm.CanvasColor = SystemColors.Control; gpTerm.ColorSchemeStyle = DevComponents.DotNetBar.eDotNetBarStyle.Office2007; gpTerm.ColorTable = ePanelColorTable.Red; gpTerm.Controls.Add(pbCar); gpTerm.Controls.Add(pbNum); gpTerm.DisabledBackColor = Color.Empty; gpTerm.Name = "term" + id + "gp"; gpTerm.Size = new Size(105, 112); gpTerm.Style.BackColor = Color.FromArgb(((int)(((byte)(229)))), ((int)(((byte)(191)))), ((int)(((byte)(191))))); gpTerm.Style.BackColor2 = Color.FromArgb(((int)(((byte)(211)))), ((int)(((byte)(150)))), ((int)(((byte)(150))))); gpTerm.Style.BackColorGradientAngle = 90; gpTerm.Style.BorderBottom = DevComponents.DotNetBar.eStyleBorderType.Solid; gpTerm.Style.BorderBottomWidth = 1; gpTerm.Style.BorderColor = Color.FromArgb(((int)(((byte)(149)))), ((int)(((byte)(55)))), ((int)(((byte)(52))))); gpTerm.Style.BorderLeft = DevComponents.DotNetBar.eStyleBorderType.Solid; gpTerm.Style.BorderLeftWidth = 1; gpTerm.Style.BorderRight = DevComponents.DotNetBar.eStyleBorderType.Solid; gpTerm.Style.BorderRightWidth = 1; gpTerm.Style.BorderTop = DevComponents.DotNetBar.eStyleBorderType.Solid; gpTerm.Style.BorderTopWidth = 1; gpTerm.Style.CornerDiameter = 4; gpTerm.Style.CornerType = DevComponents.DotNetBar.eCornerType.Rounded; gpTerm.Style.TextAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center; gpTerm.Style.TextColor = Color.FromArgb(((int)(((byte)(99)))), ((int)(((byte)(36)))), ((int)(((byte)(35))))); gpTerm.Style.TextLineAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Near; gpTerm.StyleMouseDown.CornerType = DevComponents.DotNetBar.eCornerType.Square; gpTerm.StyleMouseOver.CornerType = DevComponents.DotNetBar.eCornerType.Square; gpTerm.Text = "地面车位" + id; TermModel tm = new TermModel(gpTerm, pbNum, pbCar); UpdateTermGP(tm, id, park, launched, groundSensor, numMachine); TermDispList.Add(tm); flp_Term.Controls.Add(gpTerm); } private void UpdateTermGP(TermModel tm, int id, bool park, bool launched, bool groundSensor, bool numMachine) { tm.UpdateStatus(id, park, launched, groundSensor, numMachine); if (tm.gp != null) { //停取状态 if (park) tm.gp.ColorTable = ePanelColorTable.Red; else tm.gp.ColorTable = ePanelColorTable.Green; //启动/空闲 if (launched) tm.gp.BackColor = SystemColors.Highlight; else tm.gp.BackColor = SystemColors.AppWorkspace; //地感 if (groundSensor && tm.pbCar != null) tm.pbCar.Visible = true; else tm.pbCar.Visible = false; //号牌机 if (numMachine && tm.pbNum != null) tm.pbNum.Visible = true; else tm.pbNum.Visible = false; } } #region 测试 /// /// 获取号牌测试 /// /// /// private void button1_Click(object sender, EventArgs e) { string ip = textBox1.Text; IPAddress temp; if (IPAddress.TryParse(ip, out temp)) { string license = ""; int id = 0; int.TryParse(textBox1.Text, out id); Task.Factory.StartNew(() => { license = Monitor.Monitor.numMachineLinker.GetLicensePlate(id); if (textBox2.InvokeRequired) { textBox2.Invoke(new Action(() => { textBox2.Text = license; })); } else { textBox2.Text = license; } }); } } /// /// 屏显测试 /// /// /// private void button2_Click(object sender, EventArgs e) { string disp = textBox1.Text; string audio = textBox2.Text; Monitor.Monitor.allInOneMachine.DispForAWhile(0, disp, 10, 1, audio); } #endregion #region 系统 /// /// 结束按钮 /// /// /// 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 文件 /// /// 打开日志 /// /// /// private void btn_openFile_Click(object sender, EventArgs e) { } /// /// 刷新停车记录按钮 /// /// /// private void btn_recordManagement_Click(object sender, EventArgs e) { FormRecordsManager formRecordsManager = new FormRecordsManager(); formRecordsManager.ShowDialog(); } /// /// 导出停车记录按钮 /// /// /// private void btn_exportRecords_Click(object sender, EventArgs e) { Export(dgvx_parkingRecords, Directory.GetCurrentDirectory() + "\\"); Console.WriteLine(Directory.GetCurrentDirectory() + "\\"); } /// /// 显示与设置收费策略 /// /// /// private void btn_setScheme_Click(object sender, EventArgs e) { FormPaymentScheme form_PaymentScheme = new FormPaymentScheme(); form_PaymentScheme.ShowDialog(); } /// /// 手动更新广告 /// /// /// private void btn_advertUpdate_Click(object sender, EventArgs e) { Task.Factory.StartNew(() => { MessageBox.Show("更新中,请稍候。", "提示"); string adAlert = ""; if (Monitor.Monitor.advertMgr != null) { if (Monitor.Monitor.advertMgr.UpdateAdvert(out adAlert)) MessageBox.Show("更新成功\n" + adAlert, "提示"); else MessageBox.Show("更新失败,请稍后重试", "提示"); } }); } #endregion #region 视图 /// /// 车位视图 /// /// /// 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; } } /// /// 显示号牌机视图 /// /// /// 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; } } /// /// 支付视图 /// /// /// 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; } } /// /// 显示测试选项卡 /// /// /// 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; } } /// /// 终端视图 /// /// /// 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; } } /// /// 停取记录 /// /// /// 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 设备 /// /// 手动连接PLC /// /// /// private void btn_linkPLC_Click(object sender, EventArgs e) { if (!Monitor.Monitor.PLC.isConnected) { Monitor.Monitor.PLC.PLCConnect(); } } /// /// PLC数据调试工具 /// /// /// private void btn_PLCConf_Click(object sender, EventArgs e) { FormPLCConf formPLCConf = new FormPLCConf(); formPLCConf.Show(); } /// /// 启动号牌机调试工具 /// /// /// private void btn_numMachineConf_Click(object sender, EventArgs e) { FormModbus form = new FormModbus(); form.Show(); } #endregion /// /// 更新提示信息 /// /// /// public void UpdateText(string str, TextColor tc) { if (str == "clear") { rtb_notification.Clear(); } else { string appendingText = str; rtb_notification.AppendText(appendingText); string currentTxt = rtb_notification.Text; string temp = currentTxt.Substring(0, currentTxt.Length - appendingText.Length + 2); int index = temp.LastIndexOf("\n", temp.Length); //rtb_notification.Select(index < 0 ? 0 : index, appendingText.Length - 1); rtb_notification.Select(index < 0 ? 0 : index, appendingText.Length - 2); //Console.WriteLine("******************"); //Console.WriteLine("选中 "+rtb_notification.SelectedText); //Console.WriteLine("******************"); //rtb_notification.SelectionFont = new Font(rtb_notification.SelectionFont, FontStyle.Bold); switch (tc) { case TextColor.Log: rtb_notification.SelectionColor = Color.Gray; break; case TextColor.Info: rtb_notification.SelectionColor = Color.Black; break; case TextColor.Warning: rtb_notification.SelectionColor = Color.Orange; break; case TextColor.Error: rtb_notification.SelectionColor = Color.Red; break; } rtb_notification.Select(index + appendingText.Length - 1, 0); } //rtb_notification.Text = Monitor.Monitor.ins.GetNotification(); //rtb_notification.ScrollToCaret(); } /// /// 定时更新时间与空闲车位显示 /// /// /// private void UpdateFreeSpace(object sender, EventArgs e) { string currentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string freeSpace = "剩余车位:" + Monitor.Monitor.ins.GetFreeSpaceCount(0); string bookableSpace = "可预约车位:" + Monitor.Monitor.ins.GetFreeSpaceCount(2); lbx_freeSpace.Text = currentTime + "\r\n" + freeSpace + " " + bookableSpace; } /// /// 刷新停车记录显示 /// /// private void RefreshRecords(object data, EventArgs e) { dgvx_parkingRecords.Rows.Clear(); List list = Monitor.Monitor.GetParkingRecords(); List.Enumerator listEnumer = list.GetEnumerator(); while (listEnumer.MoveNext()) { dgvx_parkingRecords.Rows.Add(listEnumer.Current); } } /// /// 导出excel文件 /// /// /// 绝对路径,"D:\\abc\\xxx.xls" 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); } } /// /// 动态调整号牌机界面 /// /// /// private void flowLayoutPanel1_Resize(object sender, EventArgs e) { Control.ControlCollection flpCC = flowLayoutPanel1.Controls; try { int rows = (int)Math.Sqrt(flowLayoutPanel1.Controls.Count + 1); int columns = (int)(flowLayoutPanel1.Controls.Count * 1.0 / rows + 1); for (int i = 0; i < flpCC.Count; i++) { flpCC[i].Width = (int)(flowLayoutPanel1.Width * 1.0 / (columns * 1.1)); flpCC[i].Height = (int)(flowLayoutPanel1.Height * 1.0 / (rows * 1.15)); 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 = (int)(flpCC[i].Width * 0.95); tlpCC[0].Height = (int)(flpCC[i].Height * 0.79); tlpCC[1].Width = (int)(flpCC[i].Width * 0.95); tlpCC[1].Height = (int)(flpCC[i].Height * 0.20); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } /// /// 右上角关闭函数 /// /// /// private void centralController_FormClosing(object sender, FormClosingEventArgs e) { DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel); if (result.Equals(DialogResult.OK)) { Monitor.Monitor.ins.Stop(); closing = true; } else { e.Cancel = true; } } #region 帮助 /// /// 系统信息 /// /// /// private void btn_sysInfo_Click(object sender, EventArgs e) { FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo()); sysInfo.ShowDialog(); } #endregion #region 快捷菜单栏 private void shortcut_sysInfo_Click(object sender, EventArgs e) { FormSysInfo sysInfo = new FormSysInfo(Monitor.Monitor.GetSysInfo()); sysInfo.ShowDialog(); } private void shortcut_close_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("确定退出系统吗?", "温馨提示", MessageBoxButtons.OKCancel); if (result.Equals(DialogResult.OK)) { Monitor.Monitor.ins.Stop(); closing = true; Close(); } } #endregion } }