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

Fix Disposable issues (DB context)

parent 4df6fdfb
using MySql.Data.EntityFramework;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.IO;
using System.Linq;
using WeatherNet;
using WeatherNet.Clients;
......@@ -12,15 +14,20 @@ namespace BeeWeatherPollenTracker
public class WeatherForecast
{
#region Constantes
private const string OPEN_WEATHER_MAP_API_KEY = "3f80e65ed0609e7e52737e65d522aed8";
private const double FORECAST_LENGTH = 3.0;
#endregion
#endregion Constantes
#region Attributes
// Weather forecast data
private List<beehive_weather_forecast> _weather_forecast_list;
private static LoggerManager logs = null;
#endregion
private static LoggerManager logs = LoggerManager.GetInstance();
#endregion Attributes
/// <summary>
/// Create new instance of weatherforcase
......@@ -32,16 +39,34 @@ namespace BeeWeatherPollenTracker
// Set up API KEY
ClientSettings.SetApiKey(OPEN_WEATHER_MAP_API_KEY);
// Set up the logger manager
logs = LoggerManager.GetInstance();
}
public void Process()
{
try
{
RetreiveData();
PushForecastToDatabase();
}
catch (Exception ex)
{
logs.WriteInFile(ex);
if (_weather_forecast_list != null && _weather_forecast_list.Any())
{
try
{
File.WriteAllText(
$"weather_{DateTime.Now.ToString()}.json",
JsonConvert.SerializeObject(_weather_forecast_list)
);
}
catch (Exception inner)
{
logs.WriteInFile(inner);
}
}
}
}
/// <summary>
/// Retreive Data from API
......@@ -85,15 +110,15 @@ namespace BeeWeatherPollenTracker
try
{
// Entity context
var context = new delmeEntities1();
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)
{
......
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