public void TestMineHistoricWeatherValues_ByWundergroundLocationName()
        {
            // Arrange
            var regionSubUrl        = "ie/dublin";
            var latitude            = 53.3498;
            var longtitude          = -6.2603;
            var smartGridRegionName = "Ireland";
            var timeZone            = "GMT Standard Time";

            var startDateTime = DateTime.Now.AddDays(-10);
            var endDateTime   = DateTime.Now.AddDays(-1);

            var wundergroundApiUrl        = CloudConfigurationManager.GetSetting("WundergroundApiUrl");
            var wundergroundApiKey        = CloudConfigurationManager.GetSetting("WundergroundApiKey");
            var selfThrottlingMethod      = "AzureTableStorageCallRecollection";
            var maxNumberOfCallsPerMinute = 5;

            int regionId;

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


            var wundergroundWeatherInteraction =
                new WundergroundWeatherInteraction(
                    selfThrottlingMethod,
                    maxNumberOfCallsPerMinute);

            WundergroundWeatherDataMiner weatherDataMiner = new WundergroundWeatherDataMiner(
                wundergroundApiUrl,
                wundergroundApiKey,
                selfThrottlingMethod,
                databaseConnectionString,
                maxNumberOfCallsPerMinute,
                wundergroundWeatherInteraction);

            // Act
            weatherDataMiner.MineHistoricWeatherValues(startDateTime, endDateTime, regionSubUrl, regionId);

            // Assert
            // Verify that each data point has been recorded in the database
            var results = wundergroundWeatherInteraction.GetHistoricWeatherData(
                wundergroundApiUrl,
                regionSubUrl,
                wundergroundApiKey,
                startDateTime,
                endDateTime);

            using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
            {
                foreach (var result in results)
                {
                    var dataPoint = _objectModel.FindWeatherDataPoint(regionId, result.observationDateTime);
                    Assert.IsNotNull(dataPoint);
                }
            }
        }
        public void TestMineHistoricWeatherValues()
        {
            // Arrange
            var    regionSubUrl        = "us/nj/princeton";
            string smartGridRegionName = "PJM";
            var    timeZone            = "Eastern Standard Time";
            var    regionLat           = 40.348444276169;
            var    regionLong          = -74.6428556442261;

            var startDateTime = DateTime.Now.AddDays(-3);
            var endDateTime   = DateTime.Now.AddDays(-1);

            var wundergroundApiUrl        = CloudConfigurationManager.GetSetting("WundergroundApiUrl");
            var wundergroundApiKey        = CloudConfigurationManager.GetSetting("WundergroundApiKey");
            var selfThrottlingMethod      = "AzureTableStorageCallRecollection";
            var maxNumberOfCallsPerMinute = 5;

            int regionId;

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


            var wundergroundWeatherInteraction =
                new WundergroundWeatherInteraction(
                    selfThrottlingMethod,
                    maxNumberOfCallsPerMinute);

            WeatherDataMiner weatherDataMiner = new WeatherDataMiner(
                wundergroundApiUrl,
                wundergroundApiKey,
                selfThrottlingMethod,
                databaseConnectionString,
                maxNumberOfCallsPerMinute,
                wundergroundWeatherInteraction);

            // Act
            weatherDataMiner.MineHistoricWeatherValues(startDateTime, endDateTime, regionSubUrl, regionId);

            // Assert
            // Verify that each data point has been recorded in the database
            var results = wundergroundWeatherInteraction.GetHistoricWeatherData(
                wundergroundApiUrl,
                regionSubUrl,
                wundergroundApiKey,
                startDateTime,
                endDateTime);

            using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
            {
                foreach (var result in results)
                {
                    var dataPoint = _objectModel.FindWeatherDataPoint(regionId, result.observationDateTime);
                    Assert.IsNotNull(dataPoint);
                }
            }
        }
        public void TestMineForecastcWeatherValues_ByGPS()
        {
            // Arrange
            var startDateTime = DateTime.UtcNow;
            var endDateTime   = DateTime.UtcNow.AddDays(10);

            var latitude            = 53.3498;
            var longtitude          = -6.2603;
            var smartGridRegionName = "Ireland";
            var timeZone            = "GMT Standard Time";

            var darkSkyApiUrl             = CloudConfigurationManager.GetSetting("DarkSkyApiUrl");
            var darkSkyApiKey             = CloudConfigurationManager.GetSetting("DarkSkyApiKey");
            var selfThrottlingMethod      = "AzureTableStorageCallRecollection";
            var maxNumberOfCallsPerMinute = 5;
            var maxNumberOfCallsPerDay    = 500;

            int regionId;

            using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
            {
                regionId =
                    _objectModel.AddWeatherRegion(smartGridRegionName, timeZone, latitude, longtitude, null).WeatherRegionID;
            }

            var darkSkyWeatherInteraction =
                new DarkSkyWeatherInteraction(
                    selfThrottlingMethod,
                    maxNumberOfCallsPerMinute,
                    maxNumberOfCallsPerDay);

            DarkSkyWeatherDataMiner weatherDataMiner = new DarkSkyWeatherDataMiner(
                darkSkyApiUrl,
                darkSkyApiKey,
                selfThrottlingMethod,
                databaseConnectionString,
                maxNumberOfCallsPerMinute,
                maxNumberOfCallsPerDay,
                darkSkyWeatherInteraction);

            // Act
            weatherDataMiner.MineForecastWeatherValues(startDateTime, endDateTime, latitude, longtitude, regionId);

            // Assert
            //Verify that each data point has been recorded in the database
            var results = darkSkyWeatherInteraction.GetForecastWeatherData(
                darkSkyApiUrl,
                darkSkyApiKey,
                latitude,
                longtitude,
                startDateTime,
                endDateTime);

            using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
            {
                foreach (var result in results)
                {
                    var dataPoint = _objectModel.FindWeatherDataPoint(regionId, result.dateTime);
                    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 wattTimeApiV2Url     = regionConfiguration.EmissionsMiningRegion.WattTimeApiV2Url;
                        var wattTimeUsername     = regionConfiguration.EmissionsMiningRegion.WattTimeUsername;
                        var wattTimePassword     = regionConfiguration.EmissionsMiningRegion.WattTimePassword;
                        var wattTimeEmail        = regionConfiguration.EmissionsMiningRegion.WattTimeEmail;
                        var wattTimeOrganization = regionConfiguration.EmissionsMiningRegion.WattTimeOrganization;

                        var selfThrottlingMethod      = regionConfiguration.WundergroundWeatherMiningRegion.SelfThrottlingMethod;
                        var maxNumberOfCallsPerMinute =
                            regionConfiguration.WundergroundWeatherMiningRegion.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,
                            wattTimeUsername,
                            wattTimePassword,
                            startDateTime,
                            endDateTime,
                            null);

                        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.FindCarbonEmissionsDataPoint(regionId, result.point_time);
                                Assert.IsNotNull(dataPoint);
                            }
                        }
                    }

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

                        var wundergroundWeatherInteraction = new WundergroundWeatherInteraction(
                            selfThrottlingMethod,
                            maxNumberOfCallsPerMinute);
                        List <HourlyForecast> results = new List <HourlyForecast>();

                        switch (regionConfiguration.WundergroundWeatherMiningRegion.MiningMethod)
                        {
                        case "GPS":
                            results =
                                wundergroundWeatherInteraction.GetTenDayHourlyForecastWeatherData(
                                    wundergroundApiUrl,
                                    regionConfiguration.WundergroundWeatherMiningRegion.Latitude,
                                    regionConfiguration.WundergroundWeatherMiningRegion.Longitude,
                                    wundergroundApiKey);
                            break;

                        case "WundergroundPageSubUrl":
                        default:
                            results =
                                wundergroundWeatherInteraction.GetTenDayHourlyForecastWeatherData(
                                    wundergroundApiUrl,
                                    weatherRegionWundergroundSubUrl,
                                    wundergroundApiKey);
                            break;
                        }

                        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.FindWeatherDataPoint(
                                    regionId,
                                    result.observationDateTime);
                                Assert.IsNotNull(dataPoint);
                            }
                        }
                    }

                    // Verify weather was mined successfully for each region in the Config File
                    if (regionConfiguration.DarkSkyWeatherMiningRegion != null)
                    {
                        var regionName                = regionConfiguration.DarkSkyWeatherMiningRegion.friendlyName;
                        var timeZone                  = regionConfiguration.DarkSkyWeatherMiningRegion.TimeZone;
                        var regionLat                 = regionConfiguration.DarkSkyWeatherMiningRegion.Latitude;
                        var regionLong                = regionConfiguration.DarkSkyWeatherMiningRegion.Longitude;
                        var apiUrl                    = regionConfiguration.DarkSkyWeatherMiningRegion.ApiUrl;
                        var apiKey                    = regionConfiguration.DarkSkyWeatherMiningRegion.ApiKey;
                        var selfThrottlingMethod      = regionConfiguration.DarkSkyWeatherMiningRegion.SelfThrottlingMethod;
                        var maxNumberOfCallsPerMinute =
                            regionConfiguration.DarkSkyWeatherMiningRegion.MaxNumberOfCallsPerMinute;
                        var maxNumberOfCallsPerDay =
                            regionConfiguration.DarkSkyWeatherMiningRegion.MaxNumberOfCallsPerDay;

                        var darkSkyWeatherInteraction = new DarkSkyWeatherInteraction(
                            selfThrottlingMethod,
                            maxNumberOfCallsPerMinute,
                            maxNumberOfCallsPerDay);
                        List <HourlyDatum> results = new List <HourlyDatum>();

                        var forecastStartDateTime = DateTime.UtcNow;
                        var forecastEndDateTime   = DateTime.UtcNow.AddDays(10);

                        results = darkSkyWeatherInteraction.GetForecastWeatherData(
                            apiUrl,
                            apiKey,
                            regionLat,
                            regionLong,
                            forecastStartDateTime,
                            forecastEndDateTime);

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

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