IOManager.cs 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml;
  5. using Aitex.Core.Common.DeviceData;
  6. using Aitex.Core.RT.DataCenter;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.RT.IOCore;
  9. using Aitex.Core.RT.Log;
  10. using Aitex.Core.RT.OperationCenter;
  11. using Aitex.Core.RT.SCCore;
  12. using Aitex.Core.Util;
  13. using MECF.Framework.RT.Core.IoProviders;
  14. namespace MECF.Framework.Common.IOCore
  15. {
  16. public class IoManager : Singleton<IoManager>, IIoBuffer
  17. {
  18. private Dictionary<string, DIAccessor> _diMap = new Dictionary<string, DIAccessor>();
  19. private Dictionary<string, DOAccessor> _doMap = new Dictionary<string, DOAccessor>();
  20. private Dictionary<string, AIAccessor> _aiMap = new Dictionary<string, AIAccessor>();
  21. private Dictionary<string, AOAccessor> _aoMap = new Dictionary<string, AOAccessor>();
  22. private Dictionary<string, Dictionary<int, bool[]>> _diBuffer = new Dictionary<string, Dictionary<int, bool[]>>();
  23. private Dictionary<string, Dictionary<int, bool[]>> _doBuffer = new Dictionary<string, Dictionary<int, bool[]>>();
  24. private Dictionary<string, Dictionary<int, short[]>> _aiBufferShort = new Dictionary<string, Dictionary<int, short[]>>();
  25. private Dictionary<string, Dictionary<int, short[]>> _aoBufferShort = new Dictionary<string, Dictionary<int, short[]>>();
  26. private Dictionary<string, Dictionary<int, float[]>> _aiBufferFloat = new Dictionary<string, Dictionary<int, float[]>>();
  27. private Dictionary<string, Dictionary<int, float[]>> _aoBufferFloat = new Dictionary<string, Dictionary<int, float[]>>();
  28. private Dictionary<string, Dictionary<int, Type>> _aiBufferType = new Dictionary<string, Dictionary<int, Type>>();
  29. private Dictionary<string, Dictionary<int, Type>> _aoBufferType = new Dictionary<string, Dictionary<int, Type>>();
  30. private Dictionary<string, List<DIAccessor>> _diList = new Dictionary<string, List<DIAccessor>>();
  31. private Dictionary<string, List<DOAccessor>> _doList = new Dictionary<string, List<DOAccessor>>();
  32. private Dictionary<string, List<AIAccessor>> _aiList = new Dictionary<string, List<AIAccessor>>();
  33. private Dictionary<string, List<AOAccessor>> _aoList = new Dictionary<string, List<AOAccessor>>();
  34. private Dictionary<string, List<NotifiableIoItem>> _ioItemList = new Dictionary<string, List<NotifiableIoItem>>();
  35. private PeriodicJob _monitorThread;
  36. /// <summary>
  37. /// 只有互锁用到
  38. /// </summary>
  39. /// <param name="interlockConfigFile"></param>
  40. public void Initialize(string interlockConfigFile)
  41. {
  42. string reason = string.Empty;
  43. if (!InterlockManager.Instance.Initialize(interlockConfigFile, _doMap, _diMap, _aoMap, _aiMap, out reason))
  44. {
  45. throw new Exception(string.Format("interlock define file found error: \r\n {0}", reason));
  46. }
  47. _monitorThread = new PeriodicJob(200, OnTimer, "IO Monitor Thread", true);
  48. }
  49. private bool OnTimer()
  50. {
  51. try
  52. {
  53. InterlockManager.Instance.Monitor();
  54. }
  55. catch (Exception ex)
  56. {
  57. LOG.Write(ex);
  58. }
  59. return true;
  60. }
  61. internal List<Tuple<int, int, string>> GetIONameList(string group, IOType ioType)
  62. {
  63. return null;
  64. }
  65. public bool CanSetDo(string doName, bool onOff, out string reason)
  66. {
  67. return InterlockManager.Instance.CanSetDo(doName, onOff, out reason);
  68. }
  69. public List<DIAccessor> GetDIList(string source)
  70. {
  71. return _diList.ContainsKey(source) ? _diList[source] : null;
  72. }
  73. public List<DOAccessor> GetDOList(string source)
  74. {
  75. return _doList.ContainsKey(source) ? _doList[source] : null;
  76. }
  77. public List<AIAccessor> GetAIList(string source)
  78. {
  79. return _aiList.ContainsKey(source) ? _aiList[source] : null;
  80. }
  81. public List<AOAccessor> GetAOList(string source)
  82. {
  83. return _aoList.ContainsKey(source) ? _aoList[source] : null;
  84. }
  85. public IoManager()
  86. {
  87. OP.Subscribe("System.SetDoValue", InvokeSetDo);
  88. OP.Subscribe("System.SetAoValue", InvokeSetAo);
  89. OP.Subscribe("System.SetAoValueFloat", InvokeSetAoFloat);
  90. OP.Subscribe("System.SetDoValueWithPrivoder", InvokeSetDoWithPrivoder);
  91. OP.Subscribe("System.SetAoValueWithPrivoder", InvokeSetAoWithPrivoder);
  92. OP.Subscribe("System.SetAiBuffer", InvokeSetAiBuffer);
  93. OP.Subscribe("System.SetDiBuffer", InvokeSetDiBuffer);
  94. }
  95. private bool InvokeSetDo(string arg1, object[] args)
  96. {
  97. string name = (string)args[0];
  98. bool setpoint = (bool)args[1];
  99. string reason;
  100. if (!CanSetDo(name, setpoint, out reason))
  101. {
  102. EV.PostWarningLog("System", $"Can not set DO {name} to {setpoint}, {reason}");
  103. return false;
  104. }
  105. DOAccessor do1 = GetIO<DOAccessor>(name);
  106. if (do1 == null)
  107. {
  108. EV.PostWarningLog("System", $"Can not set DO {name} to {setpoint}, not defined do");
  109. return false;
  110. }
  111. if (!do1.SetValue(setpoint, out reason))
  112. {
  113. EV.PostWarningLog("System", $"Can not set DO {name} to {setpoint}, {reason}");
  114. return false;
  115. }
  116. EV.PostInfoLog("System", $"Change DO {name} to {setpoint}");
  117. return true;
  118. }
  119. private bool InvokeSetAo(string arg1, object[] args)
  120. {
  121. string name = (string)args[0];
  122. short setpoint = (short)args[1];
  123. AOAccessor io = GetIO<AOAccessor>(name);
  124. if (io == null)
  125. {
  126. EV.PostWarningLog("System", $"Can not set AO {name} to {setpoint}, not defined do");
  127. return false;
  128. }
  129. io.Value = setpoint;
  130. EV.PostInfoLog("System", $"Change AO {name} to {setpoint}");
  131. return true;
  132. }
  133. private bool InvokeSetAoFloat(string arg1, object[] args)
  134. {
  135. string name = (string)args[0];
  136. float setpoint = (float)args[1];
  137. AOAccessor io = GetIO<AOAccessor>(name);
  138. if (io == null)
  139. {
  140. EV.PostWarningLog("System", $"Can not set AO {name} to {setpoint}, not defined");
  141. return false;
  142. }
  143. io.FloatValue = setpoint;
  144. EV.PostInfoLog("System", $"Change AO {name} to {setpoint}");
  145. return true;
  146. }
  147. private bool InvokeSetDoWithPrivoder(string arg1, object[] args)
  148. {
  149. string provider = (string)args[0];
  150. int offset = (int)args[1];
  151. string name = (string)args[2];
  152. bool setpoint = (bool)args[3];
  153. string reason;
  154. if (!CanSetDo(name, setpoint, out reason))
  155. {
  156. EV.PostWarningLog("System", $"Can not set DO {provider}.{name} to {setpoint}, {reason}");
  157. return false;
  158. }
  159. var doList = GetDOList(provider);
  160. if (doList == null)
  161. {
  162. EV.PostWarningLog("System", $"Can not set DO {provider}.{name} to {setpoint}, {reason}");
  163. return false;
  164. }
  165. var doAccessor = doList.FirstOrDefault(x => x.Name == name);
  166. if (doAccessor == default(DOAccessor))
  167. {
  168. EV.PostWarningLog("System", $"Can not set DO {provider}.{name} to {setpoint}, {reason}");
  169. return false;
  170. }
  171. if (!doAccessor.SetValue(setpoint, out reason))
  172. {
  173. EV.PostWarningLog("System", $"Can not set DO {provider}.{name} to {setpoint}, {reason}");
  174. return false;
  175. }
  176. EV.PostInfoLog("System", $"Change DO {provider}.{name} to {setpoint}");
  177. return true;
  178. }
  179. private bool InvokeSetAiBuffer(string arg1, object[] args)
  180. {
  181. string provider = (string)args[0];
  182. int offset = (int)args[1];
  183. short[] buffer = (short[])args[2];
  184. SetAiBuffer(provider, offset, buffer);
  185. return true;
  186. }
  187. private bool InvokeSetDiBuffer(string arg1, object[] args)
  188. {
  189. string provider = (string)args[0];
  190. int offset = (int)args[1];
  191. bool[] buffer = (bool[])args[2];
  192. SetDiBuffer(provider, offset, buffer);
  193. return true;
  194. }
  195. private bool InvokeSetAoWithPrivoder(string arg1, object[] args)
  196. {
  197. string provider = (string)args[0];
  198. int offset = (int)args[1];
  199. string name = (string)args[2];
  200. float setpoint = (float)args[3];
  201. string reason = "";
  202. var aoList = GetAOList(provider);
  203. if (aoList == null)
  204. {
  205. EV.PostWarningLog("System", $"Can not set AO {provider}.{name} to {setpoint}, {reason}");
  206. return false;
  207. }
  208. var aoAccessor = aoList.FirstOrDefault(x => x.Name == name);
  209. if (aoAccessor == default(AOAccessor))
  210. {
  211. EV.PostWarningLog("System", $"Can not set AO {provider}.{name} to {setpoint}, {reason}");
  212. return false;
  213. }
  214. aoAccessor.Value = (short)setpoint;
  215. EV.PostInfoLog("System", $"Change DO {provider}.{name} to {setpoint}");
  216. return true;
  217. }
  218. public T GetIO<T>(string name) where T : class
  219. {
  220. if (typeof(T) == typeof(DIAccessor) && _diMap.ContainsKey(name))
  221. {
  222. return _diMap[name] as T;
  223. }
  224. if (typeof(T) == typeof(DOAccessor) && _doMap.ContainsKey(name))
  225. {
  226. return _doMap[name] as T;
  227. }
  228. if (typeof(T) == typeof(AIAccessor) && _aiMap.ContainsKey(name))
  229. {
  230. return _aiMap[name] as T;
  231. }
  232. if (typeof(T) == typeof(AOAccessor) && _aoMap.ContainsKey(name))
  233. {
  234. return _aoMap[name] as T;
  235. }
  236. return null;
  237. }
  238. public Dictionary<int, bool[]> GetDiBuffer(string source)
  239. {
  240. if (_diBuffer.ContainsKey(source))
  241. {
  242. return _diBuffer[source];
  243. }
  244. return null;
  245. }
  246. public Dictionary<int, bool[]> GetDoBuffer(string source)
  247. {
  248. if (_doBuffer.ContainsKey(source))
  249. {
  250. return _doBuffer[source];
  251. }
  252. return null;
  253. }
  254. public Dictionary<int, short[]> GetAiBuffer(string source)
  255. {
  256. if (_aiBufferShort.ContainsKey(source))
  257. {
  258. return _aiBufferShort[source];
  259. }
  260. return null;
  261. }
  262. public Dictionary<int, float[]> GetAoBufferFloat(string source)
  263. {
  264. if (_aoBufferFloat.ContainsKey(source))
  265. {
  266. return _aoBufferFloat[source];
  267. }
  268. return null;
  269. }
  270. public Dictionary<int, float[]> GetAiBufferFloat(string source)
  271. {
  272. if (_aiBufferFloat.ContainsKey(source))
  273. {
  274. return _aiBufferFloat[source];
  275. }
  276. return null;
  277. }
  278. public Dictionary<int, short[]> GetAoBuffer(string source)
  279. {
  280. if (_aoBufferShort.ContainsKey(source))
  281. {
  282. return _aoBufferShort[source];
  283. }
  284. return null;
  285. }
  286. public void SetDiBuffer(string source, int offset, bool[] buffer)
  287. {
  288. if (_diBuffer.ContainsKey(source) && _diBuffer[source].ContainsKey(offset))
  289. {
  290. for (int i = 0; i < buffer.Length && i < _diBuffer[source][offset].Length; i++)
  291. {
  292. _diBuffer[source][offset][i] = buffer[i];
  293. }
  294. }
  295. }
  296. public void SetAiBuffer(string source, int offset, short[] buffer, int skipSize = 0)
  297. {
  298. if (_aiBufferShort.ContainsKey(source) && _aiBufferShort[source].ContainsKey(offset))
  299. {
  300. for (int i = 0; i < buffer.Length && i < _aiBufferShort[source][offset].Length; i++)
  301. {
  302. _aiBufferShort[source][offset][i + skipSize] = buffer[i];
  303. }
  304. }
  305. }
  306. public void SetAiBufferFloat(string source, int offset, float[] buffer, int skipSize = 0)
  307. {
  308. if (_aiBufferFloat.ContainsKey(source) && _aiBufferFloat[source].ContainsKey(offset))
  309. {
  310. for (int i = 0; i < buffer.Length && i < _aiBufferFloat[source][offset].Length; i++)
  311. {
  312. _aiBufferFloat[source][offset][i + skipSize] = buffer[i];
  313. }
  314. }
  315. }
  316. public void SetDoBuffer(string source, int offset, bool[] buffer)
  317. {
  318. if (_doBuffer.ContainsKey(source) && _doBuffer[source].ContainsKey(offset))
  319. {
  320. for (int i = 0; i < buffer.Length && i < _doBuffer[source][offset].Length; i++)
  321. {
  322. _doBuffer[source][offset][i] = buffer[i];
  323. }
  324. }
  325. }
  326. public void SetAoBuffer(string source, int offset, short[] buffer)
  327. {
  328. if (_aoBufferShort.ContainsKey(source) && _aoBufferShort[source].ContainsKey(offset))
  329. {
  330. for (int i = 0; i < buffer.Length && i < _aoBufferShort[source][offset].Length; i++)
  331. {
  332. _aoBufferShort[source][offset][i] = buffer[i];
  333. }
  334. }
  335. }
  336. public void SetAoBufferFloat(string source, int offset, float[] buffer, int bufferStartIndex = 0)
  337. {
  338. if (_aoBufferFloat.ContainsKey(source) && _aoBufferFloat[source].ContainsKey(offset))
  339. {
  340. for (int i = 0; i < buffer.Length && i < _aoBufferFloat[source][offset].Length; i++)
  341. {
  342. _aoBufferFloat[source][offset][i] = buffer[i];
  343. }
  344. }
  345. }
  346. //spin recipe set
  347. public void SetAoBuffer(string source, int offset, short[] buffer, int bufferStartIndex)
  348. {
  349. if (_aoBufferShort.ContainsKey(source) && _aoBufferShort[source].ContainsKey(offset) && _aoBufferShort[source][offset].Length > bufferStartIndex)
  350. {
  351. for (int i = 0; i < buffer.Length && bufferStartIndex + i < _aoBufferShort[source][offset].Length; i++)
  352. {
  353. _aoBufferShort[source][offset][bufferStartIndex + i] = buffer[i];
  354. }
  355. }
  356. }
  357. public void SetBufferBlock(string provider, List<IoBlockItem> lstBlocks)
  358. {
  359. foreach (var ioBlockItem in lstBlocks)
  360. {
  361. switch (ioBlockItem.Type)
  362. {
  363. case IoType.AI:
  364. if (!_aiBufferShort.ContainsKey(provider))
  365. {
  366. _aiBufferShort[provider] = new Dictionary<int, short[]>();
  367. _aiBufferFloat[provider] = new Dictionary<int, float[]>();
  368. _aiBufferType[provider] = new Dictionary<int, Type>();
  369. }
  370. if (!_aiBufferShort[provider].ContainsKey(ioBlockItem.Offset))
  371. {
  372. _aiBufferShort[provider][ioBlockItem.Offset] = new short[ioBlockItem.Size];
  373. _aiBufferFloat[provider][ioBlockItem.Offset] = new float[ioBlockItem.Size];
  374. _aiBufferType[provider][ioBlockItem.Offset] = ioBlockItem.AIOType;
  375. }
  376. break;
  377. case IoType.AO:
  378. if (!_aoBufferShort.ContainsKey(provider))
  379. {
  380. _aoBufferShort[provider] = new Dictionary<int, short[]>();
  381. _aoBufferFloat[provider] = new Dictionary<int, float[]>();
  382. _aoBufferType[provider] = new Dictionary<int, Type>();
  383. }
  384. if (!_aoBufferShort[provider].ContainsKey(ioBlockItem.Offset))
  385. {
  386. _aoBufferShort[provider][ioBlockItem.Offset] = new short[ioBlockItem.Size];
  387. _aoBufferFloat[provider][ioBlockItem.Offset] = new float[ioBlockItem.Size];
  388. _aoBufferType[provider][ioBlockItem.Offset] = ioBlockItem.AIOType;
  389. }
  390. break;
  391. case IoType.DI:
  392. if (!_diBuffer.ContainsKey(provider))
  393. {
  394. _diBuffer[provider] = new Dictionary<int, bool[]>();
  395. }
  396. if (!_diBuffer[provider].ContainsKey(ioBlockItem.Offset))
  397. {
  398. _diBuffer[provider][ioBlockItem.Offset] = new bool[ioBlockItem.Size];
  399. }
  400. break;
  401. case IoType.DO:
  402. if (!_doBuffer.ContainsKey(provider))
  403. {
  404. _doBuffer[provider] = new Dictionary<int, bool[]>();
  405. }
  406. if (!_doBuffer[provider].ContainsKey(ioBlockItem.Offset))
  407. {
  408. _doBuffer[provider][ioBlockItem.Offset] = new bool[ioBlockItem.Size];
  409. }
  410. break;
  411. }
  412. }
  413. }
  414. List<NotifiableIoItem> SubscribeDiData()
  415. {
  416. List<NotifiableIoItem> result = new List<NotifiableIoItem>();
  417. foreach (var ioDefine in _diMap)
  418. {
  419. NotifiableIoItem di = new NotifiableIoItem()
  420. {
  421. Address = ioDefine.Value.Addr,
  422. Name = ioDefine.Value.Name,
  423. Description = ioDefine.Value.Description,
  424. Index = ioDefine.Value.Index,
  425. BoolValue = ioDefine.Value.Value,
  426. Provider = ioDefine.Value.Provider,
  427. BlockOffset = ioDefine.Value.BlockOffset,
  428. BlockIndex = ioDefine.Value.Index,
  429. };
  430. result.Add(di);
  431. }
  432. return result;
  433. }
  434. List<NotifiableIoItem> SubscribeDoData()
  435. {
  436. List<NotifiableIoItem> result = new List<NotifiableIoItem>();
  437. foreach (var ioDefine in _doMap)
  438. {
  439. NotifiableIoItem io = new NotifiableIoItem()
  440. {
  441. Address = ioDefine.Value.Addr,
  442. Name = ioDefine.Value.Name,
  443. Description = ioDefine.Value.Description,
  444. Index = ioDefine.Value.Index,
  445. BoolValue = ioDefine.Value.Value,
  446. Provider = ioDefine.Value.Provider,
  447. BlockOffset = ioDefine.Value.BlockOffset,
  448. BlockIndex = ioDefine.Value.Index,
  449. };
  450. result.Add(io);
  451. }
  452. return result;
  453. }
  454. List<NotifiableIoItem> SubscribeAiData()
  455. {
  456. List<NotifiableIoItem> result = new List<NotifiableIoItem>();
  457. foreach (var ioDefine in _aiMap)
  458. {
  459. NotifiableIoItem di = new NotifiableIoItem()
  460. {
  461. Address = ioDefine.Value.Addr,
  462. Name = ioDefine.Value.Name,
  463. Description = ioDefine.Value.Description,
  464. Index = ioDefine.Value.Index,
  465. ShortValue = ioDefine.Value.Value,
  466. FloatValue = ioDefine.Value.FloatValue,
  467. Provider = ioDefine.Value.Provider,
  468. BlockOffset = ioDefine.Value.BlockOffset,
  469. BlockIndex = ioDefine.Value.Index,
  470. };
  471. result.Add(di);
  472. }
  473. return result;
  474. }
  475. List<NotifiableIoItem> SubscribeAoData()
  476. {
  477. List<NotifiableIoItem> result = new List<NotifiableIoItem>();
  478. foreach (var ioDefine in _aoMap)
  479. {
  480. NotifiableIoItem ao = new NotifiableIoItem()
  481. {
  482. Address = ioDefine.Value.Addr,
  483. Name = ioDefine.Value.Name,
  484. Description = ioDefine.Value.Description,
  485. Index = ioDefine.Value.Index,
  486. ShortValue = ioDefine.Value.Value,
  487. FloatValue = ioDefine.Value.FloatValue,
  488. Provider = ioDefine.Value.Provider,
  489. BlockOffset = ioDefine.Value.BlockOffset,
  490. BlockIndex = ioDefine.Value.Index,
  491. };
  492. result.Add(ao);
  493. }
  494. return result;
  495. }
  496. public void SetIoMap(string provider, int blockOffset, List<DIAccessor> ioList)
  497. {
  498. SubscribeIoItemList(provider);
  499. var scConfig = SC.GetConfigItem("System.IsIgnoreSaveDB");
  500. var isIgnoreSaveDB = scConfig != null && scConfig.BoolValue;
  501. foreach (var accessor in ioList)
  502. {
  503. accessor.Provider = provider;
  504. accessor.BlockOffset = blockOffset;
  505. _diMap[accessor.Name] = accessor;
  506. if (!_diList.ContainsKey(provider))
  507. _diList[provider] = new List<DIAccessor>();
  508. _diList[provider].Add(accessor);
  509. _ioItemList[$"{provider}.DIItemList"].Add(new NotifiableIoItem()
  510. {
  511. Address = accessor.Addr,
  512. Name = accessor.Name,
  513. Description = accessor.Description,
  514. Index = accessor.Index,
  515. Provider = provider,
  516. BlockOffset = blockOffset,
  517. BlockIndex = accessor.BlockOffset,
  518. });
  519. if (!isIgnoreSaveDB)
  520. DATA.Subscribe($"IO.{accessor.Name}", () => accessor.Value);
  521. }
  522. }
  523. public void SetIoMap(string provider, int blockOffset, List<DOAccessor> ioList)
  524. {
  525. SubscribeIoItemList(provider);
  526. var scConfig = SC.GetConfigItem("System.IsIgnoreSaveDB");
  527. var isIgnoreSaveDB = scConfig != null && scConfig.BoolValue;
  528. foreach (var accessor in ioList)
  529. {
  530. accessor.Provider = provider;
  531. accessor.BlockOffset = blockOffset;
  532. _doMap[accessor.Name] = accessor;
  533. if (!_doList.ContainsKey(provider))
  534. _doList[provider] = new List<DOAccessor>();
  535. _doList[provider].Add(accessor);
  536. _ioItemList[$"{provider}.DOItemList"].Add(new NotifiableIoItem()
  537. {
  538. Address = accessor.Addr,
  539. Name = accessor.Name,
  540. Description = accessor.Description,
  541. Index = accessor.Index,
  542. Provider = provider,
  543. BlockOffset = blockOffset,
  544. BlockIndex = accessor.BlockOffset,
  545. });
  546. if (!isIgnoreSaveDB)
  547. DATA.Subscribe($"IO.{accessor.Name}", () => accessor.Value);
  548. }
  549. }
  550. public void SetIoMap(string provider, int blockOffset, List<AIAccessor> ioList)
  551. {
  552. SubscribeIoItemList(provider);
  553. var scConfig = SC.GetConfigItem("System.IsIgnoreSaveDB");
  554. var isIgnoreSaveDB = scConfig != null && scConfig.BoolValue;
  555. foreach (var accessor in ioList)
  556. {
  557. accessor.Provider = provider;
  558. accessor.BlockOffset = blockOffset;
  559. _aiMap[accessor.Name] = accessor;
  560. if (!_aiList.ContainsKey(provider))
  561. _aiList[provider] = new List<AIAccessor>();
  562. _aiList[provider].Add(accessor);
  563. _ioItemList[$"{provider}.AIItemList"].Add(new NotifiableIoItem()
  564. {
  565. Address = accessor.Addr,
  566. Name = accessor.Name,
  567. Description = accessor.Description,
  568. Index = accessor.Index,
  569. Provider = provider,
  570. BlockOffset = blockOffset,
  571. BlockIndex = accessor.BlockOffset,
  572. });
  573. if (!isIgnoreSaveDB)
  574. DATA.Subscribe($"IO.{accessor.Name}", () => accessor.Value);
  575. }
  576. }
  577. public void SetIoMap(string provider, int blockOffset, List<AOAccessor> ioList)
  578. {
  579. SubscribeIoItemList(provider);
  580. var scConfig = SC.GetConfigItem("System.IsIgnoreSaveDB");
  581. var isIgnoreSaveDB = scConfig != null && scConfig.BoolValue;
  582. foreach (var accessor in ioList)
  583. {
  584. accessor.Provider = provider;
  585. accessor.BlockOffset = blockOffset;
  586. _aoMap[accessor.Name] = accessor;
  587. if (!_aoList.ContainsKey(provider))
  588. _aoList[provider] = new List<AOAccessor>();
  589. _aoList[provider].Add(accessor);
  590. _ioItemList[$"{provider}.AOItemList"].Add(new NotifiableIoItem()
  591. {
  592. Address = accessor.Addr,
  593. Name = accessor.Name,
  594. Description = accessor.Description,
  595. Index = accessor.Index,
  596. Provider = provider,
  597. BlockOffset = blockOffset,
  598. BlockIndex = accessor.BlockOffset,
  599. });
  600. if (!isIgnoreSaveDB)
  601. DATA.Subscribe($"IO.{accessor.Name}", () => accessor.Value);
  602. }
  603. }
  604. public void SetIoMap(string provider, int blockOffset, string xmlPathFile, string module = "")
  605. {
  606. SubscribeIoItemList(provider);
  607. XmlDocument xml = new XmlDocument();
  608. xml.Load(xmlPathFile);
  609. XmlNodeList lstDi = xml.SelectNodes("IO_DEFINE/Dig_In/DI_ITEM");
  610. var scConfig = SC.GetConfigItem("System.IsIgnoreSaveDB");
  611. var isIgnoreSaveDB = scConfig == null ? false : scConfig.BoolValue;
  612. //<DI_ITEM Index="0" Name="" BufferOffset="0" Addr="0" Description=""/>
  613. List<DIAccessor> diList = new List<DIAccessor>();
  614. foreach (var diItem in lstDi)
  615. {
  616. XmlElement element = diItem as XmlElement;
  617. if (element == null)
  618. continue;
  619. string index = element.GetAttribute("Index");
  620. string bufferOffset = element.GetAttribute("BufferOffset");
  621. if (string.IsNullOrEmpty(bufferOffset))
  622. bufferOffset = index;
  623. string name = element.GetAttribute("Name");
  624. string address = element.GetAttribute("Addr");
  625. string type = element.GetAttribute("Type");
  626. string description = element.GetAttribute("Description");
  627. if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(index) || string.IsNullOrEmpty(bufferOffset))
  628. continue;
  629. name = name.Trim();
  630. index = index.Trim();
  631. bufferOffset = bufferOffset.Trim();
  632. string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  633. int intIndex;
  634. if (!int.TryParse(index, out intIndex))
  635. continue;
  636. int intBufferOffset;
  637. if (!int.TryParse(bufferOffset, out intBufferOffset))
  638. continue;
  639. if (!_diBuffer.ContainsKey(provider) )
  640. {
  641. throw new Exception("Not defined DI buffer from IO provider, " + provider);
  642. }
  643. if(!_diBuffer[provider].ContainsKey(blockOffset))
  644. {
  645. throw new Exception("Not defined DI buffer from IO provider, " + provider);
  646. }
  647. DIAccessor diAccessor = new DIAccessor(moduleName, intBufferOffset, _diBuffer[provider][blockOffset], _diBuffer[provider][blockOffset]);
  648. diAccessor.IoTableIndex = intIndex;
  649. diAccessor.Addr = address;
  650. diAccessor.Provider = provider;
  651. diAccessor.BlockOffset = blockOffset;
  652. diAccessor.Description = description;
  653. diAccessor.Type = type;
  654. diList.Add(diAccessor);
  655. _diMap[moduleName] = diAccessor;
  656. if (!_diList.ContainsKey(provider))
  657. _diList[provider] = new List<DIAccessor>();
  658. _diList[provider].Add(diAccessor);
  659. _ioItemList[$"{provider}.DIItemList"].Add(new NotifiableIoItem()
  660. {
  661. Address = address,
  662. Name = moduleName,
  663. Description = description,
  664. Index = intIndex,
  665. Provider = provider,
  666. BlockOffset = blockOffset,
  667. BlockIndex = intIndex,
  668. });
  669. if (!isIgnoreSaveDB)
  670. {
  671. DATA.Subscribe($"IO.{moduleName}", () => diAccessor.Value);
  672. }
  673. }
  674. XmlNodeList lstDo = xml.SelectNodes("IO_DEFINE/Dig_Out/DO_ITEM");
  675. // < DO_ITEM Index = "0" BufferOffset="0" Name = "" Addr = "0" Description = "" />
  676. foreach (var doItem in lstDo)
  677. {
  678. XmlElement element = doItem as XmlElement;
  679. if (element == null)
  680. continue;
  681. string index = element.GetAttribute("Index");
  682. string bufferOffset = element.GetAttribute("BufferOffset");
  683. if (string.IsNullOrEmpty(bufferOffset))
  684. bufferOffset = index;
  685. string name = element.GetAttribute("Name");
  686. string address = element.GetAttribute("Addr");
  687. string type = element.GetAttribute("Type");
  688. string description = element.GetAttribute("Description");
  689. if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(index) || string.IsNullOrEmpty(bufferOffset))
  690. continue;
  691. name = name.Trim();
  692. index = index.Trim();
  693. bufferOffset = bufferOffset.Trim();
  694. string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  695. int intIndex;
  696. if (!int.TryParse(index, out intIndex))
  697. continue;
  698. int intBufferOffset;
  699. if (!int.TryParse(bufferOffset, out intBufferOffset))
  700. continue;
  701. if (!_doBuffer.ContainsKey(provider) || !_doBuffer[provider].ContainsKey(blockOffset))
  702. {
  703. throw new Exception("Not defined DO buffer from IO provider, " + provider);
  704. }
  705. DOAccessor doAccessor = new DOAccessor(moduleName, intBufferOffset, _doBuffer[provider][blockOffset]);
  706. _doMap[moduleName] = doAccessor;
  707. doAccessor.IoTableIndex = intIndex;
  708. doAccessor.Addr = address;
  709. doAccessor.Provider = provider;
  710. doAccessor.BlockOffset = blockOffset;
  711. doAccessor.Description = description;
  712. doAccessor.Type = type;
  713. if (!_doList.ContainsKey(provider))
  714. _doList[provider] = new List<DOAccessor>();
  715. _doList[provider].Add(doAccessor);
  716. _ioItemList[$"{provider}.DOItemList"].Add(new NotifiableIoItem()
  717. {
  718. Address = address,
  719. Name = moduleName,
  720. Description = description,
  721. Index = intIndex,
  722. Provider = provider,
  723. BlockOffset = blockOffset,
  724. BlockIndex = intIndex,
  725. });
  726. if (!isIgnoreSaveDB)
  727. DATA.Subscribe($"IO.{moduleName}", () => doAccessor.Value);
  728. }
  729. XmlNodeList lstAo = xml.SelectNodes("IO_DEFINE/Ana_Out/AO_ITEM");
  730. // < AO_ITEM Index = "0" BufferOffset="0" Name = "" Addr = "0" Description = "" />
  731. foreach (var aoItem in lstAo)
  732. {
  733. XmlElement element = aoItem as XmlElement;
  734. if (element == null)
  735. continue;
  736. string index = element.GetAttribute("Index");
  737. string bufferOffset = element.GetAttribute("BufferOffset");
  738. if (string.IsNullOrEmpty(bufferOffset))
  739. bufferOffset = index;
  740. string name = element.GetAttribute("Name");
  741. string address = element.GetAttribute("Addr");
  742. string type = element.GetAttribute("Type");
  743. string description = element.GetAttribute("Description");
  744. if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(index) || string.IsNullOrEmpty(bufferOffset))
  745. continue;
  746. name = name.Trim();
  747. index = index.Trim();
  748. bufferOffset = bufferOffset.Trim();
  749. string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  750. int intIndex;
  751. if (!int.TryParse(index, out intIndex))
  752. continue;
  753. int intBufferOffset;
  754. if (!int.TryParse(bufferOffset, out intBufferOffset))
  755. continue;
  756. if (!_aoBufferShort.ContainsKey(provider) || !_aoBufferShort[provider].ContainsKey(blockOffset))
  757. {
  758. throw new Exception("Not defined AO buffer from IO provider, " + provider);
  759. }
  760. AOAccessor aoAccessor = new AOAccessor(moduleName, intBufferOffset, _aoBufferShort[provider][blockOffset], _aoBufferFloat[provider][blockOffset]);
  761. _aoMap[moduleName] = aoAccessor;
  762. aoAccessor.IoTableIndex = intIndex;
  763. aoAccessor.Addr = address;
  764. aoAccessor.Provider = provider;
  765. aoAccessor.BlockOffset = blockOffset;
  766. aoAccessor.Description = description;
  767. aoAccessor.Type = type;
  768. if (!_aoList.ContainsKey(provider))
  769. _aoList[provider] = new List<AOAccessor>();
  770. _aoList[provider].Add(aoAccessor);
  771. _ioItemList[$"{provider}.AOItemList"].Add(new NotifiableIoItem()
  772. {
  773. Address = address,
  774. Name = moduleName,
  775. Description = description,
  776. Index = intIndex,
  777. Provider = provider,
  778. BlockOffset = blockOffset,
  779. BlockIndex = intIndex,
  780. });
  781. if (!isIgnoreSaveDB)
  782. {
  783. if (_aoBufferType.ContainsKey(provider) && _aoBufferType[provider].ContainsKey(blockOffset) &&
  784. _aoBufferType[provider][blockOffset] == typeof(float))
  785. {
  786. DATA.Subscribe($"IO.{moduleName}", () => aoAccessor.FloatValue);
  787. }
  788. else
  789. {
  790. DATA.Subscribe($"IO.{moduleName}", () => aoAccessor.Value);
  791. }
  792. }
  793. }
  794. XmlNodeList lstAi = xml.SelectNodes("IO_DEFINE/Ana_In/AI_ITEM");
  795. // < AO_ITEM Index = "0" BufferOffset="0" Name = "" Addr = "0" Description = "" />
  796. foreach (var aiItem in lstAi)
  797. {
  798. XmlElement element = aiItem as XmlElement;
  799. if (element == null)
  800. continue;
  801. string index = element.GetAttribute("Index");
  802. string bufferOffset = element.GetAttribute("BufferOffset");
  803. if (string.IsNullOrEmpty(bufferOffset))
  804. bufferOffset = index;
  805. string name = element.GetAttribute("Name");
  806. string address = element.GetAttribute("Addr");
  807. string type = element.GetAttribute("Type");
  808. string description = element.GetAttribute("Description");
  809. if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(index) || string.IsNullOrEmpty(bufferOffset))
  810. continue;
  811. name = name.Trim();
  812. index = index.Trim();
  813. bufferOffset = bufferOffset.Trim();
  814. string nameWithModule = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  815. int intIndex;
  816. if (!int.TryParse(index, out intIndex))
  817. continue;
  818. int intBufferOffset;
  819. if (!int.TryParse(bufferOffset, out intBufferOffset))
  820. continue;
  821. if (!_aiBufferShort.ContainsKey(provider) || !_aiBufferShort[provider].ContainsKey(blockOffset))
  822. {
  823. throw new Exception("Not defined AI buffer from IO provider, " + provider);
  824. }
  825. AIAccessor aiAccessor = new AIAccessor(nameWithModule, intBufferOffset, _aiBufferShort[provider][blockOffset], _aiBufferFloat[provider][blockOffset]);
  826. _aiMap[nameWithModule] = aiAccessor;
  827. aiAccessor.IoTableIndex = intIndex;
  828. aiAccessor.Addr = address;
  829. aiAccessor.Provider = provider;
  830. aiAccessor.BlockOffset = blockOffset;
  831. aiAccessor.Description = description;
  832. aiAccessor.Type = type;
  833. if (!_aiList.ContainsKey(provider))
  834. _aiList[provider] = new List<AIAccessor>();
  835. _aiList[provider].Add(aiAccessor);
  836. _ioItemList[$"{provider}.AIItemList"].Add(new NotifiableIoItem()
  837. {
  838. Address = address,
  839. Name = nameWithModule,
  840. Description = description,
  841. Index = intIndex,
  842. Provider = provider,
  843. BlockOffset = blockOffset,
  844. BlockIndex = intIndex,
  845. });
  846. if (!isIgnoreSaveDB)
  847. {
  848. if (_aiBufferType.ContainsKey(provider) && _aiBufferType[provider].ContainsKey(blockOffset) &&
  849. _aiBufferType[provider][blockOffset] == typeof(float))
  850. {
  851. DATA.Subscribe($"IO.{nameWithModule}", () => aiAccessor.FloatValue);
  852. }
  853. else
  854. {
  855. DATA.Subscribe($"IO.{nameWithModule}", () => aiAccessor.Value);
  856. }
  857. }
  858. }
  859. }
  860. public void SetIoMap(string provider, Dictionary<int, string> ioMappingPathFile)
  861. {
  862. foreach (var map in ioMappingPathFile)
  863. {
  864. SetIoMap(provider, map.Key, map.Value);
  865. }
  866. DATA.Subscribe(provider, "DIList", SubscribeDiData);
  867. DATA.Subscribe(provider, "DOList", SubscribeDoData);
  868. DATA.Subscribe(provider, "AIList", SubscribeAiData);
  869. DATA.Subscribe(provider, "AOList", SubscribeAoData);
  870. }
  871. public void SetIoMapByModule(string provider, int offset, string ioMappingPathFile, string module)
  872. {
  873. SetIoMap(provider, offset, ioMappingPathFile, module);
  874. DATA.Subscribe(provider, "DIList", SubscribeDiData);
  875. DATA.Subscribe(provider, "DOList", SubscribeDoData);
  876. DATA.Subscribe(provider, "AIList", SubscribeAiData);
  877. DATA.Subscribe(provider, "AOList", SubscribeAoData);
  878. }
  879. private void SubscribeIoItemList(string provider)
  880. {
  881. string diKey = $"{provider}.DIItemList";
  882. if (!_ioItemList.ContainsKey(diKey))
  883. {
  884. _ioItemList[diKey] = new List<NotifiableIoItem>();
  885. DATA.Subscribe(diKey, () => _ioItemList[diKey]);
  886. }
  887. string doKey = $"{provider}.DOItemList";
  888. if (!_ioItemList.ContainsKey(doKey))
  889. {
  890. _ioItemList[doKey] = new List<NotifiableIoItem>();
  891. DATA.Subscribe(doKey, () => _ioItemList[doKey]);
  892. }
  893. string aiKey = $"{provider}.AIItemList";
  894. if (!_ioItemList.ContainsKey(aiKey))
  895. {
  896. _ioItemList[aiKey] = new List<NotifiableIoItem>();
  897. DATA.Subscribe(aiKey, () => _ioItemList[aiKey]);
  898. }
  899. string aoKey = $"{provider}.AOItemList";
  900. if (!_ioItemList.ContainsKey(aoKey))
  901. {
  902. _ioItemList[aoKey] = new List<NotifiableIoItem>();
  903. DATA.Subscribe(aoKey, () => _ioItemList[aoKey]);
  904. }
  905. }
  906. }
  907. }