IOManager.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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);
  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. {
  532. DATA.Subscribe($"IO.{accessor.Name}", () => accessor.Value, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  533. }
  534. else
  535. {
  536. DATA.Subscribe($"IO.{accessor.Name}", () => accessor.Value);
  537. }
  538. }
  539. }
  540. public void SetIoMap(string provider, int blockOffset, List<AIAccessor> ioList)
  541. {
  542. SubscribeIoItemList(provider);
  543. var scConfig = SC.GetConfigItem("System.IsIgnoreSaveDB");
  544. var isIgnoreSaveDB = scConfig != null && scConfig.BoolValue;
  545. foreach (var accessor in ioList)
  546. {
  547. accessor.Provider = provider;
  548. accessor.BlockOffset = blockOffset;
  549. _aiMap[accessor.Name] = accessor;
  550. if (!_aiList.ContainsKey(provider))
  551. _aiList[provider] = new List<AIAccessor>();
  552. _aiList[provider].Add(accessor);
  553. _ioItemList[$"{provider}.AIItemList"].Add(new NotifiableIoItem()
  554. {
  555. Address = accessor.Addr,
  556. Name = accessor.Name,
  557. Description = accessor.Description,
  558. Index = accessor.Index,
  559. Provider = provider,
  560. BlockOffset = blockOffset,
  561. BlockIndex = accessor.BlockOffset,
  562. });
  563. if (isIgnoreSaveDB)
  564. {
  565. DATA.Subscribe($"IO.{accessor.Name}", () => accessor.Value, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  566. DATA.Subscribe($"IO32.{accessor.Name}", () =>
  567. {
  568. if (accessor.Index < accessor.Buffer.Length - 1)
  569. {
  570. byte[] high = BitConverter.GetBytes(accessor.Buffer[accessor.Index]);
  571. byte[] low = BitConverter.GetBytes(accessor.Buffer[accessor.Index + 1]);
  572. return BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  573. }
  574. else
  575. {
  576. return accessor.Value;
  577. }
  578. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  579. }
  580. else
  581. {
  582. DATA.Subscribe($"IO.{accessor.Name}", () => accessor.Value);
  583. DATA.Subscribe($"IO32.{accessor.Name}", () =>
  584. {
  585. if (accessor.Index < accessor.Buffer.Length - 1)
  586. {
  587. byte[] high = BitConverter.GetBytes(accessor.Buffer[accessor.Index]);
  588. byte[] low = BitConverter.GetBytes(accessor.Buffer[accessor.Index + 1]);
  589. return BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  590. }
  591. else
  592. {
  593. return accessor.Value;
  594. }
  595. });
  596. }
  597. }
  598. }
  599. public void SetIoMap(string provider, int blockOffset, List<AOAccessor> ioList)
  600. {
  601. SubscribeIoItemList(provider);
  602. var scConfig = SC.GetConfigItem("System.IsIgnoreSaveDB");
  603. var isIgnoreSaveDB = scConfig != null && scConfig.BoolValue;
  604. foreach (var accessor in ioList)
  605. {
  606. accessor.Provider = provider;
  607. accessor.BlockOffset = blockOffset;
  608. _aoMap[accessor.Name] = accessor;
  609. if (!_aoList.ContainsKey(provider))
  610. _aoList[provider] = new List<AOAccessor>();
  611. _aoList[provider].Add(accessor);
  612. _ioItemList[$"{provider}.AOItemList"].Add(new NotifiableIoItem()
  613. {
  614. Address = accessor.Addr,
  615. Name = accessor.Name,
  616. Description = accessor.Description,
  617. Index = accessor.Index,
  618. Provider = provider,
  619. BlockOffset = blockOffset,
  620. BlockIndex = accessor.BlockOffset,
  621. });
  622. if (isIgnoreSaveDB)
  623. {
  624. DATA.Subscribe($"IO.{accessor.Name}", () => accessor.Value,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  625. DATA.Subscribe($"IO32.{accessor.Name}", () =>
  626. {
  627. if (accessor.Index < accessor.Buffer.Length - 1)
  628. {
  629. byte[] high = BitConverter.GetBytes(accessor.Buffer[accessor.Index]);
  630. byte[] low = BitConverter.GetBytes(accessor.Buffer[accessor.Index + 1]);
  631. return BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  632. }
  633. else
  634. {
  635. return accessor.Value;
  636. }
  637. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  638. }
  639. else
  640. {
  641. DATA.Subscribe($"IO.{accessor.Name}", () => accessor.Value);
  642. DATA.Subscribe($"IO32.{accessor.Name}", () =>
  643. {
  644. if (accessor.Index < accessor.Buffer.Length - 1)
  645. {
  646. byte[] high = BitConverter.GetBytes(accessor.Buffer[accessor.Index]);
  647. byte[] low = BitConverter.GetBytes(accessor.Buffer[accessor.Index + 1]);
  648. return BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  649. }
  650. else
  651. {
  652. return accessor.Value;
  653. }
  654. });
  655. }
  656. }
  657. }
  658. public void SetIoMap(string provider, int blockOffset, string xmlPathFile, string module="")
  659. {
  660. SubscribeIoItemList(provider);
  661. XmlDocument xml = new XmlDocument();
  662. xml.Load(xmlPathFile);
  663. XmlNodeList lstDi = xml.SelectNodes("IO_DEFINE/Dig_In/DI_ITEM");
  664. var scConfig = SC.GetConfigItem("System.IsIgnoreSaveDB");
  665. var isIgnoreSaveDB = scConfig == null ? false : scConfig.BoolValue;
  666. //<DI_ITEM Index="0" Name="" BufferOffset="0" Addr="0" Description=""/>
  667. List<DIAccessor> diList = new List<DIAccessor>();
  668. foreach (var diItem in lstDi)
  669. {
  670. XmlElement element = diItem as XmlElement;
  671. if (element == null)
  672. continue;
  673. string index = element.GetAttribute("Index");
  674. string bufferOffset = element.GetAttribute("BufferOffset");
  675. if (string.IsNullOrEmpty(bufferOffset))
  676. bufferOffset = index;
  677. string name = element.GetAttribute("Name");
  678. string address = element.GetAttribute("Addr");
  679. string description = element.GetAttribute("Description");
  680. if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(index) || string.IsNullOrEmpty(bufferOffset))
  681. continue;
  682. name = name.Trim();
  683. index = index.Trim();
  684. bufferOffset = bufferOffset.Trim();
  685. string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  686. int intIndex;
  687. if (!int.TryParse(index, out intIndex))
  688. continue;
  689. int intBufferOffset;
  690. if (!int.TryParse(bufferOffset, out intBufferOffset))
  691. continue;
  692. if (!_diBuffer.ContainsKey(provider) || !_diBuffer[provider].ContainsKey(blockOffset))
  693. {
  694. throw new Exception("Not defined DI buffer from IO provider, " + provider);
  695. }
  696. DIAccessor diAccessor = new DIAccessor(moduleName, intBufferOffset, _diBuffer[provider][blockOffset], _diBuffer[provider][blockOffset]);
  697. diAccessor.IoTableIndex = intIndex;
  698. diAccessor.Addr = address;
  699. diAccessor.Provider = provider;
  700. diAccessor.BlockOffset = blockOffset;
  701. diAccessor.Description = description;
  702. diList.Add(diAccessor);
  703. _diMap[moduleName] = diAccessor;
  704. if (!_diList.ContainsKey(provider))
  705. _diList[provider] = new List<DIAccessor>();
  706. _diList[provider].Add(diAccessor);
  707. _ioItemList[$"{provider}.DIItemList"].Add(new NotifiableIoItem()
  708. {
  709. Address = address,
  710. Name = moduleName,
  711. Description = description,
  712. Index = intIndex,
  713. Provider = provider,
  714. BlockOffset = blockOffset,
  715. BlockIndex = intIndex,
  716. });
  717. if (isIgnoreSaveDB)
  718. {
  719. DATA.Subscribe($"IO.{moduleName}", () => diAccessor.Value, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  720. }
  721. else
  722. {
  723. DATA.Subscribe($"IO.{moduleName}", () => diAccessor.Value);
  724. }
  725. }
  726. XmlNodeList lstDo = xml.SelectNodes("IO_DEFINE/Dig_Out/DO_ITEM");
  727. // < DO_ITEM Index = "0" BufferOffset="0" Name = "" Addr = "0" Description = "" />
  728. foreach (var doItem in lstDo)
  729. {
  730. XmlElement element = doItem as XmlElement;
  731. if (element == null)
  732. continue;
  733. string index = element.GetAttribute("Index");
  734. string bufferOffset = element.GetAttribute("BufferOffset");
  735. if (string.IsNullOrEmpty(bufferOffset))
  736. bufferOffset = index;
  737. string name = element.GetAttribute("Name");
  738. string address = element.GetAttribute("Addr");
  739. string description = element.GetAttribute("Description");
  740. if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(index) || string.IsNullOrEmpty(bufferOffset))
  741. continue;
  742. name = name.Trim();
  743. index = index.Trim();
  744. bufferOffset = bufferOffset.Trim();
  745. string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  746. int intIndex;
  747. if (!int.TryParse(index, out intIndex))
  748. continue;
  749. int intBufferOffset;
  750. if (!int.TryParse(bufferOffset, out intBufferOffset))
  751. continue;
  752. if (!_doBuffer.ContainsKey(provider) || !_doBuffer[provider].ContainsKey(blockOffset))
  753. {
  754. throw new Exception("Not defined DO buffer from IO provider, " + provider);
  755. }
  756. DOAccessor doAccessor = new DOAccessor(moduleName, intBufferOffset, _doBuffer[provider][blockOffset]);
  757. _doMap[moduleName] = doAccessor;
  758. doAccessor.IoTableIndex = intIndex;
  759. doAccessor.Addr = address;
  760. doAccessor.Provider = provider;
  761. doAccessor.BlockOffset = blockOffset;
  762. doAccessor.Description = description;
  763. if (!_doList.ContainsKey(provider))
  764. _doList[provider] = new List<DOAccessor>();
  765. _doList[provider].Add(doAccessor);
  766. _ioItemList[$"{provider}.DOItemList"].Add(new NotifiableIoItem()
  767. {
  768. Address = address,
  769. Name = moduleName,
  770. Description = description,
  771. Index = intIndex,
  772. Provider = provider,
  773. BlockOffset = blockOffset,
  774. BlockIndex = intIndex,
  775. });
  776. if (isIgnoreSaveDB)
  777. {
  778. DATA.Subscribe($"IO.{moduleName}", () => doAccessor.Value,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  779. }
  780. else
  781. {
  782. DATA.Subscribe($"IO.{moduleName}", () => doAccessor.Value);
  783. }
  784. }
  785. XmlNodeList lstAo = xml.SelectNodes("IO_DEFINE/Ana_Out/AO_ITEM");
  786. // < AO_ITEM Index = "0" BufferOffset="0" Name = "" Addr = "0" Description = "" />
  787. foreach (var aoItem in lstAo)
  788. {
  789. XmlElement element = aoItem as XmlElement;
  790. if (element == null)
  791. continue;
  792. string index = element.GetAttribute("Index");
  793. string bufferOffset = element.GetAttribute("BufferOffset");
  794. if (string.IsNullOrEmpty(bufferOffset))
  795. bufferOffset = index;
  796. string name = element.GetAttribute("Name");
  797. string address = element.GetAttribute("Addr");
  798. string description = element.GetAttribute("Description");
  799. if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(index) || string.IsNullOrEmpty(bufferOffset))
  800. continue;
  801. name = name.Trim();
  802. index = index.Trim();
  803. bufferOffset = bufferOffset.Trim();
  804. string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  805. int intIndex;
  806. if (!int.TryParse(index, out intIndex))
  807. continue;
  808. int intBufferOffset;
  809. if (!int.TryParse(bufferOffset, out intBufferOffset))
  810. continue;
  811. if (!_aoBuffer.ContainsKey(provider) || !_aoBuffer[provider].ContainsKey(blockOffset))
  812. {
  813. throw new Exception("Not defined AO buffer from IO provider, " + provider);
  814. }
  815. AOAccessor aoAccessor = new AOAccessor(moduleName, intBufferOffset, _aoBuffer[provider][blockOffset]);
  816. _aoMap[moduleName] = aoAccessor;
  817. aoAccessor.IoTableIndex = intIndex;
  818. aoAccessor.Addr = address;
  819. aoAccessor.Provider = provider;
  820. aoAccessor.BlockOffset = blockOffset;
  821. aoAccessor.Description = description;
  822. if (!_aoList.ContainsKey(provider))
  823. _aoList[provider] = new List<AOAccessor>();
  824. _aoList[provider].Add(aoAccessor);
  825. _ioItemList[$"{provider}.AOItemList"].Add(new NotifiableIoItem()
  826. {
  827. Address = address,
  828. Name = moduleName,
  829. Description = description,
  830. Index = intIndex,
  831. Provider = provider,
  832. BlockOffset = blockOffset,
  833. BlockIndex = intIndex,
  834. });
  835. if (isIgnoreSaveDB)
  836. {
  837. DATA.Subscribe($"IO.{moduleName}", () => aoAccessor.Value,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  838. DATA.Subscribe($"IO32.{moduleName}", () =>
  839. {
  840. if (aoAccessor.Index < aoAccessor.Buffer.Length - 1)
  841. {
  842. byte[] high = BitConverter.GetBytes(aoAccessor.Buffer[aoAccessor.Index]);
  843. byte[] low = BitConverter.GetBytes(aoAccessor.Buffer[aoAccessor.Index + 1]);
  844. return BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  845. }
  846. else
  847. {
  848. return aoAccessor.Value;
  849. }
  850. },SubscriptionAttribute.FLAG.IgnoreSaveDB);
  851. }
  852. else
  853. {
  854. DATA.Subscribe($"IO.{moduleName}", () => aoAccessor.Value);
  855. DATA.Subscribe($"IO32.{moduleName}", () =>
  856. {
  857. if (aoAccessor.Index < aoAccessor.Buffer.Length - 1)
  858. {
  859. byte[] high = BitConverter.GetBytes(aoAccessor.Buffer[aoAccessor.Index]);
  860. byte[] low = BitConverter.GetBytes(aoAccessor.Buffer[aoAccessor.Index + 1]);
  861. return BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  862. }
  863. else
  864. {
  865. return aoAccessor.Value;
  866. }
  867. });
  868. }
  869. }
  870. XmlNodeList lstAi = xml.SelectNodes("IO_DEFINE/Ana_In/AI_ITEM");
  871. // < AO_ITEM Index = "0" BufferOffset="0" Name = "" Addr = "0" Description = "" />
  872. foreach (var aiItem in lstAi)
  873. {
  874. XmlElement element = aiItem as XmlElement;
  875. if (element == null)
  876. continue;
  877. string index = element.GetAttribute("Index");
  878. string bufferOffset = element.GetAttribute("BufferOffset");
  879. if (string.IsNullOrEmpty(bufferOffset))
  880. bufferOffset = index;
  881. string name = element.GetAttribute("Name");
  882. string address = element.GetAttribute("Addr");
  883. string description = element.GetAttribute("Description");
  884. if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(index) || string.IsNullOrEmpty(bufferOffset))
  885. continue;
  886. name = name.Trim();
  887. index = index.Trim();
  888. bufferOffset = bufferOffset.Trim();
  889. string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  890. int intIndex;
  891. if (!int.TryParse(index, out intIndex))
  892. continue;
  893. int intBufferOffset;
  894. if (!int.TryParse(bufferOffset, out intBufferOffset))
  895. continue;
  896. if (!_aiBuffer.ContainsKey(provider) || !_aiBuffer[provider].ContainsKey(blockOffset))
  897. {
  898. throw new Exception("Not defined AI buffer from IO provider, " + provider);
  899. }
  900. AIAccessor aiAccessor = new AIAccessor(moduleName, intBufferOffset, _aiBuffer[provider][blockOffset]);
  901. _aiMap[moduleName] = aiAccessor;
  902. aiAccessor.IoTableIndex = intIndex;
  903. aiAccessor.Addr = address;
  904. aiAccessor.Provider = provider;
  905. aiAccessor.BlockOffset = blockOffset;
  906. aiAccessor.Description = description;
  907. if (!_aiList.ContainsKey(provider))
  908. _aiList[provider] = new List<AIAccessor>();
  909. _aiList[provider].Add(aiAccessor);
  910. _ioItemList[$"{provider}.AIItemList"].Add(new NotifiableIoItem()
  911. {
  912. Address = address,
  913. Name = moduleName,
  914. Description = description,
  915. Index = intIndex,
  916. Provider = provider,
  917. BlockOffset = blockOffset,
  918. BlockIndex = intIndex,
  919. });
  920. if (isIgnoreSaveDB)
  921. {
  922. DATA.Subscribe($"IO.{moduleName}", () => aiAccessor.Value, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  923. DATA.Subscribe($"IO32.{moduleName}", () =>
  924. {
  925. if (aiAccessor.Index < aiAccessor.Buffer.Length - 1)
  926. {
  927. byte[] high = BitConverter.GetBytes(aiAccessor.Buffer[aiAccessor.Index]);
  928. byte[] low = BitConverter.GetBytes(aiAccessor.Buffer[aiAccessor.Index + 1]);
  929. return BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  930. }
  931. else
  932. {
  933. return aiAccessor.Value;
  934. }
  935. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  936. }
  937. else
  938. {
  939. DATA.Subscribe($"IO.{moduleName}", () => aiAccessor.Value);
  940. DATA.Subscribe($"IO32.{moduleName}", () =>
  941. {
  942. if (aiAccessor.Index < aiAccessor.Buffer.Length - 1)
  943. {
  944. byte[] high = BitConverter.GetBytes(aiAccessor.Buffer[aiAccessor.Index]);
  945. byte[] low = BitConverter.GetBytes(aiAccessor.Buffer[aiAccessor.Index + 1]);
  946. return BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);
  947. }
  948. else
  949. {
  950. return aiAccessor.Value;
  951. }
  952. });
  953. }
  954. }
  955. }
  956. public void SetIoMap(string provider, Dictionary<int, string> ioMappingPathFile)
  957. {
  958. foreach (var map in ioMappingPathFile)
  959. {
  960. SetIoMap(provider, map.Key, map.Value);
  961. }
  962. DATA.Subscribe(provider, "DIList", SubscribeDiData);
  963. DATA.Subscribe(provider, "DOList", SubscribeDoData);
  964. DATA.Subscribe(provider, "AIList", SubscribeAiData);
  965. DATA.Subscribe(provider, "AOList", SubscribeAoData);
  966. }
  967. public void SetIoMapByModule(string provider, int offset, string ioMappingPathFile, string module)
  968. {
  969. SetIoMap(provider, offset, ioMappingPathFile, module);
  970. DATA.Subscribe(provider, "DIList", SubscribeDiData);
  971. DATA.Subscribe(provider, "DOList", SubscribeDoData);
  972. DATA.Subscribe(provider, "AIList", SubscribeAiData);
  973. DATA.Subscribe(provider, "AOList", SubscribeAoData);
  974. }
  975. private void SubscribeIoItemList(string provider)
  976. {
  977. string diKey = $"{provider}.DIItemList";
  978. if (!_ioItemList.ContainsKey(diKey))
  979. {
  980. _ioItemList[diKey] = new List<NotifiableIoItem>();
  981. DATA.Subscribe(diKey, () => _ioItemList[diKey]);
  982. }
  983. string doKey = $"{provider}.DOItemList";
  984. if (!_ioItemList.ContainsKey(doKey))
  985. {
  986. _ioItemList[doKey] = new List<NotifiableIoItem>();
  987. DATA.Subscribe(doKey, () => _ioItemList[doKey]);
  988. }
  989. string aiKey = $"{provider}.AIItemList";
  990. if (!_ioItemList.ContainsKey(aiKey))
  991. {
  992. _ioItemList[aiKey] = new List<NotifiableIoItem>();
  993. DATA.Subscribe(aiKey, () => _ioItemList[aiKey]);
  994. }
  995. string aoKey = $"{provider}.AOItemList";
  996. if (!_ioItemList.ContainsKey(aoKey))
  997. {
  998. _ioItemList[aoKey] = new List<NotifiableIoItem>();
  999. DATA.Subscribe(aoKey, () => _ioItemList[aoKey]);
  1000. }
  1001. }
  1002. }
  1003. }