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

Contents of /trunk/libThermoControl/Configuration.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 29 - (show annotations) (download)
Wed Feb 12 03:28:37 2014 UTC (7 years ago) by william
File size: 7789 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 using libhtmlscrapper;
10
11 namespace libThermoControl
12 {
13 public static class Configuration
14 {
15 private const string config_file = "config.xml";
16 private const string temp_file = "current_temp.bin";
17
18
19 public static int CurrentTemp { get { return ReadTemp(); } set { WriteTemp(value); } }
20 public static int ExternalTemp { get { return GetExternalTemp(); } }
21 public static int GetTemperatureAdjustment()
22 {
23 int e_temp = Configuration.ExternalTemp;
24 int d_temp = Configuration.CurrentTemp;
25 int scale = 5;
26 int s = (d_temp - e_temp) / scale;
27 int offset = 4;
28 int t = (d_temp + s) - offset;
29 return t;
30 }
31
32 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 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 public static readonly string DEFAULT_MODE = "cool"; // cool
38 public static readonly string DEFAULT_FAN_SPEED = "auto_fan"; // auto_fan
39
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 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 }
77 else
78 {
79 var t = xdoc.Root.Elements(field.Name).ToList().FirstOrDefault().Value;
80 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 }
92 }
93
94 // get the current temp from file
95 //ReadTemp();
96 }
97
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 bw.Write((byte)temp);
105 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 temp = br.ReadByte();
132 br.Close();
133 }
134 }
135 return temp;
136 }
137
138 static int GetExternalTemp()
139 {
140 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 catch (Exception)
158 {
159 temp = 0;
160 }
161 }
162 }
163 }
164 return temp;
165 }
166
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 //Console.WriteLine("Found Parsed: {0}", raw_line);
188 UrlsToDownload.Add(new URLLink("Now", new Uri(scrapper.URL), raw_line));
189 break;
190 }
191 }
192 }
193 }
194 sr.Close();
195 }
196 }
197 }

  ViewVC Help
Powered by ViewVC 1.1.22