IOManager.cs 39 KB

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