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

Annotation of /trunk/libThermoControl/Configuration.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 22 - (hide annotations) (download)
Wed Nov 13 02:43:01 2013 UTC (7 years, 3 months ago) by william
File size: 3982 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22