IDevice.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using Aitex.Core.RT.IOCore;
  7. using Aitex.Core.RT.SCCore;
  8. using Aitex.Core.Util;
  9. using Aitex.Core.RT.Log;
  10. using System.Timers;
  11. using System.Diagnostics;
  12. using System.Collections.Concurrent;
  13. namespace Aitex.Core.RT.Device
  14. {
  15. public interface IDevice
  16. {
  17. string Module { get; set; }
  18. string Name { get; set; }
  19. bool Initialize();
  20. void Monitor();
  21. void Terminate();
  22. void Reset();
  23. }
  24. public interface IModuleDevice
  25. {
  26. bool IsReady { get; }
  27. bool IsError { get; }
  28. bool IsInit { get; }
  29. bool Home(out string reason);
  30. }
  31. public class BaseDevice
  32. {
  33. public string UniqueName { get; set; }
  34. public string Module { get; set; }
  35. public string Name { get; set; }
  36. public string Display { get; set; }
  37. public string DeviceID { get; set; }
  38. private DeviceTimer counter = new DeviceTimer();
  39. public string ScBasePath { get; set; }
  40. public string IoBasePath { get; set; }
  41. public event Action<string> sendDataChangedEvent;
  42. public List<string> SerachCommandList;
  43. public BlockingCollection<string> SetPointCommandQueue = new BlockingCollection<string>();
  44. public Timer baseTimer = new Timer(50);
  45. public Stopwatch baseStopwatch = new Stopwatch();
  46. private int index = 0;
  47. public int intervalTime = 500;
  48. public BaseDevice()
  49. {
  50. ScBasePath = "System";
  51. IoBasePath = "System";
  52. baseTimer.Elapsed += BaseTimer_Elapsed;
  53. baseTimer.Enabled = false;
  54. }
  55. private void BaseTimer_Elapsed(object sender, ElapsedEventArgs e)
  56. {
  57. if (baseStopwatch.ElapsedMilliseconds > intervalTime)
  58. {
  59. if (SetPointCommandQueue.Count > 0)
  60. {
  61. string value;
  62. if (SetPointCommandQueue.TryTake(out value))
  63. {
  64. sendDataChangedEvent.Invoke(value);
  65. }
  66. }
  67. else
  68. {
  69. sendDataChangedEvent.Invoke(SerachCommandList[index]);
  70. index = (index + 1) % SerachCommandList.Count;
  71. }
  72. baseStopwatch.Restart();
  73. }
  74. }
  75. public BaseDevice(string module, string name, string display, string id):this()
  76. {
  77. display = string.IsNullOrEmpty(display) ? name : display;
  78. id = string.IsNullOrEmpty(id) ? name : id;
  79. Module = module;
  80. Name = name;
  81. Display = display;
  82. DeviceID = id;
  83. UniqueName = $"{module}.{name}";
  84. }
  85. public DOAccessor ParseDoNode(string name, XmlElement node, string ioModule = "")
  86. {
  87. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  88. return IO.DO[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
  89. //LOG.Write(string.Format("{0},未定义{1}.{2}.{3}.{4}", node.InnerXml, Module, Display, Name, name));
  90. return null;
  91. }
  92. public DIAccessor ParseDiNode(string name, XmlElement node, string ioModule = "")
  93. {
  94. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  95. return IO.DI[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
  96. //LOG.Write(string.Format("{0},未定义{1}.{2}.{3}.{4}", node.InnerXml, Module, Display, Name, name));
  97. return null;
  98. }
  99. public AOAccessor ParseAoNode(string name, XmlElement node, string ioModule = "")
  100. {
  101. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  102. return IO.AO[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
  103. //LOG.Write(string.Format("{0},未定义{1}.{2}.{3}.{4}", node.InnerXml, Module, Display, Name, name));
  104. return null;
  105. }
  106. public AIAccessor ParseAiNode(string name, XmlElement node, string ioModule = "")
  107. {
  108. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  109. return IO.AI[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
  110. //LOG.Write(string.Format("{0},未定义{1}.{2}.{3}.{4}", node.InnerXml, Module, Display, Name, name));
  111. return null;
  112. }
  113. public SCConfigItem ParseScNode(string name, XmlElement node, string ioModule="", string defaultScPath="")
  114. {
  115. SCConfigItem result = null;
  116. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  117. result = SC.GetConfigItem(node.GetAttribute(name));
  118. if (result == null && !string.IsNullOrEmpty(defaultScPath) && SC.ContainsItem(defaultScPath))
  119. result = SC.GetConfigItem(defaultScPath);
  120. return result;
  121. }
  122. public SCConfigItem ParseScNodeEx(string name, XmlElement node, string ioModule = "")
  123. {
  124. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  125. {
  126. string aName = node.GetAttribute(name);
  127. if (string.IsNullOrWhiteSpace(ioModule))
  128. {
  129. return SC.GetConfigItem(aName);
  130. }
  131. else
  132. {
  133. return SC.GetConfigItem($"{ioModule}.{aName}");
  134. }
  135. }
  136. //LOG.Write(string.Format("{0},未定义{1}.{2}.{3}.{4}", node.InnerXml, Module, Display, Name, name));
  137. return null;
  138. }
  139. public static T ParseDeviceNode<T>(string name, XmlElement node) where T : class, IDevice
  140. {
  141. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  142. return DEVICE.GetDevice<T>(node.GetAttribute(name));
  143. //LOG.Write(string.Format("{0},未定义{1}", node.InnerXml, name));
  144. return null;
  145. }
  146. public static T ParseDeviceNode<T>(string module, string name, XmlElement node) where T : class, IDevice
  147. {
  148. string device_id = node.GetAttribute(name);
  149. if (!string.IsNullOrEmpty(device_id) && !string.IsNullOrEmpty(device_id.Trim()))
  150. {
  151. return DEVICE.GetDevice<T>($"{module}.{device_id}");
  152. }
  153. //LOG.Write(string.Format("{0},未定义{1}", node.InnerXml, name));
  154. return null;
  155. }
  156. protected void Counter()
  157. {
  158. if (counter.IsIdle())
  159. {
  160. counter.Start(10000);
  161. }
  162. else
  163. {
  164. //LOG.Info(String.Format("{0} Monitor elapsed {1}", Name, counter.GetElapseTime()));
  165. counter.Start(10000);
  166. }
  167. }
  168. protected float _GetRealFloat(IOAccessor<short> accessor)
  169. {
  170. byte[] low = BitConverter.GetBytes(accessor.Buffer[accessor.Index]);
  171. byte[] high = BitConverter.GetBytes(accessor.Buffer[accessor.Index + 1]);
  172. float res = BitConverter.ToSingle(new[] { low[0], low[1], high[0], high[1] }, 0);
  173. return res;
  174. }
  175. protected void _SetRealFloat(IOAccessor<short> accessor, float val)
  176. {
  177. //val = val / 50 * 4000;
  178. byte[] val2 = BitConverter.GetBytes(val);
  179. accessor.Buffer[accessor.Index] = BitConverter.ToInt16(val2, 0);
  180. accessor.Buffer[accessor.Index + 1] = BitConverter.ToInt16(val2, 2);
  181. }
  182. protected float GetAiValue(string name)
  183. {
  184. byte[] high = BitConverter.GetBytes(IO.AI[name].Buffer[IO.AI[name].Index]);
  185. byte[] low = BitConverter.GetBytes(IO.AI[name].Buffer[IO.AI[name].Index + 1]);
  186. float flow = BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  187. return flow;
  188. }
  189. protected float GetAoValue(string name)
  190. {
  191. byte[] high = BitConverter.GetBytes(IO.AO[name].Buffer[IO.AO[name].Index]);
  192. byte[] low = BitConverter.GetBytes(IO.AO[name].Buffer[IO.AO[name].Index + 1]);
  193. float flow = BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  194. return flow;
  195. }
  196. }
  197. }