Skip to content
This repository was archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
Modernized Core project a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
scarpentier committed Jan 26, 2013
1 parent 4761022 commit fa1a7d8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 54 deletions.
24 changes: 10 additions & 14 deletions RezNetUsage.Core/AppartHelper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Linq;

namespace RezNetUsage.Core
{
using System.Data;
using System.IO;
using System.Reflection;

public static class AppartHelper
{
public static bool IsAppartExist(int phase, int appart)
Expand All @@ -21,8 +21,8 @@ public static bool IsAppartExist(int phase, int appart)
DataRow[] foundrows = chambres.Tables[0].Select("Phase = " + phase + " AND Appartement = " + appart);

// Retourner le résultat
return (foundrows.Count() == 1);
} catch (Exception ex)
return (foundrows.Any());
} catch
{
return false;
}
Expand All @@ -47,6 +47,7 @@ public static Chambres GetChambres()
var chambres = new Chambres();

// Loader le data dans le dataset
// TODO: Trouver un meilleur plan que d'utiliser la réflexion pour aller chercher un XML à chaque fois que quelqu'un demande une chambre!
var _assembly = Assembly.GetExecutingAssembly();
chambres.ReadXml(new StreamReader(_assembly.GetManifestResourceStream("RezNetUsage.Core.Chambres.xml")));

Expand All @@ -55,7 +56,7 @@ public static Chambres GetChambres()

public static Usage GetUsage(Chambres chambres, int mois)
{
Usage usage = new Usage();
var usage = new Usage();

foreach (DataRow row in chambres.Tables[0].Rows)
{
Expand All @@ -64,8 +65,8 @@ public static Usage GetUsage(Chambres chambres, int mois)
(string)row["Phase"]);
try
{
usage = UsageFactory.GetUsage(
usage, int.Parse((string)row["Phase"]), int.Parse((string)row["Appartement"]), mois);
usage = new Usage().GetUsage(
int.Parse((string)row["Phase"]), int.Parse((string)row["Appartement"]), mois);
}
catch (Exception ex)
{
Expand All @@ -75,10 +76,5 @@ public static Usage GetUsage(Chambres chambres, int mois)

return usage;
}

public static Usage GetUsage(int phase, int appart, int mois)
{
return UsageFactory.GetUsage(phase, appart, mois);
}
}
}
34 changes: 19 additions & 15 deletions RezNetUsage.Core/Net.cs → RezNetUsage.Core/NetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
using System.Net;
using System.Text;

