123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- 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<TwincatConfigManager>
- {
- #region 常量
- private string PREFIX = "MAIN";
- #endregion
- public void Initialize()
- {
- SetAllConfigValue();
- }
- #region 属性
- public Dictionary<string,string> _TwincatConfigs = new Dictionary<string,string>();
- #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<bool>("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));
- }
- }
- }
- /// <summary>
- /// 获取Twincat变量名称
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- private string GetTwincatVariableName(string module, string name)
- {
- if (!string.IsNullOrEmpty(PREFIX))
- {
- return $"{PREFIX}.{module}.{name}";
- }
- else
- {
- return name;
- }
- }
- private void SetAllConfigValue()
- {
- List<SCConfigItem> 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);
- }
- }
- }
- }
- }
|