using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Windows.Forms; using DevExpress.XtraEditors; using ProjectBase.Controls; using ProjectBase.Data.BaseDAL; using ProjectBase.Data.Logs; using ProjectBase.Data.Redis; using SIMDP.BLL; using SIMDP.Model; using ProjectBase.Util; using StackExchange.Redis; namespace SIMDP.View { public partial class FormMonitorNode : BaseDock { private List pointList = BLLFactory.Instance.GetAll(); private List selectPointList = new List(); private List plcList = BLLFactory.Instance.GetActivePLC(); private RedisHelper redis = new RedisHelper(0); private delegate void plcConnectStatusDelegate(); private delegate void opcDataChangeDelegate(); private string currentPlcName = ""; public FormMonitorNode() { InitializeComponent(); } private void FormMonitorNode_Load(object sender, EventArgs e) { this.KeyPreview = true; RefreshPLCStatus(); RefreshOpcDataPointValue(); PlcSubscribeEvent(); InitTreeNode(); } private void PlcSubscribeEvent() { if (plcList == null || plcList.Count == 0) { DevExpress.XtraEditors.XtraMessageBox.Show("未找到需要监控的PLC!"); return; } string channelPlc = SysEnvironment.PlcStatusChannel; redis.Subscribe(channelPlc, new Action(ShowPLCStatus)); if (pointList == null || pointList.Count == 0) { DevExpress.XtraEditors.XtraMessageBox.Show("未找到监控的OPC数据点!"); return; } string channelData = SysEnvironment.OpcDataChannel; redis.Subscribe(channelData, new Action(ShowOpcDatapointValue)); //this.gridControl_Node.DataSource = pointList; } /// /// PLC监控初始化 /// private void RefreshPLCStatus() { this.layoutControlPLC.BeginUpdate(); this.layoutControlPLC.Controls.Clear(); for (int i = 0; i < plcList.Count; i++) { LabelControl lable = new LabelControl(); lable.Name = plcList[i].PlcId.ToString(); lable.Location = new System.Drawing.Point(150 + 300 * i, 0); lable.Size = new System.Drawing.Size(300, 36); lable.Text = plcList[i].PlcName; lable.ImageAlignToText = ImageAlignToText.LeftCenter; string value = redis.GetString(SysEnvironment.PlcStatusPrefix + ":" + plcList[i].PlcId + plcList[i].PlcName); if (Convert.ToInt32(value) == 3) lable.Appearance.Image = Properties.Resources.ball_3; else if (Convert.ToInt32(value) == 2) lable.Appearance.Image = Properties.Resources.ball_2; else lable.Appearance.Image = Properties.Resources.ball_1; IEnumerable> list = SysEnvironment.OpcServerStatus.Where(d => d.Key == Convert.ToInt32(value)); if (list == null || list.Count() == 0) { lable.Text += " 连接状态:未连接"; } foreach (KeyValuePair item in list) { lable.Text += " 连接状态:" + item.Value; } this.layoutControlPLC.Controls.Add(lable); } this.layoutControlPLC.EndUpdate(); } /// /// 刷新PLC节点值 /// private void RefreshOpcDataPointValue() { //int forcusRow = gridView_Node.FocusedRowHandle; // int topRowIndex = gridView_Node.TopRowIndex; // gridControl_Node.DataSource = null; foreach (MoDataPoint item in selectPointList) { string key = SysEnvironment.PlcPointsPrefix + ":" + currentPlcName + ":" + item.DataPointId + item.DataPointName; string value = redis.GetString(key); if (value == null) continue; if (item.Data2 != value) { item.Data2 = value; item.Data3 = DateTime.Now.ToString(); } } gridControl_Node.DataSource = selectPointList; gridControl_Node.RefreshDataSource(); // gridView_Node.FocusedRowHandle = forcusRow; // gridView_Node.TopRowIndex = topRowIndex; } /// /// 显示OPC数据点 /// /// /// private void ShowOpcDatapointValue(RedisChannel channel, RedisValue message) { try { if (!this.IsActive) return; if (this.InvokeRequired) { opcDataChangeDelegate opcData = new opcDataChangeDelegate(RefreshOpcDataPointValue); this.Invoke(opcData, null); } else { RefreshOpcDataPointValue(); } } catch (Exception ex) { LogHelper.log.Error("显示订阅OPC data point 出现错误" + ex); } } /// /// 显示PLC监控数据刷新 /// /// /// private void ShowPLCStatus(RedisChannel channel, RedisValue message) { try { if (this.InvokeRequired) { plcConnectStatusDelegate plcConnect = new plcConnectStatusDelegate(RefreshPLCStatus); this.Invoke(plcConnect, null); } else { RefreshPLCStatus(); } } catch (Exception ex) { LogHelper.log.Error("显示订阅PLC 连接状态 出现错误" + ex); } } /// /// 初始化树 /// private void InitTreeNode() { this.tree_Node.BeginUpdate(); this.tree_Node.Nodes.Clear(); this.tree_Node.ImageList = this.imageList1; // List plcList = BLLFactory.Instance.GetActivePLC(); if (plcList.Count <= 0) { DevExpress.XtraEditors.XtraMessageBox.Show("无PLC数据。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } foreach (MoPlcInfo plcInfo in plcList) { if (plcInfo != null) { TreeNode rootNode = new TreeNode(); //根节点 rootNode.Text = plcInfo.PlcName; rootNode.Name = plcInfo.PlcId.ToString(); rootNode.ImageIndex = 0; rootNode.SelectedImageIndex = 0; this.tree_Node.Nodes.Add(rootNode); List dataGroupList = BLLFactory.Instance.Find(string.Format(" data_group_plc_id = {0}", plcInfo.PlcId.ToString())); if (dataGroupList.Count <= 0) { continue; } foreach (MoDataGroup dataGroup in dataGroupList) { TreeNode oneNode = new TreeNode(); //一级节点 oneNode.Text = dataGroup.DataGroupName; oneNode.Name = dataGroup.DataGroupId.ToString(); oneNode.ImageIndex = 1; oneNode.SelectedImageIndex = 1; rootNode.Nodes.Add(oneNode); List dataPointsList = BLLFactory.Instance.Find(string.Format(" data_point_plc_id = {0} And data_point_group_id = {1}", plcInfo.PlcId.ToString(), dataGroup.DataGroupId.ToString())); if (dataPointsList.Count <= 0) { continue; } foreach (MoDataPoint dataPoint in dataPointsList) { TreeNode twoNode = new TreeNode(); //二级节点 twoNode.Text = dataPoint.DataPointName; twoNode.Name = dataPoint.DataPointId.ToString(); twoNode.ImageIndex = 2; twoNode.SelectedImageIndex = 2; oneNode.Nodes.Add(twoNode); } } } } //this.tree_Node.ExpandAll(); this.tree_Node.EndUpdate(); } private void tree_Node_AfterSelect(object sender, TreeViewEventArgs e) { if (0 == e.Node.Level) { string plcId = e.Node.Name; if (e.Node.Nodes.Count <= 0) { DevExpress.XtraEditors.XtraMessageBox.Show("该PLC无组数据。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //string groupId = e.Node.FirstNode.Name; currentPlcName = plcList.Find(es => es.PlcId.ToString() == plcId).PlcName; BindData(plcId, null, null); } else if (1 == e.Node.Level) { string plcId = e.Node.Parent.Name; if (e.Node.Nodes.Count <= 0) { DevExpress.XtraEditors.XtraMessageBox.Show("该组无节点数据。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } string groupId = e.Node.Name; BindData(plcId, groupId, null); } else if (2 == e.Node.Level) { string plcId = e.Node.Parent.Parent.Name; string groupId = e.Node.Parent.Name; string pointId = e.Node.Name; BindData(plcId, groupId, pointId); } } /// /// 为GridControl添加数据源 /// /// /// /// private void BindData(string plcId, string groupId, string pointId) { //currentPlcName = BLLFactory.Instance.FindByID(Convert.ToInt64(plcId)).PlcName; //currentPlcName = plcList.Find(e => e.PlcId.ToString() == plcId).PlcName; string sql = " data_point_plc_id = " + plcId; sql += string.IsNullOrEmpty(groupId) ? string.Format("") : string.Format(" And data_point_group_id = {0}", groupId); sql += string.IsNullOrEmpty(pointId) ? string.Format("") : string.Format(" And data_point_id = {0}", pointId); selectPointList = pointList.FindAll((MoDataPoint mo) => mo.DataPointPlcId == Convert.ToInt64(plcId)); if (!string.IsNullOrEmpty(groupId)) { selectPointList.Clear(); selectPointList = pointList.FindAll((MoDataPoint mo) => mo.DataPointPlcId == Convert.ToInt64(plcId) && mo.DataPointGroupId == Convert.ToInt64(groupId)); } if (!string.IsNullOrEmpty(pointId)) { selectPointList.Clear(); selectPointList = pointList.FindAll((MoDataPoint mo) => mo.DataPointPlcId == Convert.ToInt64(plcId) && mo.DataPointGroupId == Convert.ToInt64(groupId) && mo.DataPointId == Convert.ToInt64(pointId)); } for (int i = 0; i < this.gridView_Node.Columns.Count; i++) { if (i == 6) this.gridView_Node.Columns[i].MinWidth = 110;//数据点值那一列需要加宽 if (i == 7) this.gridView_Node.Columns[i].MinWidth = 100;//时间那一列需要加宽 this.gridView_Node.Columns[i].BestFit(); this.gridView_Node.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; } this.gridControl_Node.DataSource = selectPointList; this.gridControl_Node.RefreshDataSource(); } private void gridView_Node_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e) { if (e.Info.IsRowIndicator && e.RowHandle >= 0) { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } } private void gridView_Node_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e) { if (e.Column.Name == "gridColumn_Status") { gridView_Node.SetRowCellValue(e.RowHandle, "Data3", "100"); } } private void gridView_Node_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) { if (e.Column.FieldName == "DataPointType") { e.DisplayText = SysEnvironment.dirType.FirstOrDefault(p => p.Key == Convert.ToInt32(e.Value)).Value; } } } }