-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
216 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace DKSRDomain; | ||
|
||
public class WeatherResponse | ||
{ | ||
public List<WeatherSensorData> SensorData { get; set; } | ||
} | ||
|
||
// { | ||
// "rain": 0, | ||
// "weather_id": 800, | ||
// "temp": 299.37, | ||
// "sunrise": "2024-07-19T03:24:11.000Z", | ||
// "visibility": 10000, | ||
// "city": "Weißdorf", | ||
// "countrycode": "DE", | ||
// "lon": 11.85, | ||
// "clouds": 0, | ||
// "pressure": 1018, | ||
// "weather_icon": "01d", | ||
// "feels_like": 299.37, | ||
// "temp_max": 299.37, | ||
// "SID": "fbc81ae8-87a2-49e7-948e-aeef30e66f71", | ||
// "wind_deg": 190, | ||
// "temp_min": 299.26, | ||
// "grnd_level": 949, | ||
// "sunset": "2024-07-19T19:13:15.000Z", | ||
// "humidity": 53, | ||
// "wind_speed": 2.06, | ||
// "sea_level": 1018, | ||
// "lat": 50.1833, | ||
// "timestamp": "2024-07-19T15:43:50.730Z", | ||
// "_headers": { | ||
// "eventType": "OpenWeatherMapHofV1EventType" | ||
// } | ||
// } | ||
|
||
public class WeatherSensorData | ||
{ | ||
[JsonProperty("rain")] public int Rain { get; set; } | ||
|
||
[JsonProperty("weather_id")] public int WeatherId { get; set; } | ||
|
||
[JsonProperty("temp")] public double Temp { get; set; } | ||
|
||
[JsonProperty("sunrise")] public DateTime Sunrise { get; set; } | ||
|
||
[JsonProperty("visibility")] public int Visibility { get; set; } | ||
|
||
[JsonProperty("city")] public string City { get; set; } | ||
|
||
[JsonProperty("countrycode")] public string CountryCode { get; set; } | ||
|
||
[JsonProperty("lon")] public double Lon { get; set; } | ||
|
||
[JsonProperty("clouds")] public int Clouds { get; set; } | ||
|
||
[JsonProperty("pressure")] public int Pressure { get; set; } | ||
|
||
[JsonProperty("weather_icon")] public string WeatherIcon { get; set; } | ||
|
||
[JsonProperty("feels_like")] public double FeelsLike { get; set; } | ||
|
||
[JsonProperty("temp_max")] public double TempMax { get; set; } | ||
|
||
[JsonProperty("SID")] public string SID { get; set; } | ||
|
||
[JsonProperty("wind_deg")] public int WindDeg { get; set; } | ||
|
||
[JsonProperty("temp_min")] public double TempMin { get; set; } | ||
|
||
[JsonProperty("grnd_level")] public int GrndLevel { get; set; } | ||
|
||
[JsonProperty("sunset")] public DateTime Sunset { get; set; } | ||
|
||
[JsonProperty("humidity")] public int Humidity { get; set; } | ||
|
||
[JsonProperty("wind_speed")] public double WindSpeed { get; set; } | ||
|
||
[JsonProperty("sea_level")] public int SeaLevel { get; set; } | ||
|
||
[JsonProperty("lat")] public double Lat { get; set; } | ||
|
||
[JsonProperty("timestamp")] public DateTime Timestamp { get; set; } | ||
|
||
[JsonProperty("_headers")] public Dictionary<string, string> Headers { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,10 +24,21 @@ public abstract class Importer | |
|
||
protected Importer(ILogger logger, string dataType, string dataStreamName) | ||
{ | ||
_frostApi = new FrostApi.FrostApi(Environment.GetEnvironmentVariable("FROST_SERVER_BASE_URL") ?? | ||
throw new Exception("FROST_SERVER_BASE_URL not set")); | ||
Username = Environment.GetEnvironmentVariable("DKSR_HISTORIC_USERNAME") ?? throw new Exception("DKSR_HISTORIC_USERNAME not set"); | ||
Password = Environment.GetEnvironmentVariable("DKSR_HISTORIC_PASSWORD") ?? throw new Exception("DKSR_HISTORIC_PASSWORD not set"); | ||
// _frostApi = new FrostApi.FrostApi(Environment.GetEnvironmentVariable("FROST_SERVER_BASE_URL") ?? | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Jonohas
Author
Collaborator
|
||
// throw new Exception("FROST_SERVER_BASE_URL not set")); | ||
// | ||
// Username = Environment.GetEnvironmentVariable("DKSR_HISTORIC_USERNAME") ?? throw new Exception("DKSR_HISTORIC_USERNAME not set"); | ||
// Password = Environment.GetEnvironmentVariable("DKSR_HISTORIC_PASSWORD") ?? throw new Exception("DKSR_HISTORIC_PASSWORD not set"); | ||
// Client = SetupHttpClient(); | ||
// Logger = logger; | ||
// DataType = dataType; | ||
// _dataStreamName = dataStreamName; | ||
|
||
_frostApi = new FrostApi.FrostApi(Environment.GetEnvironmentVariable("FROST_SERVER_BASE_URL_WEATHER") ?? | ||
throw new Exception("FROST_SERVER_BASE_URL_WEATHER not set")); | ||
|
||
Username = Environment.GetEnvironmentVariable("DKSR_HISTORIC_USERNAME_WEATHER") ?? throw new Exception("DKSR_HISTORIC_USERNAME_WEATHER not set"); | ||
Password = Environment.GetEnvironmentVariable("DKSR_HISTORIC_PASSWORD_WEATHER") ?? throw new Exception("DKSR_HISTORIC_PASSWORD_WEATHER not set"); | ||
Client = SetupHttpClient(); | ||
Logger = logger; | ||
DataType = dataType; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using DKSRDomain; | ||
using FrostApi.Models.Thing; | ||
using Importer.Constants; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Importer.Importers; | ||
|
||
public class WeatherImporter : Importer | ||
{ | ||
private Timer _importerTimer; | ||
|
||
public WeatherImporter(ILogger logger) : base(logger, "Weather", "weather") | ||
{ | ||
_importerTimer = new Timer(Import, null, 0, 60 * 1000 * 60); // every hour | ||
} | ||
|
||
protected override async void Import(object? _) | ||
{ | ||
try | ||
{ | ||
Logger.LogInformation($"{DateTime.Now} - Updating {DataType} Data..."); | ||
var data = await GetDksrData(); | ||
foreach (var weather in data.SensorData) | ||
try | ||
{ | ||
Thing thing; | ||
var frostThing = await GetFrostThingData(weather.SID); | ||
if (frostThing.Value.Count == 0) | ||
{ | ||
thing = Mappers.MapDksrResponse(weather, DataType); | ||
await CreateNewThing(thing); | ||
frostThing = await GetFrostThingData(weather.SID); | ||
} | ||
|
||
if (frostThing.Value.Count < 1) | ||
throw new Exception($"Creating new thing with id {weather.SID} seems to have failed..."); | ||
|
||
thing = Mappers.MapDksrResponse(weather, DataType); | ||
thing.Id = frostThing.Value.First().Id; | ||
await Update(thing); | ||
} | ||
catch (Exception e) | ||
{ | ||
Logger.LogError($"{DateTime.Now} - {e}"); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Logger.LogError($"{DateTime.Now} - {e}"); | ||
} | ||
} | ||
|
||
|
||
private async Task<WeatherResponse> GetDksrData() | ||
{ | ||
try | ||
{ | ||
var response = | ||
await Client.GetAsync( | ||
Endpoints.GetAuthenticatedDKSREndpointUrl(Username, Password, Endpoints.WeatherEndpoint)); | ||
var result = await response.Content.ReadAsAsync<WeatherResponse>(); | ||
return result; | ||
} | ||
catch (Exception e) | ||
{ | ||
Logger.LogError("Getting data from DKSR failed, returning empty response"); | ||
throw new Exception($"{DateTime.Now} - {e}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mag weg zeker?