IOManager.cs 46 KB

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