123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- 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<MoPlcInfo> filtered_Plclist = new List<MoPlcInfo>();//201102添加:用于过滤测试PLC
- private List<long> eliminating_DataPoint_PlcID = new List<long>();
- private List<MoDataPoint> filtered_PointList = new List<MoDataPoint>();
- private List<MoPlcInfo> plclist = new List<MoPlcInfo>();
- private List<MoDataPoint> pointlist = new List<MoDataPoint>();
- private List<MoDataPoint> selectPointList = new List<MoDataPoint>();
- 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<BlPlcInfo>.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<BlDataPoint>.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<RedisChannel, RedisValue>(ShowPLCStatus));
- if (filtered_PointList == null || filtered_PointList.Count == 0)
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("未找到监控的OPC数据点!");
- return;
- }
- string channelData = SysEnvironment.OpcDataChannel;
- redis.Subscribe(channelData, new Action<RedisChannel, RedisValue>(ShowOpcDatapointValue));
- //this.gridControl_Node.DataSource = pointList;
- }
- /// <summary>
- /// PLC监控初始化
- /// </summary>
- 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<KeyValuePair<int, string>> list = SysEnvironment.OpcServerStatus.Where(d => d.Key == Convert.ToInt32(value));
- if (list == null || list.Count() == 0)
- {
- lable.Text += " 连接状态:未连接";
- }
- foreach (KeyValuePair<int, string> item in list)
- {
- lable.Text += " 连接状态:" + item.Value;
- }
- this.layoutControlPLC.Controls.Add(lable);
- }
- this.layoutControlPLC.EndUpdate();
- }
- /// <summary>
- /// 刷新PLC节点值
- /// </summary>
- 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();
- }
- /// <summary>
- /// 显示OPC数据点
- /// </summary>
- /// <param name="channel"></param>
- /// <param name="message"></param>
- 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);
- }
- }
- /// <summary>
- /// 显示PLC监控数据刷新
- /// </summary>
- /// <param name="channel"></param>
- /// <param name="message"></param>
- 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);
- }
- }
- /// <summary>
- /// 初始化树
- /// </summary>
- private void InitTreeNode()
- {
- this.tree_Node.BeginUpdate();
- this.tree_Node.Nodes.Clear();
- this.tree_Node.ImageList = this.imageList1;
- //List<MoPlcInfo> real_Plclist = BLLFactory<BlPlcInfo>.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<MoDataGroup> dataGroupList = BLLFactory<BlDataGroup>.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<MoDataPoint> dataPointsList = BLLFactory<BlDataPoint>.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);
- }
- }
- /// <summary>
- /// 为GridControl添加数据源
- /// </summary>
- /// <param name="plcId"></param>
- /// <param name="groupId"></param>
- /// <param name="pointId"></param>
- 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();
- }
- }
- }
|