Commit 8efe30c9 authored by Anthony Mochel's avatar Anthony Mochel
Browse files

feat(log): add weather backup log

parent b8821338
......@@ -6,23 +6,24 @@ namespace BeeWeatherPollenTracker
{
public class LoggerManager
{
private static LoggerManager Logger = null;
private String Path = "";
private FileStream fs = null;
public LoggerManager()
{
Path = ".\\logfile.txt";
if (!File.Exists(Path)) // Check if the file is existing and create him if not.
{
fs = File.Create(Path);
}
else
{
fs = File.OpenWrite(Path);
}
// define type of log
public enum DataForcastType {
WEATHER,
POLL
}
// Instance of class
private static LoggerManager Logger = null;
/// <summary>
/// Create new instance of logger manager
/// </summary>
public LoggerManager() {}
/// <summary>
/// Get unique instance of logger manager
/// </summary>
/// <returns></returns>
public static LoggerManager GetInstance()
{
if (Logger == null)
......@@ -32,13 +33,49 @@ namespace BeeWeatherPollenTracker
return Logger;
}
/// <summary>
/// Write execption in log file
/// </summary>
/// <param name="type">type of log</param>
/// <param name="e"></param>
public void WriteInFile(Exception e)
{
// retreive current datetime
DateTime dt = DateTime.Now;
string res = dt.ToString() + " : " + e.Message;
string path = String.Format(".\\LOG_{0}.txt", dt.ToString("yyyy-MM-dd"));
// initialize file stream
FileStream fs = new FileStream(path, FileMode.Append);
// write content in file
string res = dt.ToString() + " : "+ e.Message ;
byte[] data = Encoding.UTF8.GetBytes(res);
fs.Write(data, 0, data.Length);
fs.Close();
}
/// <summary>
/// Write data in log file
/// </summary>
/// <param name="json">data to write</param>
public void WriteBackup(DataForcastType type, string json)
{
// define backup file
string path = String.Format(".\\BACKUP_{0}_{1}.json", type.ToString(), DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
// initialize file stream
FileStream fs = new FileStream(path, FileMode.Append);
// write content in file
byte[] d = Encoding.UTF8.GetBytes(json);
fs.Write(d, 0, d.Length);
fs.Close();
}
}
}
\ No newline at end of file
......@@ -4,7 +4,6 @@ using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.IO;
using System.Linq;
using WeatherNet;
using WeatherNet.Clients;
......@@ -14,58 +13,35 @@ namespace BeeWeatherPollenTracker
public class WeatherForecast
{
#region Constantes
private const string OPEN_WEATHER_MAP_API_KEY = "3f80e65ed0609e7e52737e65d522aed8";
private const double FORECAST_LENGTH = 3.0;
#endregion Constantes
#endregion
#region Attributes
// Weather forecast data
private List<beehive_weather_forecast> _weather_forecast_list;
private static LoggerManager logs = LoggerManager.GetInstance();
#endregion Attributes
private static LoggerManager logs = null;
#endregion
/// <summary>
/// Create new instance of weatherforcase
/// Create new instance of weatherforcase
/// </summary>
public WeatherForecast()
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);
// 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);
}
}
}
RetreiveData();
PushForecastToDatabase();
}
/// <summary>
......@@ -86,7 +62,7 @@ namespace BeeWeatherPollenTracker
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();
......@@ -100,8 +76,9 @@ namespace BeeWeatherPollenTracker
_weather_forecast_list.Add(w);
}
}
}
/// <summary>
/// Push weather forecast to database
/// </summary>
......@@ -110,25 +87,30 @@ namespace BeeWeatherPollenTracker
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);
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();
}
}
// Push data in databse
context.SaveChanges();
}
catch (Exception ex)
{
// write
logs.WriteInFile(ex);
// write data backup
logs.WriteBackup(LoggerManager.DataForcastType.WEATHER, JsonConvert.SerializeObject(_weather_forecast_list));
}
finally
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