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

Contents of /trunk/libThermoControl/Configuration.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 25 - (show annotations) (download)
Wed Nov 13 03:08:55 2013 UTC (7 years, 3 months ago) by william
File size: 6538 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
21 public static int ExternalTemp { get { return GetExternalTemp(); } }
22
23 public static readonly string WEATHER_URL = ""; // 60
24 public static readonly string THERMISTAT_DEVICE = ""; // 60
25 public static readonly int MIN_TEMP = 0; // 60
26 public static readonly int MAX_TEMP = 0; // 90
27 public static readonly int DEFAULT_TEMP = 0; // 70
28 public static readonly string DEFAULT_MODE = ""; // cool
29 public static readonly string DEFAULT_FAN_SPEED = ""; // auto_fan
30
31 [Conditional("CONFIG_UNITTEST")]
32 public static void UnitTest()
33 {
34 }
35
36 static Configuration()
37 {
38 // load configuration
39 //using (FileStream fs = new FileStream(config_file, FileMode.Open, FileAccess.Read, FileShare.Read))
40 //{
41
42 //}
43 XDocument xdoc = XDocument.Load(config_file);
44
45 Type p = typeof(Configuration);
46 var fields = new List<FieldInfo>(p.GetFields(BindingFlags.Public | BindingFlags.Static));
47 foreach (var field in fields)
48 {
49 //Console.WriteLine("Name: '{0}' value: '{1}'", field.Name, xdoc);
50 //var lv1s = from lv1 in xdoc.Descendants("level1")
51 // select lv1.Attribute("name").Value;
52 //var t = xdoc.Root.DescendantNodesAndSelf().ToList();
53
54 var o = xdoc.Root.Elements(field.Name);
55 var list = o.ToList();
56 if (list.Count == 0)
57 {
58 Console.WriteLine("Name: '{0}' Default: '{1}' Configured: '{1}'", field.Name, field.GetValue(null).ToString());
59 }
60 else
61 {
62 var t = xdoc.Root.Elements(field.Name).ToList().FirstOrDefault().Value;
63 Console.WriteLine("Name: '{0}' Default: '{1}' Configured: '{2}'", field.Name, field.GetValue(null).ToString(), t);
64 field.SetValue(null, Convert.ChangeType(t,field.FieldType));
65 }
66 }
67
68 // get the current temp from file
69 //ReadTemp();
70 }
71
72 static void WriteTemp(int temp)
73 {
74 using (FileStream fs = new FileStream(temp_file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
75 {
76 using (BinaryWriter bw = new BinaryWriter(fs))
77 {
78 bw.Write((byte)temp);
79 bw.Flush();
80 bw.Close();
81 }
82 }
83 }
84 static int ReadTemp()
85 {
86 int temp = 0;
87 if (!File.Exists(temp_file))
88 {
89 WriteTemp(Configuration.DEFAULT_TEMP);
90 }
91 else
92 {
93 FileInfo fi = new FileInfo(temp_file);
94 if (fi.Length == 0)
95 {
96 WriteTemp(Configuration.DEFAULT_TEMP);
97 }
98 }
99
100
101 using (FileStream fs = new FileStream(temp_file, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
102 {
103 using (BinaryReader br = new BinaryReader(fs))
104 {
105 temp = br.ReadByte();
106 br.Close();
107 }
108 }
109 return temp;
110 }
111
112 static int GetExternalTemp()
113 {
114 int temp = 0;
115 List<string> BannedLinks = new List<string>();
116 using (GenericScrapper scrapper = new GenericScrapper(WEATHER_URL, true, BannedLinks, false, true))
117 {
118 scrapper.BannedHTMLLinks = BannedLinks;
119 scrapper.GetUrlsToDownload += scrapper_GetUrlsToDownload;
120 scrapper.CreateDownloadUrls();
121 if (scrapper.UrlsToDownload.Count > 0)
122 {
123 var t = scrapper.UrlsToDownload.FirstOrDefault();
124 if (t != null && t.Tag != null)
125 {
126 try
127 {
128 int k = Convert.ToInt32(t.Tag);
129 temp = k;
130 }
131 catch (Exception ex)
132 {
133 temp = 0;
134 }
135 }
136 }
137 }
138 return temp;
139 }
140
141 static void scrapper_GetUrlsToDownload(out List<URLLink> UrlsToDownload, GenericScrapper scrapper)
142 {
143 UrlsToDownload = new List<URLLink>();
144 StreamReader sr = scrapper.HTMLReader;
145 while (!sr.EndOfStream)
146 {
147 string line = sr.ReadLine();
148 if (line.ToLower().Contains(string.Format("<tr class=\"pre\">").ToLower()))
149 {
150 line = sr.ReadLine();
151 if (line.ToLower().Contains(string.Format("<th scope=\"row\">Now</th>").ToLower()))
152 {
153 line = sr.ReadLine();
154 if (line.ToLower().Contains(string.Format("<td>").ToLower()))
155 {
156 //Console.WriteLine("Found: {0}", line);
157 string raw_line = line;
158 raw_line = raw_line.Replace("<td>", "").Replace("</td>", "").Replace("&deg;","");
159 raw_line = raw_line.Trim();
160 //Console.WriteLine("Found Raw: {0}", line);
161 //Console.WriteLine("Found Parsed: {0}", raw_line);
162 UrlsToDownload.Add(new URLLink("Now", new Uri(scrapper.URL), raw_line));
163 break;
164 }
165 }
166 }
167 }
168 sr.Close();
169 }
170 }
171 }

  ViewVC Help
Powered by ViewVC 1.1.22