/// <summary> /// Mine Or Calculate Relative Merit Data for emissions values between the given dates and store them in the database. /// The Relative Merit is a value between 0 (meaning best) and 1 (being worst). /// </summary> /// <param name="startDateTime">Optional StartDateTime. If not supplied, a default value will be used</param> /// <param name="endDateTime">Optional endDateTime. If not supplied, a default value will be used</param> /// <param name="regionWattTimeName">Abbreviation for the required region (e.g. "PJM"). See https://api.watttime.org/faq/#where </param> /// <param name="regionId">regionId of this region in the application's database</param> public void MineOrCalculateHistoricRelativeMeritData( double latitude, double longitude, DateTime?startDateTime, DateTime?endDateTime, string regionWattTimeName, int regionId) { var historicStartDateTime = startDateTime ?? DateTime.UtcNow.AddDays(-2); var historicEndDateTime = endDateTime ?? DateTime.UtcNow.AddMinutes(15); try { Logger.Information( $"Entering method for {regionWattTimeName} from WattTime URL {this.wattTimeApiUrl} with method of relative data retrieval / calculation of {this.RelativeMeritDataSource}.", "CarbonEmissionsMiner.MineOrCalculateHistoricRelativeMeritData()"); switch (this.RelativeMeritDataSource) { case "WattTime": throw new NotImplementedException("WattTime does not currently offer historic relative merit data querying"); break; case "CustomInternalCalculation": var calculatedHistoricRelativeMeritResults = this.CalculateHistoricRelativeMeritDataResults( regionId, historicStartDateTime, historicEndDateTime); // Insert results in the database using (var _objectModel = new SmartEnergyOM(this.DatabaseConnectionString)) { foreach (var res in calculatedHistoricRelativeMeritResults) { _objectModel.InsertOrUpdateCarbonEmissionsRelativeMeritDataPoints( regionId, res.Timestamp, res.EmissionsRelativeMerit, res.EmissionsRelativeMerit_Forcast); } } break; default: Logger.Information( $"No known defined method of MineOrCalculateHistoricRelativeMeritData supplied to method. Not mining or calculating Historic Relative Merit Data for this region ({regionWattTimeName})", "CarbonEmissionsMiner.MineOrCalculateHistoricRelativeMeritData()"); return; } } catch (Exception e) { Logger.Error( $"CarbonEmissionsMiner: MineForecastMarginalCarbonResults(): Exception encountered while emissions Data figures for {regionWattTimeName} between {historicStartDateTime} and {historicEndDateTime}.", "CarbonEmissionsMiner.MineForecastMarginalCarbonResults()", null, e); } }
/// <summary> /// Mine Or Calculate Relative Merit Data for emissions values between the given dates and store them in the database. /// The Relative Merit is a value between 0 (meaning best) and 1 (being worst). /// </summary> /// <param name="startDateTime">Optional StartDateTime. If not supplied, a default value will be used</param> /// <param name="endDateTime">Optional endDateTime. If not supplied, a default value will be used</param> /// <param name="regionWattTimeName">Abbreviation for the required region (e.g. "PJM"). See https://api.watttime.org/faq/#where </param> /// <param name="regionId">regionId of this region in the application's database</param> public void MineOrCalculateCarbonEmissionsRelativeMerit( string regionWattTimeName, int regionId) { var historicStartDateTime = DateTime.UtcNow.AddMinutes(-1); var historicEndDateTime = DateTime.UtcNow.AddMinutes(15); try { Logger.Information( $"Entering method for {regionWattTimeName} from WattTime URL {this.wattTimeApiUrl} with method of relative data retrieval / calculation of {this.RelativeMeritDataSource}.", "CarbonEmissionsMiner.MineOrCalculateHistoricRelativeMeritData()"); switch (this.RelativeMeritDataSource) { case "WattTime": var result = this.wattTimeEmissionsInteraction.GetCarbonEmissionsRelativeMeritResults( WattTimeV2ApiUrl, regionWattTimeName, WattTimeUsername, WattTimePassword ); if (result != null) { Logger.Information( $"Received result for RelativeMeritData for {regionWattTimeName} from WattTime. Inserting into the database", "CarbonEmissionsMiner.MineOrCalculateRelativeMeritData()"); // Insert results in the database using (var _objectModel = new SmartEnergyOM(this.DatabaseConnectionString)) { var dateTime = result.validUntil; double emissionsRelativeMerit = result.percent / 100; // Normalise percentage to a value between 0 and 1 _objectModel.InsertOrUpdateCarbonEmissionsRelativeMeritDataPoints( regionId, dateTime, emissionsRelativeMerit, null); } } else { Logger.Information( $"No result found when requesting RelativeMeritData for {regionWattTimeName} from WattTime at UTC: {DateTime.UtcNow}.e", "CarbonEmissionsMiner.MineOrCalculateRelativeMeritData()"); } break; case "CustomInternalCalculation": var calculatedHistoricRelativeMeritResults = this.CalculateHistoricRelativeMeritDataResults( regionId, historicStartDateTime, historicEndDateTime); // Insert results in the database using (var _objectModel = new SmartEnergyOM(this.DatabaseConnectionString)) { foreach (var res in calculatedHistoricRelativeMeritResults) { _objectModel.InsertOrUpdateCarbonEmissionsRelativeMeritDataPoints( regionId, res.Timestamp, res.EmissionsRelativeMerit, res.EmissionsRelativeMerit_Forcast); } } break; default: Logger.Information( $"No known defined method of MineOrCalculateRelativeMeritData supplied to method. Not mining or calculating Historic Relative Merit Data for this region ({regionWattTimeName})", "CarbonEmissionsMiner.MineOrCalculateRelativeMeritData()"); return; } } catch (Exception e) { Logger.Error( $"CarbonEmissionsMiner: MineOrCalculateRelativeMeritData(): Exception encountered while emissions Data figures for {regionWattTimeName} between {historicStartDateTime} and {historicEndDateTime}.", "CarbonEmissionsMiner.MineOrCalculateRelativeMeritData()", null, e); } }