using System; using DevExpress.XtraBars; using UAS_DeviceMonitor.DataOperate; using System.Data; using System.Text; using UAS_DeviceMonitor.Entity; using UAS_DeviceMonitor.PublicMethod; using DevExpress.XtraGrid.Views.Grid; using System.Collections.Generic; using System.Drawing; using DevExpress.XtraBars.Ribbon; using System.Threading; using UAS_DeviceMonitor.CustomerControl.PictureEditWithText; using System.Windows.Forms; using DevExpress.XtraEditors.Controls; using System.Threading.Tasks; namespace UAS_DeviceMonitor { public partial class Main : RibbonForm { StringBuilder sql = new StringBuilder(); DataHelper dh; ModeBusTCPServer mbt = new ModeBusTCPServer(); #region 初始化代码 public Main() { dh = new DataHelper(); SystemInf.dh = dh; InitializeComponent(); dpc_enableCheckEdit.QueryCheckStateByValue += CheckedEdit_QueryCheckStateByValue; CommandSetRepositoryItemComboBox1.ParseEditValue += CommandSetRepositoryItemComboBox1_ParseEditValue; CheckEditCommandSet.QueryCheckStateByValue += CheckedEdit_QueryCheckStateByValue; PollSettingItemSearchLookUpEdit.ParseEditValue += PollSettingItemSearchLookUpEdit_ParseEditValue; GridViewPollSetting.RowCellStyle += GridViewPollSetting_RowCellStyle; #region 添加发送接收编码规则的静态值 ComboBoxData cbd = new ComboBoxData(); cbd.Text = "UTF-8"; cbd.Value = "UTF-8"; ComboBoxData cbd1 = new ComboBoxData(); cbd1.Text = "Hexadecimal"; cbd1.Value = "Hexadecimal"; ComboBoxData cbd2 = new ComboBoxData(); cbd2.Text = "ASCII"; cbd2.Value = "ASCII"; CommandSetRepositoryItemComboBox1.Items.Add(cbd); CommandSetRepositoryItemComboBox1.Items.Add(cbd1); CommandSetRepositoryItemComboBox1.Items.Add(cbd2); #endregion } private void CommandSetRepositoryItemComboBox1_ParseEditValue(object sender, DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs e) { string val = ""; if (e.Value != null) { val = e.Value.ToString(); } else { val = "FALSE";//默认为不选 } e.Value = val; e.Handled = true; } private void PollSettingItemSearchLookUpEdit_ParseEditValue(object sender, DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs e) { e.Handled = true; } private void Main_Load(object sender, EventArgs e) { //设备列表 GridDeviceList.GetDataSQL = "select de_id,de_code,de_name,de_spec,de_indate,de_runstatus,de_address,de_wccode,de_vendcode,de_vendname from device".ToUpper(); ToolPageControlDeviceList.Gridcontrol = GridDeviceList; //轮询业务 //GridPolling.GetDataSQL = "select 0 CHECKEDCOLUMN,pl_id,pl_code,pl_name,pl_type,pl_dccode,pl_remark from polling".ToUpper(); //GridPolling.ID = "pl_id"; //GridPolling.TableName = "polling"; //GridPolling.InsertSQL = "insert into polling(pl_id,pl_code,pl_name,pl_type,pl_dccode,pl_remark) values(polling_seq.nextval,:pl_code,:pl_name,:pl_type,:pl_dccode,:pl_remark)"; //ButtonDeletePolling.Grid = GridPolling; //ButtonSavePolling.Grid = GridPolling; //ButtonAddPolling.Grid = GridPolling; //轮询配置界面 GridPollingSetting.GetDataSQL = "SELECT '' POLLSETTINGSTATUSCOLUMN,0 CHECKEDCOLUMN,DPC_ID,DPC_DECODE ,DPC_INTERVAL ,DPC_DCCODE ,DPC_FUNCTION , nvl(DPC_ENABLE,0)DPC_ENABLE,DPC_STATUS ,DPC_REMARK FROM DEVICEPOLLINGCONFIG ORDER BY DPC_ID"; GridPollingSetting.ID = "DPC_ID"; GridPollingSetting.TableName = "DEVICEPOLLINGCONFIG"; GridPollingSetting.InsertSQL = "insert into DEVICEPOLLINGCONFIG(DPC_ID,DPC_DECODE ,DPC_INTERVAL ,DPC_DCCODE ,DPC_FUNCTION , DPC_ENABLE,DPC_STATUS ,DPC_REMARK) values(DEVICEPOLLINGCONFIG_seq.nextval,:DPC_DECODE ,:DPC_INTERVAL ,:DPC_DCCODE ,:DPC_FUNCTION , :DPC_ENABLE,:DPC_STATUS ,:DPC_REMARK)"; ButtonSaveCommandSet.Grid = GridPollingSetting; ButtonNewCommandSet.Grid = GridPollingSetting; ButtonDeleteCommandSet.Grid = GridPollingSetting; //指令设置界面 GridCommandSetting.GetDataSQL = "select 0 CHECKEDCOLUMN,dc_id,dc_code,dc_name,dc_value,dc_sendcoding,dc_receivecoding from devicecommand ".ToUpper(); GridCommandSetting.TableName = "devicecommand"; GridCommandSetting.ID = "dc_id"; GridCommandSetting.InsertSQL = "insert into devicecommand(dc_id,dc_code,dc_name,dc_value,dc_debrand,dc_sendcoding,dc_receivecoding) values(devicecommand_seq.nextval,:dc_code,:dc_name,:dc_value,'" + BaseUtil.GetComboxEditValue(Brand) + "',:dc_sendcoding,:dc_receivecoding)"; ButtonSaveCommand.Grid = GridCommandSetting; ButtonDeleteCommand.Grid = GridCommandSetting; ButtonNewCommand.Grid = GridCommandSetting; //设备联网配置界面 GridDeviceNetSetting.GetDataSQL = "SELECT 0 CHECKEDCOLUMN,DNC_ID,DNC_DECODE,DNC_GATEWAY,DNC_UPPERIP,DNC_PORT,DNC_MAC,DNC_IP,DNC_TYPE FROM DEVICENETCONFIG order by DNC_ID"; GridDeviceNetSetting.ID = "dnc_id"; GridDeviceNetSetting.TableName = "DEVICENETCONFIG"; GridDeviceNetSetting.InsertSQL = "insert into DEVICENETCONFIG(DNC_ID,DNC_DECODE,DNC_GATEWAY,DNC_UPPERIP,DNC_PORT,DNC_MAC,DNC_IP,DNC_TYPE) values(DEVICENETCONFIG_SEQ.NEXTVAL,:DNC_DECODE,:DNC_GATEWAY,:DNC_UPPERIP,:DNC_PORT,:DNC_MAC,:DNC_IP,:DNC_TYPE)"; ButtonDeleteNetConfig.Grid = GridDeviceNetSetting; ButtonAddNetConfig.Grid = GridDeviceNetSetting; ButtonSaveNetConfig.Grid = GridDeviceNetSetting; Ptime = new Dictionary(); } #endregion #region 界面通用事件 /// /// 选项卡切换不同XPage /// /// /// private void ButtonItem_ItemClick(object sender, ItemClickEventArgs e) { HideXPage(e.Item.Tag.ToString()); } /// /// 根据点击按钮的Tag显示对应的XPage /// /// private void HideXPage(string PageName) { for (int i = 0; i < MainTabControl.TabPages.Count; i++) { if (MainTabControl.TabPages[i].Name == PageName) MainTabControl.TabPages[i].PageVisible = true; else MainTabControl.TabPages[i].PageVisible = false; } } /// /// 勾选Grid的CheckBox /// /// /// private void CheckedEdit_QueryCheckStateByValue(object sender, DevExpress.XtraEditors.Controls.QueryCheckStateByValueEventArgs e) { string val = ""; if (e.Value != null) { val = e.Value.ToString(); } else { val = "FALSE";//默认为不选 } switch (val.ToUpper()) { case "TRUE": case "YES": case "1": e.CheckState = System.Windows.Forms.CheckState.Checked; break; case "FALSE": case "NO": case "0": e.CheckState = System.Windows.Forms.CheckState.Unchecked; break; default: e.CheckState = System.Windows.Forms.CheckState.Checked; break; } e.Handled = true; } #endregion #region PageCommandSet业务代码(指令设置) /// /// 判断指令编号不能重复 /// /// /// private void ButtonSaveCommand_Click(object sender, EventArgs e) { GridCommandSetting.InsertSQL = "insert into devicecommand(dc_id,dc_code,dc_name,dc_value,dc_debrand,dc_sendcoding,dc_receivecoding) values(devicecommand_seq.nextval,:dc_code,:dc_name,:dc_value,'" + BaseUtil.GetComboxEditValue(Brand) + "',:dc_sendcoding,:dc_receivecoding)"; ButtonSaveCommand.DoSave(); } /// /// 加载ComBox品牌数据 /// /// /// private void PageCommandSet_VisibleChanged(object sender, EventArgs e) { if (PageCommandSet.PageVisible) { DataTable dt = (DataTable)SystemInf.dh.ExecuteSql("select * from devicebrand", "select"); BaseUtil.FillComBoxEditWidthDataTable(Brand, "db_name", "db_code", dt); GridCommandSetting.Condition = " where dc_debrand='" + BaseUtil.GetComboxEditValue(Brand) + "' order by dc_id"; } } private void Brand_SelectedIndexChanged(object sender, EventArgs e) { GridCommandSetting.Condition = " where dc_debrand='" + BaseUtil.GetComboxEditValue(Brand) + "' order by dc_id"; GridCommandSetting.RefreshData(); } #endregion #region PagePollingSetting业务代码(轮询配置) PollingTask pt = new PollingTask(); /// /// 绘制轮询配置状态栏 /// List PollSettingPaintRowIndex = new List(); private void ButtonStartPolling_Click(object sender, EventArgs e) { GridView grid = GridViewPollSetting; mbt.Open(); for (int i = 0; i < GridPollingSetting.RowCount; i++) { Polling pl = new Polling(); pl.Id = int.Parse(GridViewPollSetting.GetRowCellValue(i, "DPC_ID").ToString()); pl.DeviceCode = GridViewPollSetting.GetRowCellValue(i, "DPC_DECODE").ToString(); pl.CommandCode = GridViewPollSetting.GetRowCellValue(i, "DPC_DCCODE").ToString(); pl.Interval = int.Parse(GridViewPollSetting.GetRowCellValue(i, "DPC_INTERVAL").ToString()); pl.SendRadix = GridViewPollSetting.GetRowCellValue(i, "DPC_INTERVAL").ToString(); pl.Enable = GridViewPollSetting.GetRowCellValue(i, "DPC_ENABLE").ToString() != "0"; pl.Dh = new DataHelper(); if (pl.Enable) { pt.AddTask(RunTask, pl); //添加到状态为运行的行 PollSettingPaintRowIndex.Add(i); GridPollingSetting.Focus(); } } } /// /// 绘制轮询状态 /// /// /// private void GridViewPollSetting_RowCellStyle(object sender, RowCellStyleEventArgs e) { if (e.Column.Name.ToUpper() == "POLLSETTINGSTATUSCOLUMN" && PollSettingPaintRowIndex.Contains(e.RowHandle)) { e.Appearance.BackColor = Color.Green; } } Dictionary Ptime; private void RunTask(object i) { PollingTimer timer = new PollingTimer(); Polling pl = (Polling)i; timer.Polling = i; timer.Interval = pl.Interval * 5000; timer.Elapsed += Timer_Tick; timer.Start(); try { if (!Ptime.ContainsKey(pl.Id)) Ptime.Add(pl.Id, timer); } catch (Exception) { if (!Ptime.ContainsKey(pl.Id)) Ptime.Add(pl.Id, timer); } } /// /// 轮询执行的业务 /// /// /// private void Timer_Tick(object sender, EventArgs e) { PollingTimer timer = (PollingTimer)sender; Polling pl = (Polling)timer.Polling; DataHelper dh = pl.Dh; string Decode = pl.DeviceCode; string Dccode = pl.CommandCode; DataTable dt = (DataTable)dh.ExecuteSql("select dnc_ip,dnc_port from DEVICENETCONFIG where dnc_decode='" + Decode + "'", "select"); DataTable dt1 = (DataTable)dh.ExecuteSql("select dc_sendcoding,dc_value,dc_receivecoding from devicecommand where dc_code='" + Dccode + "'", "select"); if (dt.Rows.Count > 0) { string IP = dt.Rows[0]["dnc_ip"].ToString() + ":" + dt.Rows[0]["dnc_port"].ToString(); string SendCoding = ""; string ReceiveCoding = ""; string Command = ""; if (dt1.Rows.Count > 0) { SendCoding = dt1.Rows[0]["dc_sendcoding"].ToString(); ReceiveCoding = dt1.Rows[0]["dc_receivecoding"].ToString(); Command = dt1.Rows[0]["dc_value"].ToString(); } mbt.Send(IP, SendCoding, ReceiveCoding, Command); //处理2秒之内返回的指令 Thread.Sleep(2000); if (mbt.Returnvalue.ContainsKey(IP)) { mbt.Returnvalue.Remove(IP); dh.UpdateByCondition("DEVICEPOLLINGCONFIG", "dpc_status='Running'", "dpc_decode='" + Decode + "' and dpc_dccode='" + Dccode + "'"); } else { dh.UpdateByCondition("DEVICEPOLLINGCONFIG", "dpc_status='Stop'", "dpc_decode='" + Decode + "' and dpc_dccode='" + Dccode + "'"); } } } /// /// 停止全部轮询 /// /// /// private void ButtonPausePolling_Click(object sender, EventArgs e) { foreach (var item in Ptime) { item.Value.Stop(); } PollSettingPaintRowIndex.Clear(); GridPollingSetting.Focus(); Ptime.Clear(); } private void GridViewPollSetting_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e) { GridViewPollSetting.SetRowCellValue(e.RowHandle, e.Column, e.Value); if (e.Column.Name.ToUpper() == "DPC_ENABLE") { if (GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ENABLE").ToString() == "0") { int id = int.Parse(GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ID").ToString()); if (Ptime.ContainsKey(id)) { Ptime[id].Stop(); Ptime.Remove(id); PollSettingPaintRowIndex.Remove(e.RowHandle); } } else { Polling pl = new Polling(); pl.Id = int.Parse(GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ID").ToString()); pl.DeviceCode = GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_DECODE").ToString(); pl.Interval = int.Parse(GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_INTERVAL").ToString()); pl.Enable = GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ENABLE").ToString() != "0"; pl.Dh = new DataHelper(); if (pl.Enable) pt.AddTask(RunTask, pl); PollSettingPaintRowIndex.Add(e.RowHandle); } } } /// /// 设置GridView多选放大镜 /// /// /// private void PagePollingSetting_VisibleChanged(object sender, EventArgs e) { if (PagePollingSetting.PageVisible) { DataTable dt = (DataTable)SystemInf.dh.ExecuteSql("SELECT DC_CODE,DC_NAME,DC_REMARK FROM DEVICECOMMAND", "select"); PollSettingItemSearchLookUpEdit.DataSource = dt; PollSettingItemSearchLookUpEdit.DisplayMember = "DC_CODE"; PollSettingItemSearchLookUpEdit.ValueMember = "DC_CODE"; } } #endregion #region 设备查看界面 private void PageDeviceStatus_VisibleChanged(object sender, EventArgs e) { if (PageDeviceStatus.PageVisible) { DataTable dt = (DataTable)dh.ExecuteSql("select distinct de_code,dpc_status,dpc_id from device left join DEVICEPOLLINGCONFIG on dpc_decode=de_code order by de_code", "select"); //界面重新展示时如过数量不一致则重新绘制 if (PanelDeviceStatus.Controls.Count != dt.Rows.Count) { int CountPerRow = PanelDeviceStatus.Width / 110; int RightPadding = PanelDeviceStatus.Width % 110; int Count = dt.Rows.Count; int RowCount = Count % CountPerRow == 0 ? Count / CountPerRow : Count / CountPerRow + 1; int LastRowCount = 0; for (int i = 0; i < PanelDeviceStatus.Controls.Count; i++) { PanelDeviceStatus.Controls[i].Dispose(); } for (int j = 0; j < RowCount; j++) { //如果是最后一行则循环尾数 if (j == RowCount - 1) { LastRowCount = Count % CountPerRow; } for (int i = 0; i < (j == RowCount - 1 ? LastRowCount : CountPerRow); i++) { PictureEditWithText pic = new PictureEditWithText(); pic.Name = dt.Rows[j * CountPerRow + i]["de_code"].ToString() + i + j; pic.Anchor = AnchorStyles.Left; pic.Anchor = AnchorStyles.Top; pic.Picedit.Properties.SizeMode = PictureSizeMode.Squeeze; if (dt.Rows[j * CountPerRow + i]["dpc_status"].ToString() != "Running") { CommonTipController.SetToolTip(pic.Picedit, "离线"); pic.Picedit.Image = Properties.Resources.network_offline; } else { CommonTipController.SetToolTip(pic.Picedit, "在线"); pic.Picedit.Image = Properties.Resources.net_connected; } pic.Text = dt.Rows[j * CountPerRow + i]["de_code"].ToString(); pic.Picedit.BorderStyle = BorderStyles.HotFlat; pic.Location = new Point((10 + RightPadding) / 2 + 110 * i, 10 + j * 130); PanelDeviceStatus.Controls.Add(pic); } } BaseUtil.CleanMemory(); TimerDeviceStatus.Start(); } } } //重新展示设备图像界面 private void ShowDeviceStatus() { DataTable dt = (DataTable)dh.ExecuteSql("select distinct de_code,dpc_status,dpc_id from device left join DEVICEPOLLINGCONFIG on dpc_decode=de_code order by de_code", "select"); int CountPerRow = PanelDeviceStatus.Width / 110; int RightPadding = PanelDeviceStatus.Width % 110; int Count = dt.Rows.Count; int RowCount = Count % CountPerRow == 0 ? Count / CountPerRow : Count / CountPerRow + 1; int LastRowCount = 0; //只更换现有控件的图片,不必重新绘制控件 for (int j = 0; j < RowCount; j++) { //如果是最后一行则循环尾数 if (j == RowCount - 1) { LastRowCount = Count % CountPerRow; } for (int i = 0; i < (j == RowCount - 1 ? LastRowCount : CountPerRow); i++) { if (dt.Rows[j * CountPerRow + i]["dpc_status"].ToString() != "Running") { (PanelDeviceStatus.Controls[dt.Rows[j * CountPerRow + i]["de_code"].ToString() + i + j] as PictureEditWithText).Picedit.Image = Properties.Resources.network_offline; } else { (PanelDeviceStatus.Controls[dt.Rows[j * CountPerRow + i]["de_code"].ToString() + i + j] as PictureEditWithText).Picedit.Image = Properties.Resources.net_connected; } } } } //刷新界面 private void TimerDeviceStatus_Tick(object sender, EventArgs e) { if (PageDeviceStatus.PageVisible) { ShowDeviceStatus(); } } private void PanelDeviceStatus_SizeChanged(object sender, EventArgs e) { if (PageDeviceStatus.PageVisible) { ShowDeviceStatus(); TimerDeviceStatus.Start(); } } #endregion } }