/[ThermoControl]/trunk/libThermoControl/Configuration.cs
ViewVC logotype

Contents of /trunk/libThermoControl/Configuration.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 21 - (show annotations) (download)
Wed Nov 13 02:39:41 2013 UTC (7 years, 5 months ago) by william
File size: 3817 byte(s)

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 private const string temp_file = "current_temp.bin";
16
17
18 public static int CurrentTemp { get { return ReadTemp(); } set { WriteTemp(value); } }
19
20 public static readonly string THERMISTAT_DEVICE = ""; // 60
21 public static readonly int MIN_TEMP = 0; // 60
22 public static readonly int MAX_TEMP = 0; // 90
23 public static readonly int DEFAULT_TEMP = 0; // 70
24 public static readonly string DEFAULT_MODE = ""; // cool
25 public static readonly string DEFAULT_FAN_SPEED = ""; // auto_fan
26
27 [Conditional("CONFIG_UNITTEST")]
28 public static void UnitTest()
29 {
30 }
31
32 static Configuration()
33 {
34 // load configuration
35 //using (FileStream fs = new FileStream(config_file, FileMode.Open, FileAccess.Read, FileShare.Read))
36 //{
37
38 //}
39 XDocument xdoc = XDocument.Load(config_file);
40
41 Type p = typeof(Configuration);
42 var fields = new List<FieldInfo>(p.GetFields(BindingFlags.Public | BindingFlags.Static));
43 foreach (var field in fields)
44 {
45 //Console.WriteLine("Name: '{0}' value: '{1}'", field.Name, xdoc);
46 //var lv1s = from lv1 in xdoc.Descendants("level1")
47 // select lv1.Attribute("name").Value;
48 //var t = xdoc.Root.DescendantNodesAndSelf().ToList();
49
50 var o = xdoc.Root.Elements(field.Name);
51 var list = o.ToList();
52 if (list.Count == 0)
53 {
54 Console.WriteLine("Name: '{0}' Default: '{1}' Configured: '{1}'", field.Name, field.GetValue(null).ToString());
55 }
56 else
57 {
58 var t = xdoc.Root.Elements(field.Name).ToList().FirstOrDefault().Value;
59 Console.WriteLine("Name: '{0}' Default: '{1}' Configured: '{2}'", field.Name, field.GetValue(null).ToString(), t);
60 field.SetValue(null, Convert.ChangeType(t,field.FieldType));
61 }
62 }
63
64 // get the current temp from file
65 //ReadTemp();
66 }
67
68 static void WriteTemp(int temp)
69 {
70 using (FileStream fs = new FileStream(temp_file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
71 {
72 using (BinaryWriter bw = new BinaryWriter(fs))
73 {
74 bw.Write((byte)temp);
75 bw.Flush();
76 bw.Close();
77 }
78 }
79 }
80 static int ReadTemp()
81 {
82 int temp = 0;
83 if (!File.Exists(temp_file))
84 {
85 WriteTemp(Configuration.DEFAULT_TEMP);
86 }
87 else
88 {
89 FileInfo fi = new FileInfo(temp_file);
90 if (fi.Length == 0)
91 {
92 WriteTemp(Configuration.DEFAULT_TEMP);
93 }
94 }
95
96
97 using (FileStream fs = new FileStream(temp_file, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
98 {
99 using (BinaryReader br = new BinaryReader(fs))
100 {
101 temp = br.ReadByte();
102 br.Close();
103 }
104 }
105 return temp;
106 }
107 }
108 }

  ViewVC Help
Powered by ViewVC 1.1.22