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

Contents of /trunk/libThermoControl/Configuration.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 28 - (show annotations) (download)
Wed Feb 12 03:22:55 2014 UTC (7 years ago) by william
File size: 6989 byte(s)
+ update default settings ... define them incase they are not present in config.xml

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 Console.WriteLine("Name: '{0}' Default: '{1}' Configured: '{1}'", field.Name, field.GetValue(null).ToString());
68 }
69 else
70 {
71 var t = xdoc.Root.Elements(field.Name).ToList().FirstOrDefault().Value;
72 Console.WriteLine("Name: '{0}' Default: '{1}' Configured: '{2}'", field.Name, field.GetValue(null).ToString(), t);
73 field.SetValue(null, Convert.ChangeType(t,field.FieldType));
74 }
75 }
76
77 // get the current temp from file
78 //ReadTemp();
79 }
80
81 static void WriteTemp(int temp)
82 {
83 using (FileStream fs = new FileStream(temp_file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
84 {
85 using (BinaryWriter bw = new BinaryWriter(fs))
86 {
87 bw.Write((byte)temp);
88 bw.Flush();
89 bw.Close();
90 }
91 }
92 }
93 static int ReadTemp()
94 {
95 int temp = 0;
96 if (!File.Exists(temp_file))
97 {
98 WriteTemp(Configuration.DEFAULT_TEMP);
99 }
100 else
101 {
102 FileInfo fi = new FileInfo(temp_file);
103 if (fi.Length == 0)
104 {
105 WriteTemp(Configuration.DEFAULT_TEMP);
106 }
107 }
108
109
110 using (FileStream fs = new FileStream(temp_file, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
111 {
112 using (BinaryReader br = new BinaryReader(fs))
113 {
114 temp = br.ReadByte();
115 br.Close();
116 }
117 }
118 return temp;
119 }
120
121 static int GetExternalTemp()
122 {
123 int temp = 0;
124 List<string> BannedLinks = new List<string>();
125 using (GenericScrapper scrapper = new GenericScrapper(WEATHER_URL, true, BannedLinks, false, true))
126 {
127 scrapper.BannedHTMLLinks = BannedLinks;
128 scrapper.GetUrlsToDownload += scrapper_GetUrlsToDownload;
129 scrapper.CreateDownloadUrls();
130 if (scrapper.UrlsToDownload.Count > 0)
131 {
132 var t = scrapper.UrlsToDownload.FirstOrDefault();
133 if (t != null && t.Tag != null)
134 {
135 try
136 {
137 int k = Convert.ToInt32(t.Tag);
138 temp = k;
139 }
140 catch (Exception)
141 {
142 temp = 0;
143 }
144 }
145 }
146 }
147 return temp;
148 }
149
150 static void scrapper_GetUrlsToDownload(out List<URLLink> UrlsToDownload, GenericScrapper scrapper)
151 {
152 UrlsToDownload = new List<URLLink>();
153 StreamReader sr = scrapper.HTMLReader;
154 while (!sr.EndOfStream)
155 {
156 string line = sr.ReadLine();
157 if (line.ToLower().Contains(string.Format("<tr class=\"pre\">").ToLower()))
158 {
159 line = sr.ReadLine();
160 if (line.ToLower().Contains(string.Format("<th scope=\"row\">Now</th>").ToLower()))
161 {
162 line = sr.ReadLine();
163 if (line.ToLower().Contains(string.Format("<td>").ToLower()))
164 {
165 //Console.WriteLine("Found: {0}", line);
166 string raw_line = line;
167 raw_line = raw_line.Replace("<td>", "").Replace("</td>", "").Replace("&deg;","");
168 raw_line = raw_line.Trim();
169 //Console.WriteLine("Found Raw: {0}", line);
170 //Console.WriteLine("Found Parsed: {0}", raw_line);
171 UrlsToDownload.Add(new URLLink("Now", new Uri(scrapper.URL), raw_line));
172 break;
173 }
174 }
175 }
176 }
177 sr.Close();
178 }
179 }
180 }

  ViewVC Help
Powered by ViewVC 1.1.22