PlcDataMonitorbak.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. using IACommService4CSharp;
  2. using PlcCom;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.ComponentModel.Design;
  8. using System.Diagnostics;
  9. using System.Drawing.Design;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using System.Xml.Linq;
  15. using static PlcComponent.PlcDataMonitor;
  16. namespace PlcComponent
  17. {
  18. //public delegate void DataTriggered(object sender, MonitorEventArgs e);
  19. public partial class PlcDataMonitor :Component, IMultiOutputComponent
  20. {
  21. [Browsable(false)]
  22. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  23. public IPlcComProtocol Protocol { get; set; }
  24. [Category("PLC")]
  25. [Description("添加数据集合地址入口")]
  26. [Browsable(false)]
  27. [Editor(typeof(NodeTypeEditor), typeof(UITypeEditor))]
  28. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  29. public Node NodeToAdd
  30. {
  31. get => nodeToAdd;
  32. set
  33. {
  34. if (value != null && !readNodes.Contains(value))
  35. {
  36. nodeToAdd = value;
  37. readNodes.Add(nodeToAdd);
  38. ConfigReadNodes();
  39. }
  40. }
  41. }
  42. private Node nodeToAdd;
  43. [Category("PLC")]
  44. [Description("数据集合,请不要调用Add或者Remove方法")]
  45. [Browsable(false)]
  46. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  47. [Editor(typeof(NodesCollectionEditor), typeof(UITypeEditor))]
  48. public List<Node> ReadNodes
  49. {
  50. get => readNodes;
  51. set
  52. {
  53. readNodes = value;
  54. ConfigReadNodes();
  55. }
  56. }
  57. private List<Node> readNodes;
  58. public Dictionary<string, object> NodeStringValuePairs;
  59. [Category("PLC")]
  60. [Description("数据更新周期ms")]
  61. [DefaultValue(100)]
  62. public int ReadInterval { get => readInterval; set{ readInterval = value < 10 ? 10 : value; } }
  63. private int readInterval=100;
  64. [Category("PLC")]
  65. [Description("是否启用")]
  66. [DefaultValue(true)]
  67. public bool BackgroundRefreshEnable { get; set; } = true;
  68. [Category("PLC")]
  69. [Description("数据监控集合")]
  70. [Editor(typeof(PlcDataMonitorEditor), typeof(UITypeEditor))]
  71. public List<MonitorRule> MonitorRules
  72. {
  73. get => monitorRules;
  74. set
  75. {
  76. monitorRules=value;
  77. readNodes.Clear();
  78. List<MonitorRule> rulesNeedToRemove=new List<MonitorRule>();
  79. foreach (var item in monitorRules)
  80. {
  81. //NodeToAdd = item.Node;
  82. if (item.Node != null && !readNodes.Contains(item.Node))
  83. {
  84. readNodes.Add(item.Node);
  85. }
  86. else if(readNodes.Contains(item.Node))
  87. {
  88. rulesNeedToRemove.Add(item);//剔除重复的
  89. }
  90. }
  91. if (rulesNeedToRemove.Count > 0)
  92. {
  93. foreach (var item in rulesNeedToRemove)
  94. monitorRules.Remove(item);
  95. }
  96. ConfigReadNodes();
  97. }
  98. }
  99. private List<MonitorRule> monitorRules;
  100. [Category("PLC")]
  101. [Description("是否检测有和指定值相同")]
  102. [DefaultValue(true)]
  103. public bool CheckAnyEqualsTargetValue { get; set; } = true;
  104. public bool HaveDataEqualsTargetValue { get;private set; }
  105. // 新增:报警/事件专用事件
  106. [Description("监控的某个寄存器被触发时")]
  107. public event Action<object, List<MonitorEventArgs>> OnDataTriggered;
  108. [Description("监控的某个寄存器触发被清除时")]
  109. public event Action<object, List<MonitorEventArgs>> OnDataCleared;
  110. [Description("监控的所有寄存器都不满足触发条件时的事件")]
  111. public event Action<object> OnAllDataCleared;
  112. [Description("监控的所有寄存器的值都刷新过后")]
  113. [Browsable(true)]
  114. public event Action<object, List<MonitorRule>> OnAllDataRefreshed;
  115. [Description("有监控的值和目标值相同时")]
  116. [Browsable(true)]
  117. public event Action<object, List<MonitorRule>> OnDatasEqualTarget;
  118. /// <summary>
  119. /// Occurs when reading nodes from the PLC fails.
  120. /// </summary>
  121. /// <remarks>This event is triggered when an attempt to read nodes from the PLC does not succeed.
  122. /// Subscribers can use this event to handle errors or retry logic.</remarks>
  123. public event Action<IPlcBindableComponent, List<Node>> ReadNodesFailed;
  124. public class MonitorEventArgs : EventArgs
  125. {
  126. public MonitorRule Rule { get; set; }
  127. public object CurrentValue { get; set; }
  128. public DateTime Timestamp { get; set; }
  129. public string Message => Rule?.Message;
  130. }
  131. public PlcDataMonitor()
  132. {
  133. InitializeComponent();
  134. readNodes = new List<Node>();
  135. monitorRules = new List<MonitorRule>();
  136. }
  137. public PlcDataMonitor(IContainer container)
  138. {
  139. container.Add(this);
  140. InitializeComponent();
  141. readNodes = new List<Node>();
  142. monitorRules = new List<MonitorRule>();
  143. }
  144. private void ConfigReadNodes()
  145. {
  146. NodeStringValuePairs = new Dictionary<string, object>();
  147. foreach (var node in readNodes)
  148. {
  149. if (node != null)
  150. {
  151. if (!string.IsNullOrEmpty(node.Value))
  152. {
  153. NodeStringValuePairs.Add(node.Value, null);
  154. }
  155. }
  156. }
  157. }
  158. public List<Node> RemoveInvalidNodes()
  159. {
  160. var nodesToRemove = new List<Node>();
  161. List<string> strings = ReadNodes.Select(n => n.Value).ToList();
  162. try
  163. {
  164. var rt = Protocol.ReadValue(strings, out List<object> results);
  165. if (rt == 0)
  166. {
  167. for (int i = 0; i < strings.Count; i++)
  168. {
  169. if (results[i] == null)
  170. {
  171. nodesToRemove.Add(ReadNodes[i]);
  172. }
  173. }
  174. }
  175. }
  176. catch (Exception)
  177. {
  178. }
  179. foreach (var item in nodesToRemove)
  180. {
  181. ReadNodes.Remove(item);
  182. //ReadOneNodeFailed.Invoke(c, item, 0);
  183. }
  184. if (nodesToRemove.Count > 0)
  185. {
  186. var itemsToRemove = new HashSet<Node>(nodesToRemove);
  187. MonitorRules = MonitorRules.Where(b => !itemsToRemove.Contains(b.Node)).ToList();
  188. ReadNodesFailed?.Invoke(this, nodesToRemove);
  189. }
  190. return nodesToRemove;
  191. }
  192. public event RegisterValueChangedHandler OnPLCRegisterValueChanged;
  193. public event ReadRegisterFailedHandler OnReadRegisterFailed;
  194. public event UpdateComponentStateFailedHandler OnUpdateComponentStateFailed;
  195. private bool isReadNodesCached = false;
  196. private List<string> addresses=new List<string>();
  197. private List<string> CacheReadNodes()
  198. {
  199. if(!isReadNodesCached)
  200. {
  201. addresses = ReadNodes.Select(n => n.Value).ToList();
  202. isReadNodesCached = true;
  203. }
  204. return addresses;
  205. }
  206. public List<object> Read()
  207. {
  208. if (Protocol != null && ReadNodes != null)
  209. {
  210. CacheReadNodes();
  211. var rt = Protocol.ReadValue(addresses, out List<object> results);
  212. if (rt == 0)
  213. {
  214. UpdateValues(ReadNodes, results);
  215. return results;
  216. }
  217. else
  218. {
  219. OnReadRegisterFailed?.Invoke(this, ReadNodes[0], rt);
  220. }
  221. }
  222. return null;
  223. }
  224. private Dictionary<Node, MonitorRule> _ruleDictCache;
  225. private bool _isCacheValid = false;
  226. private void EnsureRuleDict()
  227. {
  228. if (!_isCacheValid)
  229. {
  230. _ruleDictCache = MonitorRules.ToDictionary(r => r.Node, r => r);
  231. _isCacheValid = true;
  232. }
  233. }
  234. List<MonitorRule> dataEqualsTargetRules = new List<MonitorRule>();
  235. List<MonitorEventArgs> triggeredData = new List<MonitorEventArgs>();
  236. List<MonitorEventArgs> clearedData = new List<MonitorEventArgs>();
  237. public void UpdateValues(List<Node> nodes, List<object> values)
  238. {
  239. EnsureRuleDict(); //缓存字典
  240. bool haveDataEqualsTarget = false;
  241. dataEqualsTargetRules.Clear();
  242. triggeredData.Clear();
  243. clearedData.Clear();
  244. for (int i = 0; i < nodes.Count; i++)
  245. {
  246. // 1. 原有值变化通知
  247. if (NodeStringValuePairs.TryGetValue(nodes[i].Value, out var pair))
  248. {
  249. if (!object.Equals(pair, values[i]))
  250. {
  251. NodeStringValuePairs[nodes[i].Value] = values[i];
  252. OnPLCRegisterValueChanged?.Invoke(this, ReadNodes[i], values[i]);
  253. }
  254. }
  255. // 2. 监控值的变化通知
  256. if (_ruleDictCache.TryGetValue(nodes[i], out var rule))
  257. {
  258. rule.CurrentValue = values[i];
  259. if (CheckAnyEqualsTargetValue && object.Equals(rule.TargetValue, values[i]))
  260. {
  261. haveDataEqualsTarget = true;
  262. dataEqualsTargetRules.Add(rule);
  263. }
  264. var result = CheckAndTrigger(rule);
  265. if (result== CompareResult.Rising)
  266. {
  267. triggeredData.Add(new MonitorEventArgs
  268. {
  269. Rule = rule,
  270. CurrentValue = rule.CurrentValue,
  271. Timestamp = DateTime.Now
  272. });
  273. }
  274. else if (result== CompareResult.Falling)
  275. {
  276. clearedData.Add(new MonitorEventArgs
  277. {
  278. Rule = rule,
  279. CurrentValue = rule.CurrentValue,
  280. Timestamp = DateTime.Now
  281. });
  282. }
  283. }
  284. }
  285. if(triggeredData.Count>0)
  286. {
  287. OnDataTriggered?.Invoke(this, triggeredData);
  288. }
  289. if (clearedData.Count > 0)
  290. {
  291. OnDataCleared?.Invoke(this, clearedData);
  292. }
  293. if (HaveDataEqualsTargetValue && !haveDataEqualsTarget)
  294. {
  295. OnAllDataCleared?.Invoke(this);
  296. }
  297. HaveDataEqualsTargetValue = haveDataEqualsTarget;
  298. if(haveDataEqualsTarget)
  299. {
  300. OnDatasEqualTarget?.Invoke(this, dataEqualsTargetRules);
  301. }
  302. OnAllDataRefreshed?.Invoke(this, MonitorRules);
  303. }
  304. private Dictionary<Node, (object value, DateTime timestamp)> lastStableValues = new Dictionary<Node, (object value, DateTime timestamp)>();
  305. private CompareResult CheckAndTrigger(MonitorRule rule)
  306. {
  307. int rt=0;
  308. object x=rule.CurrentValue, y=null;
  309. CompareResult result= rule.LatestCompareResult;
  310. if (rule.CompareType == CompareType.ToTarget)
  311. {
  312. y = rule.TargetValue;
  313. if (!rule.TargetIsBoolFalse)
  314. {
  315. rt = Comparer<object>.Default.Compare(x, y);
  316. if (rule.HaveComparedOnce)
  317. {
  318. if (rt > 0)
  319. result = CompareResult.Rising;
  320. else if (rt < 0)
  321. result = CompareResult.Falling;
  322. if (result != rule.LatestCompareResult)
  323. {
  324. rule.LatestCompareResult=result;
  325. return result;
  326. }
  327. else
  328. return CompareResult.NoChange;
  329. }
  330. else
  331. {
  332. rule.HaveComparedOnce = true;
  333. if (rule.FirstRefreshTrigger)
  334. {
  335. if (rt >= 0)
  336. result = CompareResult.Rising;
  337. else
  338. result = CompareResult.Falling;
  339. rule.LatestCompareResult = result;
  340. if (result != rule.LatestCompareResult)
  341. {
  342. rule.LatestCompareResult = result;
  343. return result;
  344. }
  345. else
  346. return CompareResult.None;
  347. }
  348. else
  349. {
  350. rule.LatestCompareResult = CompareResult.None;
  351. return CompareResult.None;
  352. }
  353. }
  354. }
  355. else//目标是bool类型,且是false
  356. {
  357. rt = Comparer<object>.Default.Compare(x, y);
  358. if (rule.HaveComparedOnce)
  359. {
  360. if (rt < 0)
  361. result = CompareResult.Rising;//由于y永远是false,故不可能达到这一步。
  362. else if (rt > 0)
  363. result = CompareResult.Falling;
  364. if (result != rule.LatestCompareResult)
  365. {
  366. rule.LatestCompareResult = result;
  367. return result;
  368. }
  369. else
  370. return CompareResult.NoChange;
  371. }
  372. else
  373. {
  374. rule.HaveComparedOnce = true;
  375. if (rule.FirstRefreshTrigger)
  376. {
  377. if (rt <= 0)
  378. result = CompareResult.Rising;
  379. if (result != rule.LatestCompareResult)
  380. {
  381. rule.LatestCompareResult = result;
  382. return result;
  383. }
  384. else
  385. return CompareResult.NoChange;
  386. }
  387. return CompareResult.None;
  388. }
  389. }
  390. }
  391. else if (rule.CompareType == CompareType.ToPreviewValue)
  392. {
  393. // 获取上一次值
  394. if (lastStableValues.TryGetValue(rule.Node, out var lastValue))
  395. {
  396. y = lastValue.value;
  397. }
  398. else if (rule.FirstRefreshTrigger)
  399. {
  400. y = GetDefaultValue(rule.CurrentValue);
  401. }
  402. lastStableValues[rule.Node] = (x, DateTime.Now);
  403. if (y != null)
  404. {
  405. rt = Comparer<object>.Default.Compare(x, y);
  406. if (rt > 0)
  407. result = CompareResult.Rising;
  408. else if (rt == 0)
  409. result = CompareResult.NoChange;
  410. else
  411. result = CompareResult.Falling;
  412. return result;
  413. }
  414. else
  415. {
  416. return CompareResult.None;
  417. }
  418. }
  419. else
  420. return CompareResult.None;
  421. }
  422. static object GetDefaultValue(object boxedValue)
  423. {
  424. if (boxedValue == null)
  425. throw new ArgumentNullException(nameof(boxedValue));
  426. Type t = boxedValue.GetType();
  427. if (!t.IsValueType)
  428. throw new ArgumentException("Only value types are supported.");
  429. return Activator.CreateInstance(t);
  430. }
  431. }
  432. public enum MonitorType
  433. {
  434. RisingEdge,
  435. FallingEdge,
  436. RisingOrFallingEdge,
  437. SameValue,
  438. }
  439. [Serializable]
  440. public class MonitorRule
  441. {
  442. private object targetValue = true;
  443. [Description("读取地址")]
  444. [Editor(typeof(NodeTypeEditor), typeof(UITypeEditor))]
  445. public Node Node { get; set; }
  446. [Description("监控变量类型")]
  447. [DefaultValue(MonitorType.RisingOrFallingEdge)]
  448. public MonitorType MonitorType { get; set; } = MonitorType.RisingOrFallingEdge;
  449. [Description("当前值")]
  450. public object CurrentValue { get; set; }
  451. [Description("数据比较方式")]
  452. [DefaultValue(CompareType.ToTarget)]
  453. public CompareType CompareType { get; set; }
  454. public bool HaveComparedOnce { get; set; }
  455. public CompareResult LatestCompareResult { get; set; } = CompareResult.None;
  456. [Description("当CompareType=ToTarget 时所要对比的值")]
  457. [DefaultValue(true)]
  458. [TypeConverter(typeof(StringConverter))]
  459. public object TargetValue { get => targetValue; set { targetValue = value;if (targetValue is bool b) TargetIsBoolFalse = !b; } }
  460. [Description("第一次满足上升或者下降沿时是否触发事件")]
  461. [DefaultValue(true)]
  462. public bool FirstRefreshTrigger { get; set; } = true; // 第一次满足条件是否触发
  463. [Description("触发事件的消息")]
  464. public string Message { get; set; }
  465. internal bool TargetIsBoolFalse { get; set; }
  466. }
  467. public enum CompareType
  468. {
  469. /// <summary>
  470. /// 和目标值比较
  471. /// </summary>
  472. ToTarget,
  473. /// <summary>
  474. /// 和前一次数据比较
  475. /// </summary>
  476. ToPreviewValue
  477. }
  478. public enum CompareResult
  479. {
  480. None,
  481. NoChange,
  482. Rising,
  483. Falling
  484. }
  485. }