IOManager.cs 40 KB

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