IoProvider.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Xml;
  7. using Aitex.Core.RT.DataCenter;
  8. using Aitex.Core.RT.Event;
  9. using Aitex.Core.RT.IOCore;
  10. using Aitex.Core.RT.Log;
  11. using Aitex.Core.Util;
  12. namespace MECF.Framework.RT.Core.IoProviders
  13. {
  14. //
  15. public abstract class IoProvider : IIoProvider
  16. {
  17. public string Module { get; set; }
  18. public string Name { get; set; }
  19. public bool IsOpened
  20. {
  21. get { return State == IoProviderStateEnum.Opened; }
  22. }
  23. public IoProviderStateEnum State { get; set; }
  24. protected PeriodicJob _thread;
  25. protected IIoBuffer _buffer;
  26. protected R_TRIG _trigError = new R_TRIG();
  27. protected RD_TRIG _trigNotConnected = new RD_TRIG();
  28. protected string _source;
  29. protected XmlElement _nodeParameter;
  30. protected List<IoBlockItem> _blockSections;
  31. protected int _interval = 50;
  32. protected abstract void SetParameter(XmlElement nodeParameter);
  33. protected abstract void Open();
  34. protected abstract void Close();
  35. protected abstract bool[] ReadDi(int offset, int size);
  36. protected abstract short[] ReadAi(int offset, int size);
  37. protected abstract void WriteDo(int offset, bool[] buffer);
  38. protected abstract void WriteAo(int offset, short[] buffer);
  39. protected virtual float[] ReadAiFloat(int offset, int size)
  40. {
  41. return null;
  42. }
  43. protected virtual void WriteAoFloat(int offset, float[] buffer)
  44. {
  45. }
  46. /// <summary>
  47. /// 单腔体
  48. /// </summary>
  49. /// <param name="module"></param>
  50. /// <param name="name"></param>
  51. /// <param name="lstBuffers"></param>
  52. /// <param name="buffer"></param>
  53. /// <param name="nodeParameter"></param>
  54. /// <param name="ioMappingPathFile"></param>
  55. public virtual void Initialize(string module, string name,List<IoBlockItem> lstBuffers , IIoBuffer buffer, XmlElement nodeParameter, Dictionary<int, string> ioMappingPathFile)
  56. {
  57. Module = module;
  58. Name = name;
  59. _source = module + "." + name;
  60. _buffer = buffer;
  61. _nodeParameter = nodeParameter;
  62. _blockSections = lstBuffers;
  63. buffer.SetBufferBlock(_source, lstBuffers);
  64. buffer.SetIoMap(_source, ioMappingPathFile);
  65. SetParameter(nodeParameter);
  66. State = IoProviderStateEnum.Uninitialized;
  67. _thread = new PeriodicJob(_interval, OnTimer, name);
  68. }
  69. /// <summary>
  70. /// 多腔体
  71. /// </summary>
  72. /// <param name="module"></param>
  73. /// <param name="name"></param>
  74. /// <param name="lstBuffers"></param>
  75. /// <param name="buffer"></param>
  76. /// <param name="nodeParameter"></param>
  77. /// <param name="ioMappingPathFile"></param>
  78. /// <param name="ioModule"></param>
  79. public virtual void Initialize(string module, string name, List<IoBlockItem> lstBuffers, IIoBuffer buffer, XmlElement nodeParameter, string ioMappingPathFile, string ioModule )
  80. {
  81. Module = module;
  82. Name = name;
  83. _source = module + "." + name;
  84. _buffer = buffer;
  85. _nodeParameter = nodeParameter;
  86. _blockSections = lstBuffers;
  87. buffer.SetBufferBlock(_source, lstBuffers);
  88. buffer.SetIoMapByModule(_source, 0, ioMappingPathFile, ioModule);
  89. SetParameter(nodeParameter);
  90. State = IoProviderStateEnum.Uninitialized;
  91. _thread = new PeriodicJob(_interval, OnTimer, name);
  92. }
  93. protected void SetState(IoProviderStateEnum newState)
  94. {
  95. State = newState;
  96. }
  97. public void TraceArray(bool[] data)
  98. {
  99. string[] values = new string[data.Length];
  100. for (int i = 0; i < data.Length; i++)
  101. {
  102. values[i++] = data[i] ? "1" : "0";
  103. }
  104. System.Diagnostics.Trace.WriteLine(string.Join(",", values));
  105. }
  106. protected virtual bool OnTimer()
  107. {
  108. if (State == IoProviderStateEnum.Uninitialized)
  109. {
  110. SetState(IoProviderStateEnum.Opening);
  111. Open();
  112. }
  113. if (State == IoProviderStateEnum.Opened)
  114. {
  115. try
  116. {
  117. bool isAIOFloatType = false;
  118. foreach (var bufferSection in _blockSections)
  119. {
  120. if (bufferSection.Type == IoType.DI)
  121. {
  122. bool[] diBuffer = ReadDi(bufferSection.Offset, bufferSection.Size);
  123. if (diBuffer != null)
  124. {
  125. _buffer.SetDiBuffer(_source, bufferSection.Offset, diBuffer);
  126. //TraceArray(diBuffer);
  127. }
  128. }
  129. else if (bufferSection.Type == IoType.AI)
  130. {
  131. short[] aiBuffer = ReadAi(bufferSection.Offset, bufferSection.Size);
  132. if (aiBuffer != null)
  133. {
  134. _buffer.SetAiBuffer(_source, bufferSection.Offset, aiBuffer);
  135. if (bufferSection.AIOType == typeof(float))
  136. {
  137. isAIOFloatType = true;
  138. _buffer.SetAiBufferFloat(_source, bufferSection.Offset, Array.ConvertAll(aiBuffer, x => (float)x).ToArray());
  139. }
  140. }
  141. }
  142. }
  143. Dictionary<int, short[]> aos = _buffer.GetAoBuffer(_source);
  144. if (aos != null)
  145. {
  146. foreach (var ao in aos)
  147. {
  148. WriteAo(ao.Key, ao.Value);
  149. if (isAIOFloatType)
  150. WriteAoFloat(ao.Key, Array.ConvertAll(ao.Value, x => (float)x).ToArray());
  151. }
  152. }
  153. Dictionary<int, bool[]> dos = _buffer.GetDoBuffer(_source);
  154. if (dos != null)
  155. {
  156. foreach (var doo in dos)
  157. {
  158. WriteDo(doo.Key, doo.Value);
  159. }
  160. }
  161. }
  162. catch (Exception ex)
  163. {
  164. LOG.Write(ex);
  165. SetState(IoProviderStateEnum.Error);
  166. Close();
  167. }
  168. }
  169. _trigError.CLK = State == IoProviderStateEnum.Error;
  170. if (_trigError.Q)
  171. {
  172. EV.PostAlarmLog(Module, $"{_source} error");
  173. }
  174. _trigNotConnected.CLK = State != IoProviderStateEnum.Opened;
  175. if (_trigNotConnected.T)
  176. {
  177. EV.PostInfoLog(Module, $"{_source} connected");
  178. }
  179. if (_trigNotConnected.R)
  180. {
  181. EV.PostWarningLog(Module, $"{_source} not connected");
  182. }
  183. return true;
  184. }
  185. public virtual void Reset()
  186. {
  187. _trigError.RST = true;
  188. _trigNotConnected.RST = true;
  189. }
  190. public void Start()
  191. {
  192. if(_thread != null)
  193. _thread.Start();
  194. }
  195. public void Stop()
  196. {
  197. SetState(IoProviderStateEnum.Closing);
  198. Close();
  199. _thread.Stop();
  200. }
  201. public virtual bool SetValue(AOAccessor aoItem, short value)
  202. {
  203. return true;
  204. }
  205. public virtual bool SetValueFloat(AOAccessor aoItem, float value)
  206. {
  207. return true;
  208. }
  209. public virtual bool SetValue(DOAccessor doItem, bool value)
  210. {
  211. return true;
  212. }
  213. }
  214. }