TwincatConfigManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Core.RT.SCCore;
  3. using Aitex.Core.Util;
  4. using DocumentFormat.OpenXml.Office2013.PowerPoint.Roaming;
  5. using DocumentFormat.OpenXml.Wordprocessing;
  6. using MECF.Framework.Common.IOCore;
  7. using MECF.Framework.Common.SCCore;
  8. using MECF.Framework.Common.Twincat;
  9. using MECF.Framework.RT.Core.IoProviders;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Reflection;
  14. using System.Runtime.InteropServices;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows.Forms;
  18. using System.Windows.Input;
  19. using System.Xml;
  20. using Venus_Core;
  21. namespace MECF.Framework.Common.SCCore
  22. {
  23. public class TwincatConfigManager : Singleton<TwincatConfigManager>
  24. {
  25. #region 常量
  26. private string PREFIX = "MAIN";
  27. #endregion
  28. public void Initialize()
  29. {
  30. SetAllConfigValue();
  31. }
  32. #region 属性
  33. public Dictionary<string,string> _TwincatConfigs = new Dictionary<string,string>();
  34. #endregion
  35. public void SetValue(string key, object value, string type)
  36. {
  37. //判断key值是否存在TwincatConfig中
  38. string _configKey = key;
  39. string Module = _configKey.ToString().Substring(0, 3);
  40. if (_TwincatConfigs.Keys.Contains(_configKey + ".SV"))
  41. {
  42. _configKey = key + ".SV";
  43. }
  44. string[] keys = key.Split('.');
  45. if (keys.Length == 3 && keys[0].Contains("Simulator"))
  46. {
  47. _configKey = keys[1] + "." + keys[0] + "." + keys[2];
  48. Module = keys[1];
  49. }
  50. if (!_TwincatConfigs.Keys.Contains(_configKey))
  51. {
  52. return;
  53. }
  54. TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(Module);
  55. if (adoManager != null)
  56. {
  57. object writeValue = GetConfigValue(value, type);
  58. var result = adoManager.WriteValue(PREFIX + "." + _configKey, writeValue);
  59. if (!result.success)
  60. {
  61. return;
  62. }
  63. if (!_TwincatConfigs.Keys.Contains(key+".PV"))
  64. {
  65. return;
  66. }
  67. var readResult = adoManager.ReadValue($"{PREFIX}.{key}.PV", writeValue.GetType());
  68. if (!readResult.success || readResult.obj == null)
  69. {
  70. return;
  71. }
  72. if(!(readResult.obj.ToString() == writeValue.ToString())&&!SC.GetValue<bool>("System.IsSimulatorMode"))
  73. {
  74. LOG.Write(eEvent.ERR_TWINCAT, Module, $"Twincat write variable {key} error");
  75. }
  76. }
  77. }
  78. //public bool SetValue(string key,object value, string type)
  79. //{
  80. // //判断key值是否存在TwincatConfig中
  81. // string _configKey = key;
  82. // string Module = _configKey.ToString().Substring(0, 3);
  83. // if (_TwincatConfigs.Keys.Contains(_configKey+ ".SV"))
  84. // {
  85. // _configKey = key + ".SV";
  86. // }
  87. // string[] keys = key.Split('.');
  88. // if (keys.Length == 3 && keys[0].Contains("Simulator"))
  89. // {
  90. // _configKey = keys[1] +"."+ keys[0] + "." + keys[2];
  91. // Module = keys[1];
  92. // }
  93. // if (!_TwincatConfigs.Keys.Contains(_configKey))
  94. // {
  95. // return false;
  96. // }
  97. // TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(Module);
  98. // if(adoManager!=null)
  99. // {
  100. // object writeValue = GetConfigValue(value, type);
  101. // var result= adoManager.WriteValue(PREFIX + "." + _configKey,writeValue );
  102. // if (result.success)
  103. // {
  104. // if(!_TwincatConfigs.Keys.Contains(_configKey))
  105. // {
  106. // return true;
  107. // }
  108. // var readResult = adoManager.ReadValue($"{PREFIX}.{key}.PV", writeValue.GetType());
  109. // if (readResult.success && readResult.obj != null)
  110. // {
  111. // return readResult.obj.ToString() == writeValue.ToString();
  112. // }
  113. // }
  114. // else
  115. // {
  116. // return false;
  117. // }
  118. // }
  119. // return false;
  120. //}
  121. private object GetConfigValue(object value, string valueType)
  122. {
  123. switch (valueType)
  124. {
  125. case "Bool":
  126. return Convert.ToBoolean(value);
  127. case "Integer":
  128. return Convert.ToInt32(value);
  129. case "Double":
  130. return Convert.ToDouble(value);
  131. case "String":
  132. // 转换为twincat字符串长度
  133. int targetLength = 81;
  134. byte[] byteArray = Encoding.UTF8.GetBytes(value.ToString());
  135. if (byteArray.Length < targetLength)
  136. {
  137. byte[] paddedByteArray = new byte[targetLength];
  138. Array.Copy(byteArray, paddedByteArray, byteArray.Length);
  139. // 默认填充零字节
  140. byteArray = paddedByteArray;
  141. }
  142. return byteArray;
  143. default: return value;
  144. }
  145. }
  146. public void SetConfigMap(string xmlPathFile, string module)
  147. {
  148. XmlDocument xml = new XmlDocument();
  149. xml.Load(xmlPathFile);
  150. XmlNodeList lstBool = xml.SelectNodes("CONFIG_DEFINE/BOOL_TYPE/BOOL_ITEM");
  151. XmlNodeList lstInt = xml.SelectNodes("CONFIG_DEFINE/INT_TYPE/INT_ITEM");
  152. XmlNodeList lstReal = xml.SelectNodes("CONFIG_DEFINE/REAL_TYPE/REAL_ITEM");
  153. XmlNodeList lstLReal = xml.SelectNodes("CONFIG_DEFINE/LREAL_TYPE/LREAL_ITEM");
  154. XmlNodeList lstString = xml.SelectNodes("CONFIG_DEFINE/STRING_TYPE/STRING_ITEM");
  155. foreach (var boolItem in lstBool)
  156. {
  157. XmlElement element = boolItem as XmlElement;
  158. if (element == null)
  159. continue;
  160. string name = element.GetAttribute("Name");
  161. string configType = element.GetAttribute("ConfigType");
  162. if (string.IsNullOrEmpty(name))
  163. continue;
  164. name = name.Trim();
  165. string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  166. _TwincatConfigs.Add(moduleName, configType);
  167. TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(module);
  168. if (adoManager != null)
  169. {
  170. //adoManager.Subscribe(GetTwincatVariableName(module, name), 1, UpdateInputVariable);
  171. adoManager.SubscribeOutput(GetTwincatVariableName(module, name));
  172. }
  173. }
  174. foreach (var intItem in lstInt)
  175. {
  176. XmlElement element = intItem as XmlElement;
  177. if (element == null)
  178. continue;
  179. string name = element.GetAttribute("Name");
  180. string configType = element.GetAttribute("ConfigType");
  181. if (string.IsNullOrEmpty(name))
  182. continue;
  183. name = name.Trim();
  184. string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  185. string[] SVINT = { "MinSV", "MaxSV", "PV", "SV" };
  186. if (configType == "Group")
  187. {
  188. foreach (var item in SVINT)
  189. {
  190. string _moduleName = moduleName + $".{item}";
  191. string _name = name + $".{item}";
  192. _TwincatConfigs.Add(_moduleName, configType);
  193. TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(module);
  194. if (adoManager != null)
  195. {
  196. if (item == "PV")
  197. {
  198. adoManager.SubscribeInput(GetTwincatVariableName(module, _name));
  199. }
  200. else
  201. {
  202. //adoManager.Subscribe(GetTwincatVariableName(module, _name), 2, UpdateInputVariable);//int在Twincat中16位,数据长度为2
  203. adoManager.SubscribeOutput(GetTwincatVariableName(module, _name));
  204. }
  205. }
  206. }
  207. }
  208. else
  209. {
  210. _TwincatConfigs.Add(moduleName, configType);
  211. TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(module);
  212. if (adoManager != null)
  213. {
  214. //adoManager.Subscribe(GetTwincatVariableName(module, name), 2, UpdateInputVariable);
  215. adoManager.SubscribeOutput(GetTwincatVariableName(module, name));
  216. }
  217. }
  218. }
  219. foreach (var realItem in lstReal)
  220. {
  221. XmlElement element = realItem as XmlElement;
  222. if (element == null)
  223. continue;
  224. string name = element.GetAttribute("Name");
  225. string configType = element.GetAttribute("ConfigType");
  226. if (string.IsNullOrEmpty(name))
  227. continue;
  228. name = name.Trim();
  229. string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  230. _TwincatConfigs.Add(moduleName, configType);
  231. TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(module);
  232. if (adoManager != null)
  233. {
  234. //adoManager.Subscribe(GetTwincatVariableName(module, name), 4, UpdateInputVariable);//float数据长度4
  235. adoManager.SubscribeOutput(GetTwincatVariableName(module, name));
  236. }
  237. }
  238. foreach (var LRealItem in lstLReal)
  239. {
  240. XmlElement element = LRealItem as XmlElement;
  241. if (element == null)
  242. continue;
  243. string name = element.GetAttribute("Name");
  244. string configType = element.GetAttribute("ConfigType");
  245. if (string.IsNullOrEmpty(name))
  246. continue;
  247. name = name.Trim();
  248. string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  249. string[] SVLREAL = { "MinSV", "MaxSV", "PV", "SV" };
  250. if (configType == "Group")
  251. {
  252. foreach (var item in SVLREAL)
  253. {
  254. string _moduleName = moduleName + $".{item}";
  255. string _name = name + $".{item}";
  256. _TwincatConfigs.Add(_moduleName, configType);
  257. TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(module);
  258. if (adoManager != null)
  259. {
  260. if (item == "PV")
  261. {
  262. adoManager.SubscribeInput(GetTwincatVariableName(module, _name));
  263. }
  264. else
  265. {
  266. //adoManager.Subscribe(GetTwincatVariableName(module, name), 8, UpdateInputVariable);//double数据长度8
  267. adoManager.SubscribeOutput(GetTwincatVariableName(module, _name));
  268. }
  269. }
  270. }
  271. }
  272. else
  273. {
  274. _TwincatConfigs.Add(moduleName, configType);
  275. TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(module);
  276. if (adoManager != null)
  277. {
  278. //adoManager.Subscribe(GetTwincatVariableName(module, name), 8, UpdateInputVariable);//double数据长度8
  279. adoManager.SubscribeOutput(GetTwincatVariableName(module, name));
  280. }
  281. }
  282. }
  283. foreach (var StringItem in lstString)
  284. {
  285. XmlElement element = StringItem as XmlElement;
  286. if (element == null)
  287. continue;
  288. string name = element.GetAttribute("Name");
  289. string configType = element.GetAttribute("ConfigType");
  290. if (string.IsNullOrEmpty(name))
  291. continue;
  292. name = name.Trim();
  293. string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}";
  294. _TwincatConfigs.Add(moduleName, configType);
  295. TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(module);
  296. if (adoManager != null)
  297. {
  298. //adoManager.Subscribe(GetTwincatVariableName(module, name), 81, UpdateInputVariable);//twincat中string默认数据长度81
  299. adoManager.SubscribeOutput(GetTwincatVariableName(module, name));
  300. }
  301. }
  302. }
  303. /// <summary>
  304. /// 获取Twincat变量名称
  305. /// </summary>
  306. /// <param name="name"></param>
  307. /// <returns></returns>
  308. private string GetTwincatVariableName(string module, string name)
  309. {
  310. if (!string.IsNullOrEmpty(PREFIX))
  311. {
  312. return $"{PREFIX}.{module}.{name}";
  313. }
  314. else
  315. {
  316. return name;
  317. }
  318. }
  319. private void SetAllConfigValue()
  320. {
  321. List<SCConfigItem> configs = SystemConfigManager.Instance.GetAllConfigs();
  322. object configDefaultValue;
  323. foreach (var config in configs)
  324. {
  325. if (config.Default == "")
  326. {
  327. continue;
  328. }
  329. //twincat不支持Max、Min作为变量名,需要进行替换
  330. if (config.Max != "")
  331. {
  332. SetValue(config.PathName + ".MaxSV", config.Max, config.Type);
  333. }
  334. if (config.Min != "")
  335. {
  336. SetValue(config.PathName + ".MinSV", config.Min, config.Type);
  337. }
  338. object oldValue = SystemConfigManager.Instance.GetConfigValueAsObject(config.PathName);
  339. if (oldValue != null)
  340. {
  341. SetValue(config.PathName, oldValue, config.Type);
  342. }
  343. else
  344. {
  345. SetValue(config.PathName, config.Default, config.Type);
  346. }
  347. }
  348. }
  349. }
  350. }