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

Annotation of /trunk/libThermoControl/Configuration.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 29 - (hide annotations) (download)
Wed Feb 12 03:28:37 2014 UTC (7 years ago) by william
File size: 7789 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 william 24 using libhtmlscrapper;
10 william 15
11     namespace libThermoControl
12     {
13     public static class Configuration
14     {
15     private const string config_file = "config.xml";
16 william 20 private const string temp_file = "current_temp.bin";
17    
18    
19     public static int CurrentTemp { get { return ReadTemp(); } set { WriteTemp(value); } }
20 william 22 public static int ExternalTemp { get { return GetExternalTemp(); } }
21 william 26 public static int GetTemperatureAdjustment()
22     {
23     int e_temp = Configuration.ExternalTemp;
24 william 28 int d_temp = Configuration.CurrentTemp;
25 william 26 int scale = 5;
26     int s = (d_temp - e_temp) / scale;
27 william 28 int offset = 4;
28 william 26 int t = (d_temp + s) - offset;
29     return t;
30     }
31 william 22
32 william 28 public static readonly string WEATHER_URL = "http://www.accuweather.com/en/us/chattanooga-tn/37421/current-weather/331086"; // 60
33     public static readonly string THERMISTAT_DEVICE = "AC_THERMOSTAT"; // 60
34 william 15 public static readonly int MIN_TEMP = 0; // 60
35     public static readonly int MAX_TEMP = 0; // 90
36     public static readonly int DEFAULT_TEMP = 0; // 70
37 william 28 public static readonly string DEFAULT_MODE = "cool"; // cool
38     public static readonly string DEFAULT_FAN_SPEED = "auto_fan"; // auto_fan
39 william 15
40     [Conditional("CONFIG_UNITTEST")]
41     public static void UnitTest()
42     {
43     }
44    
45     static Configuration()
46     {
47     // load configuration
48     //using (FileStream fs = new FileStream(config_file, FileMode.Open, FileAccess.Read, FileShare.Read))
49     //{
50    
51     //}
52     XDocument xdoc = XDocument.Load(config_file);
53    
54     Type p = typeof(Configuration);
55     var fields = new List<FieldInfo>(p.GetFields(BindingFlags.Public | BindingFlags.Static));
56     foreach (var field in fields)
57     {
58     //Console.WriteLine("Name: '{0}' value: '{1}'", field.Name, xdoc);
59     //var lv1s = from lv1 in xdoc.Descendants("level1")
60     // select lv1.Attribute("name").Value;
61     //var t = xdoc.Root.DescendantNodesAndSelf().ToList();
62    
63     var o = xdoc.Root.Elements(field.Name);
64     var list = o.ToList();
65     if (list.Count == 0)
66     {
67 william 29 var f = field.GetValue(null).ToString();
68     if (string.IsNullOrEmpty(f))
69     {
70     throw new ArgumentNullException("f", string.Format("Error - Field: {0} is null or an empty string", field.Name));
71     }
72     else
73     {
74     Console.WriteLine("Name: '{0}' Default: '{1}' Configured: '{1}'", field.Name, f);
75     }
76 william 15 }
77     else
78     {
79     var t = xdoc.Root.Elements(field.Name).ToList().FirstOrDefault().Value;
80 william 29 if (string.IsNullOrEmpty(t))
81     {
82     t = field.GetValue(null).ToString();
83     Console.WriteLine("Name: '{0}' Default: '{1}' Configured: '{2}'", field.Name, t, t);
84     field.SetValue(null, Convert.ChangeType(t, field.FieldType));
85     }
86     else
87     {
88     Console.WriteLine("Name: '{0}' Default: '{1}' Configured: '{2}'", field.Name, field.GetValue(null).ToString(), t);
89     field.SetValue(null, Convert.ChangeType(t, field.FieldType));
90     }
91 william 15 }
92     }
93 william 20
94     // get the current temp from file
95     //ReadTemp();
96 william 15 }
97 william 20
98     static void WriteTemp(int temp)
99     {
100     using (FileStream fs = new FileStream(temp_file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
101     {
102     using (BinaryWriter bw = new BinaryWriter(fs))
103     {
104 william 21 bw.Write((byte)temp);
105 william 20 bw.Flush();
106     bw.Close();
107     }
108     }
109     }
110     static int ReadTemp()
111     {
112     int temp = 0;
113     if (!File.Exists(temp_file))
114     {
115     WriteTemp(Configuration.DEFAULT_TEMP);
116     }
117     else
118     {
119     FileInfo fi = new FileInfo(temp_file);
120     if (fi.Length == 0)
121     {
122     WriteTemp(Configuration.DEFAULT_TEMP);
123     }
124     }
125    
126    
127     using (FileStream fs = new FileStream(temp_file, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
128     {
129     using (BinaryReader br = new BinaryReader(fs))
130     {
131 william 21 temp = br.ReadByte();
132 william 20 br.Close();
133     }
134     }
135     return temp;
136     }
137 william 22
138     static int GetExternalTemp()
139     {
140 william 24 int temp = 0;
141     List<string> BannedLinks = new List<string>();
142     using (GenericScrapper scrapper = new GenericScrapper(WEATHER_URL, true, BannedLinks, false, true))
143     {
144     scrapper.BannedHTMLLinks = BannedLinks;
145     scrapper.GetUrlsToDownload += scrapper_GetUrlsToDownload;
146     scrapper.CreateDownloadUrls();
147     if (scrapper.UrlsToDownload.Count > 0)
148     {
149     var t = scrapper.UrlsToDownload.FirstOrDefault();
150     if (t != null && t.Tag != null)
151     {
152     try
153     {
154     int k = Convert.ToInt32(t.Tag);
155     temp = k;
156     }
157 william 26 catch (Exception)
158 william 24 {
159     temp = 0;
160     }
161     }
162     }
163     }
164     return temp;
165 william 22 }
166 william 24
167     static void scrapper_GetUrlsToDownload(out List<URLLink> UrlsToDownload, GenericScrapper scrapper)
168     {
169     UrlsToDownload = new List<URLLink>();
170     StreamReader sr = scrapper.HTMLReader;
171     while (!sr.EndOfStream)
172     {
173     string line = sr.ReadLine();
174     if (line.ToLower().Contains(string.Format("<tr class=\"pre\">").ToLower()))
175     {
176     line = sr.ReadLine();
177     if (line.ToLower().Contains(string.Format("<th scope=\"row\">Now</th>").ToLower()))
178     {
179     line = sr.ReadLine();
180     if (line.ToLower().Contains(string.Format("<td>").ToLower()))
181     {
182     //Console.WriteLine("Found: {0}", line);
183     string raw_line = line;
184     raw_line = raw_line.Replace("<td>", "").Replace("</td>", "").Replace("&deg;","");
185     raw_line = raw_line.Trim();
186     //Console.WriteLine("Found Raw: {0}", line);
187 william 25 //Console.WriteLine("Found Parsed: {0}", raw_line);
188 william 24 UrlsToDownload.Add(new URLLink("Now", new Uri(scrapper.URL), raw_line));
189     break;
190     }
191     }
192     }
193     }
194     sr.Close();
195     }
196 william 15 }
197     }

  ViewVC Help
Powered by ViewVC 1.1.22