Exemplo n.º 1
0
        public void TestRegisterWithWattTime()
        {
            // Arrange
            EmissionsApiInteraction emissionsApiInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

            // Act
            var tokenReturned = emissionsApiInteraction.RegisterWithWattTime(this.wattTimeApiV2Url, WattTimeUsername, WattTimePassword, WattTimeEmail, WattTimeOrganization);

            // Assert
            Assert.IsNotNull(tokenReturned);
        }
Exemplo n.º 2
0
        public void TestGetMarginalCarbonResults()
        {
            // Arrange
            var regionAbbreviation = "PJM";
            EmissionsApiInteraction emissionsApiInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

            // Act
            var pointsReturned = emissionsApiInteraction.GetMarginalCarbonResults(this.wattTimeApiUrl, regionAbbreviation, WattTimeUsername, WattTimePassword, DateTime.Now.AddDays(-2), DateTime.Now.AddDays(2), null, wattTimeApiKey);

            // Assert
            Assert.IsTrue(pointsReturned.Count > 0);
        }
        public void TestGetMostRecentMarginalCarbonEmissionsResult()
        {
            // Arrange
            var regionAbbreviation = "PJM";
            EmissionsApiInteraction emissionsApiInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

            // Act
            var pointsReturned = emissionsApiInteraction.GetMostRecentMarginalCarbonEmissionsResult(this.wattTimeApiUrl, regionAbbreviation, null, wattTimeApiKey);

            // Assert
            Assert.IsTrue(pointsReturned.marginal_carbon.value > -1);
        }
        public void TestGetGenerationMixAndSystemWideEmissionsResults()
        {
            // Arrange
            var regionAbbreviation = "PJM";
            EmissionsApiInteraction emissionsApiInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

            // Act
            var pointsReturned = emissionsApiInteraction.GetGenerationMixAndSystemWideEmissionsResults(this.wattTimeApiUrl, regionAbbreviation, DateTime.Now.AddDays(-25), DateTime.Now.AddDays(20), null, wattTimeApiKey);

            // Assert
            Assert.IsTrue(pointsReturned.Count > 0);
        }
Exemplo n.º 5
0
        public void TestGetGenerationMixAndSystemWideEmissionsResults()
        {
            // Arrange
            var regionAbbreviation = "PJM";
            EmissionsApiInteraction emissionsApiInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

            emissionsApiInteraction.RegisterWithWattTime(this.wattTimeApiV2Url, WattTimeUsername, WattTimePassword, WattTimeEmail, WattTimeOrganization);

            // Act
            var pointsReturned = emissionsApiInteraction.GetGenerationMixAndSystemWideEmissionsResults(this.wattTimeApiUrl, regionAbbreviation, DateTime.Now.AddDays(-15), DateTime.Now.AddDays(2), null, wattTimeApiKey);

            // Assert
            Assert.IsTrue(pointsReturned.Count > 0);
        }
Exemplo n.º 6
0
        public void TestGetMostRecentMarginalCarbonEmissionsResult()
        {
            // Arrange
            var regionAbbreviation = "PJM";
            EmissionsApiInteraction emissionsApiInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

            emissionsApiInteraction.RegisterWithWattTime(this.wattTimeApiV2Url, WattTimeUsername, WattTimePassword, WattTimeEmail, WattTimeOrganization);

            // Act
            var pointsReturned = emissionsApiInteraction.GetMostRecentMarginalCarbonEmissionsResult(this.wattTimeApiV2Url, regionAbbreviation, WattTimeUsername, WattTimePassword);

            // Assert
            Assert.IsTrue(pointsReturned.value > -1);
        }
