IOManager.cs 46 KB

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