private void InsertWeatherValuesIntoDatabase(int regionId, List <HourlyDatum> results, bool areForecastRows = false) { using (var _objectModel = new SmartEnergyOM(this.DatabaseConnectionString)) { foreach (var res in results) { var dateTime = res.dateTime; _objectModel.InsertOrUpdateWeatherDataPoints( regionId, dateTime, res.temperature, res.dewPoint, res.windSpeed, null, res.windBearing, res.apparentTemperature, res.visibility, null, null, null, res.pressure, res.humidity, res.summary, areForecastRows); } } }
/// <summary> /// InsertTenDatForecastValuesIntoDatabase /// </summary> /// <param name="regionId"></param> /// <param name="results"></param> private void InsertTenDatForecastValuesIntoDatabase(int regionId, List <HourlyForecast> results) { using (var _objectModel = new SmartEnergyOM(this.DatabaseConnectionString)) { foreach (var res in results) { var dateTime = res.observationDateTime; double tempcelcuis; double.TryParse(res.temp.metric, out tempcelcuis); double dewPoint; double.TryParse(res.dewpoint.metric, out dewPoint); double windSpeedKmPh; double.TryParse(res.wspd.metric, out windSpeedKmPh); double windDirectionDegrees; double.TryParse(res.wdir.degrees, out windDirectionDegrees); double windChill; double.TryParse(res.windchill.metric, out windChill); double uvIndex; double.TryParse(res.uvi, out uvIndex); double snow; double.TryParse(res.snow.metric, out snow); double pressure; double.TryParse(res.mslp.metric, out pressure); double humidityPercentage; double.TryParse(res.humidity, out humidityPercentage); double precipitation; double.TryParse(res.qpf.metric, out precipitation); var conditionDescription = res.condition ?? string.Empty; // Clean up "-9999" values that sometimes come back if (windSpeedKmPh < 0) { windSpeedKmPh = -1; } _objectModel.InsertOrUpdateWeatherDataPoints( regionId, dateTime, tempcelcuis, dewPoint, windSpeedKmPh, null, // No wind gusts in Wunderground forecasts windDirectionDegrees, windChill, null, // No visibilty in Wunderground forecasts uvIndex, precipitation, snow, pressure, humidityPercentage, conditionDescription, true); } } }
private void InsertHistoricWeatherValuesIntoDatabase(int regionId, List <Observation> results) { using (var _objectModel = new SmartEnergyOM(this.DatabaseConnectionString)) { foreach (var res in results) { var dateTime = res.observationDateTime; double tempcelcuis; double.TryParse(res.tempm, out tempcelcuis); double dewPoint; double.TryParse(res.dewptm, out dewPoint); double humidityPercentage; double.TryParse(res.hum, out humidityPercentage); double windSpeedKmPh; double.TryParse(res.wspdm, out windSpeedKmPh); double windGustKmPh; double.TryParse(res.wgustm, out windGustKmPh); double windDirectionDegrees; double.TryParse(res.wdird, out windDirectionDegrees); double windChill; double.TryParse(res.windchillm, out windChill); double visibilityMetric; double.TryParse(res.vism, out visibilityMetric); double snow; double.TryParse(res.snow, out snow); double pressure; double.TryParse(res.pressurem, out pressure); double precipitation; double.TryParse(res.precipm, out precipitation); var conditionDescription = res.conds ?? string.Empty; // Clean up "-9999" values that sometimes come back if (windSpeedKmPh < 0) { windSpeedKmPh = -1; } if (windGustKmPh < 0) { windGustKmPh = -1; } _objectModel.InsertOrUpdateWeatherDataPoints( regionId, dateTime, tempcelcuis, dewPoint, windSpeedKmPh, windGustKmPh, windDirectionDegrees, windChill, visibilityMetric, null, // No UNIndex in Wunderground historical data entries precipitation, snow, pressure, humidityPercentage, conditionDescription, false); } } }
/// <summary> /// Mine historic weather records from the Wunderground Weather service for the given regionSubUrl (e.g. CA/San_Francisco) and add it to the database /// </summary> /// <param name="startDateTime">startDateTime</param> /// <param name="endDateTime">endDateTime</param> /// <param name="regionSubUrl">Sub Url of the region on the Wunderground weather API e.g. CA/San_Francisco</param> /// <param name="regionId">regionId of this region in the application's database</param> public void MineHistoricWeatherValues( DateTime?startDateTime, DateTime?endDateTime, string regionSubUrl, int regionId) { var historicStartDateTime = startDateTime ?? DateTime.Now.AddDays(-2); var historicEndDateTime = endDateTime ?? DateTime.Now.AddMinutes(15); try { var results = this.wundergroundWeatherInteraction.GetHistoricWeatherData( this.wundergroundApiUrl, regionSubUrl, this.wundergroundApiKey, historicStartDateTime, historicEndDateTime); Logger.Information( $"Received {results.Count} HistoricWeatherValues Results for region with SubUrl {regionSubUrl} from Wunderground. Inserting them into the database", "WeatherDataMiner.MineHistoricWeatherValues()"); // Insert results in the database using (var _objectModel = new SmartEnergyOM(this.DatabaseConnectionString)) { foreach (var res in results) { var dateTime = res.observationDateTime; double tempcelcuis; double.TryParse(res.tempm, out tempcelcuis); double dewPoint; double.TryParse(res.dewptm, out dewPoint); double humidityPercentage; double.TryParse(res.hum, out humidityPercentage); double windSpeedKmPh; double.TryParse(res.wspdm, out windSpeedKmPh); double windGustKmPh; double.TryParse(res.wgustm, out windGustKmPh); double windDirectionDegrees; double.TryParse(res.wdird, out windDirectionDegrees); double windChill; double.TryParse(res.windchillm, out windChill); double visibilityMetric; double.TryParse(res.vism, out visibilityMetric); double snow; double.TryParse(res.snow, out snow); double pressure; double.TryParse(res.pressurem, out pressure); double precipitation; double.TryParse(res.precipm, out precipitation); var conditionDescription = res.conds; _objectModel.InsertOrUpdateWeatherDataPoints( regionId, dateTime, tempcelcuis, dewPoint, windSpeedKmPh, windGustKmPh, windDirectionDegrees, windChill, visibilityMetric, null, // No UNIndex in Wunderground historical data entries precipitation, snow, pressure, humidityPercentage, conditionDescription, false); } } } catch (Exception e) { Logger.Error( $"WeatherDataMiner: MineHistoricWeatherValues(): Exception encountered while retrieving historical Weather Data figures for {regionSubUrl} between {historicStartDateTime} and {historicEndDateTime}.", "WeatherDataMiner.MineHistoricWeatherValues()", null, e); } }
/// <summary> /// Retrieve forecast weather data from the Wunderground Weather service for the given regionSubUrl (e.g. CA/San_Francisco) and add it to the database /// </summary> /// <param name="regionSubUrl">Sub Url of the region on the Wunderground weather API e.g. CA/San_Francisco</param> /// <param name="regionId">regionId of this region in the application's database</param> public void MineTenDayHourlyForecastWeatherValues(string regionSubUrl, int regionId) { try { var results = this.wundergroundWeatherInteraction.GetTenDayHourlyForecastWeatherData( this.wundergroundApiUrl, regionSubUrl, this.wundergroundApiKey); Logger.Information( $"Received {results.Count} TenDayHourlyForecastWeatherValues Results for region with SubUrl {regionSubUrl} from Wunderground. Inserting them into the database", "WeatherDataMiner.MineTenDayHourlyForecastWeatherValues()"); // Insert results in the database using (var _objectModel = new SmartEnergyOM(this.DatabaseConnectionString)) { foreach (var res in results) { var dateTime = res.observationDateTime; double tempcelcuis; double.TryParse(res.temp.metric, out tempcelcuis); double dewPoint; double.TryParse(res.dewpoint.metric, out dewPoint); double windSpeedKmPh; double.TryParse(res.wspd.metric, out windSpeedKmPh); double windDirectionDegrees; double.TryParse(res.wdir.degrees, out windDirectionDegrees); double windChill; double.TryParse(res.windchill.metric, out windChill); double uvIndex; double.TryParse(res.uvi, out uvIndex); double snow; double.TryParse(res.snow.metric, out snow); double pressure; double.TryParse(res.mslp.metric, out pressure); double humidityPercentage; double.TryParse(res.humidity, out humidityPercentage); double precipitation; double.TryParse(res.qpf.metric, out precipitation); var conditionDescription = res.condition; _objectModel.InsertOrUpdateWeatherDataPoints( regionId, dateTime, tempcelcuis, dewPoint, windSpeedKmPh, null, // No wind gusts in Wunderground forecasts windDirectionDegrees, windChill, null, // No visibilty in Wunderground forecasts uvIndex, precipitation, snow, pressure, humidityPercentage, conditionDescription, true); } } } catch (Exception e) { Logger.Error( $"WeatherDataMiner: MineHistoricWeatherValues(): Exception encountered while retrieving historical Weather Data figures for {regionSubUrl}.", "WeatherDataMiner.MineTenDayHourlyForecastWeatherValues()", null, e); } }