Exemplo n.º 1
0
        public override void Monitor()
        {
            IAuthentifier authentifier = new FreeMobileAuthentifier(Profile);
            WebClient client = authentifier.GetAuthenticatedClient();

            string response = client.DownloadString(Url);
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(response);

            HtmlNodeCollection activeElements = doc.DocumentNode.SelectNodes("//span[@class='actif']");
            HtmlNodeCollection progressElements = doc.DocumentNode.SelectNodes("//div[@class='progress-text']");

            if (progressElements == null || progressElements.Count() != 1)
            {
                throw new HtmlStructureChangedException(Url, "Expected one element with the class 'progress-text'");
            }
            string result = progressElements.ElementAt(0).InnerText;
            Regex regex = new Regex("reste ([\\d\\.]+) Mio");
            if (!regex.IsMatch(result))
            {
                throw new HtmlStructureChangedException(Url, "Result string should formatted like this : 'reste ([\\d\\.]+) Mio'");
            }

            FreeMobileConsumption consumption = new FreeMobileConsumption()
            {
                User = Profile,
                RemainingData = Decimal.Parse(regex.Match(result).Groups[1].Value, CultureInfo.InvariantCulture),
                StartDate = DateTime.ParseExact(activeElements[0].InnerText, "dd/MM/yyyy", CultureInfo.InvariantCulture),
                ConsumedVoice = TimeSpan.ParseExact(activeElements[2].InnerText, "h'h'm'min'ss's'", CultureInfo.InvariantCulture)
            };

            FreeMobileRepository repository = new FreeMobileRepository();
            FreeMobileConsumption previous = repository.GetLastConsumption(Profile.IdProfile);
            repository.SaveConsumption(consumption);

            if ((consumption.IsDataNearMax && !previous.IsDataNearMax) // if the new data consumption is almost over when the previous wasn't
                || consumption.IsVoiceNearMax && !previous.IsVoiceNearMax) // OR if the new voice consumption is almost over when the previous wasn't
            {
                OnMonitorEnded(consumption);
            }
        }
Exemplo n.º 2
0
        public void SaveConsumption(FreeMobileConsumption consumption)
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AWSDatabase"].ConnectionString))
            {
                using (SqlCommand command = connection.CreateCommand())
                {
                    command.CommandText = @"
INSERT INTO 
    FreeConsumptionHistory (IdUser, CheckDate, VoiceConsumption, DataConsumption)
VALUES
    (@IdUser, @CheckDate, @VoiceConsumption, @DataConsumption)";
                    command.Parameters.AddParameter("@IdUser", SqlDbType.Int, consumption.User.IdProfile);
                    command.Parameters.AddParameter("@CheckDate", SqlDbType.DateTime2, DateTime.Now);
                    command.Parameters.AddParameter("@VoiceConsumption", SqlDbType.Time, consumption.ConsumedVoice);
                    command.Parameters.AddParameter("@DataConsumption", SqlDbType.Decimal, consumption.ConsumedData);

                    connection.Open();
                    command.ExecuteNonQuery();
                }
            }
        }