Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Ilka Kistinger
bee-weather
Commits
b8821338
Commit
b8821338
authored
Mar 12, 2020
by
COLOMBO Benjamin
Browse files
Fix Disposable issues (DB context)
parent
4df6fdfb
Changes
1
Hide whitespace changes
Inline
Side-by-side
BeeWeatherPollenTracker/WeatherForecast.cs
View file @
b8821338
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
;
private
static
LoggerManager
logs
=
null
;
#
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
);
// Set up the logger manager
logs
=
LoggerManager
.
GetInstance
();
}
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
)
{
logs
.
WriteInFile
(
ex
);
}
finally
{
// Clear weather data
this
.
_weather_forecast_list
.
Clear
();
}
}
}
}
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
;
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
#
region
Attributes
// Weather forecast data
private
List
<
beehive_weather_forecast
>
_weather_forecast_list
;
private
static
LoggerManager
logs
=
LoggerManager
.
GetInstance
();
#
endregion
Attributes
/// <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
()
{
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
/// </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
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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment