Commit af09757e authored by Anthony Mochel's avatar Anthony Mochel
Browse files

refactor(weather): add weather service

parent 865d5ebe
......@@ -186,6 +186,7 @@
<Compile Include="ref_beehive_poll_forecast.cs">
<DependentUpon>BeesWeatherModel.tt</DependentUpon>
</Compile>
<Compile Include="WeatherForecast.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
......
......@@ -14,8 +14,6 @@ namespace BeeWeatherPollenTracker
internal static class Program
{
#region Constantes
private const string OPEN_WEATHER_MAP_API_KEY = "3f80e65ed0609e7e52737e65d522aed8";
private const double FORECAST_LENGTH = 3.0;
private const string POLLEN_FORECAST_URL = "https://opendata.dwd.de/climate_environment/health/alerts/s31fg.json";
#endregion
......@@ -27,28 +25,26 @@ namespace BeeWeatherPollenTracker
{
// Entity Framework setup
DbConfiguration.SetConfiguration(new MySqlEFConfiguration());
// Set up API KEY
ClientSettings.SetApiKey(OPEN_WEATHER_MAP_API_KEY);
#region Weather
WeatherForecast weather = new WeatherForecast();
Thread t = new Thread(new ThreadStart(weather.Process));
t.Start();
#endregion
#region Pool
// TODO: Create new service to retreive poll info (like weather)
// TODO: Launch service with thread
// Set up the logger manager
logs = LoggerManager.GetInstance();
// Retrieve forecast data from from OWM API
// Retrieve poll forecast data
try
{
var cityId = int.Parse(ConfigurationManager.AppSettings["CityId"]);
var apiResults = FiveDaysForecast.GetByCityId(cityId);
if (!apiResults.Success)
{
throw new ApplicationException(apiResults.Message);
}
// Filter: keep only 3 next days forecast
var forecast = apiResults.Items
.Where(x => x.Date < DateTime.Now.AddDays(FORECAST_LENGTH))
.ToList();
{
// Get Pollen Data
ClimateEnvironment pollenData;
using (HttpClient pollenClient = new HttpClient())
......@@ -69,11 +65,8 @@ namespace BeeWeatherPollenTracker
{
logs.WriteInFile(e);
}
}
#endregion
}
}
}
using MySql.Data.EntityFramework;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Linq;
using WeatherNet;
using WeatherNet.Clients;
namespace BeeWeatherPollenTracker
{
public class WeatherForecast
{
#region Constantes
private const string OPEN_WEATHER_MAP_API_KEY = "3f80e65ed0609e7e52737e65d522aed8";
private const double FORECAST_LENGTH = 3.0;
#endregion
#region Attributes
// Weather forecast data
private List<beehive_weather_forecast> _weather_forecast_list;
#endregion
/// <summary>
/// Create new instance of weatherforcase
/// </summary>
public WeatherForecast()
{
// Initialize weather forecast list
this._weather_forecast_list = new List<beehive_weather_forecast>();
// Set up API KEY
ClientSettings.SetApiKey(OPEN_WEATHER_MAP_API_KEY);
}
public void Process()
{
RetreiveData();
PushForecastToDatabase();
}
/// <summary>
/// Retreive Data from API
/// </summary>
private void RetreiveData()
{
// Retrieve forecast data from from OWM API
var cityId = int.Parse(ConfigurationManager.AppSettings["CityId"]);
var apiResults = FiveDaysForecast.GetByCityId(cityId);
if (!apiResults.Success)
{
throw new ApplicationException(apiResults.Message);
}
// Filter: keep only 3 next days forecast
var forecast = apiResults.Items
.Where(x => x.Date < DateTime.Now.AddDays(FORECAST_LENGTH))
.ToList();
foreach (var item in forecast)
{
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;
w.Wind = item.WindSpeed;
w.Cloudiness = item.Description;
w.Location = item.City;
_weather_forecast_list.Add(w);
}
}
/// <summary>
/// Push weather forecast to database
/// </summary>
private void PushForecastToDatabase()
{
try
{
// Entity context
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)
{
Console.WriteLine(ex.Message);
}
finally
{
// Clear weather data
this._weather_forecast_list.Clear();
}
}
}
}
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