IOManager.cs 42 KB

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