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

Annotation of /trunk/libThermoControl/Configuration.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 26 - (hide annotations) (download)
Wed Nov 13 03:39:04 2013 UTC (7 years, 3 months ago) by william
File size: 6889 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     int d_temp = Configuration.DEFAULT_TEMP;
25     int scale = 5;
26     int s = (d_temp - e_temp) / scale;
27     int offset = 2;
28     int t = (d_temp + s) - offset;
29     return t;
30     }
31 william 22
32 william 24 public static readonly string WEATHER_URL = ""; // 60
33 william 17 public static readonly string THERMISTAT_DEVICE = ""; // 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     public static readonly string DEFAULT_MODE = ""; // cool
38 william 16 public static readonly string DEFAULT_FAN_SPEED = ""; // 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     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 william 20
77     // get the current temp from file
78     //ReadTemp();
79 william 15 }
80 william 20
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 william 21 bw.Write((byte)temp);
88 william 20 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 william 21 temp = br.ReadByte();
115 william 20 br.Close();
116     }
117     }
118     return temp;
119     }
120 william 22
121     static int GetExternalTemp()
122     {
123 william 24 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 william 26 catch (Exception)
141 william 24 {
142     temp = 0;
143     }
144     }
145     }
146     }
147     return temp;
148 william 22 }
149 william 24
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 william 25 //Console.WriteLine("Found Parsed: {0}", raw_line);
171 william 24 UrlsToDownload.Add(new URLLink("Now", new Uri(scrapper.URL), raw_line));
172     break;
173     }
174     }
175     }
176     }
177     sr.Close();
178     }
179 william 15 }
180     }

  ViewVC Help
Powered by ViewVC 1.1.22