Commit 4df6fdfb authored by COLOMBO Benjamin's avatar COLOMBO Benjamin
Browse files

Implements exportation od data in backup file in case of failure

parent 31ffb722
using System; using System;
using System.IO; using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;
namespace BeeWeatherPollenTracker namespace BeeWeatherPollenTracker
{ {
...@@ -38,10 +35,10 @@ namespace BeeWeatherPollenTracker ...@@ -38,10 +35,10 @@ namespace BeeWeatherPollenTracker
public void WriteInFile(Exception e) public void WriteInFile(Exception e)
{ {
DateTime dt = DateTime.Now; DateTime dt = DateTime.Now;
string res = dt.ToString() + " : "+ e.Message ; string res = dt.ToString() + " : " + e.Message;
byte[] data = Encoding.UTF8.GetBytes(res); byte[] data = Encoding.UTF8.GetBytes(res);
fs.Write(data, 0, data.Length); fs.Write(data, 0, data.Length);
} }
} }
} }
\ No newline at end of file
 using MySql.Data.EntityFramework;
using MySql.Data.EntityFramework;
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Configuration; using System.Configuration;
using System.Data.Entity; using System.Data.Entity;
using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Threading; using System.Threading;
...@@ -16,44 +16,44 @@ namespace BeeWeatherPollenTracker ...@@ -16,44 +16,44 @@ namespace BeeWeatherPollenTracker
internal static class Program internal static class Program
{ {
#region Constantes #region Constantes
private const string POLLEN_FORECAST_URL = "https://opendata.dwd.de/climate_environment/health/alerts/s31fg.json"; private const string POLLEN_FORECAST_URL = "https://opendata.dwd.de/climate_environment/health/alerts/s31fg.json";
#endregion
#endregion Constantes
private static LoggerManager logs = null; private static LoggerManager logs = null;
private static void Main(string[] args) private static void Main(string[] args)
{ {
string pollenJson = null;
// Set up the logger manager
logs = LoggerManager.GetInstance();
// Entity Framework setup // Entity Framework setup
DbConfiguration.SetConfiguration(new MySqlEFConfiguration()); DbConfiguration.SetConfiguration(new MySqlEFConfiguration());
#region Weather #region Weather
WeatherForecast weather = new WeatherForecast(); WeatherForecast weather = new WeatherForecast();
Thread t = new Thread(new ThreadStart(weather.Process)); Thread t = new Thread(new ThreadStart(weather.Process));
t.Start(); t.Start();
#endregion
#endregion Weather
#region Pool #region Pollen
// TODO: Create new service to retreive poll info (like weather)
// TODO: Launch service with thread
// Set up the logger manager // Retrieve poll forecast data
logs = LoggerManager.GetInstance();
// Retrieve poll forecast data
try try
{ {
// Get Pollen Data // Get Pollen Data
ClimateEnvironment pollenData; ClimateEnvironment pollenData;
using (HttpClient pollenClient = new HttpClient()) using (HttpClient pollenClient = new HttpClient())
{ {
var pollenResults = pollenClient.GetAsync(POLLEN_FORECAST_URL).Result; var pollenResults = pollenClient.GetAsync(POLLEN_FORECAST_URL).Result;
var pollenJson = pollenResults.Content.ReadAsStringAsync().Result; pollenJson = pollenResults.Content.ReadAsStringAsync().Result;
pollenData = JsonConvert.DeserializeObject<ClimateEnvironment>(pollenJson); pollenData = JsonConvert.DeserializeObject<ClimateEnvironment>(pollenJson);
} }
...@@ -62,7 +62,7 @@ namespace BeeWeatherPollenTracker ...@@ -62,7 +62,7 @@ namespace BeeWeatherPollenTracker
.First(p => p.PartregionId.ToString() == ConfigurationManager.AppSettings["PartregionId"]); .First(p => p.PartregionId.ToString() == ConfigurationManager.AppSettings["PartregionId"]);
// 2020-03-11 11:00 Uhr // 2020-03-11 11:00 Uhr
beehive_poll_forecast poll_measure = new beehive_poll_forecast(); beehive_poll_forecast poll_measure = new beehive_poll_forecast();
poll_measure.Last_update_on = DateTime.Parse(pollenData.LastUpdate.Substring(0,pollenData.LastUpdate.Length-3)); poll_measure.Last_update_on = DateTime.Parse(pollenData.LastUpdate.Substring(0, pollenData.LastUpdate.Length - 3));
poll_measure.Sender = pollenData.Sender; poll_measure.Sender = pollenData.Sender;
poll_measure.Region_name = pollenRegionForecast.PartregionName; poll_measure.Region_name = pollenRegionForecast.PartregionName;
//pollenRegionForecast.Pollen.Erle //pollenRegionForecast.Pollen.Erle
...@@ -70,14 +70,27 @@ namespace BeeWeatherPollenTracker ...@@ -70,14 +70,27 @@ namespace BeeWeatherPollenTracker
poll_measure.Today = pollenRegionForecast.Pollen.Erle.Today; poll_measure.Today = pollenRegionForecast.Pollen.Erle.Today;
poll_measure.Tomorrow = pollenRegionForecast.Pollen.Erle.Tomorrow; poll_measure.Tomorrow = pollenRegionForecast.Pollen.Erle.Tomorrow;
poll_measure.Day_after_to = pollenRegionForecast.Pollen.Erle.DayafterTo; poll_measure.Day_after_to = pollenRegionForecast.Pollen.Erle.DayafterTo;
poll_measure. // poll_measure.
} }
catch (Exception e) catch (Exception e)
{ {
logs.WriteInFile(e); logs.WriteInFile(e);
if (string.IsNullOrEmpty(pollenJson))
{
try
{
File.WriteAllText($"pollen_{DateTime.Now.ToString()}.json", pollenJson);
}
catch (Exception inner)
{
logs.WriteInFile(inner);
}
}
} }
#endregion #endregion Pollen
t.Join();
} }
} }
} }
\ 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