BaseDevice.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.IOCore;
  7. using Aitex.Core.RT.Log;
  8. using Aitex.Core.RT.SCCore;
  9. using MECF.Framework.Common.Event;
  10. namespace Aitex.Core.RT.Device
  11. {
  12. public class BaseDevice : IAlarmHandler
  13. {
  14. public string UniqueName { get; set; }
  15. public string Module { get; set; }
  16. public string Name { get; set; }
  17. public string Display { get; set; }
  18. public string DeviceID { get; set; }
  19. public string ScBasePath { get; set; }
  20. public string IoBasePath { get; set; }
  21. public event Action<string, AlarmEventItem> OnDeviceAlarmStateChanged;
  22. private Dictionary<string, AlarmEventItem> AlarmList { get; set; }
  23. public bool HasAlarm
  24. {
  25. get
  26. {
  27. return AlarmList!=null && (AlarmList.Values.FirstOrDefault(x => !x.IsAcknowledged && x.Level==EventLevel.Alarm)!=null);
  28. }
  29. }
  30. public bool IsSimulator = SC.GetConfigItem("System.IsSimulatorMode").BoolValue;
  31. public BaseDevice()
  32. {
  33. ScBasePath = "System";
  34. IoBasePath = "System";
  35. AlarmList = new Dictionary<string, AlarmEventItem>();
  36. }
  37. public BaseDevice(string module, string name, string display, string id) : this()
  38. {
  39. display = string.IsNullOrEmpty(display) ? name : display;
  40. id = string.IsNullOrEmpty(id) ? name : id;
  41. Module = module;
  42. Name = name;
  43. Display = display;
  44. DeviceID = id;
  45. UniqueName = $"{module}.{name}";
  46. }
  47. public void AlarmStateChanged(AlarmEventItem args)
  48. {
  49. if (OnDeviceAlarmStateChanged != null)
  50. {
  51. OnDeviceAlarmStateChanged($"{UniqueName}", args);
  52. }
  53. }
  54. protected AlarmEventItem SubscribeAlarm(string name, string description, Func<bool> resetChecker, EventLevel level=EventLevel.Alarm)
  55. {
  56. AlarmEventItem item = new AlarmEventItem(Module, name, description, resetChecker, this);
  57. item.Level = level;
  58. AlarmList[name] = item;
  59. EV.Subscribe(item);
  60. return item;
  61. }
  62. protected AlarmEventItem SubscribeAlarm(EventItem item, Func<bool> resetChecker)
  63. {
  64. AlarmEventItem alarmItem = new AlarmEventItem(Module, item, resetChecker, this);
  65. //item.Level = level;
  66. //AlarmList[name] = item;
  67. EV.Subscribe(item);
  68. return alarmItem;
  69. }
  70. protected void ResetAlarm()
  71. {
  72. foreach (var alarmEventItem in AlarmList)
  73. {
  74. alarmEventItem.Value.Reset();
  75. }
  76. }
  77. public DOAccessor ParseDoNode(string name, XmlElement node, string ioModule = "")
  78. {
  79. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  80. return IO.DO[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
  81. return null;
  82. }
  83. public DIAccessor ParseDiNode(string name, XmlElement node, string ioModule = "")
  84. {
  85. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  86. return IO.DI[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
  87. return null;
  88. }
  89. public AOAccessor ParseAoNode(string name, XmlElement node, string ioModule = "")
  90. {
  91. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  92. return IO.AO[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
  93. return null;
  94. }
  95. public AIAccessor ParseAiNode(string name, XmlElement node, string ioModule = "")
  96. {
  97. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  98. return IO.AI[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
  99. return null;
  100. }
  101. public SCConfigItem ParseScNode(string name, XmlElement node, string ioModule = "", string defaultScPath = "")
  102. {
  103. SCConfigItem result = null;
  104. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  105. result = SC.GetConfigItem(node.GetAttribute(name));
  106. if (result == null && !string.IsNullOrEmpty(defaultScPath) && SC.ContainsItem(defaultScPath))
  107. result = SC.GetConfigItem(defaultScPath);
  108. return result;
  109. }
  110. public static T ParseDeviceNode<T>(string name, XmlElement node) where T : class, IDevice
  111. {
  112. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  113. return DEVICE.GetDevice<T>(node.GetAttribute(name));
  114. LOG.Write(string.Format("{0},未定义{1}", node.InnerXml, name));
  115. return null;
  116. }
  117. public static T ParseDeviceNode<T>(string module, string name, XmlElement node) where T : class, IDevice
  118. {
  119. string device_id = node.GetAttribute(name);
  120. if (!string.IsNullOrEmpty(device_id) && !string.IsNullOrEmpty(device_id.Trim()))
  121. {
  122. return DEVICE.GetDevice<T>($"{module}.{device_id}");
  123. }
  124. LOG.Write(string.Format("{0},未定义{1}", node.InnerXml, name));
  125. return null;
  126. }
  127. }
  128. }