using System; using System.IO; using System.Collections.Generic; using System.Xml; using Aitex.Core.Util; using Aitex.Core.RT.SCCore; namespace Venus_Simulator.Instances { public class SystemConfig : Singleton { private Dictionary _items = new Dictionary(); public void Initialize() { var processs = System.Diagnostics.Process.GetProcessesByName("Venus_RT"); if(processs.Length > 0) { var venus_path = processs[0].MainModule.FileName; var venus_dir = Path.GetDirectoryName(venus_path); if (venus_dir != null) { string config_path = $"{venus_dir}\\Config\\_sc.data"; string system_cfg = $"{venus_dir}\\Config\\System.sccfg"; if(File.Exists(system_cfg)) { BuildItems(system_cfg); if (File.Exists(config_path)) { XmlDocument xmlData = new XmlDocument(); xmlData.Load(config_path); XmlNodeList scdatas = xmlData.SelectNodes("root/scdata"); foreach (XmlElement nodedata in scdatas) { string name = nodedata.GetAttribute("name"); if (_items.ContainsKey(name)) { InitializeItemValue(_items[name], nodedata.GetAttribute("value")); } } } } } } } private bool InitializeItemValue(SCConfigItem item, string value) { bool changed = false; switch (item.Type) { case "Bool": bool boolValue; if (bool.TryParse(value, out boolValue) && boolValue != item.BoolValue) { item.BoolValue = boolValue; changed = true; } break; case "Integer": int intValue; if (int.TryParse(value, out intValue) && intValue != item.IntValue) { int.TryParse(item.Min, out int min); int.TryParse(item.Max, out int max); if (intValue < min || intValue > max) { //EV.PostWarningLog(ModuleNameString.System, $"SC {item.PathName} value {intValue} out of setting range ({item.Min}, {item.Max})"); break; } item.IntValue = intValue; changed = true; } break; case "Double": double doubleValue; if (double.TryParse(value, out doubleValue) && Math.Abs(doubleValue - item.DoubleValue) > 0.0001) { double.TryParse(item.Min, out double min); double.TryParse(item.Max, out double max); if (doubleValue < min || doubleValue > max) { //EV.PostWarningLog(ModuleNameString.System, $"SC {item.PathName} value {doubleValue} out of setting range ({item.Min}, {item.Max})"); break; } item.DoubleValue = doubleValue; changed = true; } break; case "String": if (value != item.StringValue) { item.StringValue = value; changed = true; } break; } return changed; } public T GetValue(string name) where T : struct { try { if (typeof(T) == typeof(bool)) return (T)(object)_items[name].BoolValue; if (typeof(T) == typeof(int)) return (T)(object)_items[name].IntValue; if (typeof(T) == typeof(double)) return (T)(object)_items[name].DoubleValue; } catch (KeyNotFoundException) { return default(T); } catch (Exception) { return default(T); } return default(T); } private void BuildItems(string xmlFile) { XmlDocument xml = new XmlDocument(); try { xml.Load(xmlFile); XmlNodeList nodeConfigs = xml.SelectNodes("root/configs"); foreach (XmlElement nodeConfig in nodeConfigs) { BuildPathConfigs(nodeConfig.GetAttribute("name"), nodeConfig as XmlElement); } } catch (Exception ex) { //LOG.Write(ex); } } private void BuildPathConfigs(string parentPath, XmlElement configElement) { XmlNodeList nodeConfigsList = configElement.SelectNodes("configs"); foreach (XmlElement nodeConfig in nodeConfigsList) { if (string.IsNullOrEmpty(parentPath)) { BuildPathConfigs(nodeConfig.GetAttribute("name"), nodeConfig as XmlElement); } else { BuildPathConfigs(parentPath + "." + nodeConfig.GetAttribute("name"), nodeConfig as XmlElement); } } XmlNodeList nodeConfigs = configElement.SelectNodes("config"); foreach (XmlElement nodeConfig in nodeConfigs) { SCConfigItem item = new SCConfigItem() { Default = nodeConfig.GetAttribute("default"), Name = nodeConfig.GetAttribute("name"), Description = nodeConfig.GetAttribute("description"), Max = nodeConfig.GetAttribute("max"), Min = nodeConfig.GetAttribute("min"), Parameter = nodeConfig.GetAttribute("paramter"), Path = parentPath, Tag = nodeConfig.GetAttribute("tag"), Type = nodeConfig.GetAttribute("type"), Unit = nodeConfig.GetAttribute("unit"), }; InitializeItemValue(item, item.Default); if (_items.ContainsKey(item.PathName)) { //LOG.Error("Duplicated SC item, "+ item.PathName); } _items[item.PathName] = item; } } } }