Commit b8821338 authored by COLOMBO Benjamin's avatar COLOMBO Benjamin
Browse files

Fix Disposable issues (DB context)

parent 4df6fdfb
using MySql.Data.EntityFramework; using MySql.Data.EntityFramework;
using System; using Newtonsoft.Json;
using System.Collections.Generic; using System;
using System.Configuration; using System.Collections.Generic;
using System.Data.Entity; using System.Configuration;
using System.Linq; using System.Data.Entity;
using WeatherNet; using System.IO;
using WeatherNet.Clients; using System.Linq;
using WeatherNet;
namespace BeeWeatherPollenTracker using WeatherNet.Clients;
{
public class WeatherForecast namespace BeeWeatherPollenTracker
{ {
#region Constantes public class WeatherForecast
private const string OPEN_WEATHER_MAP_API_KEY = "3f80e65ed0609e7e52737e65d522aed8"; {
private const double FORECAST_LENGTH = 3.0; #region Constantes
#endregion
private const string OPEN_WEATHER_MAP_API_KEY = "3f80e65ed0609e7e52737e65d522aed8";
#region Attributes private const double FORECAST_LENGTH = 3.0;
// Weather forecast data
private List<beehive_weather_forecast> _weather_forecast_list; #endregion Constantes
private static LoggerManager logs = null;
#endregion #region Attributes
/// <summary> // Weather forecast data
/// Create new instance of weatherforcase private List<beehive_weather_forecast> _weather_forecast_list;
/// </summary>
public WeatherForecast() private static LoggerManager logs = LoggerManager.GetInstance();
{
// Initialize weather forecast list #endregion Attributes
this._weather_forecast_list = new List<beehive_weather_forecast>();
/// <summary>
// Set up API KEY /// Create new instance of weatherforcase
ClientSettings.SetApiKey(OPEN_WEATHER_MAP_API_KEY); /// </summary>
public WeatherForecast()
// Set up the logger manager {
logs = LoggerManager.GetInstance(); // Initialize weather forecast list
} this._weather_forecast_list = new List<beehive_weather_forecast>();
public void Process() // Set up API KEY
{ ClientSettings.SetApiKey(OPEN_WEATHER_MAP_API_KEY);
RetreiveData(); }
PushForecastToDatabase();
} public void Process()
{
/// <summary> try
/// Retreive Data from API {
/// </summary> RetreiveData();
private void RetreiveData() PushForecastToDatabase();
{ }
// Retrieve forecast data from from OWM API catch (Exception ex)
var cityId = int.Parse(ConfigurationManager.AppSettings["CityId"]); {
var apiResults = FiveDaysForecast.GetByCityId(cityId); logs.WriteInFile(ex);
if (_weather_forecast_list != null && _weather_forecast_list.Any())
if (!apiResults.Success) {
{ try
throw new ApplicationException(apiResults.Message); {
} File.WriteAllText(
$"weather_{DateTime.Now.ToString()}.json",
// Filter: keep only 3 next days forecast JsonConvert.SerializeObject(_weather_forecast_list)
var forecast = apiResults.Items );
.Where(x => x.Date < DateTime.Now.AddDays(FORECAST_LENGTH)) }
.ToList(); catch (Exception inner)
{
foreach (var item in forecast) logs.WriteInFile(inner);
{ }
beehive_weather_forecast w = new beehive_weather_forecast(); }
w.Input_date = DateTime.Now; }
w.Forecast_date = item.Date; }
w.Temperature = item.Temp;
w.Humidity = item.Humidity; /// <summary>
w.Wind = item.WindSpeed; /// Retreive Data from API
w.Cloudiness = item.Description; /// </summary>
w.Location = item.City; private void RetreiveData()
{
_weather_forecast_list.Add(w); // Retrieve forecast data from from OWM API
} var cityId = int.Parse(ConfigurationManager.AppSettings["CityId"]);
} var apiResults = FiveDaysForecast.GetByCityId(cityId);
/// <summary> if (!apiResults.Success)
/// Push weather forecast to database {
/// </summary> throw new ApplicationException(apiResults.Message);
private void PushForecastToDatabase() }
{
try // Filter: keep only 3 next days forecast
{ var forecast = apiResults.Items
// Entity context .Where(x => x.Date < DateTime.Now.AddDays(FORECAST_LENGTH))
var context = new delmeEntities1(); .ToList();
// Add weather forecast to context foreach (var item in forecast)
foreach (var item in this._weather_forecast_list) {
context.beehive_weather_forecast.Add(item); beehive_weather_forecast w = new beehive_weather_forecast();
w.Input_date = DateTime.Now;
// Push data in databse w.Forecast_date = item.Date;
context.SaveChanges(); w.Temperature = item.Temp;
w.Humidity = item.Humidity;
} w.Wind = item.WindSpeed;
catch (Exception ex) w.Cloudiness = item.Description;
{ w.Location = item.City;
logs.WriteInFile(ex);
} _weather_forecast_list.Add(w);
finally }
{ }
// Clear weather data
this._weather_forecast_list.Clear(); /// <summary>
} /// Push weather forecast to database
} /// </summary>
} private void PushForecastToDatabase()
} {
try
{
// Entity context
using (var context = new delmeEntities1())
{
// Add weather forecast to context
foreach (var item in this._weather_forecast_list)
context.beehive_weather_forecast.Add(item);
// Push data in databse
context.SaveChanges();
}
}
catch (Exception ex)
{
logs.WriteInFile(ex);
}
finally
{
// Clear weather data
this._weather_forecast_list.Clear();
}
}
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment