CatchDataConfigs.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Xml;
  5. using CB.Common;
  6. using CB.LotteryCatchData.Entity;
  7. namespace CB.LotteryCatchData.Config
  8. {
  9. public class CatchDataConfigs
  10. {
  11. private static string configPath = AppDomain.CurrentDomain.BaseDirectory + "Config\\CatchDataConfig.xml";
  12. private static long _version = 0;
  13. private static CatchDataConfigInfo _config = null;
  14. private CatchDataConfigs() { }
  15. static CatchDataConfigs()
  16. {
  17. LoadConfig();
  18. }
  19. /// <summary>
  20. /// 获取配置
  21. /// </summary>
  22. /// <returns></returns>
  23. public static CatchDataConfigInfo GetConfig()
  24. {
  25. if (null == _config)
  26. {
  27. LoadConfig();
  28. return _config;
  29. }
  30. if (_version != File.GetLastWriteTime(configPath).Ticks)
  31. LoadConfig();
  32. return _config;
  33. }
  34. /// <summary>
  35. /// 加载配置
  36. /// </summary>
  37. public static void LoadConfig()
  38. {
  39. var _file = new FileInfo(configPath);
  40. if (!_file.Exists)
  41. {
  42. throw (new System.IO.FileNotFoundException("未找到数据抓取配置文件!文件路径为:" + configPath));
  43. }
  44. XmlDocument xmlDoc = new XmlDocument();
  45. xmlDoc.Load(configPath);
  46. XmlElement root = xmlDoc.DocumentElement;
  47. var entity = new CatchDataConfigInfo();
  48. XmlNode node = root.SelectSingleNode("Interval");
  49. if (null != node)
  50. entity.Interval = Convert.ToDouble(node.InnerText.Trim());
  51. node = root.SelectSingleNode("MaxRequestCount");
  52. if (null != node)
  53. entity.MaxRequestCount = TypeConverter.StrToInt(node.InnerText.Trim());
  54. node = root.SelectSingleNode("ThreadCount");
  55. if (null != node)
  56. entity.ThreadCount = TypeConverter.StrToInt(node.InnerText.Trim());
  57. node = root.SelectSingleNode("TimeOut");
  58. if (null != node)
  59. entity.TimeOut = TypeConverter.StrToInt(node.InnerText.Trim());
  60. var list = root.SelectNodes("LotteryList/Lottery");
  61. if (null != list && 0 < list.Count)
  62. {
  63. entity.List = new List<LotteryConfigInfo>();
  64. foreach (XmlNode item in list)
  65. {
  66. var lottery = new LotteryConfigInfo()
  67. {
  68. DataUrl = item.InnerText.Trim()
  69. };
  70. XmlAttribute attr = item.Attributes["Name"];
  71. if (null != attr)
  72. lottery.Name = attr.Value.Trim();
  73. attr = item.Attributes["Path"];
  74. if (null != attr)
  75. lottery.Path = attr.Value.Trim();
  76. attr = item.Attributes["TimeOut"];
  77. lottery.TimeOut = entity.TimeOut;
  78. if (null != attr)
  79. lottery.TimeOut = TypeConverter.StrToInt(attr.Value.Trim(), entity.TimeOut);
  80. attr = item.Attributes["OpenTimeFomart"];
  81. if (null != attr)
  82. lottery.OpenTimeFomart = attr.Value.Trim();
  83. attr = item.Attributes["StartTime"];
  84. if (null != attr)
  85. lottery.StartTime = TimeSpan.Parse(attr.Value.Trim());
  86. attr = item.Attributes["OpenPeriod"];
  87. if (null != attr)
  88. lottery.OpenPeriod = TypeConverter.StrToInt(attr.Value.Trim());
  89. attr = item.Attributes["EndTime"];
  90. if (null != attr)
  91. lottery.EndTime = TimeSpan.Parse(attr.Value.Trim());
  92. attr = item.Attributes["CatchDataProvider"];
  93. if (null != attr)
  94. lottery.CatchDataProvider = attr.Value.Trim();
  95. entity.List.Add(lottery);
  96. }
  97. }
  98. _version = File.GetLastWriteTime(configPath).Ticks;
  99. _config = entity;
  100. }
  101. /// <summary>
  102. /// 保存配置
  103. /// </summary>
  104. /// <param name="config"></param>
  105. public static void SaveConfig()
  106. {
  107. FileUtil.WriteFile(configPath, _config.ToString());
  108. LoadConfig();
  109. }
  110. }
  111. }