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 string THERMISTAT_DEVICE = ""; // 60 |
17 |
public static readonly int MIN_TEMP = 0; // 60 |
18 |
public static readonly int MAX_TEMP = 0; // 90 |
19 |
public static readonly int DEFAULT_TEMP = 0; // 70 |
20 |
public static readonly string DEFAULT_MODE = ""; // cool |
21 |
public static readonly string DEFAULT_FAN_SPEED = ""; // auto_fan |
22 |
|
23 |
[Conditional("CONFIG_UNITTEST")] |
24 |
public static void UnitTest() |
25 |
{ |
26 |
} |
27 |
|
28 |
static Configuration() |
29 |
{ |
30 |
// load configuration |
31 |
//using (FileStream fs = new FileStream(config_file, FileMode.Open, FileAccess.Read, FileShare.Read)) |
32 |
//{ |
33 |
|
34 |
//} |
35 |
XDocument xdoc = XDocument.Load(config_file); |
36 |
|
37 |
Type p = typeof(Configuration); |
38 |
var fields = new List<FieldInfo>(p.GetFields(BindingFlags.Public | BindingFlags.Static)); |
39 |
foreach (var field in fields) |
40 |
{ |
41 |
//Console.WriteLine("Name: '{0}' value: '{1}'", field.Name, xdoc); |
42 |
//var lv1s = from lv1 in xdoc.Descendants("level1") |
43 |
// select lv1.Attribute("name").Value; |
44 |
//var t = xdoc.Root.DescendantNodesAndSelf().ToList(); |
45 |
|
46 |
var o = xdoc.Root.Elements(field.Name); |
47 |
var list = o.ToList(); |
48 |
if (list.Count == 0) |
49 |
{ |
50 |
Console.WriteLine("Name: '{0}' Default: '{1}' Configured: '{1}'", field.Name, field.GetValue(null).ToString()); |
51 |
} |
52 |
else |
53 |
{ |
54 |
var t = xdoc.Root.Elements(field.Name).ToList().FirstOrDefault().Value; |
55 |
Console.WriteLine("Name: '{0}' Default: '{1}' Configured: '{2}'", field.Name, field.GetValue(null).ToString(), t); |
56 |
field.SetValue(null, Convert.ChangeType(t,field.FieldType)); |
57 |
} |
58 |
} |
59 |
} |
60 |
} |
61 |
} |