123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- 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<DeviceConfigManager2>
- {
- 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("<?xml version=\"1.0\" encoding=\"utf-8\" ?><DeviceConfig></DeviceConfig>");
- }
- 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);
- }
- }
- }
|