123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Xml;
- using CB.Common;
- using CB.LotteryCatchData.Entity;
- namespace CB.LotteryCatchData.Config
- {
- public class CatchDataConfigs
- {
- private static string configPath = AppDomain.CurrentDomain.BaseDirectory + "Config\\CatchDataConfig.xml";
- private static long _version = 0;
- private static CatchDataConfigInfo _config = null;
- private CatchDataConfigs() { }
- static CatchDataConfigs()
- {
- LoadConfig();
- }
- /// <summary>
- /// 获取配置
- /// </summary>
- /// <returns></returns>
- public static CatchDataConfigInfo GetConfig()
- {
- if (null == _config)
- {
- LoadConfig();
- return _config;
- }
- if (_version != File.GetLastWriteTime(configPath).Ticks)
- LoadConfig();
- return _config;
- }
- /// <summary>
- /// 加载配置
- /// </summary>
- public static void LoadConfig()
- {
- var _file = new FileInfo(configPath);
- if (!_file.Exists)
- {
- throw (new System.IO.FileNotFoundException("未找到数据抓取配置文件!文件路径为:" + configPath));
- }
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(configPath);
- XmlElement root = xmlDoc.DocumentElement;
- var entity = new CatchDataConfigInfo();
- XmlNode node = root.SelectSingleNode("Interval");
- if (null != node)
- entity.Interval = Convert.ToDouble(node.InnerText.Trim());
- node = root.SelectSingleNode("MaxRequestCount");
- if (null != node)
- entity.MaxRequestCount = TypeConverter.StrToInt(node.InnerText.Trim());
- node = root.SelectSingleNode("ThreadCount");
- if (null != node)
- entity.ThreadCount = TypeConverter.StrToInt(node.InnerText.Trim());
- node = root.SelectSingleNode("TimeOut");
- if (null != node)
- entity.TimeOut = TypeConverter.StrToInt(node.InnerText.Trim());
- var list = root.SelectNodes("LotteryList/Lottery");
- if (null != list && 0 < list.Count)
- {
- entity.List = new List<LotteryConfigInfo>();
- foreach (XmlNode item in list)
- {
- var lottery = new LotteryConfigInfo()
- {
- DataUrl = item.InnerText.Trim()
- };
- XmlAttribute attr = item.Attributes["Name"];
- if (null != attr)
- lottery.Name = attr.Value.Trim();
- attr = item.Attributes["Path"];
- if (null != attr)
- lottery.Path = attr.Value.Trim();
- attr = item.Attributes["TimeOut"];
- lottery.TimeOut = entity.TimeOut;
- if (null != attr)
- lottery.TimeOut = TypeConverter.StrToInt(attr.Value.Trim(), entity.TimeOut);
- attr = item.Attributes["OpenTimeFomart"];
- if (null != attr)
- lottery.OpenTimeFomart = attr.Value.Trim();
- attr = item.Attributes["StartTime"];
- if (null != attr)
- lottery.StartTime = TimeSpan.Parse(attr.Value.Trim());
- attr = item.Attributes["OpenPeriod"];
- if (null != attr)
- lottery.OpenPeriod = TypeConverter.StrToInt(attr.Value.Trim());
- attr = item.Attributes["EndTime"];
- if (null != attr)
- lottery.EndTime = TimeSpan.Parse(attr.Value.Trim());
- attr = item.Attributes["CatchDataProvider"];
- if (null != attr)
- lottery.CatchDataProvider = attr.Value.Trim();
- entity.List.Add(lottery);
- }
- }
- _version = File.GetLastWriteTime(configPath).Ticks;
- _config = entity;
- }
- /// <summary>
- /// 保存配置
- /// </summary>
- /// <param name="config"></param>
- public static void SaveConfig()
- {
- FileUtil.WriteFile(configPath, _config.ToString());
- LoadConfig();
- }
- }
- }
|