FormMonitorNode.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Windows.Forms;
  6. using DevExpress.XtraEditors;
  7. using ProjectBase.Controls;
  8. using ProjectBase.Data.BaseDAL;
  9. using ProjectBase.Data.Logs;
  10. using ProjectBase.Data.Redis;
  11. using SIMDP.BLL;
  12. using SIMDP.Model;
  13. using ProjectBase.Util;
  14. using StackExchange.Redis;
  15. namespace SIMDP.View
  16. {
  17. public partial class FormMonitorNode : BaseDock
  18. {
  19. private List<MoDataPoint> pointList = BLLFactory<BlDataPoint>.Instance.GetAll();
  20. private List<MoDataPoint> selectPointList = new List<MoDataPoint>();
  21. private List<MoPlcInfo> plcList = BLLFactory<BlPlcInfo>.Instance.GetActivePLC();
  22. private RedisHelper redis = new RedisHelper(0);
  23. private delegate void plcConnectStatusDelegate();
  24. private delegate void opcDataChangeDelegate();
  25. private string currentPlcName = "";
  26. public FormMonitorNode()
  27. {
  28. InitializeComponent();
  29. }
  30. private void FormMonitorNode_Load(object sender, EventArgs e)
  31. {
  32. this.KeyPreview = true;
  33. RefreshPLCStatus();
  34. RefreshOpcDataPointValue();
  35. PlcSubscribeEvent();
  36. InitTreeNode();
  37. }
  38. private void PlcSubscribeEvent()
  39. {
  40. if (plcList == null || plcList.Count == 0)
  41. {
  42. DevExpress.XtraEditors.XtraMessageBox.Show("未找到需要监控的PLC!");
  43. return;
  44. }
  45. string channelPlc = SysEnvironment.PlcStatusChannel;
  46. redis.Subscribe(channelPlc, new Action<RedisChannel, RedisValue>(ShowPLCStatus));
  47. if (pointList == null || pointList.Count == 0)
  48. {
  49. DevExpress.XtraEditors.XtraMessageBox.Show("未找到监控的OPC数据点!");
  50. return;
  51. }
  52. string channelData = SysEnvironment.OpcDataChannel;
  53. redis.Subscribe(channelData, new Action<RedisChannel, RedisValue>(ShowOpcDatapointValue));
  54. //this.gridControl_Node.DataSource = pointList;
  55. }
  56. /// <summary>
  57. /// PLC监控初始化
  58. /// </summary>
  59. private void RefreshPLCStatus()
  60. {
  61. this.layoutControlPLC.BeginUpdate();
  62. this.layoutControlPLC.Controls.Clear();
  63. for (int i = 0; i < plcList.Count; i++)
  64. {
  65. LabelControl lable = new LabelControl();
  66. lable.Name = plcList[i].PlcId.ToString();
  67. lable.Location = new System.Drawing.Point(150 + 300 * i, 0);
  68. lable.Size = new System.Drawing.Size(300, 36);
  69. lable.Text = plcList[i].PlcName;
  70. lable.ImageAlignToText = ImageAlignToText.LeftCenter;
  71. string value = redis.GetString(SysEnvironment.PlcStatusPrefix + ":" + plcList[i].PlcId + plcList[i].PlcName);
  72. if (Convert.ToInt32(value) == 3)
  73. lable.Appearance.Image = Properties.Resources.ball_3;
  74. else if (Convert.ToInt32(value) == 2)
  75. lable.Appearance.Image = Properties.Resources.ball_2;
  76. else
  77. lable.Appearance.Image = Properties.Resources.ball_1;
  78. IEnumerable<KeyValuePair<int, string>> list = SysEnvironment.OpcServerStatus.Where(d => d.Key == Convert.ToInt32(value));
  79. if (list == null || list.Count() == 0)
  80. {
  81. lable.Text += " 连接状态:未连接";
  82. }
  83. foreach (KeyValuePair<int, string> item in list)
  84. {
  85. lable.Text += " 连接状态:" + item.Value;
  86. }
  87. this.layoutControlPLC.Controls.Add(lable);
  88. }
  89. this.layoutControlPLC.EndUpdate();
  90. }
  91. /// <summary>
  92. /// 刷新PLC节点值
  93. /// </summary>
  94. private void RefreshOpcDataPointValue()
  95. {
  96. //int forcusRow = gridView_Node.FocusedRowHandle;
  97. // int topRowIndex = gridView_Node.TopRowIndex;
  98. // gridControl_Node.DataSource = null;
  99. foreach (MoDataPoint item in selectPointList)
  100. {
  101. string key = SysEnvironment.PlcPointsPrefix + ":" + currentPlcName + ":" + item.DataPointId + item.DataPointName;
  102. string value = redis.GetString(key);
  103. if (value == null) continue;
  104. if (item.Data2 != value)
  105. {
  106. item.Data2 = value;
  107. item.Data3 = DateTime.Now.ToString();
  108. }
  109. }
  110. gridControl_Node.DataSource = selectPointList;
  111. gridControl_Node.RefreshDataSource();
  112. // gridView_Node.FocusedRowHandle = forcusRow;
  113. // gridView_Node.TopRowIndex = topRowIndex;
  114. }
  115. /// <summary>
  116. /// 显示OPC数据点
  117. /// </summary>
  118. /// <param name="channel"></param>
  119. /// <param name="message"></param>
  120. private void ShowOpcDatapointValue(RedisChannel channel, RedisValue message)
  121. {
  122. try
  123. {
  124. if (!this.IsActive) return;
  125. if (this.InvokeRequired)
  126. {
  127. opcDataChangeDelegate opcData = new opcDataChangeDelegate(RefreshOpcDataPointValue);
  128. this.Invoke(opcData, null);
  129. }
  130. else
  131. {
  132. RefreshOpcDataPointValue();
  133. }
  134. }
  135. catch (Exception ex)
  136. {
  137. LogHelper.log.Error("显示订阅OPC data point 出现错误" + ex);
  138. }
  139. }
  140. /// <summary>
  141. /// 显示PLC监控数据刷新
  142. /// </summary>
  143. /// <param name="channel"></param>
  144. /// <param name="message"></param>
  145. private void ShowPLCStatus(RedisChannel channel, RedisValue message)
  146. {
  147. try
  148. {
  149. if (this.InvokeRequired)
  150. {
  151. plcConnectStatusDelegate plcConnect = new plcConnectStatusDelegate(RefreshPLCStatus);
  152. this.Invoke(plcConnect, null);
  153. }
  154. else
  155. {
  156. RefreshPLCStatus();
  157. }
  158. }
  159. catch (Exception ex)
  160. {
  161. LogHelper.log.Error("显示订阅PLC 连接状态 出现错误" + ex);
  162. }
  163. }
  164. /// <summary>
  165. /// 初始化树
  166. /// </summary>
  167. private void InitTreeNode()
  168. {
  169. this.tree_Node.BeginUpdate();
  170. this.tree_Node.Nodes.Clear();
  171. this.tree_Node.ImageList = this.imageList1;
  172. // List<MoPlcInfo> plcList = BLLFactory<BlPlcInfo>.Instance.GetActivePLC();
  173. if (plcList.Count <= 0)
  174. {
  175. DevExpress.XtraEditors.XtraMessageBox.Show("无PLC数据。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  176. return;
  177. }
  178. foreach (MoPlcInfo plcInfo in plcList)
  179. {
  180. if (plcInfo != null)
  181. {
  182. TreeNode rootNode = new TreeNode(); //根节点
  183. rootNode.Text = plcInfo.PlcName;
  184. rootNode.Name = plcInfo.PlcId.ToString();
  185. rootNode.ImageIndex = 0;
  186. rootNode.SelectedImageIndex = 0;
  187. this.tree_Node.Nodes.Add(rootNode);
  188. List<MoDataGroup> dataGroupList = BLLFactory<BlDataGroup>.Instance.Find(string.Format(" data_group_plc_id = {0}", plcInfo.PlcId.ToString()));
  189. if (dataGroupList.Count <= 0)
  190. {
  191. continue;
  192. }
  193. foreach (MoDataGroup dataGroup in dataGroupList)
  194. {
  195. TreeNode oneNode = new TreeNode(); //一级节点
  196. oneNode.Text = dataGroup.DataGroupName;
  197. oneNode.Name = dataGroup.DataGroupId.ToString();
  198. oneNode.ImageIndex = 1;
  199. oneNode.SelectedImageIndex = 1;
  200. rootNode.Nodes.Add(oneNode);
  201. 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()));
  202. if (dataPointsList.Count <= 0)
  203. {
  204. continue;
  205. }
  206. foreach (MoDataPoint dataPoint in dataPointsList)
  207. {
  208. TreeNode twoNode = new TreeNode(); //二级节点
  209. twoNode.Text = dataPoint.DataPointName;
  210. twoNode.Name = dataPoint.DataPointId.ToString();
  211. twoNode.ImageIndex = 2;
  212. twoNode.SelectedImageIndex = 2;
  213. oneNode.Nodes.Add(twoNode);
  214. }
  215. }
  216. }
  217. }
  218. //this.tree_Node.ExpandAll();
  219. this.tree_Node.EndUpdate();
  220. }
  221. private void tree_Node_AfterSelect(object sender, TreeViewEventArgs e)
  222. {
  223. if (0 == e.Node.Level)
  224. {
  225. string plcId = e.Node.Name;
  226. if (e.Node.Nodes.Count <= 0)
  227. {
  228. DevExpress.XtraEditors.XtraMessageBox.Show("该PLC无组数据。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  229. return;
  230. }
  231. //string groupId = e.Node.FirstNode.Name;
  232. currentPlcName = plcList.Find(es => es.PlcId.ToString() == plcId).PlcName;
  233. BindData(plcId, null, null);
  234. }
  235. else if (1 == e.Node.Level)
  236. {
  237. string plcId = e.Node.Parent.Name;
  238. if (e.Node.Nodes.Count <= 0)
  239. {
  240. DevExpress.XtraEditors.XtraMessageBox.Show("该组无节点数据。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  241. return;
  242. }
  243. string groupId = e.Node.Name;
  244. BindData(plcId, groupId, null);
  245. }
  246. else if (2 == e.Node.Level)
  247. {
  248. string plcId = e.Node.Parent.Parent.Name;
  249. string groupId = e.Node.Parent.Name;
  250. string pointId = e.Node.Name;
  251. BindData(plcId, groupId, pointId);
  252. }
  253. }
  254. /// <summary>
  255. /// 为GridControl添加数据源
  256. /// </summary>
  257. /// <param name="plcId"></param>
  258. /// <param name="groupId"></param>
  259. /// <param name="pointId"></param>
  260. private void BindData(string plcId, string groupId, string pointId)
  261. {
  262. //currentPlcName = BLLFactory<BlPlcInfo>.Instance.FindByID(Convert.ToInt64(plcId)).PlcName;
  263. //currentPlcName = plcList.Find(e => e.PlcId.ToString() == plcId).PlcName;
  264. string sql = " data_point_plc_id = " + plcId;
  265. sql += string.IsNullOrEmpty(groupId) ? string.Format("") : string.Format(" And data_point_group_id = {0}", groupId);
  266. sql += string.IsNullOrEmpty(pointId) ? string.Format("") : string.Format(" And data_point_id = {0}", pointId);
  267. selectPointList = pointList.FindAll((MoDataPoint mo) => mo.DataPointPlcId == Convert.ToInt64(plcId));
  268. if (!string.IsNullOrEmpty(groupId))
  269. {
  270. selectPointList.Clear();
  271. selectPointList = pointList.FindAll((MoDataPoint mo) => mo.DataPointPlcId == Convert.ToInt64(plcId) &&
  272. mo.DataPointGroupId == Convert.ToInt64(groupId));
  273. }
  274. if (!string.IsNullOrEmpty(pointId))
  275. {
  276. selectPointList.Clear();
  277. selectPointList = pointList.FindAll((MoDataPoint mo) => mo.DataPointPlcId == Convert.ToInt64(plcId) &&
  278. mo.DataPointGroupId == Convert.ToInt64(groupId) && mo.DataPointId == Convert.ToInt64(pointId));
  279. }
  280. for (int i = 0; i < this.gridView_Node.Columns.Count; i++)
  281. {
  282. if (i == 6) this.gridView_Node.Columns[i].MinWidth = 110;//数据点值那一列需要加宽
  283. if (i == 7) this.gridView_Node.Columns[i].MinWidth = 100;//时间那一列需要加宽
  284. this.gridView_Node.Columns[i].BestFit();
  285. this.gridView_Node.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
  286. }
  287. this.gridControl_Node.DataSource = selectPointList;
  288. this.gridControl_Node.RefreshDataSource();
  289. }
  290. private void gridView_Node_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
  291. {
  292. if (e.Info.IsRowIndicator && e.RowHandle >= 0)
  293. {
  294. e.Info.DisplayText = (e.RowHandle + 1).ToString();
  295. }
  296. }
  297. private void gridView_Node_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
  298. {
  299. if (e.Column.Name == "gridColumn_Status")
  300. {
  301. gridView_Node.SetRowCellValue(e.RowHandle, "Data3", "100");
  302. }
  303. }
  304. private void gridView_Node_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
  305. {
  306. if (e.Column.FieldName == "DataPointType")
  307. {
  308. e.DisplayText = SysEnvironment.dirType.FirstOrDefault(p => p.Key == Convert.ToInt32(e.Value)).Value;
  309. }
  310. }
  311. }
  312. }