FormMonitorNode.cs 15 KB

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