HomeGenie Forum

Automation Program Plugins and Wizard Scripting => Help => Topic started by: codedmind on November 17, 2014, 10:40:38 PM

Title: Best way to
Post by: codedmind on November 17, 2014, 10:40:38 PM
Hy there,

What is the best way to get values from a webpage?
1 - c# and parse it (how to do it?)
2 - c# to run shell script and get the values in json format and working them with c#
3 - python, but how to scrap the html?

Thanks
Title: Re: Best way to
Post by: bkenobi on November 17, 2014, 11:18:23 PM
I can't help with an example for you, but perhaps you could review the weather codes to see if they provide any useful information.
Title: Re: Best way to
Post by: mvdarend on November 18, 2014, 08:46:47 AM
There are plenty of good articles on Screen Scraping in C# (https://www.google.com/webhp?q=screen%20scraping%20in%20c%23#safe=off&q=screen+scraping+in+c%23)

If the above links don't help, could you give an example of the webpage value you would like to retrieve? I should be able to create some example code for you.

Title: Re: Best way to
Post by: bkenobi on November 18, 2014, 04:12:40 PM
How many of those components will work within HG?  I assume  you would need to use the NetHelper class and whatever methods Gene has included in it.  I've found that many times using code snippets won't work when they require outside classes since they may not be compiled into HG.
Title: Re: Best way to
Post by: mvdarend on November 18, 2014, 07:16:16 PM
The WebClient is a standard .NET class, Gene probably makes use of it in his NetHelper Class.

Here is a simple example:
Code: [Select]
// declare the URL we want to scrape
string url = "http://www.homegenie.it/";
string webresult;

using (System.Net.WebClient client = new System.Net.WebClient())
{
  // populate webresult with the contents of the website
  webresult = client.DownloadString(url);
}

// Test. Count the number of characters in the string
Program.Notify("result length", webresult.Length.ToString());

The string 'webresult' contains the HTML content of the given website. At the end I just display a count of all the characters to see if something has been retrieved.
Title: Re: Best way to
Post by: codedmind on November 19, 2014, 09:54:21 PM
I get what i want in a mixed way...

Create a python scritp that i the run in hg using c#

But now i have another problem... how to pass the value to HG as watts values so i can record the watts and the see them in the analyse?
Title: Re: Best way to
Post by: mvdarend on November 20, 2014, 06:13:27 AM
But now i have another problem... how to pass the value to HG as watts values so i can record the watts and the see them in the analyse?
Check the Energy Monitor code, a few lines out of that is what you need.
Title: Re: Best way to
Post by: codedmind on November 22, 2014, 09:33:07 PM
done that but never get values into analyse graphs...
Title: Re: Best way to
Post by: Gene on November 22, 2014, 11:53:25 PM
you have to store the value in the "Meter.Watts" parameter:

Code: [Select]
Program.Parameter("Meter.Watts").Value = <value_read_from_sensor>;

stats will be shown after 5 minutes sampling.
Also read this:

http://www.homegenie.it/forum/index.php?topic=227.msg1201 (http://www.homegenie.it/forum/index.php?topic=227.msg1201)

g.

Title: Re: Best way to
Post by: codedmind on November 23, 2014, 11:34:17 PM
Thank you.

Is woking now.

Gene using c# hg has any helper to work with json strings? What is the best way?
Title: Re: Best way to
Post by: Gene on November 24, 2014, 12:47:38 AM
Try this:

Code: [Select]
// example json input string
string jsonText = "{ \"Bar\" : \"something\" }";

// deserialize as dynamic
dynamic foo = JObject.Parse(jsonText);

// get the Bar field value
string bar = foo.Bar;

// output the result
Program.Notify("JSON Test", bar);

reference: http://thewayofcode.wordpress.com/2012/09/18/c-dynamic-object-and-json-serialization-with-json-net/ (http://thewayofcode.wordpress.com/2012/09/18/c-dynamic-object-and-json-serialization-with-json-net/)

Have you considered using a Javascript app instead of a c# one? Perhaps it's easier to code.

g.
Title: Re: Best way to
Post by: codedmind on April 17, 2015, 10:23:13 AM
How can i make an javascript program to load a webpage and then serialize the html content?