1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.IO; |
4 |
using System.Reflection; |
5 |
using System.Text; |
6 |
using System.Linq; |
7 |
using System.Xml.Linq; |
8 |
using System.Diagnostics; |
9 |
|
10 |
namespace libThermoControl |
11 |
{ |
12 |
public static class Configuration |
13 |
{ |
14 |
private const string config_file = "config.xml"; |
15 |
|
16 |
public static readonly int MIN_TEMP = 0; // 60 |
17 |
public static readonly int MAX_TEMP = 0; // 90 |
18 |
public static readonly int DEFAULT_TEMP = 0; // 70 |
19 |
public static readonly string DEFAULT_MODE = ""; // cool |
20 |
public static readonly string NONE = ""; // cool |
21 |
|
22 |
[Conditional("CONFIG_UNITTEST")] |
23 |
public static void UnitTest() |
24 |
{ |
25 |
} |
26 |
|
27 |
static Configuration() |
28 |
{ |
29 |
// load configuration |
30 |
//using (FileStream fs = new FileStream(config_file, FileMode.Open, FileAccess.Read, FileShare.Read)) |
31 |
//{ |
32 |
|
33 |
//} |
34 |
XDocument xdoc = XDocument.Load(config_file); |
35 |
|
36 |
Type p = typeof(Configuration); |
37 |
var fields = new List<FieldInfo>(p.GetFields(BindingFlags.Public | BindingFlags.Static)); |
38 |
foreach (var field in fields) |
39 |
{ |
40 |
//Console.WriteLine("Name: '{0}' value: '{1}'", field.Name, xdoc); |
41 |
//var lv1s = from lv1 in xdoc.Descendants("level1") |
42 |
// select lv1.Attribute("name").Value; |
43 |
//var t = xdoc.Root.DescendantNodesAndSelf().ToList(); |
44 |
|
45 |
var o = xdoc.Root.Elements(field.Name); |
46 |
var list = o.ToList(); |
47 |
if (list.Count == 0) |
48 |
{ |
49 |
Console.WriteLine("Name: '{0}' Default: '{1}' Configured: '{1}'", field.Name, field.GetValue(null).ToString()); |
50 |
} |
51 |
else |
52 |
{ |
53 |
var t = xdoc.Root.Elements(field.Name).ToList().FirstOrDefault().Value; |
54 |
Console.WriteLine("Name: '{0}' Default: '{1}' Configured: '{2}'", field.Name, field.GetValue(null).ToString(), t); |
55 |
field.SetValue(null, Convert.ChangeType(t,field.FieldType)); |
56 |
} |
57 |
} |
58 |
} |
59 |
} |
60 |
} |