123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- 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<MoDataPoint> pointList = BLLFactory<BlDataPoint>.Instance.GetAll();
- private List<MoDataPoint> selectPointList = new List<MoDataPoint>();
- private List<MoPlcInfo> plcList = BLLFactory<BlPlcInfo>.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<RedisChannel, RedisValue>(ShowPLCStatus));
- if (pointList == null || 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 < 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<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()
- {
- //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;
- }
- /// <summary>
- /// 显示OPC数据点
- /// </summary>
- /// <param name="channel"></param>
- /// <param name="message"></param>
- 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);
- }
- }
- /// <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> plcList = BLLFactory<BlPlcInfo>.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<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;
- 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);
- }
- }
- /// <summary>
- /// 为GridControl添加数据源
- /// </summary>
- /// <param name="plcId"></param>
- /// <param name="groupId"></param>
- /// <param name="pointId"></param>
- private void BindData(string plcId, string groupId, string pointId)
- {
- //currentPlcName = BLLFactory<BlPlcInfo>.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;
- }
- }
- }
- }
|