using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Xml; using Aitex.Core.RT.Log; using Aitex.Core.Util; using Npgsql; namespace Aitex.Core.RT.ConfigCenter { public class DeviceConfigManager2 : Singleton { public bool HasBoostPump { get; set; } public bool HasThrottleValve { get; set; } public void Initialize() { string fileDefault = string.Format("{0}\\config\\DeviceConfig.default.xml", Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)); string fileCurrent = string.Format("{0}\\config\\DeviceConfig.xml", Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)); if (!File.Exists(fileDefault) && !File.Exists(fileCurrent)) throw new ApplicationException("没有找到DeviceConfig配置文件。\r\n"+fileDefault + "\r\n或者" + fileCurrent); if (File.Exists(fileDefault)) { XmlDocument doc = new XmlDocument(); doc.Load(fileDefault); try { ParseNode(doc ); } catch (Exception ex) { LOG.WriteExeption(ex); } } if (File.Exists(fileCurrent)) { XmlDocument doc = new XmlDocument(); doc.Load(fileCurrent); try { ParseNode(doc ); } catch (Exception ex) { LOG.WriteExeption(ex); } } CONFIG.Subscribe("Device", "HasBoostPump", () => HasBoostPump); CONFIG.Subscribe("Device", "HasThrottleValve", () => HasThrottleValve); } public bool ParseBoolenNode(XmlDocument doc, string type) { var node = doc.SelectSingleNode(string.Format("DeviceConfig/DeviceConfigItem[@Type='{0}']", type)); if (node == null) return false; var element = node as XmlElement; if (element == null) return false; bool result = false; if (element.HasAttribute("Value") && !string.IsNullOrEmpty(element.GetAttribute("Value"))) { if (!bool.TryParse(element.GetAttribute("Value").Trim(), out result)) { //LOG.Write(string.Format("Error format in device config item, type {0}, value {1}", type, element.GetAttribute("Value").Trim())); } } return result; } public void ParseNode(XmlDocument doc) { HasBoostPump = ParseBoolenNode(doc, "HasBoostPump"); HasThrottleValve = ParseBoolenNode(doc, "HasThrottleValve"); } void Update(string type, string value) { switch (type) { case "HasBoostPump" : HasBoostPump = bool.Parse(value); break; case "HasThrottleValve" : HasThrottleValve = bool.Parse(value); break; } } public void SetConfig(string type, string value) { string fileCurrent = string.Format("{0}\\config\\DeviceConfig.xml", Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)); XmlDocument doc = new XmlDocument(); if (File.Exists(fileCurrent)) { doc.Load(fileCurrent); } else { doc.LoadXml(""); } var nodeRoot = doc.SelectSingleNode(string.Format("DeviceConfig")); if (nodeRoot == null) { nodeRoot = doc.CreateElement("DeviceConfig"); doc.AppendChild(nodeRoot); } var node = doc.SelectSingleNode(string.Format("DeviceConfig/DeviceConfigItem[@Type='{0}']", type)) as XmlElement; if (node == null) { node = doc.CreateElement("DeviceConfigItem"); nodeRoot.AppendChild(node); } node.SetAttribute("Type", type); node.SetAttribute("Value", value); doc.Save(fileCurrent); Update(type, value); } } }