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
c7ec7087
Commit
c7ec7087
authored
Mar 12, 2020
by
Charles Still
Browse files
Add push data in mysql db
parent
31ffb722
Changes
4
Show whitespace changes
Inline
Side-by-side
BeeWeatherPollenTracker/BeeWeatherPollenTracker.csproj
View file @
c7ec7087
...
...
@@ -186,6 +186,7 @@
<Compile
Include=
"ref_beehive_poll_forecast.cs"
>
<DependentUpon>
BeesWeatherModel.tt
</DependentUpon>
</Compile>
<Compile
Include=
"PollForecast.cs"
/>
<Compile
Include=
"WeatherForecast.cs"
/>
</ItemGroup>
<ItemGroup>
...
...
BeeWeatherPollenTracker/LoggerManager.cs
View file @
c7ec7087
...
...
@@ -38,7 +38,7 @@ namespace BeeWeatherPollenTracker
public
void
WriteInFile
(
Exception
e
)
{
DateTime
dt
=
DateTime
.
Now
;
string
res
=
dt
.
ToString
()
+
" : "
+
e
.
Message
;
string
res
=
dt
.
ToString
()
+
" : "
+
e
.
Message
+
" \n\t"
+
e
.
StackTrace
;
byte
[]
data
=
Encoding
.
UTF8
.
GetBytes
(
res
);
fs
.
Write
(
data
,
0
,
data
.
Length
);
...
...
BeeWeatherPollenTracker/PollForecast.cs
0 → 100644
View file @
c7ec7087
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
;
using
System.Net.Http
;
using
Newtonsoft.Json
;
namespace
BeeWeatherPollenTracker
{
public
class
PollForecast
{
#
region
Constantes
private
const
string
POLLEN_FORECAST_URL
=
"https://opendata.dwd.de/climate_environment/health/alerts/s31fg.json"
;
#
endregion
#
region
Attributes
// Weather forecast data
private
List
<
string
>
_poll_type
=
new
List
<
string
>();
private
List
<
beehive_poll_forecast
>
_poll_forecast_list
;
private
static
LoggerManager
logs
=
null
;
#
endregion
/// <summary>
/// Create new instance of weatherforcase
/// </summary>
public
PollForecast
()
{
// Initialize weather forecast list
this
.
_poll_forecast_list
=
new
List
<
beehive_poll_forecast
>();
_poll_type
=
new
List
<
String
>(
new
string
[]{
"Erle"
,
"Ambrosia"
,
"Beifuss"
,
"Birke"
,
"Esche"
,
"Graeser"
,
"Hasel"
,
"Roggen"
});
// Set up the logger manager
logs
=
LoggerManager
.
GetInstance
();
}
/// <summary>
/// Execute the retrieve and the push data in the mysql database
/// </summary>
public
void
Process
()
{
try
{
RetrievAndPushData
();
}
catch
(
Exception
ex
)
{
logs
.
WriteInFile
(
ex
);
}
finally
{
// Clear weather data
this
.
_poll_forecast_list
.
Clear
();
}
}
/// <summary>
/// Retreive Data from API
/// </summary>
private
void
RetrievAndPushData
()
{
// 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
// Get Pollen Data
ClimateEnvironment
pollenData
;
using
(
HttpClient
pollenClient
=
new
HttpClient
())
{
var
pollenResults
=
pollenClient
.
GetAsync
(
POLLEN_FORECAST_URL
).
Result
;
var
pollenJson
=
pollenResults
.
Content
.
ReadAsStringAsync
().
Result
;
pollenData
=
JsonConvert
.
DeserializeObject
<
ClimateEnvironment
>(
pollenJson
);
}
var
pollenRegionForecast
=
pollenData
.
Content
.
First
(
p
=>
p
.
PartregionId
.
ToString
()
==
ConfigurationManager
.
AppSettings
[
"PartregionId"
]);
var
context
=
new
delmeEntities1
();
// Retrieve poll forecast data
for
(
int
i
=
0
;
i
<
_poll_type
.
Count
;
i
++)
{
// 2020-03-11 11:00 Uhr
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
.
Sender
=
pollenData
.
Sender
;
poll_measure
.
Region_name
=
pollenRegionForecast
.
RegionName
;
poll_measure
.
Part_region_name
=
pollenRegionForecast
.
PartregionName
;
List
<
ref_beehive_poll_forecast
>
fk_poll
=
new
List
<
ref_beehive_poll_forecast
>(
context
.
ref_beehive_poll_forecast
.
ToArray
());
if
(
"Erle"
==
_poll_type
[
i
])
{
poll_measure
.
Pollen
=
"Erle"
;
poll_measure
.
Today
=
pollenRegionForecast
.
Pollen
.
Erle
.
Today
;
poll_measure
.
Tomorrow
=
pollenRegionForecast
.
Pollen
.
Erle
.
Tomorrow
;
poll_measure
.
Day_after_to
=
pollenRegionForecast
.
Pollen
.
Erle
.
DayafterTo
;
poll_measure
.
ref_beehive_poll_forecast
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Erle
.
DayafterTo
,
fk_poll
);
poll_measure
.
ref_beehive_poll_forecast1
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Erle
.
Today
,
fk_poll
);
poll_measure
.
ref_beehive_poll_forecast2
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Erle
.
Tomorrow
,
fk_poll
);
}
else
if
(
"Ambrosia"
==
_poll_type
[
i
])
{
poll_measure
.
Pollen
=
"Ambrosia"
;
poll_measure
.
Today
=
pollenRegionForecast
.
Pollen
.
Ambrosia
.
Today
;
poll_measure
.
Tomorrow
=
pollenRegionForecast
.
Pollen
.
Ambrosia
.
Tomorrow
;
poll_measure
.
Day_after_to
=
pollenRegionForecast
.
Pollen
.
Ambrosia
.
DayafterTo
;
poll_measure
.
ref_beehive_poll_forecast
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Ambrosia
.
DayafterTo
,
fk_poll
);
poll_measure
.
ref_beehive_poll_forecast1
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Ambrosia
.
Today
,
fk_poll
);
poll_measure
.
ref_beehive_poll_forecast2
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Ambrosia
.
Tomorrow
,
fk_poll
);
}
else
if
(
"Beifuss"
==
_poll_type
[
i
])
{
poll_measure
.
Pollen
=
"Beifuss"
;
poll_measure
.
Today
=
pollenRegionForecast
.
Pollen
.
Beifuss
.
Today
;
poll_measure
.
Tomorrow
=
pollenRegionForecast
.
Pollen
.
Beifuss
.
Tomorrow
;
poll_measure
.
Day_after_to
=
pollenRegionForecast
.
Pollen
.
Beifuss
.
DayafterTo
;
poll_measure
.
ref_beehive_poll_forecast
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Beifuss
.
DayafterTo
,
fk_poll
);
poll_measure
.
ref_beehive_poll_forecast1
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Beifuss
.
Today
,
fk_poll
);
poll_measure
.
ref_beehive_poll_forecast2
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Beifuss
.
Tomorrow
,
fk_poll
);
}
else
if
(
"Birke"
==
_poll_type
[
i
])
{
poll_measure
.
Pollen
=
"Birke"
;
poll_measure
.
Today
=
pollenRegionForecast
.
Pollen
.
Birke
.
Today
;
poll_measure
.
Tomorrow
=
pollenRegionForecast
.
Pollen
.
Birke
.
Tomorrow
;
poll_measure
.
Day_after_to
=
pollenRegionForecast
.
Pollen
.
Birke
.
DayafterTo
;
poll_measure
.
ref_beehive_poll_forecast
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Birke
.
DayafterTo
,
fk_poll
);
poll_measure
.
ref_beehive_poll_forecast1
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Birke
.
Today
,
fk_poll
);
poll_measure
.
ref_beehive_poll_forecast2
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Birke
.
Tomorrow
,
fk_poll
);
}
else
if
(
"Esche"
==
_poll_type
[
i
])
{
poll_measure
.
Pollen
=
"Esche"
;
poll_measure
.
Today
=
pollenRegionForecast
.
Pollen
.
Esche
.
Today
;
poll_measure
.
Tomorrow
=
pollenRegionForecast
.
Pollen
.
Esche
.
Tomorrow
;
poll_measure
.
Day_after_to
=
pollenRegionForecast
.
Pollen
.
Esche
.
DayafterTo
;
poll_measure
.
ref_beehive_poll_forecast
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Esche
.
DayafterTo
,
fk_poll
);
poll_measure
.
ref_beehive_poll_forecast1
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Esche
.
Today
,
fk_poll
);
poll_measure
.
ref_beehive_poll_forecast2
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Esche
.
Tomorrow
,
fk_poll
);
}
else
if
(
"Graeser"
==
_poll_type
[
i
])
{
poll_measure
.
Pollen
=
"Graeser"
;
poll_measure
.
Today
=
pollenRegionForecast
.
Pollen
.
Graeser
.
Today
;
poll_measure
.
Tomorrow
=
pollenRegionForecast
.
Pollen
.
Graeser
.
Tomorrow
;
poll_measure
.
Day_after_to
=
pollenRegionForecast
.
Pollen
.
Graeser
.
DayafterTo
;
poll_measure
.
ref_beehive_poll_forecast
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Graeser
.
DayafterTo
,
fk_poll
);
poll_measure
.
ref_beehive_poll_forecast1
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Graeser
.
Today
,
fk_poll
);
poll_measure
.
ref_beehive_poll_forecast2
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Graeser
.
Tomorrow
,
fk_poll
);
}
else
if
(
"Hasel"
==
_poll_type
[
i
])
{
poll_measure
.
Pollen
=
"Hasel"
;
poll_measure
.
Today
=
pollenRegionForecast
.
Pollen
.
Hasel
.
Today
;
poll_measure
.
Tomorrow
=
pollenRegionForecast
.
Pollen
.
Hasel
.
Tomorrow
;
poll_measure
.
Day_after_to
=
pollenRegionForecast
.
Pollen
.
Hasel
.
DayafterTo
;
poll_measure
.
ref_beehive_poll_forecast
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Hasel
.
DayafterTo
,
fk_poll
);
poll_measure
.
ref_beehive_poll_forecast1
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Hasel
.
Today
,
fk_poll
);
poll_measure
.
ref_beehive_poll_forecast2
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Hasel
.
Tomorrow
,
fk_poll
);
}
else
if
(
"Roggen"
==
_poll_type
[
i
])
{
poll_measure
.
Pollen
=
"Roggen"
;
poll_measure
.
Today
=
pollenRegionForecast
.
Pollen
.
Roggen
.
Today
;
poll_measure
.
Tomorrow
=
pollenRegionForecast
.
Pollen
.
Roggen
.
Tomorrow
;
poll_measure
.
Day_after_to
=
pollenRegionForecast
.
Pollen
.
Roggen
.
DayafterTo
;
poll_measure
.
ref_beehive_poll_forecast
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Roggen
.
DayafterTo
,
fk_poll
);
poll_measure
.
ref_beehive_poll_forecast1
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Roggen
.
Today
,
fk_poll
);
poll_measure
.
ref_beehive_poll_forecast2
=
GetRefPoll
(
pollenRegionForecast
.
Pollen
.
Roggen
.
Tomorrow
,
fk_poll
);
}
context
.
beehive_poll_forecast
.
Add
(
poll_measure
);
context
.
SaveChanges
();
}
}
/// <summary>
/// return the correct foreign key
/// </summary>
/// <param name="originatingKey"> Key for finding the right foreign key</param>
/// <param name="fk_poll"></param>
/// <returns></returns>
private
static
ref_beehive_poll_forecast
GetRefPoll
(
string
originatingKey
,
List
<
ref_beehive_poll_forecast
>
fk_poll
)
{
foreach
(
var
item
in
fk_poll
)
{
if
(
item
.
Id_pollen_measure
==
originatingKey
)
{
return
item
;
}
}
return
null
;
}
}
}
BeeWeatherPollenTracker/Program.cs
View file @
c7ec7087
using
MySql.Data.EntityFramework
;
using
Newtonsoft.Json
;
using
System
;
using
System.Collections.Generic
;
using
System.Configuration
;
using
System.Data.Entity
;
using
System.Linq
;
using
System.Net.Http
;
using
System.Threading
;
using
WeatherNet
;
using
WeatherNet.Clients
;
...
...
@@ -15,20 +13,17 @@ namespace BeeWeatherPollenTracker
{
internal
static
class
Program
{
#
region
Constantes
private
const
string
POLLEN_FORECAST_URL
=
"https://opendata.dwd.de/climate_environment/health/alerts/s31fg.json"
;
#
endregion
private
static
LoggerManager
logs
=
null
;
private
static
void
Main
(
string
[]
args
)
{
// Set up the logger manager
logs
=
LoggerManager
.
GetInstance
();
try
{
// Entity Framework setup
DbConfiguration
.
SetConfiguration
(
new
MySqlEFConfiguration
());
#
region
Weather
WeatherForecast
weather
=
new
WeatherForecast
();
...
...
@@ -36,48 +31,19 @@ namespace BeeWeatherPollenTracker
t
.
Start
();
#
endregion
#
region
Pool
// TODO: Create new service to retreive poll info (like weather)
// TODO: Launch service with thread
PollForecast
poll
=
new
PollForecast
();
// Set up the logger manager
logs
=
LoggerManager
.
GetInstance
();
// Retrieve poll forecast data
try
{
// Get Pollen Data
ClimateEnvironment
pollenData
;
using
(
HttpClient
pollenClient
=
new
HttpClient
())
{
var
pollenResults
=
pollenClient
.
GetAsync
(
POLLEN_FORECAST_URL
).
Result
;
var
pollenJson
=
pollenResults
.
Content
.
ReadAsStringAsync
().
Result
;
pollenData
=
JsonConvert
.
DeserializeObject
<
ClimateEnvironment
>(
pollenJson
);
}
Thread
tPoll
=
new
Thread
(
new
ThreadStart
(
poll
.
Process
));
tPoll
.
Start
();
#
endregion
var
pollenRegionForecast
=
pollenData
.
Content
.
First
(
p
=>
p
.
PartregionId
.
ToString
()
==
ConfigurationManager
.
AppSettings
[
"PartregionId"
]);
// 2020-03-11 11:00 Uhr
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
.
Sender
=
pollenData
.
Sender
;
poll_measure
.
Region_name
=
pollenRegionForecast
.
PartregionName
;
//pollenRegionForecast.Pollen.Erle
poll_measure
.
Pollen
=
"Erle"
;
poll_measure
.
Today
=
pollenRegionForecast
.
Pollen
.
Erle
.
Today
;
poll_measure
.
Tomorrow
=
pollenRegionForecast
.
Pollen
.
Erle
.
Tomorrow
;
poll_measure
.
Day_after_to
=
pollenRegionForecast
.
Pollen
.
Erle
.
DayafterTo
;
poll_measure
.
}
catch
(
Exception
e
)
{
logs
.
WriteInFile
(
e
);
}
#
endregion
}
}
}
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