This is a working C# .Net application example getting data from a
HA7net and writing
the results to the console.
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
namespace screenScrapeConsoleApp0
{
class Program
{
static
void Main(string[] args)
{
while
(1 == 1)
{
string x = extractTemp("http://192.168.1.100/1Wire/ReadTemperature.html?Address_Array=2C00080152F24710,4E0000000AB26810,AC000000085DA210,1C000000085CAE10,31000000085D7110,");
Console.WriteLine(x);
writeLogFile(x);
}
}
public
static void writeLogFile(string log)
{
string
year = DateTime.Now.Year.ToString();
string
month = DateTime.Now.Month.ToString().Length <
2 ? string.Format("0{0}",
DateTime.Now.Month.ToString()) :
DateTime.Now.Month.ToString();
string
day = DateTime.Now.Day.ToString().Length < 2 ?
string.Format("0{0}",
DateTime.Now.Day.ToString()) : DateTime.Now.Day.ToString();
StringBuilder
builder = new StringBuilder();
builder.Append(string.Format(@"c:\templogs\{0}{1}{2}Log.txt",
year, month, day));
string
fileName = builder.ToString();
FileStream
fileStream = new FileStream(fileName,
FileMode.Append);
using
(var sw = new StreamWriter(fileStream))
{
sw.WriteLine(log);
}
}
public
static String GetHtmlPage(string strURL)
{
//
the html retrieved from the page
String
strResult;
WebResponse
objResponse;
WebRequest
objRequest = System.Net.HttpWebRequest.Create(strURL);
objResponse
= objRequest.GetResponse();
//
the using keyword will automatically dispose the object
//
once complete
using
(StreamReader sr =
new
StreamReader(objResponse.GetResponseStream()))
{
strResult
= sr.ReadToEnd();
//extractTemp(strResult);
sr.Close();
}
return
strResult;
}
private
static double returnValue(string htmpPage, string
strSensorNum)
{
string
s = htmpPage;
string
num = strSensorNum;
double
returnValue = 0;
if
(!double.TryParse(HttpUtility.UrlEncodeUnicode(Regex.Match(s,
string.Format("<INPUT CLASS=\"HA7Value\"
NAME=\"Temperature_{0}\" ID=\"Temperature_{1}\" TYPE=\"text\" VALUE=\"(?<val>.*?)\">",
num, num)).Groups["val"].Value),
out returnValue))
{
returnValue
= 0;
}
return
returnValue;
}
public
static string extractTemp(string strUrl)
{
StringBuilder
builder = new StringBuilder();
List<double> temp0c = new
List<double>();
List<double> temp1c = new
List<double>();
List<double> temp2c = new
List<double>();
List<double> temp3c = new
List<double>();
List<double> temp4c = new
List<double>();
for
(int i = 0; i < 5; i++)
{
string s = GetHtmlPage(strUrl);
temp0c.Add(returnValue(s,
"0")); // The number is the sensor
number
temp1c.Add(returnValue(s,
"1"));
temp2c.Add(returnValue(s,
"2"));
temp3c.Add(returnValue(s,
"3"));
temp4c.Add(returnValue(s,
"4"));
System.Threading.Thread.Sleep(5000);
}
temp0c.Sort();
temp1c.Sort();
temp2c.Sort();
temp3c.Sort();
temp4c.Sort();
List<double> tempsF = new
List<double>();
tempsF.Add(Math.Round(temp0c[2] * 9 / 5 + 32, 2));
tempsF.Add(Math.Round(temp1c[2] * 9 / 5 + 32, 2));
tempsF.Add(Math.Round(temp2c[2] * 9 / 5 + 32, 2));
tempsF.Add(Math.Round(temp3c[2] * 9 / 5 + 32, 2));
tempsF.Add(Math.Round(temp4c[2] * 9 / 5 + 32, 2));
string
myString = "";
foreach
(double temp in tempsF)
{
myString
+= temp.ToString() + ", ";
}
builder.Append(" ");
builder.Append(DateTime.Now.ToShortDateString());
builder.Append(" ");
builder.Append(DateTime.Now.ToShortTimeString());
myString
+= builder.ToString();
return
myString;
}
}
}
* This source code was highlighted with
Source Code Highlighter.