Exemplo n.º 7
0
        public void TestCarbonEmissionsRelativeMeritResults()
        {
            // Arrange
            var regionAbbreviation = "PJM";
            EmissionsApiInteraction emissionsApiInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

            emissionsApiInteraction.RegisterWithWattTime(this.wattTimeApiV2Url, WattTimeUsername, WattTimePassword, WattTimeEmail, WattTimeOrganization);

            // Act
            var indexReturned = emissionsApiInteraction.GetCarbonEmissionsRelativeMeritResults(this.wattTimeApiV2Url, regionAbbreviation, WattTimeUsername, WattTimePassword);

            // Assert
            Assert.IsTrue(-1 < indexReturned.rating);
            Assert.IsTrue(indexReturned.rating < 6);
        }
        /// <summary>
        /// Create an instance of a WeatherDataMiner for the given wattTimeApiUrl and wattTimeApiKey
        /// </summary>
        /// <param name="wattTimeApiUrl">URL of the Watt Time API</param>
        /// <param name="wattTimeApiKey">>WattTime Api Key</param>
        /// <param name="wattTimeV2ApiUrl">URL of the Watt Time V2 API</param>
        /// <param name="wattTimeUsername">Username for the WattTime service</param>
        /// <param name="wattTimePassword">assword for the WattTime service</param>
        /// <param name="wattTimeEmail">Email for the WattTime service</param>
        /// <param name="wattTimeOrganization">Organization for the WattTime service</param>
        /// <param name="selfThrottlingMethod">The method to use to limit calls to the API to below the given threshold. Options: {None, InMemoryCallRecollection, AzureTableStorageCallRecollection}</param>
        /// <param name="databaseConnectionString">Entity Framrwork Connection string for the Applications' Database</param>
        /// <param name="maxNumberOfCallsPerMinute">Maximum number of calls to make to the API per minute</param>
        /// <param name="wattTimeEmissionsInteraction">Optional EmissionsApiInteraction object. If not supplied, it will be created</param>
        /// <param name="relativeMeritDataSource">Optional RelativeMeritDataSource string. If not supplied, no relative data will be miner or calculated</param>
        public CarbonEmissionsMiner(
            string wattTimeApiUrl,
            string wattTimeApiKey,
            string wattTimeV2ApiUrl,
            string wattTimeUsername,
            string wattTimePassword,
            string wattTimeEmail,
            string wattTimeOrganization,
            string selfThrottlingMethod,
            string databaseConnectionString,
            int maxNumberOfCallsPerMinute = -1,
            EmissionsApiInteraction wattTimeEmissionsInteraction = null,
            string relativeMeritDataSource = "WattTime")
        {
            if (wattTimeEmissionsInteraction == null)
            {
                this.wattTimeEmissionsInteraction = new EmissionsApiInteraction(
                    selfThrottlingMethod,
                    maxNumberOfCallsPerMinute);
            }
            else
            {
                this.wattTimeEmissionsInteraction = wattTimeEmissionsInteraction;
            }
            this.wattTimeApiUrl           = wattTimeApiUrl;
            this.wattTimeApiKey           = wattTimeApiKey;
            this.WattTimeV2ApiUrl         = wattTimeV2ApiUrl;
            this.WattTimeUsername         = wattTimeUsername;
            this.WattTimePassword         = wattTimePassword;
            this.WattTimeEmail            = wattTimeEmail;
            this.WattTimeOrganization     = wattTimeOrganization;
            this.DatabaseConnectionString = databaseConnectionString;
            this.RelativeMeritDataSource  = relativeMeritDataSource;

            // Perform any service specific actions such as registeration
            switch (this.RelativeMeritDataSource)
            {
            case "WattTime":
                // Register the given user details with the WattTime API in case they haven't already been registered.
                this.wattTimeEmissionsInteraction.RegisterWithWattTime(this.WattTimeV2ApiUrl, this.WattTimeUsername, this.WattTimePassword, this.WattTimeEmail, this.WattTimeOrganization, true);
                break;
            }
        }
 /// <summary>
 /// Create an instance of a WeatherDataMiner for the given wattTimeApiUrl and wattTimeApiKey
 /// </summary>
 /// <param name="wattTimeApiUrl">URL of the Watt Time API</param>
 /// <param name="wattTimeApiKey">>WattTime Api Key</param>
 /// <param name="selfThrottlingMethod">The method to use to limit calls to the API to below the given threshold. Options: {None, InMemoryCallRecollection, AzureTableStorageCallRecollection}</param>
 /// <param name="databaseConnectionString">Entity Framrwork Connection string for the Applications' Database</param>
 /// <param name="maxNumberOfCallsPerMinute">Maximum number of calls to make to the API per minute</param>
 /// <param name="wattTimeEmissionsInteraction">Optional EmissionsApiInteraction object. If not supplied, it will be created</param>
 public CarbonEmissionsMiner(
     string wattTimeApiUrl,
     string wattTimeApiKey,
     string selfThrottlingMethod,
     string databaseConnectionString,
     int maxNumberOfCallsPerMinute = -1,
     EmissionsApiInteraction wattTimeEmissionsInteraction = null)
 {
     if (wattTimeEmissionsInteraction == null)
     {
         this.wattTimeEmissionsInteraction = new EmissionsApiInteraction(
             selfThrottlingMethod,
             maxNumberOfCallsPerMinute);
     }
     else
     {
         this.wattTimeEmissionsInteraction = wattTimeEmissionsInteraction;
     }
     this.wattTimeApiUrl           = wattTimeApiUrl;
     this.wattTimeApiKey           = wattTimeApiKey;
     this.DatabaseConnectionString = databaseConnectionString;
 }
        public void TestMineHistoricMarginalCarbonResults()
        {
            // Arrange
            var startDateTime = DateTime.Now.AddDays(-10);
            var endDateTime   = DateTime.Now.AddDays(-9);

            var wattTimeApiUrl            = CloudConfigurationManager.GetSetting("WattTimeApiUrl");
            var wattTimeApiKey            = CloudConfigurationManager.GetSetting("WattTimeApiKey");
            var selfThrottlingMethod      = "AzureTableStorageCallRecollection";
            var maxNumberOfCallsPerMinute = 9;

            List <WattTimeBalancingAuthorityInformation> regionsToMine =
                new List <WattTimeBalancingAuthorityInformation>
            {
                new WattTimeBalancingAuthorityInformation(
                    "PJM",
                    "US_PJM",
                    "Eastern Standard Time",
                    40.348444276169,
                    -74.6428556442261)
            };

            foreach (var region in regionsToMine)
            {
                int regionId;
                using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                {
                    regionId =
                        _objectModel.AddEmissionsRegion(region.smartGridRegionName, region.timeZone, region.regionLat, region.regionLong)
                        .EmissionsRegionID;

                    _objectModel.AddMarketWeatherEmissionsRegionMapping(
                        region.smartGridRegionName,
                        null,
                        null,
                        regionId);
                }


                var wattTimeInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

                CarbonEmissionsMiner carbonEmissionsMiner = new CarbonEmissionsMiner(
                    wattTimeApiUrl,
                    wattTimeApiKey,
                    selfThrottlingMethod,
                    databaseConnectionString,
                    maxNumberOfCallsPerMinute,
                    wattTimeInteraction);

                // Act
                carbonEmissionsMiner.MineHistoricMarginalCarbonResults(
                    startDateTime,
                    endDateTime,
                    region.regionWattTimeName,
                    regionId);

                // Assert
                // Verify that each data point has been recorded in the database
                var results = wattTimeInteraction.GetObservedMarginalCarbonResults(
                    wattTimeApiUrl,
                    region.regionWattTimeName,
                    startDateTime,
                    endDateTime,
                    null,
                    wattTimeApiKey);

                using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                {
                    foreach (var result in results)
                    {
                        var dataPoint = _objectModel.FindCarbonEmissionsDataPoints(regionId, result.timestamp);
                        Assert.IsNotNull(dataPoint);
                    }
                }
            }
        }
        public void TestParseMinerSettingsFileAndMineData()
        {
            // Arrange
            var ConfigPath = @".\ApiDataMinerConfigs\ApiDataMinerConfigs.xml";

            // Act
            var apiDataMiner = new ApiDataMiner(databaseConnectionString);

            apiDataMiner.ParseMinerSettingsFileAndMineData(ConfigPath);

            // Assert
            using (var streamReader = new StreamReader(ConfigPath))
            {
                var xmlSerializer = new XmlSerializer(typeof(ApiMinerConfigLayout));
                var minerConfigs  = (ApiMinerConfigLayout)xmlSerializer.Deserialize(streamReader);

                foreach (var regionConfiguration in minerConfigs.Regions)
                {
                    // Verify emissions were mined successfully for each region in the Config File
                    if (regionConfiguration.EmissionsMiningRegion != null)
                    {
                        var emissionsRegionName       = regionConfiguration.EmissionsMiningRegion.friendlyName;
                        var timeZone                  = regionConfiguration.EmissionsMiningRegion.TimeZone;
                        var regionLat                 = regionConfiguration.EmissionsMiningRegion.Latitude;
                        var regionLong                = regionConfiguration.EmissionsMiningRegion.Longitude;
                        var regionWattTimeName        = regionConfiguration.EmissionsMiningRegion.EmissionsWattTimeAbbreviation;
                        var wattTimeApiUrl            = regionConfiguration.EmissionsMiningRegion.ApiUrl;
                        var wattTimeApiKey            = regionConfiguration.EmissionsMiningRegion.ApiKey;
                        var selfThrottlingMethod      = regionConfiguration.WeatherMiningRegion.SelfThrottlingMethod;
                        var maxNumberOfCallsPerMinute =
                            regionConfiguration.WeatherMiningRegion.MaxNumberOfCallsPerMinute;
                        var startDateTime = DateTime.UtcNow.AddDays(-2);
                        var endDateTime   = DateTime.UtcNow.AddDays(10);

                        var wattTimeInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);
                        var results             = wattTimeInteraction.GetObservedMarginalCarbonResults(
                            wattTimeApiUrl,
                            regionWattTimeName,
                            startDateTime,
                            endDateTime,
                            null,
                            wattTimeApiKey);

                        int regionId;
                        using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                        {
                            regionId =
                                _objectModel.AddEmissionsRegion(emissionsRegionName, timeZone, regionLat, regionLong)
                                .EmissionsRegionID;
                        }

                        using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                        {
                            foreach (var result in results)
                            {
                                var dataPoint = _objectModel.FindCarbonEmissionsDataPoints(regionId, result.timestamp);
                                Assert.IsNotNull(dataPoint);
                            }
                        }
                    }

                    // Verify weather was mined successfully for each region in the Config File
                    if (regionConfiguration.WeatherMiningRegion != null)
                    {
                        var emissionsRegionName             = regionConfiguration.WeatherMiningRegion.friendlyName;
                        var timeZone                        = regionConfiguration.WeatherMiningRegion.TimeZone;
                        var regionLat                       = regionConfiguration.WeatherMiningRegion.Latitude;
                        var regionLong                      = regionConfiguration.WeatherMiningRegion.Longitude;
                        var weatherRegionWundergroundSubUrl =
                            regionConfiguration.WeatherMiningRegion.weatherRegionWundergroundSubUrl;
                        var wundergroundApiUrl        = regionConfiguration.WeatherMiningRegion.ApiUrl;
                        var wundergroundApiKey        = regionConfiguration.WeatherMiningRegion.ApiKey;
                        var selfThrottlingMethod      = regionConfiguration.WeatherMiningRegion.SelfThrottlingMethod;
                        var maxNumberOfCallsPerMinute =
                            regionConfiguration.WeatherMiningRegion.MaxNumberOfCallsPerMinute;

                        var wundergroundWeatherInteraction = new WundergroundWeatherInteraction(
                            selfThrottlingMethod,
                            maxNumberOfCallsPerMinute);
                        var results =
                            wundergroundWeatherInteraction.GetTenDayHourlyForecastWeatherData(
                                wundergroundApiUrl,
                                weatherRegionWundergroundSubUrl,
                                wundergroundApiKey);

                        int regionId;
                        using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                        {
                            regionId =
                                _objectModel.AddWeatherRegion(emissionsRegionName, timeZone, regionLat, regionLong)
                                .WeatherRegionID;
                        }

                        using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                        {
                            foreach (var result in results)
                            {
                                var dataPoint = _objectModel.FindWeatherDataPoints(
                                    regionId,
                                    result.observationDateTime);
                                Assert.IsNotNull(dataPoint);
                            }
                        }
                    }
                }
            }
        }
        public void TestMineForecastMarginalCarbonResults()
        {
            // Arrange
            var regionWattTimeName  = "PJM";
            var smartGridRegionName = "PJM";
            var timeZone            = "Eastern Standard Time";
            var regionLat           = 40.348444276169;
            var regionLong          = -74.6428556442261;

            var startDateTime = DateTime.UtcNow.AddDays(-2);
            var endDateTime   = DateTime.UtcNow.AddDays(10);

            var wattTimeApiUrl            = CloudConfigurationManager.GetSetting("WattTimeApiUrl");
            var wattTimeApiKey            = CloudConfigurationManager.GetSetting("WattTimeApiKey");
            var selfThrottlingMethod      = "AzureTableStorageCallRecollection";
            var maxNumberOfCallsPerMinute = 9;

            int regionId;

            using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
            {
                regionId =
                    _objectModel.AddEmissionsRegion(smartGridRegionName, timeZone, regionLat, regionLong)
                    .EmissionsRegionID;
            }


            var wattTimeInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

            CarbonEmissionsMiner carbonEmissionsMiner = new CarbonEmissionsMiner(
                wattTimeApiUrl,
                wattTimeApiKey,
                selfThrottlingMethod,
                databaseConnectionString,
                maxNumberOfCallsPerMinute,
                wattTimeInteraction);

            // Act
            carbonEmissionsMiner.MineForecastMarginalCarbonResults(
                startDateTime,
                endDateTime,
                regionWattTimeName,
                regionId);

            // Assert
            // Verify that each data point has been recorded in the database
            var results = wattTimeInteraction.GetForecastMarginalCarbonResults(
                wattTimeApiUrl,
                regionWattTimeName,
                startDateTime,
                endDateTime,
                null,
                wattTimeApiKey);

            using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
            {
                foreach (var result in results)
                {
                    if (result.marginal_carbon.value != null)
                    {
                        var dataPoint = _objectModel.FindCarbonEmissionsDataPoints(regionId, result.timestamp);
                        Assert.IsNotNull(dataPoint);
                        Assert.AreEqual(
                            wattTimeInteraction.ConvertLbsPerMWhTo_GCo2PerkWh((double)result.marginal_carbon.value),
                            dataPoint.MarginalCO2Intensity_gCO2kWh);
                    }
                }
            }
        }
        public void TestMineOrCalculateCarbonEmissionsRelativeMerit_CustomInternalCalculation()
        {
            // Arrange
            var regionWattTimeName  = "PJM";
            var smartGridRegionName = "US_PJM";

            var startDateTime = DateTime.UtcNow.AddDays(-10);
            var endDateTime   = DateTime.UtcNow.AddDays(-9);

            var    wattTimeApiUrl            = CloudConfigurationManager.GetSetting("WattTimeApiUrl");
            var    wattTimeApiKey            = CloudConfigurationManager.GetSetting("WattTimeApiKey");
            string wattTimeApiV2Url          = CloudConfigurationManager.GetSetting("WattTimeApiV2Url");
            string WattTimeUsername          = CloudConfigurationManager.GetSetting("WattTimeUsername");
            string WattTimePassword          = CloudConfigurationManager.GetSetting("WattTimePassword");
            string WattTimeEmail             = CloudConfigurationManager.GetSetting("WattTimeEmail");
            string WattTimeOrganization      = CloudConfigurationManager.GetSetting("WattTimeOrganization");
            var    selfThrottlingMethod      = "AzureTableStorageCallRecollection";
            var    maxNumberOfCallsPerMinute = 200;

            var wattTimeInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

            CarbonEmissionsMiner carbonEmissionsMiner = new CarbonEmissionsMiner(
                wattTimeApiUrl,
                wattTimeApiKey,
                wattTimeApiV2Url,
                WattTimeUsername,
                WattTimePassword,
                WattTimeEmail,
                WattTimeOrganization,
                selfThrottlingMethod,
                databaseConnectionString,
                maxNumberOfCallsPerMinute,
                wattTimeInteraction,
                "CustomInternalCalculation");

            int regionId;

            using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
            {
                regionId = _objectModel.FindEmissionsRegion(smartGridRegionName).EmissionsRegionID;
            }
            Assert.IsNotNull(regionId, "Couldn't find specified emissions region in the database");

            // Act
            carbonEmissionsMiner.MineOrCalculateCarbonEmissionsRelativeMerit(
                regionWattTimeName,
                regionId);

            // Assert
            // Verify that each data point has been recorded in the database
            var results = carbonEmissionsMiner.CalculateHistoricRelativeMeritDataResults(
                regionId,
                startDateTime,
                endDateTime);

            foreach (var result in results)
            {
                using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                {
                    var dataPoint = _objectModel.FindCarbonEmissionsRelativeMeritDataPoints(regionId, result.Timestamp.AddMinutes(-5), result.Timestamp.AddMinutes(15));
                    Assert.IsNotNull(dataPoint);
                }
            }
        }
        public void TestCalculateHistoricRelativeMeritDataResults()
        {
            // Arrange
            var smartGridRegionName = "US_PJM";

            var startDateTime = DateTime.UtcNow.AddDays(-10);
            var endDateTime   = DateTime.UtcNow.AddDays(-9);

            var    wattTimeApiUrl            = CloudConfigurationManager.GetSetting("WattTimeApiUrl");
            var    wattTimeApiKey            = CloudConfigurationManager.GetSetting("WattTimeApiKey");
            string wattTimeApiV2Url          = CloudConfigurationManager.GetSetting("WattTimeApiV2Url");
            string WattTimeUsername          = CloudConfigurationManager.GetSetting("WattTimeUsername");
            string WattTimePassword          = CloudConfigurationManager.GetSetting("WattTimePassword");
            string WattTimeEmail             = CloudConfigurationManager.GetSetting("WattTimeEmail");
            string WattTimeOrganization      = CloudConfigurationManager.GetSetting("WattTimeOrganization");
            var    selfThrottlingMethod      = "AzureTableStorageCallRecollection";
            var    maxNumberOfCallsPerMinute = 200;

            var wattTimeInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

            var carbonEmissionsMiner = new CarbonEmissionsMiner(
                wattTimeApiUrl,
                wattTimeApiKey,
                wattTimeApiV2Url,
                WattTimeUsername,
                WattTimePassword,
                WattTimeEmail,
                WattTimeOrganization,
                selfThrottlingMethod,
                databaseConnectionString,
                maxNumberOfCallsPerMinute,
                wattTimeInteraction);

            int regionId;

            using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
            {
                regionId = _objectModel.FindEmissionsRegion(smartGridRegionName).EmissionsRegionID;
            }
            Assert.IsNotNull(regionId, "Couldn't find specified emissions region in the database");

            // Act
            var results = carbonEmissionsMiner.CalculateHistoricRelativeMeritDataResults(
                regionId,
                startDateTime,
                endDateTime
                );

            // Assert
            foreach (var item in results)
            {
                if (item.EmissionsRelativeMerit != null)
                {
                    Assert.IsTrue(item.EmissionsRelativeMerit >= 0, "item.EmissionsRelativeMerit >= 0");
                    Assert.IsTrue(item.EmissionsRelativeMerit <= 1, "item.EmissionsRelativeMerit <= 1");
                }
                if (item.EmissionsRelativeMerit_Forcast != null)
                {
                    Assert.IsTrue(item.EmissionsRelativeMerit_Forcast >= 0, "item.EmissionsRelativeMerit_Forcast >= 0");
                    Assert.IsTrue(item.EmissionsRelativeMerit_Forcast <= 1, "item.EmissionsRelativeMerit_Forcast <= 1");
                }
            }
        }
        public void TestMineHistoricMarginalCarbonResults()
        {
            // Arrange
            var startDateTime = DateTime.Now.AddDays(-10);
            var endDateTime   = DateTime.Now.AddDays(-1);

            var    wattTimeApiUrl            = CloudConfigurationManager.GetSetting("WattTimeApiUrl");
            var    wattTimeApiKey            = CloudConfigurationManager.GetSetting("WattTimeApiKey");
            string wattTimeApiV2Url          = CloudConfigurationManager.GetSetting("WattTimeApiV2Url");
            string WattTimeUsername          = CloudConfigurationManager.GetSetting("WattTimeUsername");
            string WattTimePassword          = CloudConfigurationManager.GetSetting("WattTimePassword");
            string WattTimeEmail             = CloudConfigurationManager.GetSetting("WattTimeEmail");
            string WattTimeOrganization      = CloudConfigurationManager.GetSetting("WattTimeOrganization");
            var    selfThrottlingMethod      = "AzureTableStorageCallRecollection";
            var    maxNumberOfCallsPerMinute = 9;

            List <WattTimeBalancingAuthorityInformation> regionsToMine = new List <WattTimeBalancingAuthorityInformation>();

            regionsToMine.Add(new WattTimeBalancingAuthorityInformation("PSEI", "US_PugetSoundEnergy", "Pacific Standard Time", 47.68009593341535, -122.11638450372567));

            regionsToMine.Add(new WattTimeBalancingAuthorityInformation("BPA", "US_BPA", "Pacific Standard Time", 40.348444276169, -74.6428556442261));
            regionsToMine.Add(new WattTimeBalancingAuthorityInformation("CAISO", "US_CAISO", "Pacific Standard Time", 41.7324, -123.409423));
            regionsToMine.Add(new WattTimeBalancingAuthorityInformation("ERCOT", "US_ERCOT", "Central Standard Time", 32.79878236662912, -96.77856445062508));
            regionsToMine.Add(new WattTimeBalancingAuthorityInformation("ISONE", "US_ISONewEngland", "Eastern Standard Time", 42.70864591994315, -72.16918945062508));
            regionsToMine.Add(new WattTimeBalancingAuthorityInformation("MISO", "US_UpperMidwestISO", "Central Standard Time", 41.91853269857261, -93.55193137872567));
            regionsToMine.Add(new WattTimeBalancingAuthorityInformation("SPP", "US_SouthwesternPublicServiceISO", "Eastern Standard Time", 34.41133502036136, -103.19243430841317));

            regionsToMine.Add(new WattTimeBalancingAuthorityInformation("PJM", "US_PJM", "Eastern Standard Time", 40.348444276169, -74.6428556442261));
            regionsToMine.Add(new WattTimeBalancingAuthorityInformation("PJM_ATLANTIC", "US_PJM_ATLANTIC", "Eastern Standard Time", 40.566564, -76.98465597395705));
            regionsToMine.Add(new WattTimeBalancingAuthorityInformation("PJM_SOUTH", "US_PJM_SOUTH", "Eastern Standard Time", 37.44276433719146, -76.87479269270705));
            regionsToMine.Add(new WattTimeBalancingAuthorityInformation("PJM_WEST", "US_PJM_WEST", "Eastern Standard Time", 38.69484915602888, -85.11453878645705));

            foreach (var region in regionsToMine)
            {
                int regionId;
                using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                {
                    regionId =
                        _objectModel.AddEmissionsRegion(region.smartGridRegionName, region.timeZone, region.regionLat, region.regionLong)
                        .EmissionsRegionID;

                    _objectModel.AddMarketWeatherEmissionsRegionMapping(
                        region.smartGridRegionName,
                        null,
                        null,
                        regionId);
                }


                var wattTimeInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

                CarbonEmissionsMiner carbonEmissionsMiner = new CarbonEmissionsMiner(
                    wattTimeApiUrl,
                    wattTimeApiKey,
                    wattTimeApiV2Url,
                    WattTimeUsername,
                    WattTimePassword,
                    WattTimeEmail,
                    WattTimeOrganization,
                    selfThrottlingMethod,
                    databaseConnectionString,
                    maxNumberOfCallsPerMinute,
                    wattTimeInteraction);

                // Act
                carbonEmissionsMiner.MineHistoricMarginalCarbonResults(
                    startDateTime,
                    endDateTime,
                    region.regionWattTimeName,
                    regionId);

                // Assert
                // Verify that each data point has been recorded in the database
                var results = wattTimeInteraction.GetObservedMarginalCarbonResults(
                    wattTimeApiV2Url,
                    region.regionWattTimeName,
                    WattTimeUsername,
                    WattTimePassword,
                    startDateTime,
                    endDateTime,
                    null,
                    null);

                using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                {
                    foreach (var result in results)
                    {
                        var dataPoint = _objectModel.FindCarbonEmissionsDataPoint(regionId, result.point_time);
                        Assert.IsNotNull(dataPoint);
                    }
                }
            }
        }
        public void TestMineHistoricSystemWideCarbonResults()
        {
            // Arrange
            var startDateTime = DateTime.Now.AddDays(-10);
            var endDateTime   = DateTime.Now.AddDays(-1);

            var    wattTimeApiUrl            = CloudConfigurationManager.GetSetting("WattTimeApiUrl");
            var    wattTimeApiKey            = CloudConfigurationManager.GetSetting("WattTimeApiKey");
            string wattTimeApiV2Url          = CloudConfigurationManager.GetSetting("WattTimeApiV2Url");
            string WattTimeUsername          = CloudConfigurationManager.GetSetting("WattTimeUsername");
            string WattTimePassword          = CloudConfigurationManager.GetSetting("WattTimePassword");
            string WattTimeEmail             = CloudConfigurationManager.GetSetting("WattTimeEmail");
            string WattTimeOrganization      = CloudConfigurationManager.GetSetting("WattTimeOrganization");
            var    selfThrottlingMethod      = "AzureTableStorageCallRecollection";
            var    maxNumberOfCallsPerMinute = 9;

            List <WattTimeBalancingAuthorityInformation> regionsToMine = new List <WattTimeBalancingAuthorityInformation>();

            regionsToMine.Add(new WattTimeBalancingAuthorityInformation("ERCOT", "US_ERCOT", "Central Standard Time", 32.79878236662912, -96.77856445062508));

            foreach (var region in regionsToMine)
            {
                int regionId;
                using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                {
                    regionId =
                        _objectModel.AddEmissionsRegion(region.smartGridRegionName, region.timeZone, region.regionLat, region.regionLong)
                        .EmissionsRegionID;

                    _objectModel.AddMarketWeatherEmissionsRegionMapping(
                        region.smartGridRegionName,
                        null,
                        null,
                        regionId);
                }


                var wattTimeInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

                CarbonEmissionsMiner carbonEmissionsMiner = new CarbonEmissionsMiner(
                    wattTimeApiUrl,
                    wattTimeApiKey,
                    wattTimeApiV2Url,
                    WattTimeUsername,
                    WattTimePassword,
                    WattTimeEmail,
                    WattTimeOrganization,
                    selfThrottlingMethod,
                    databaseConnectionString,
                    maxNumberOfCallsPerMinute,
                    wattTimeInteraction);

                // Act
                carbonEmissionsMiner.MineHistoricSystemWideCarbonResults(
                    startDateTime,
                    endDateTime,
                    region.regionWattTimeName,
                    regionId);

                // Assert
                // Verify that each data point has been recorded in the database
                var results = wattTimeInteraction.GetGenerationMixAndSystemWideEmissionsResults(
                    wattTimeApiUrl,
                    region.regionWattTimeName,
                    startDateTime,
                    endDateTime,
                    null,
                    wattTimeApiKey);

                using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                {
                    foreach (var result in results)
                    {
                        var dataPoint = _objectModel.FindCarbonEmissionsDataPoint(regionId, result.timestamp);
                        Assert.IsNotNull(dataPoint);
                    }
                }
            }
        }