IOManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.RT.PLC;
  8. using Aitex.Core.Util;
  9. using Aitex.Core.RT.DataCenter;
  10. namespace Aitex.Core.RT.IOCore
  11. {
  12. public class IOManager
  13. {
  14. private IO_DEFINE _ioDefine;
  15. private Interlock _interlock = null;
  16. private Dictionary<string, IOScale> _dicScale = new Dictionary<string,IOScale>();
  17. /// <summary>
  18. /// IO Mapping
  19. /// </summary>
  20. private object _iomapLocker;
  21. private Dictionary<string, object> _ioMap;
  22. private string _interlockFile;
  23. /// <summary>
  24. /// IO Memory buffer for Update
  25. /// </summary>
  26. #region IOUpdate
  27. private int len = 200;
  28. private bool[] _di;
  29. private bool[] _do;
  30. private float[] _ai;
  31. private float[] _ao;
  32. #endregion
  33. public IOManager()
  34. {
  35. _interlock = new Interlock();
  36. _iomapLocker = new object();
  37. _ioMap = new Dictionary<string, object>();
  38. //DI/DO/AI/AO
  39. _di = new bool[len];
  40. _do = new bool[len];
  41. _ai = new float[len];
  42. _ao = new float[len];
  43. }
  44. public bool Initialize(string ioDefineXmlFile, string interlockConfigFile)
  45. {
  46. try
  47. {
  48. _ioDefine = CustomXmlSerializer.Deserialize<IO_DEFINE>(new FileInfo(ioDefineXmlFile));
  49. }
  50. catch (Exception ex)
  51. {
  52. LOG.Error("IODefine文件格式不对," + ioDefineXmlFile + "," + ex.Message);
  53. return false;
  54. }
  55. IOMapping();
  56. _interlockFile = interlockConfigFile;
  57. string reason;
  58. if (!InitializeInterlock(out reason))
  59. {
  60. LOG.Error("Interlock file has error." + reason);
  61. return false;
  62. }
  63. return true;
  64. }
  65. public bool InitializeInterlock(out string reason)
  66. {
  67. List<Tuple<int, int, string>> doMap = GetIONameList(IOType.DO);
  68. List<Tuple<int, int, string>> diMap = GetIONameList(IOType.DI);
  69. List<Tuple<DOAccessor, DIAccessor>> list = new List<Tuple<DOAccessor, DIAccessor>>();
  70. for (int i = 0; i < 1000; i++)
  71. {
  72. int idx = _ioDefine.Dig_Out[i].Safe_Feedback_Index;
  73. if (idx >= 0 && !String.IsNullOrWhiteSpace(_ioDefine.Dig_Out[i].Name))
  74. {
  75. list.Add(Tuple.Create(IO.DO[doMap[i].Item3], IO.DI[diMap[idx].Item3]));
  76. }
  77. }
  78. _interlock.InitSafeDO(list);
  79. return _interlock.Init(_interlockFile, diMap, doMap, out reason);
  80. }
  81. public void Terminate()
  82. {
  83. }
  84. public void Monitor()
  85. {
  86. if (_interlock != null)
  87. _interlock.Monitor();
  88. }
  89. //buf中的数据
  90. /// <summary>
  91. /// 更新buf中的数据
  92. /// </summary>
  93. /// <typeparam name="T"></typeparam>
  94. /// <typeparam name="V"></typeparam>
  95. /// <param name="buf"></param>
  96. public void Update<T, V>(IDataBuffer<T, V> buf) where T : struct where V : struct
  97. {
  98. buf.UpdateDI(_di);
  99. buf.UpdateDO(_do);
  100. buf.UpdateAI(_ai);
  101. buf.UpdateAO(_ao);
  102. }
  103. ///
  104. public T GetIO<T>(string name) where T : class
  105. {
  106. T obj = null;
  107. lock (_iomapLocker)
  108. {
  109. if (_ioMap.ContainsKey(name))
  110. obj = _ioMap[name] as T;
  111. }
  112. if (obj == null)
  113. {
  114. if(!String.IsNullOrWhiteSpace(name))
  115. LOG.Error(string.Format("IO not match, can not find {0}",name));
  116. }
  117. return obj;
  118. }
  119. /// <summary>
  120. /// 返回IO的(io define index,name)列表
  121. ///
  122. ///
  123. /// iodefine index
  124. /// buffer index
  125. /// iodefine name
  126. ///
  127. /// </summary>
  128. /// <param name="ioType"></param>
  129. /// <returns></returns>
  130. public List<Tuple<int, int, string>> GetIONameList(IOType ioType)
  131. {
  132. List<Tuple<int, int, string>> result = new List<Tuple<int, int, string>>();
  133. switch (ioType)
  134. {
  135. case IOType.AI:
  136. foreach (AI_ITEM item in _ioDefine.Ana_In)
  137. {
  138. if (string.IsNullOrEmpty(item.Name))
  139. continue;
  140. result.Add(Tuple.Create(item.Index, GetIndex(item.Addr, 2000), item.Name));
  141. }
  142. break;
  143. case IOType.AO:
  144. foreach (AO_ITEM item in _ioDefine.Ana_Out)
  145. {
  146. if (string.IsNullOrEmpty(item.Name))
  147. continue;
  148. result.Add(Tuple.Create(item.Index, GetIndex(item.Addr, 1000), item.Name));
  149. }
  150. break;
  151. case IOType.DI:
  152. foreach (DI_ITEM item in _ioDefine.Dig_In)
  153. {
  154. if (string.IsNullOrEmpty(item.Name))
  155. continue;
  156. result.Add(Tuple.Create(item.Index, GetIndex(item.Addr, 0), item.Name));
  157. }
  158. break;
  159. case IOType.DO:
  160. foreach (DO_ITEM item in _ioDefine.Dig_Out)
  161. {
  162. if (string.IsNullOrEmpty(item.Name))
  163. continue;
  164. result.Add(Tuple.Create(item.Index, GetIndex(item.Addr, 0), item.Name));
  165. }
  166. break;
  167. }
  168. return result;
  169. }
  170. private void IOMapping()
  171. {
  172. lock (_iomapLocker)
  173. {
  174. //DI
  175. foreach (DI_ITEM item in _ioDefine.Dig_In)
  176. {
  177. if (string.IsNullOrEmpty(item.Name))
  178. continue;
  179. int index = GetIndex(item.Addr, 0);
  180. if (index >= 0)
  181. {
  182. DIAccessor diAccessor = new DIAccessor(item.Name, index, _di, _di);
  183. _ioMap[item.Name] = diAccessor;
  184. //DATA.Subscribe(String.Format("IO.DI.{0}", item.Name), () => { return diAccessor.Value; });
  185. }
  186. else
  187. {
  188. LOG.Write(String.Format("DI{0} mapping error, addr is {1}", item.Index, item.Addr));
  189. }
  190. }
  191. //DO
  192. foreach (DO_ITEM item in _ioDefine.Dig_Out)
  193. {
  194. if (string.IsNullOrEmpty(item.Name))
  195. continue;
  196. int index = GetIndex(item.Addr, 0);
  197. if (index >= 0)
  198. {
  199. DOAccessor doAccessor = new DOAccessor(item.Name, index, _do);
  200. _ioMap[item.Name] = doAccessor;
  201. //DATA.Subscribe(String.Format("IO.DO.{0}", item.Name), () => { return doAccessor.Value; });
  202. }
  203. else
  204. {
  205. LOG.Write(String.Format("DO{0} mapping error, addr is {1}", item.Index, item.Addr));
  206. }
  207. }
  208. //AI
  209. foreach (AI_ITEM item in _ioDefine.Ana_In)
  210. {
  211. if (string.IsNullOrEmpty(item.Name))
  212. continue;
  213. int index = GetIndex(item.Addr, 2000);
  214. if (index >= 0)
  215. {
  216. AIAccessor aiAccessor = new AIAccessor(item.Name, index, _ai);
  217. _ioMap[item.Name] = aiAccessor;
  218. //DATA.Subscribe(String.Format("IO.AI.{0}", item.Name), () => { return aiAccessor.Value; });
  219. }
  220. else
  221. {
  222. LOG.Write(String.Format("AI{0} mapping error, addr is {1}", item.Index, item.Addr));
  223. }
  224. }
  225. //AO
  226. foreach (AO_ITEM item in _ioDefine.Ana_Out)
  227. {
  228. if (string.IsNullOrEmpty(item.Name))
  229. continue;
  230. int index = GetIndex(item.Addr, 1000);
  231. if (index >= 0)
  232. {
  233. AOAccessor aoAccessor = new AOAccessor(item.Name, index, _ao);
  234. _ioMap[item.Name] = aoAccessor;
  235. //DATA.Subscribe(String.Format("IO.AO.{0}", item.Name), () => { return aoAccessor.Value; });
  236. }
  237. else
  238. {
  239. LOG.Write(String.Format("AO{0} mapping error, addr is {1}", item.Index, item.Addr));
  240. }
  241. }
  242. }
  243. }
  244. public bool CanSetDo(string doName, bool onOff, out string reason)
  245. {
  246. return _interlock.CanSetDo(doName, onOff, out reason);
  247. }
  248. public void SetScale(string ioName, double logicalMin, double logicalMax, double physicalMin, double physicalMax)
  249. {
  250. _dicScale[ioName] = new IOScale(ioName, logicalMin, logicalMax, physicalMin, physicalMax);
  251. }
  252. public Dictionary<string, IOScale> GetScale()
  253. {
  254. return _dicScale;
  255. }
  256. private int GetIndex(string addr, int first)
  257. {
  258. if (String.IsNullOrEmpty(addr))
  259. {
  260. LOG.Write("GetIndex addr is empty");
  261. return -1;
  262. }
  263. string[] parts = addr.Trim().ToUpper().Split('.');
  264. int len = parts.Length;
  265. if (len == 1)
  266. {
  267. string ch = parts[0].TrimStart('D');
  268. int index = Convert.ToUInt16(ch);
  269. return (index - first)/2;
  270. }
  271. if (len == 2)
  272. {
  273. char[] trim = {'W','C','I','O',' '};
  274. string ch = parts[0].TrimStart(trim);
  275. int index = Convert.ToUInt16(ch);
  276. int bit = Convert.ToUInt16(parts[1]);
  277. return (index - first)*16 + bit;
  278. }
  279. LOG.Info("IOManager GetIndex error");
  280. return -1;
  281. }
  282. }
  283. }