123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- 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<SystemConfig>
- {
- private Dictionary<string, SCConfigItem> _items = new Dictionary<string, SCConfigItem>();
- 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)
- {
-
- 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)
- {
-
- break;
- }
- item.DoubleValue = doubleValue;
- changed = true;
- }
- break;
- case "String":
- if (value != item.StringValue)
- {
- item.StringValue = value;
- changed = true;
- }
- break;
- }
- return changed;
- }
- public T GetValue<T>(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)
- {
-
- }
- }
- 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))
- {
-
- }
- _items[item.PathName] = item;
- }
- }
- }
- }
|