using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; 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 SIMDP.Project; using SIMDP.Util; using StackExchange.Redis; using static SIMDP.Project.MXComponent_FEM; using System.Threading; using System.Threading.Tasks; namespace SIMDP { public partial class FormMonitorNode : BaseDock { //BlPlcInfo blPlcInfo = new BlPlcInfo(); //BlDataGroup blDataGroup = new BlDataGroup(); //BlDataPoint blDataPoint = new BlDataPoint(); private List filtered_Plclist = new List();//201102添加:用于过滤测试PLC private List eliminating_DataPoint_PlcID = new List(); private List filtered_PointList = new List(); private List plclist = new List(); private List pointlist = new List(); private List selectPointList = new List(); private RedisHelper redis = new RedisHelper(0); private delegate void plcConnectStatusDelegate(); private delegate void opcDataChangeDelegate(); public FormMonitorNode() { InitializeComponent(); //InitTreeNode();//转移至LOAD } private void FormMonitorNode_Load(object sender, EventArgs e) { plclist = BLLFactory.Instance.GetAll(); foreach (var item in plclist)//过滤类型为9的PLC { if (item.LinkType != 9) filtered_Plclist.Add(item); else eliminating_DataPoint_PlcID.Add(item.PlcId); } pointlist = BLLFactory.Instance.GetAll(); foreach (var item in pointlist)//过滤类型为9的PLC的 数据点 { if (!eliminating_DataPoint_PlcID.Contains(item.DataPointPlcId)) filtered_PointList.Add(item); } RefreshPLCStatus(); //RefreshOpcDataPointValue(); PlcSubscribeEvent(); InitTreeNode(); } private void PlcSubscribeEvent() { if (filtered_Plclist == null || filtered_Plclist.Count == 0) { DevExpress.XtraEditors.XtraMessageBox.Show("未找到需要监控的PLC!"); return; } string channelPlc = SysEnvironment.PlcStatusChannel; redis.Subscribe(channelPlc, new Action(ShowPLCStatus)); if (filtered_PointList == null || filtered_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 < filtered_Plclist.Count; i++) { LabelControl lable = new LabelControl(); lable.Name = filtered_Plclist[i].PlcId.ToString(); lable.Location = new System.Drawing.Point(150 + 300 * i, 0); lable.Size = new System.Drawing.Size(300, 36); lable.Text = filtered_Plclist[i].PlcName; lable.ImageAlignToText = ImageAlignToText.LeftCenter; string value = redis.GetString(filtered_Plclist[i].PlcId + filtered_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() { this.gridControl_Node.DataSource = null; foreach (MoDataPoint item in selectPointList) { string value = redis.GetString(item.DataPointId + item.DataPointName); if (item.Data2 != value) { item.Data2 = value; item.Data3 = DateTime.Now.ToString(); } } this.gridControl_Node.DataSource = selectPointList; this.gridControl_Node.RefreshDataSource(); } /// /// 显示OPC数据点 /// /// /// private void ShowOpcDatapointValue(RedisChannel channel, RedisValue message) { try { 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 real_Plclist = BLLFactory.Instance.GetAll();//201102注释掉 plcInfoList if (filtered_Plclist.Count <= 0) { DevExpress.XtraEditors.XtraMessageBox.Show("无PLC数据。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } foreach (MoPlcInfo plcInfo in filtered_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; 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) { 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 = filtered_PointList.FindAll((MoDataPoint mo) => mo.DataPointPlcId == Convert.ToInt64(plcId)); if (!string.IsNullOrEmpty(groupId)) { selectPointList.Clear(); selectPointList = filtered_PointList.FindAll((MoDataPoint mo) => mo.DataPointPlcId == Convert.ToInt64(plcId) && mo.DataPointGroupId == Convert.ToInt64(groupId)); } if (!string.IsNullOrEmpty(pointId)) { selectPointList.Clear(); selectPointList = filtered_PointList.FindAll((MoDataPoint mo) => mo.DataPointPlcId == Convert.ToInt64(plcId) && mo.DataPointGroupId == Convert.ToInt64(groupId) && mo.DataPointId == Convert.ToInt64(pointId)); } //1130 this.gridView_Node.Columns[6].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; this.gridView_Node.Columns[7].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; this.gridView_Node.Columns[3].MaxWidth = 220; this.gridView_Node.Columns[6].MaxWidth = 190; this.gridView_Node.Columns[7].MaxWidth = 190; this.gridView_Node.Columns[3].BestFit();//自动列宽 this.gridView_Node.Columns[6].BestFit();//自动列宽 this.gridView_Node.Columns[7].BestFit();//自动列宽 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; } } public static MXComponent_FEM plc = (MXComponent_FEM)PlcHelper.plcList.Find(e => ((MXComponent_FEM)e).actLogicalStationNumber == 0); TestRunner runner = new TestRunner(plc); private void button1_Click(object sender, EventArgs e) { runner.Run(); } } }