using Aitex.Core.RT.Log; using Aitex.Core.RT.SCCore; using Aitex.Core.Util; using DocumentFormat.OpenXml.Office2013.PowerPoint.Roaming; using DocumentFormat.OpenXml.Wordprocessing; using MECF.Framework.Common.IOCore; using MECF.Framework.Common.SCCore; using MECF.Framework.Common.Twincat; using MECF.Framework.RT.Core.IoProviders; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Input; using System.Xml; using Venus_Core; namespace MECF.Framework.Common.SCCore { public class TwincatConfigManager : Singleton { #region 常量 private string PREFIX = "MAIN"; #endregion public void Initialize() { SetAllConfigValue(); } #region 属性 public Dictionary _TwincatConfigs = new Dictionary(); #endregion public void SetValue(string key, object value, string type) { //判断key值是否存在TwincatConfig中 string _configKey = key; string Module = _configKey.ToString().Substring(0, 3); if (_TwincatConfigs.Keys.Contains(_configKey + ".SV")) { _configKey = key + ".SV"; } string[] keys = key.Split('.'); if (keys.Length == 3 && keys[0].Contains("Simulator")) { _configKey = keys[1] + "." + keys[0] + "." + keys[2]; Module = keys[1]; } if (!_TwincatConfigs.Keys.Contains(_configKey)) { return; } TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(Module); if (adoManager != null) { object writeValue = GetConfigValue(value, type); var result = adoManager.WriteValue(PREFIX + "." + _configKey, writeValue); if (!result.success) { return; } if (!_TwincatConfigs.Keys.Contains(key+".PV")) { return; } var readResult = adoManager.ReadValue($"{PREFIX}.{key}.PV", writeValue.GetType()); if (!readResult.success || readResult.obj == null) { return; } if(!(readResult.obj.ToString() == writeValue.ToString())&&!SC.GetValue("System.IsSimulatorMode")) { LOG.Write(eEvent.ERR_TWINCAT, Module, $"Twincat write variable {key} error"); } } } //public bool SetValue(string key,object value, string type) //{ // //判断key值是否存在TwincatConfig中 // string _configKey = key; // string Module = _configKey.ToString().Substring(0, 3); // if (_TwincatConfigs.Keys.Contains(_configKey+ ".SV")) // { // _configKey = key + ".SV"; // } // string[] keys = key.Split('.'); // if (keys.Length == 3 && keys[0].Contains("Simulator")) // { // _configKey = keys[1] +"."+ keys[0] + "." + keys[2]; // Module = keys[1]; // } // if (!_TwincatConfigs.Keys.Contains(_configKey)) // { // return false; // } // TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(Module); // if(adoManager!=null) // { // object writeValue = GetConfigValue(value, type); // var result= adoManager.WriteValue(PREFIX + "." + _configKey,writeValue ); // if (result.success) // { // if(!_TwincatConfigs.Keys.Contains(_configKey)) // { // return true; // } // var readResult = adoManager.ReadValue($"{PREFIX}.{key}.PV", writeValue.GetType()); // if (readResult.success && readResult.obj != null) // { // return readResult.obj.ToString() == writeValue.ToString(); // } // } // else // { // return false; // } // } // return false; //} private object GetConfigValue(object value, string valueType) { switch (valueType) { case "Bool": return Convert.ToBoolean(value); case "Integer": return Convert.ToInt32(value); case "Double": return Convert.ToDouble(value); case "String": // 转换为twincat字符串长度 int targetLength = 81; byte[] byteArray = Encoding.UTF8.GetBytes(value.ToString()); if (byteArray.Length < targetLength) { byte[] paddedByteArray = new byte[targetLength]; Array.Copy(byteArray, paddedByteArray, byteArray.Length); // 默认填充零字节 byteArray = paddedByteArray; } return byteArray; default: return value; } } public void SetConfigMap(string xmlPathFile, string module) { XmlDocument xml = new XmlDocument(); xml.Load(xmlPathFile); XmlNodeList lstBool = xml.SelectNodes("CONFIG_DEFINE/BOOL_TYPE/BOOL_ITEM"); XmlNodeList lstInt = xml.SelectNodes("CONFIG_DEFINE/INT_TYPE/INT_ITEM"); XmlNodeList lstReal = xml.SelectNodes("CONFIG_DEFINE/REAL_TYPE/REAL_ITEM"); XmlNodeList lstLReal = xml.SelectNodes("CONFIG_DEFINE/LREAL_TYPE/LREAL_ITEM"); XmlNodeList lstString = xml.SelectNodes("CONFIG_DEFINE/STRING_TYPE/STRING_ITEM"); foreach (var boolItem in lstBool) { XmlElement element = boolItem as XmlElement; if (element == null) continue; string name = element.GetAttribute("Name"); string configType = element.GetAttribute("ConfigType"); if (string.IsNullOrEmpty(name)) continue; name = name.Trim(); string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}"; _TwincatConfigs.Add(moduleName, configType); TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(module); if (adoManager != null) { //adoManager.Subscribe(GetTwincatVariableName(module, name), 1, UpdateInputVariable); adoManager.SubscribeOutput(GetTwincatVariableName(module, name)); } } foreach (var intItem in lstInt) { XmlElement element = intItem as XmlElement; if (element == null) continue; string name = element.GetAttribute("Name"); string configType = element.GetAttribute("ConfigType"); if (string.IsNullOrEmpty(name)) continue; name = name.Trim(); string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}"; string[] SVINT = { "MinSV", "MaxSV", "PV", "SV" }; if (configType == "Group") { foreach (var item in SVINT) { string _moduleName = moduleName + $".{item}"; string _name = name + $".{item}"; _TwincatConfigs.Add(_moduleName, configType); TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(module); if (adoManager != null) { if (item == "PV") { adoManager.SubscribeInput(GetTwincatVariableName(module, _name)); } else { //adoManager.Subscribe(GetTwincatVariableName(module, _name), 2, UpdateInputVariable);//int在Twincat中16位,数据长度为2 adoManager.SubscribeOutput(GetTwincatVariableName(module, _name)); } } } } else { _TwincatConfigs.Add(moduleName, configType); TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(module); if (adoManager != null) { //adoManager.Subscribe(GetTwincatVariableName(module, name), 2, UpdateInputVariable); adoManager.SubscribeOutput(GetTwincatVariableName(module, name)); } } } foreach (var realItem in lstReal) { XmlElement element = realItem as XmlElement; if (element == null) continue; string name = element.GetAttribute("Name"); string configType = element.GetAttribute("ConfigType"); if (string.IsNullOrEmpty(name)) continue; name = name.Trim(); string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}"; _TwincatConfigs.Add(moduleName, configType); TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(module); if (adoManager != null) { //adoManager.Subscribe(GetTwincatVariableName(module, name), 4, UpdateInputVariable);//float数据长度4 adoManager.SubscribeOutput(GetTwincatVariableName(module, name)); } } foreach (var LRealItem in lstLReal) { XmlElement element = LRealItem as XmlElement; if (element == null) continue; string name = element.GetAttribute("Name"); string configType = element.GetAttribute("ConfigType"); if (string.IsNullOrEmpty(name)) continue; name = name.Trim(); string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}"; string[] SVLREAL = { "MinSV", "MaxSV", "PV", "SV" }; if (configType == "Group") { foreach (var item in SVLREAL) { string _moduleName = moduleName + $".{item}"; string _name = name + $".{item}"; _TwincatConfigs.Add(_moduleName, configType); TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(module); if (adoManager != null) { if (item == "PV") { adoManager.SubscribeInput(GetTwincatVariableName(module, _name)); } else { //adoManager.Subscribe(GetTwincatVariableName(module, name), 8, UpdateInputVariable);//double数据长度8 adoManager.SubscribeOutput(GetTwincatVariableName(module, _name)); } } } } else { _TwincatConfigs.Add(moduleName, configType); TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(module); if (adoManager != null) { //adoManager.Subscribe(GetTwincatVariableName(module, name), 8, UpdateInputVariable);//double数据长度8 adoManager.SubscribeOutput(GetTwincatVariableName(module, name)); } } } foreach (var StringItem in lstString) { XmlElement element = StringItem as XmlElement; if (element == null) continue; string name = element.GetAttribute("Name"); string configType = element.GetAttribute("ConfigType"); if (string.IsNullOrEmpty(name)) continue; name = name.Trim(); string moduleName = string.IsNullOrEmpty(module) ? name : $"{module}.{name}"; _TwincatConfigs.Add(moduleName, configType); TwincatAdoManager adoManager = TwincatAdoObjectManager.Instance.GetModuleAdoManager(module); if (adoManager != null) { //adoManager.Subscribe(GetTwincatVariableName(module, name), 81, UpdateInputVariable);//twincat中string默认数据长度81 adoManager.SubscribeOutput(GetTwincatVariableName(module, name)); } } } /// /// 获取Twincat变量名称 /// /// /// private string GetTwincatVariableName(string module, string name) { if (!string.IsNullOrEmpty(PREFIX)) { return $"{PREFIX}.{module}.{name}"; } else { return name; } } private void SetAllConfigValue() { List configs = SystemConfigManager.Instance.GetAllConfigs(); object configDefaultValue; foreach (var config in configs) { if (config.Default == "") { continue; } //twincat不支持Max、Min作为变量名,需要进行替换 if (config.Max != "") { SetValue(config.PathName + ".MaxSV", config.Max, config.Type); } if (config.Min != "") { SetValue(config.PathName + ".MinSV", config.Min, config.Type); } object oldValue = SystemConfigManager.Instance.GetConfigValueAsObject(config.PathName); if (oldValue != null) { SetValue(config.PathName, oldValue, config.Type); } else { SetValue(config.PathName, config.Default, config.Type); } } } } }