IOManager.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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.Write(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. io.Buffer[io.Index] = BitConverter.ToInt16(flow, 0);
  137. if (io.Index < io.Buffer.Length - 1)
  138. {
  139. io.Buffer[io.Index + 1] = BitConverter.ToInt16(flow, 2);
  140. }
  141. EV.PostInfoLog("System", $"Change AO {name} to {setpoint}");
  142. return true;
  143. }
  144. private bool InvokeSetDoWithPrivoder(string arg1, object[] args)
  145. {
  146. string provider = (string)args[0];
  147. int offset = (int)args[1];
  148. string name = (string)args[2];
  149. bool setpoint = (bool)args[3];
  150. string reason;
  151. if (!CanSetDo(name, setpoint, out reason))
  152. {
  153. EV.PostWarningLog("System", $"Can not set DO {provider}.{name} to {setpoint}, {reason}");
  154. return false;
  155. }
  156. var doList = GetDOList(provider);
  157. if(doList == null)
  158. {
  159. EV.PostWarningLog("System", $"Can not set DO {provider}.{name} to {setpoint}, {reason}");
  160. return false;
  161. }
  162. var doAccessor = doList.FirstOrDefault(x => x.Name == name);
  163. if(doAccessor == default(DOAccessor))
  164. {
  165. EV.PostWarningLog("System", $"Can not set DO {provider}.{name} to {setpoint}, {reason}");
  166. return false;
  167. }
  168. if (!doAccessor.SetValue(setpoint, out reason))
  169. {
  170. EV.PostWarningLog("System", $"Can not set DO {provider}.{name} to {setpoint}, {reason}");
  171. return false;
  172. }
  173. EV.PostInfoLog("System", $"Change DO {provider}.{name} to {setpoint}");
  174. return true;
  175. }
  176. private bool InvokeSetAiBuffer(string arg1, object[] args)
  177. {
  178. string provider = (string)args[0];
  179. int offset = (int)args[1];
  180. short[] buffer = (short [])args[2];
  181. SetAiBuffer(provider, offset, buffer);
  182. return true;
  183. }
  184. private bool InvokeSetDiBuffer(string arg1, object[] args)
  185. {
  186. string provider = (string)args[0];
  187. int offset = (int)args[1];
  188. bool[] buffer = (bool[])args[2];
  189. SetDiBuffer(provider, offset, buffer);
  190. return true;
  191. }
  192. private bool InvokeSetAoWithPrivoder(string arg1, object[] args)
  193. {
  194. string provider = (string)args[0];
  195. int offset = (int)args[1];
  196. string name = (string)args[2];
  197. float setpoint = (float)args[3];
  198. string reason = "";
  199. var aoList = GetAOList(provider);
  200. if (aoList == null)
  201. {
  202. EV.PostWarningLog("System", $"Can not set AO {provider}.{name} to {setpoint}, {reason}");
  203. return false;
  204. }
  205. var aoAccessor = aoList.FirstOrDefault(x => x.Name == name);
  206. if (aoAccessor == default(AOAccessor))
  207. {
  208. EV.PostWarningLog("System", $"Can not set AO {provider}.{name} to {setpoint}, {reason}");
  209. return false;
  210. }
  211. aoAccessor.Value = (short)setpoint;
  212. EV.PostInfoLog("System", $"Change DO {provider}.{name} to {setpoint}");
  213. return true;
  214. }
  215. public T GetIO<T>(string name) where T : class
  216. {
  217. if (typeof(T) == typeof(DIAccessor) && _diMap.ContainsKey(name))
  218. {
  219. return _diMap[name] as T;
  220. }
  221. if (typeof(T) == typeof(DOAccessor) && _doMap.ContainsKey(name))
  222. {
  223. return _doMap[name] as T;
  224. }
  225. if (typeof(T) == typeof(AIAccessor) && _aiMap.ContainsKey(name))
  226. {
  227. return _aiMap[name] as T;
  228. }
  229. if (typeof(T) == typeof(AOAccessor) && _aoMap.ContainsKey(name))
  230. {
  231. return _aoMap[name] as T;
  232. }
  233. return null;
  234. }
  235. public Dictionary<int, bool[]> GetDiBuffer(string source)
  236. {
  237. if (_diBuffer.ContainsKey(source))
  238. {
  239. return _diBuffer[source];
  240. }
  241. return null;
  242. }
  243. public Dictionary<int, bool[]> GetDoBuffer(string source)
  244. {
  245. if (_doBuffer.ContainsKey(source))
  246. {
  247. return _doBuffer[source];
  248. }
  249. return null;
  250. }
  251. public Dictionary<int, short[]> GetAiBuffer(string source)
  252. {
  253. if (_aiBuffer.ContainsKey(source))
  254. {
  255. return _aiBuffer[source];
  256. }
  257. return null;
  258. }
  259. public Dictionary<int, short[]> GetAoBuffer(string source)
  260. {
  261. if (_aoBuffer.ContainsKey(source))
  262. {
  263. return _aoBuffer[source];
  264. }
  265. return null;
  266. }
  267. public void SetDiBuffer(string source, int offset, bool[] buffer)
  268. {
  269. if (_diBuffer.ContainsKey(source) && _diBuffer[source].ContainsKey(offset))
  270. {
  271. for (int i = 0; i < buffer.Length && i < _diBuffer[source][offset].Length; i++)
  272. {
  273. _diBuffer[source][offset][i] = buffer[i];
  274. }
  275. }
  276. }
  277. public void SetAiBuffer(string source, int offset, short[] buffer, int skipSize = 0)
  278. {
  279. if (_aiBuffer.ContainsKey(source) && _aiBuffer[source].ContainsKey(offset))
  280. {
  281. for (int i = 0; i < buffer.Length && i < _aiBuffer[source][offset].Length; i++)
  282. {
  283. _aiBuffer[source][offset][i + skipSize] = buffer[i];
  284. }
  285. }
  286. }
  287. public void SetDoBuffer(string source, int offset, bool[] buffer)
  288. {
  289. if (_doBuffer.ContainsKey(source) && _doBuffer[source].ContainsKey(offset))
  290. {
  291. for (int i = 0; i < buffer.Length && i < _doBuffer[source][offset].Length; i++)
  292. {
  293. _doBuffer[source][offset][i] = buffer[i];
  294. }
  295. }
  296. }
  297. public void SetAoBuffer(string source, int offset, short[] buffer)
  298. {
  299. if (_aoBuffer.ContainsKey(source) && _aoBuffer[source].ContainsKey(offset))
  300. {
  301. for (int i = 0; i < buffer.Length && i < _aoBuffer[source][offset].Length; i++)
  302. {
  303. _aoBuffer[source][offset][i] = buffer[i];
  304. }
  305. }
  306. }
  307. //spin recipe set
  308. public void SetAoBuffer(string source, int offset, short[] buffer, int bufferStartIndex)
  309. {
  310. if (_aoBuffer.ContainsKey(source) && _aoBuffer[source].ContainsKey(offset) && _aoBuffer[source][offset].Length > bufferStartIndex)
  311. {
  312. for (int i = 0; i < buffer.Length && bufferStartIndex + i < _aoBuffer[source][offset].Length; i++)
  313. {
  314. _aoBuffer[source][offset][bufferStartIndex + i] = buffer[i];
  315. }
  316. }
  317. }
  318. public void SetBufferBlock(string provider, List<IoBlockItem> lstBlocks)
  319. {
  320. foreach (var ioBlockItem in lstBlocks)
  321. {
  322. switch (ioBlockItem.Type)
  323. {
  324. case IoType.AI:
  325. if (!_aiBuffer.ContainsKey(provider))
  326. {
  327. _aiBuffer[provider] = new Dictionary<int, short[]>();
  328. }
  329. if (!_aiBuffer[provider].ContainsKey(ioBlockItem.Offset))
  330. {
  331. _aiBuffer[provider][ioBlockItem.Offset] = new short[ioBlockItem.Size];
  332. }
  333. break;
  334. case IoType.AO:
  335. if (!_aoBuffer.ContainsKey(provider))
  336. {
  337. _aoBuffer[provider] = new Dictionary<int, short[]>();
  338. }
  339. if (!_aoBuffer[provider].ContainsKey(ioBlockItem.Offset))
  340. {
  341. _aoBuffer[provider][ioBlockItem.Offset] = new short[ioBlockItem.Size];
  342. }
  343. break;
  344. case IoType.DI:
  345. if (!_diBuffer.ContainsKey(provider))
  346. {
  347. _diBuffer[provider] = new Dictionary<int, bool[]>();
  348. }
  349. if (!_diBuffer[provider].ContainsKey(ioBlockItem.Offset))
  350. {
  351. _diBuffer[provider][ioBlockItem.Offset] = new bool[ioBlockItem.Size];
  352. }
  353. break;
  354. case IoType.DO:
  355. if (!_doBuffer.ContainsKey(provider))
  356. {
  357. _doBuffer[provider] = new Dictionary<int, bool[]>();
  358. }
  359. if (!_doBuffer[provider].ContainsKey(ioBlockItem.Offset))
  360. {
  361. _doBuffer[provider][ioBlockItem.Offset] = new bool[ioBlockItem.Size];
  362. }
  363. break;
  364. }
  365. }
  366. }
  367. List<NotifiableIoItem> SubscribeDiData()
  368. {
  369. List<NotifiableIoItem> result = new List<NotifiableIoItem>();
  370. foreach (var ioDefine in _diMap)
  371. {
  372. NotifiableIoItem di = new NotifiableIoItem()
  373. {
  374. Address = ioDefine.Value.Addr,
  375. Name = ioDefine.Value.Name,
  376. Description = ioDefine.Value.Description,
  377. Index = ioDefine.Value.Index,
  378. BoolValue = ioDefine.Value.Value,
  379. Provider = ioDefine.Value.Provider,
  380. BlockOffset = ioDefine.Value.BlockOffset,
  381. BlockIndex = ioDefine.Value.Index,
  382. };
  383. result.Add(di);
  384. }
  385. return result;
  386. }
  387. List<NotifiableIoItem> SubscribeDoData()
  388. {
  389. List<NotifiableIoItem> result = new List<NotifiableIoItem>();
  390. foreach (var ioDefine in _doMap)
  391. {
  392. NotifiableIoItem io = new NotifiableIoItem()
  393. {
  394. Address = ioDefine.Value.Addr,
  395. Name = ioDefine.Value.Name,
  396. Description = ioDefine.Value.Description,
  397. Index = ioDefine.Value.Index,
  398. BoolValue = ioDefine.Value.Value,
  399. Provider = ioDefine.Value.Provider,
  400. BlockOffset = ioDefine.Value.BlockOffset,
  401. BlockIndex = ioDefine.Value.Index,
  402. };
  403. result.Add(io);
  404. }
  405. return result;
  406. }
  407. List<NotifiableIoItem> SubscribeAiData()
  408. {
  409. List<NotifiableIoItem> result = new List<NotifiableIoItem>();
  410. foreach (var ioDefine in _aiMap)
  411. {
  412. NotifiableIoItem di = new NotifiableIoItem()
  413. {
  414. Address = ioDefine.Value.Addr,
  415. Name = ioDefine.Value.Name,
  416. Description = ioDefine.Value.Description,
  417. Index = ioDefine.Value.Index,
  418. ShortValue = ioDefine.Value.Value,
  419. Provider = ioDefine.Value.Provider,
  420. BlockOffset = ioDefine.Value.BlockOffset,
  421. BlockIndex = ioDefine.Value.Index,
  422. };
  423. result.Add(di);
  424. }
  425. return result;
  426. }
  427. List<NotifiableIoItem> SubscribeAoData()
  428. {
  429. List<NotifiableIoItem> result = new List<NotifiableIoItem>();
  430. foreach (var ioDefine in _aoMap)
  431. {
  432. NotifiableIoItem ao = new NotifiableIoItem()
  433. {
  434. Address = ioDefine.Value.Addr,
  435. Name = ioDefine.Value.Name,
  436. Description = ioDefine.Value.Description,
  437. Index = ioDefine.Value.Index,
  438. ShortValue = ioDefine.Value.Value,
  439. Provider = ioDefine.Value.Provider,
  440. BlockOffset = ioDefine.Value.BlockOffset,
  441. BlockIndex = ioDefine.Value.Index,
  442. };
  443. result.Add(ao);
  444. }
  445. return result;
  446. }
  447. public void SetIoMap(string provider, int blockOffset, List<DIAccessor> ioList)
  448. {
  449. SubscribeIoItemList(provider);
  450. var scConfig = SC.GetConfigItem("System.IsIgnoreSaveDB");
  451. var isIgnoreSaveDB = scConfig != null && scConfig.BoolValue;
  452. foreach (var accessor in ioList)
  453. {
  454. accessor.Provider = provider;
  455. accessor.BlockOffset = blockOffset;
  456. _diMap[accessor.Name] = accessor;
  457. if (!_diList.ContainsKey(provider))
  458. _diList[provider] = new List<DIAccessor>();
  459. _diList[provider].Add(accessor);
  460. _ioItemList[$"{provider}.DIItemList"].Add(new NotifiableIoItem()
  461. {
  462. Address = accessor.Addr,
  463. Name = accessor.Name,
  464. Description = accessor.Description,
  465. Index = accessor.Index,
  466. Provider = provider,
  467. BlockOffset = blockOffset,
  468. BlockIndex = accessor.BlockOffset,
  469. });
  470. if (!isIgnoreSaveDB)
  471. DATA.Subscribe($"IO.{accessor.Name}", () => accessor.Value);
  472. }
  473. }
  474. public void SetIoMap(string provider, int blockOffset, List<DOAccessor> ioList)
  475. {
  476. SubscribeIoItemList(provider);
  477. var scConfig = SC.GetConfigItem("System.IsIgnoreSaveDB");
  478. var isIgnoreSaveDB = scConfig != null && scConfig.BoolValue;
  479. foreach (var accessor in ioList)
  480. {
  481. accessor.Provider = provider;
  482. accessor.BlockOffset = blockOffset;
  483. _doMap[accessor.Name] = accessor;
  484. if (!_doList.ContainsKey(provider))
  485. _doList[provider] = new List<DOAccessor>();
  486. _doList[provider].Add(accessor);
  487. _ioItemList[$"{provider}.DOItemList"].Add(new NotifiableIoItem()
  488. {
  489. Address = accessor.Addr,
  490. Name = accessor.Name,
  491. Description = accessor.Description,
  492. Index = accessor.Index,
  493. Provider = provider,
  494. BlockOffset = blockOffset,
  495. BlockIndex = accessor.BlockOffset,
  496. });
  497. if (!isIgnoreSaveDB)
  498. DATA.Subscribe($"IO.{accessor.Name}", () => accessor.Value);
  499. }
  500. }
  501. public void SetIoMap(string provider, int blockOffset, List<AIAccessor> ioList)
  502. {
  503. SubscribeIoItemList(provider);
  504. var scConfig = SC.GetConfigItem("System.IsIgnoreSaveDB");
  505. var isIgnoreSaveDB = scConfig != null && scConfig.BoolValue;
  506. foreach (var accessor in ioList)
  507. {
  508. accessor.Provider = provider;
  509. accessor.BlockOffset = blockOffset;
  510. _aiMap[accessor.Name] = accessor;
  511. if (!_aiList.ContainsKey(provider))
  512. _aiList[provider] = new List<AIAccessor>();
  513. _aiList[provider].Add(accessor);
  514. _ioItemList[$"{provider}.AIItemList"].Add(new NotifiableIoItem()
  515. {
  516. Address = accessor.Addr,
  517. Name = accessor.Name,
  518. Description = accessor.Description,
  519. Index = accessor.Index,
  520. Provider = provider,
  521. BlockOffset = blockOffset,
  522. BlockIndex = accessor.BlockOffset,
  523. });
  524. if (!isIgnoreSaveDB)
  525. {
  526. DATA.Subscribe($"IO.{accessor.Name}", () => accessor.Value);
  527. DATA.Subscribe($"IO32.{accessor.Name}", () =>
  528. {
  529. if (accessor.Index < accessor.Buffer.Length - 1)
  530. {
  531. byte[] high = BitConverter.GetBytes(accessor.Buffer[accessor.Index]);
  532. byte[] low = BitConverter.GetBytes(accessor.Buffer[accessor.Index + 1]);
  533. return BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  534. }
  535. else
  536. {
  537. return accessor.Value;
  538. }
  539. });
  540. }
  541. }
  542. }
  543. public void SetIoMap(string provider, int blockOffset, List<AOAccessor> ioList)
  544. {
  545. SubscribeIoItemList(provider);
  546. var scConfig = SC.GetConfigItem("System.IsIgnoreSaveDB");
  547. var isIgnoreSaveDB = scConfig != null && scConfig.BoolValue;
  548. foreach (var accessor in ioList)
  549. {
  550. accessor.Provider = provider;
  551. accessor.BlockOffset = blockOffset;
  552. _aoMap[accessor.Name] = accessor;
  553. if (!_aoList.ContainsKey(provider))
  554. _aoList[provider] = new List<AOAccessor>();
  555. _aoList[provider].Add(accessor);
  556. _ioItemList[$"{provider}.AOItemList"].Add(new NotifiableIoItem()
  557. {
  558. Address = accessor.Addr,
  559. Name = accessor.Name,
  560. Description = accessor.Description,
  561. Index = accessor.Index,
  562. Provider = provider,
  563. BlockOffset = blockOffset,
  564. BlockIndex = accessor.BlockOffset,
  565. });
  566. if (!isIgnoreSaveDB)
  567. {
  568. DATA.Subscribe($"IO.{accessor.Name}", () => accessor.Value);
  569. DATA.Subscribe($"IO32.{accessor.Name}", () =>
  570. {
  571. if (accessor.Index < accessor.Buffer.Length - 1)
  572. {
  573. byte[] high = BitConverter.GetBytes(accessor.Buffer[accessor.Index]);
  574. byte[] low = BitConverter.GetBytes(accessor.Buffer[accessor.Index + 1]);
  575. return BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  576. }
  577. else
  578. {
  579. return accessor.Value;
  580. }
  581. });
  582. }
  583. }
  584. }
  585. public void SetIoMap(string provider, int blockOffset, string xmlPathFile, string module="")
  586. {
  587. SubscribeIoItemList(provider);
  588. XmlDocument xml = new XmlDocument();
  589. xml.Load(xmlPathFile);
  590. XmlNodeList lstDi = xml.SelectNodes("IO_DEFINE/Dig_In/DI_ITEM");
  591. var scConfig = SC.GetConfigItem("System.IsIgnoreSaveDB");
  592. var isIgnoreSaveDB = scConfig == null ? false : scConfig.BoolValue;
  593. //<DI_ITEM Index="0" Name="" BufferOffset="0" Addr="0" Description=""/>
  594. List<DIAccessor> diList = new List<DIAccessor>();
  595. foreach (var diItem in lstDi)
  596. {
  597. XmlElement element = diItem as XmlElement;
  598. if (element == null)
  599. continue;
  600. string index = element.GetAttribute("Index");
  601. string bufferOffset = element.GetAttribute("BufferOffset");
  602. if (string.IsNullOrEmpty(bufferOffset))
  603. bufferOffset = index;
  604. string name = element.GetAttribute("Name");
  605. string address = element.GetAttribute("Addr");
  606. string description = element.GetAttribute("Description");
  607. if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(index) || string.IsNullOrEmpty(bufferOffset))
  608. continue;
  609. name = name.Trim();
  610. index = index.Trim();
  611. bufferOffset = bufferOffset.Trim();
  612. string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  613. int intIndex;
  614. if (!int.TryParse(index, out intIndex))
  615. continue;
  616. int intBufferOffset;
  617. if (!int.TryParse(bufferOffset, out intBufferOffset))
  618. continue;
  619. if (!_diBuffer.ContainsKey(provider) || !_diBuffer[provider].ContainsKey(blockOffset))
  620. {
  621. throw new Exception("Not defined DI buffer from IO provider, " + provider);
  622. }
  623. DIAccessor diAccessor = new DIAccessor(moduleName, intBufferOffset, _diBuffer[provider][blockOffset], _diBuffer[provider][blockOffset]);
  624. diAccessor.IoTableIndex = intIndex;
  625. diAccessor.Addr = address;
  626. diAccessor.Provider = provider;
  627. diAccessor.BlockOffset = blockOffset;
  628. diAccessor.Description = description;
  629. diList.Add(diAccessor);
  630. _diMap[moduleName] = diAccessor;
  631. if (!_diList.ContainsKey(provider))
  632. _diList[provider] = new List<DIAccessor>();
  633. _diList[provider].Add(diAccessor);
  634. _ioItemList[$"{provider}.DIItemList"].Add(new NotifiableIoItem()
  635. {
  636. Address = address,
  637. Name = moduleName,
  638. Description = description,
  639. Index = intIndex,
  640. Provider = provider,
  641. BlockOffset = blockOffset,
  642. BlockIndex = intIndex,
  643. });
  644. if (!isIgnoreSaveDB)
  645. DATA.Subscribe($"IO.{moduleName}", () => diAccessor.Value);
  646. }
  647. XmlNodeList lstDo = xml.SelectNodes("IO_DEFINE/Dig_Out/DO_ITEM");
  648. // < DO_ITEM Index = "0" BufferOffset="0" Name = "" Addr = "0" Description = "" />
  649. foreach (var doItem in lstDo)
  650. {
  651. XmlElement element = doItem as XmlElement;
  652. if (element == null)
  653. continue;
  654. string index = element.GetAttribute("Index");
  655. string bufferOffset = element.GetAttribute("BufferOffset");
  656. if (string.IsNullOrEmpty(bufferOffset))
  657. bufferOffset = index;
  658. string name = element.GetAttribute("Name");
  659. string address = element.GetAttribute("Addr");
  660. string description = element.GetAttribute("Description");
  661. if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(index) || string.IsNullOrEmpty(bufferOffset))
  662. continue;
  663. name = name.Trim();
  664. index = index.Trim();
  665. bufferOffset = bufferOffset.Trim();
  666. string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  667. int intIndex;
  668. if (!int.TryParse(index, out intIndex))
  669. continue;
  670. int intBufferOffset;
  671. if (!int.TryParse(bufferOffset, out intBufferOffset))
  672. continue;
  673. if (!_doBuffer.ContainsKey(provider) || !_doBuffer[provider].ContainsKey(blockOffset))
  674. {
  675. throw new Exception("Not defined DO buffer from IO provider, " + provider);
  676. }
  677. DOAccessor doAccessor = new DOAccessor(moduleName, intBufferOffset, _doBuffer[provider][blockOffset]);
  678. _doMap[moduleName] = doAccessor;
  679. doAccessor.IoTableIndex = intIndex;
  680. doAccessor.Addr = address;
  681. doAccessor.Provider = provider;
  682. doAccessor.BlockOffset = blockOffset;
  683. doAccessor.Description = description;
  684. if (!_doList.ContainsKey(provider))
  685. _doList[provider] = new List<DOAccessor>();
  686. _doList[provider].Add(doAccessor);
  687. _ioItemList[$"{provider}.DOItemList"].Add(new NotifiableIoItem()
  688. {
  689. Address = address,
  690. Name = moduleName,
  691. Description = description,
  692. Index = intIndex,
  693. Provider = provider,
  694. BlockOffset = blockOffset,
  695. BlockIndex = intIndex,
  696. });
  697. if (!isIgnoreSaveDB)
  698. DATA.Subscribe($"IO.{moduleName}", () => doAccessor.Value);
  699. }
  700. XmlNodeList lstAo = xml.SelectNodes("IO_DEFINE/Ana_Out/AO_ITEM");
  701. // < AO_ITEM Index = "0" BufferOffset="0" Name = "" Addr = "0" Description = "" />
  702. foreach (var aoItem in lstAo)
  703. {
  704. XmlElement element = aoItem as XmlElement;
  705. if (element == null)
  706. continue;
  707. string index = element.GetAttribute("Index");
  708. string bufferOffset = element.GetAttribute("BufferOffset");
  709. if (string.IsNullOrEmpty(bufferOffset))
  710. bufferOffset = index;
  711. string name = element.GetAttribute("Name");
  712. string address = element.GetAttribute("Addr");
  713. string description = element.GetAttribute("Description");
  714. if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(index) || string.IsNullOrEmpty(bufferOffset))
  715. continue;
  716. name = name.Trim();
  717. index = index.Trim();
  718. bufferOffset = bufferOffset.Trim();
  719. string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  720. int intIndex;
  721. if (!int.TryParse(index, out intIndex))
  722. continue;
  723. int intBufferOffset;
  724. if (!int.TryParse(bufferOffset, out intBufferOffset))
  725. continue;
  726. if (!_aoBuffer.ContainsKey(provider) || !_aoBuffer[provider].ContainsKey(blockOffset))
  727. {
  728. throw new Exception("Not defined AO buffer from IO provider, " + provider);
  729. }
  730. AOAccessor aoAccessor = new AOAccessor(moduleName, intBufferOffset, _aoBuffer[provider][blockOffset]);
  731. _aoMap[moduleName] = aoAccessor;
  732. aoAccessor.IoTableIndex = intIndex;
  733. aoAccessor.Addr = address;
  734. aoAccessor.Provider = provider;
  735. aoAccessor.BlockOffset = blockOffset;
  736. aoAccessor.Description = description;
  737. if (!_aoList.ContainsKey(provider))
  738. _aoList[provider] = new List<AOAccessor>();
  739. _aoList[provider].Add(aoAccessor);
  740. _ioItemList[$"{provider}.AOItemList"].Add(new NotifiableIoItem()
  741. {
  742. Address = address,
  743. Name = moduleName,
  744. Description = description,
  745. Index = intIndex,
  746. Provider = provider,
  747. BlockOffset = blockOffset,
  748. BlockIndex = intIndex,
  749. });
  750. if (!isIgnoreSaveDB)
  751. {
  752. DATA.Subscribe($"IO.{moduleName}", () => aoAccessor.Value);
  753. DATA.Subscribe($"IO32.{moduleName}", () =>
  754. {
  755. if (aoAccessor.Index < aoAccessor.Buffer.Length-1)
  756. {
  757. byte[] high = BitConverter.GetBytes(aoAccessor.Buffer[aoAccessor.Index]);
  758. byte[] low = BitConverter.GetBytes(aoAccessor.Buffer[aoAccessor.Index + 1]);
  759. return BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  760. }
  761. else
  762. {
  763. return aoAccessor.Value;
  764. }
  765. });
  766. }
  767. }
  768. XmlNodeList lstAi = xml.SelectNodes("IO_DEFINE/Ana_In/AI_ITEM");
  769. // < AO_ITEM Index = "0" BufferOffset="0" Name = "" Addr = "0" Description = "" />
  770. foreach (var aiItem in lstAi)
  771. {
  772. XmlElement element = aiItem as XmlElement;
  773. if (element == null)
  774. continue;
  775. string index = element.GetAttribute("Index");
  776. string bufferOffset = element.GetAttribute("BufferOffset");
  777. if (string.IsNullOrEmpty(bufferOffset))
  778. bufferOffset = index;
  779. string name = element.GetAttribute("Name");
  780. string address = element.GetAttribute("Addr");
  781. string description = element.GetAttribute("Description");
  782. if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(index) || string.IsNullOrEmpty(bufferOffset))
  783. continue;
  784. name = name.Trim();
  785. index = index.Trim();
  786. bufferOffset = bufferOffset.Trim();
  787. string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  788. int intIndex;
  789. if (!int.TryParse(index, out intIndex))
  790. continue;
  791. int intBufferOffset;
  792. if (!int.TryParse(bufferOffset, out intBufferOffset))
  793. continue;
  794. if (!_aiBuffer.ContainsKey(provider) || !_aiBuffer[provider].ContainsKey(blockOffset))
  795. {
  796. throw new Exception("Not defined AI buffer from IO provider, " + provider);
  797. }
  798. AIAccessor aiAccessor = new AIAccessor(moduleName, intBufferOffset, _aiBuffer[provider][blockOffset]);
  799. _aiMap[moduleName] = aiAccessor;
  800. aiAccessor.IoTableIndex = intIndex;
  801. aiAccessor.Addr = address;
  802. aiAccessor.Provider = provider;
  803. aiAccessor.BlockOffset = blockOffset;
  804. aiAccessor.Description = description;
  805. if (!_aiList.ContainsKey(provider))
  806. _aiList[provider] = new List<AIAccessor>();
  807. _aiList[provider].Add(aiAccessor);
  808. _ioItemList[$"{provider}.AIItemList"].Add(new NotifiableIoItem()
  809. {
  810. Address = address,
  811. Name = moduleName,
  812. Description = description,
  813. Index = intIndex,
  814. Provider = provider,
  815. BlockOffset = blockOffset,
  816. BlockIndex = intIndex,
  817. });
  818. if (!isIgnoreSaveDB)
  819. {
  820. DATA.Subscribe($"IO.{moduleName}", () => aiAccessor.Value);
  821. DATA.Subscribe($"IO32.{moduleName}", () =>
  822. {
  823. if (aiAccessor.Index < aiAccessor.Buffer.Length-1)
  824. {
  825. byte[] high = BitConverter.GetBytes(aiAccessor.Buffer[aiAccessor.Index]);
  826. byte[] low = BitConverter.GetBytes(aiAccessor.Buffer[aiAccessor.Index + 1]);
  827. return BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  828. }
  829. else
  830. {
  831. return aiAccessor.Value;
  832. }
  833. });
  834. }
  835. }
  836. }
  837. public void SetIoMap(string provider, Dictionary<int, string> ioMappingPathFile)
  838. {
  839. foreach (var map in ioMappingPathFile)
  840. {
  841. SetIoMap(provider, map.Key, map.Value);
  842. }
  843. DATA.Subscribe(provider, "DIList", SubscribeDiData);
  844. DATA.Subscribe(provider, "DOList", SubscribeDoData);
  845. DATA.Subscribe(provider, "AIList", SubscribeAiData);
  846. DATA.Subscribe(provider, "AOList", SubscribeAoData);
  847. }
  848. public void SetIoMapByModule(string provider, int offset, string ioMappingPathFile, string module)
  849. {
  850. SetIoMap(provider, offset, ioMappingPathFile, module);
  851. DATA.Subscribe(provider, "DIList", SubscribeDiData);
  852. DATA.Subscribe(provider, "DOList", SubscribeDoData);
  853. DATA.Subscribe(provider, "AIList", SubscribeAiData);
  854. DATA.Subscribe(provider, "AOList", SubscribeAoData);
  855. }
  856. private void SubscribeIoItemList(string provider)
  857. {
  858. string diKey = $"{provider}.DIItemList";
  859. if (!_ioItemList.ContainsKey(diKey))
  860. {
  861. _ioItemList[diKey] = new List<NotifiableIoItem>();
  862. DATA.Subscribe(diKey, () => _ioItemList[diKey]);
  863. }
  864. string doKey = $"{provider}.DOItemList";
  865. if (!_ioItemList.ContainsKey(doKey))
  866. {
  867. _ioItemList[doKey] = new List<NotifiableIoItem>();
  868. DATA.Subscribe(doKey, () => _ioItemList[doKey]);
  869. }
  870. string aiKey = $"{provider}.AIItemList";
  871. if (!_ioItemList.ContainsKey(aiKey))
  872. {
  873. _ioItemList[aiKey] = new List<NotifiableIoItem>();
  874. DATA.Subscribe(aiKey, () => _ioItemList[aiKey]);
  875. }
  876. string aoKey = $"{provider}.AOItemList";
  877. if (!_ioItemList.ContainsKey(aoKey))
  878. {
  879. _ioItemList[aoKey] = new List<NotifiableIoItem>();
  880. DATA.Subscribe(aoKey, () => _ioItemList[aoKey]);
  881. }
  882. }
  883. }
  884. }