IOManager.cs 52 KB

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