namespace SPACEBAR
namespace RezNetUsage.Core
{
public class Net
public class NetHelper
{
public static string DownloadHTML(string url)
public static string DownloadHtml(string url)
{
try
{
WebClient webClient = new WebClient();
byte[] myDatabuffer = webClient.DownloadData(url);
var webClient = new WebClient();
var myDatabuffer = webClient.DownloadData(url);
return Encoding.Default.GetString(myDatabuffer);
}
catch (Exception ex)
Expand All @@ -20,18 +20,22 @@ public static string DownloadHTML(string url)
}
}

public static string DownloadHTML(string url, string username, string password)
public static string DownloadHtml(string url, string username, string password)
{
try
{
WebClient webClient = new WebClient();
var webClient = new WebClient();

// Credentials management
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
var cache = new CredentialCache {
{
new Uri(url),
"Basic",
new NetworkCredential(username, password)
}};
webClient.Credentials = cache;

byte[] myDatabuffer = webClient.DownloadData(url);
var myDatabuffer = webClient.DownloadData(url);
return Encoding.Default.GetString(myDatabuffer);
}
catch (Exception ex)
Expand All @@ -42,20 +46,20 @@ public static string DownloadHTML(string url, string username, string password)

public static void DownloadFile(string url, string fileName)
{
WebClient webClient = new WebClient();
var webClient = new WebClient();
webClient.DownloadFile(url, fileName);
}

public static bool DoesUrlExists(string url)
{
bool urlExists = false;
var urlExists = false;
try
{
WebRequest req = WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
var req = WebRequest.Create(url);
var response = (HttpWebResponse)req.GetResponse();
urlExists = true;
}
catch (System.Net.WebException ex)
catch (WebException ex)
{

}
Expand Down
2 changes: 1 addition & 1 deletion RezNetUsage.Core/RezNetUsage.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Chambres.xsd</DependentUpon>
</Compile>
<Compile Include="Net.cs" />
<Compile Include="NetHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Usage.cs">
<DependentUpon>Usage.xsd</DependentUpon>
Expand Down
33 changes: 10 additions & 23 deletions RezNetUsage.Core/UsageFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,63 +13,50 @@ public static class UsageFactory
/// <summary>
/// Regex pour définir l'utilisation
/// </summary>
private readonly static Regex regexUsage =
private readonly static Regex RegexUsage =
new Regex("<TR><TD>(.*)</TD><TD>(.*)</TD><TD ALIGN=\"RIGHT\">(.*)</TD><TD ALIGN=\"RIGHT\">(.*)</TD></TR>");

/// <summary>
/// Regex pour définir l'utilisation maximale permise
/// </summary>
private static readonly Regex regexMax =
private static readonly Regex RegexMax =
new Regex("<TD>Quota permis pour la p&eacute;riode</TD><TD ALIGN=\"RIGHT\">(.*)</TD></TD></TR>");

/// <summary>
/// Le parsing des doubles de cooptel dépendent de la culture locale
/// </summary>
private static readonly CultureInfo Culture = CultureInfo.CreateSpecificCulture("en-CA");

/// <summary>
/// Retourne un objet Dataset Usage pour la phase, appart et mois spécifié
/// </summary>
/// <param name="phase"></param>
/// <param name="appart"></param>
/// <param name="mois"></param>
/// <returns></returns>
public static Usage GetUsage(int phase, int appart, int mois)
{
var usage = new Usage();
return GetUsage(usage, phase, appart, mois);
}

/// <summary>
/// Ajoute les informations de la consommation internet pour un appart dans un objet Usage donné
/// </summary>
/// <param name="usage"></param>
/// <param name="phase"></param>
/// <param name="appart"></param>
/// <param name="mois"></param>
/// <returns></returns>
public static Usage GetUsage(Usage usage, int phase, int appart, int mois)
/// <returns>Objet usage plein de data</returns>
public static Usage GetUsage(this Usage usage, int phase, int appart, int mois)
{
// Build query string
// http://ets-res2-772:[email protected]/services/temps/?mois=9&cmd=Visualiser
string query = "http://www2.cooptel.qc.ca/services/temps/?mois=" + mois + "&cmd=Visualiser";
string user = "ets-res" + phase + "-" + appart;
string pass = "ets" + appart;
var query = string.Format("http://www2.cooptel.qc.ca/services/temps/?mois={0}&cmd=Visualiser", mois);
var user = string.Format("ets-res{0}-{1}", phase, appart);
var pass = string.Format("ets{0}", appart);

string html;
try
{
// Fetch data into a string
html = SPACEBAR.Net.DownloadHTML(query, user, pass);
html = NetHelper.DownloadHtml(query, user, pass);
}
catch (Exception ex)
{
throw new Exception("Could not download html:" + ex.Message);
}

// Match du regeex
MatchCollection mc = regexUsage.Matches(html);
usage.Maximum = Math.Round(Double.Parse(regexMax.Matches(html)[0].Groups[1].Value, Culture), 0);
MatchCollection mc = RegexUsage.Matches(html);
usage.Maximum = Math.Round(Double.Parse(RegexMax.Matches(html)[0].Groups[1].Value, Culture), 0);

// Objets temporaires
for (var i = 0; i <= mc.Count - 1; i++)
Expand Down
2 changes: 1 addition & 1 deletion RezNetUsage.Web/Default.aspx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected void Page_Load(object sender, EventArgs e)
try {

// Aller chercher l'utilisation
_usage = UsageFactory.GetUsage(_phase, _appart, _mois);
_usage = new Usage().GetUsage(_phase, _appart, _mois);

// Mettre les paramètres dans les contrôles
lblPhaseAppartMois.Text = String.Format(lblPhaseAppartMois.Text, _phase, _appart, Mois[_mois - 1]);
Expand Down

0 comments on commit fa1a7d8

Please sign in to comment.