예제 #1
0
        public async Task <WeatherInfo> GetWeatherInfoAsync(GeoCoords coords)
        {
            string  url  = CreateWeatherQuery(coords);
            JObject data = null;

            using (var client = new HttpClient())
            {
                var response = await client.GetAsync(url);

                var content = await response.Content.ReadAsStringAsync();

                data = JObject.Parse(content);
            }

            JObject main = (JObject)data.GetValue("main");

            double?clouds      = ((JObject)data.GetValue("clouds")).GetValue("all").Value <double?>();
            double?humidity    = main.GetValue("humidity").Value <double?>();
            double?pressure    = main.GetValue("pressure").Value <double?>();
            double?temperature = main.GetValue("temp").Value <double?>();

            return(new WeatherInfo
            {
                Coordinates = coords,
                Clouds = clouds,
                Humidity = humidity,
                Perssure = pressure,
                Temperature = temperature,
            });
        }
예제 #2
0
        public double CalculateZenith(GeoCoords geoCoords, DateTime dateTime, double timeZone)
        {
            SPACalculator.SPAData spa = new SPACalculator.SPAData
            {
                Year      = dateTime.Year,
                Month     = dateTime.Month,
                Day       = dateTime.Day,
                Hour      = dateTime.Hour,
                Minute    = dateTime.Minute,
                Second    = dateTime.Second,
                Timezone  = timeZone,
                Latitude  = geoCoords.Latitude,
                Longitude = geoCoords.Longitude,
                Function  = SPACalculator.CalculationMode.SPA_ZA
            };

            if (SPACalculator.SPACalculate(ref spa) == 0)
            {
                return(spa.Zenith);
            }
            else
            {
                throw new ArithmeticException();
            }
        }
예제 #3
0
        public double CalculateElevation(GeoCoords geoCoords, DateTime dateTime, double timeZone)
        {
            double azimuth      = CalculateAizmuth(geoCoords, dateTime, timeZone);
            double zenithCosine = Math.Cos(CalculateZenith(geoCoords, dateTime, timeZone) * Math.PI / 180.0);
            double elevation    = Math.Asin(zenithCosine) / Math.PI * 180.0;

            return(azimuth > 180.0 ? 180.0f - elevation : elevation);
        }
예제 #4
0
 public async Task <SystemInfo> GetSystemInfoAsync(
     double systemCapacity,
     PanelModuleType moduleType,
     PanelArrayType arrayType,
     double systemLosses,
     double?tilt,
     double azmuth,
     GeoCoords coords)
 {
     try
     {
         return(await GetSystemInfoAsync("intl", systemCapacity, moduleType, arrayType, systemLosses, tilt, azmuth, coords));
     }
     catch (Exception e)
     {
         return(await GetSystemInfoAsync("tmy3", systemCapacity, moduleType, arrayType, systemLosses, tilt, azmuth, coords));
     }
 }
예제 #5
0
        public async Task <SystemInfo> GetSystemInfoCachedAsync(
            long cacheID,
            double systemCapacity,
            PanelModuleType moduleType,
            PanelArrayType arrayType,
            double systemLosses,
            double?tilt,
            double azmuth,
            GeoCoords coords)
        {
            if (sysInfoCache.TryGetValue(cacheID, out SystemInfo cached))
            {
                return(cached);
            }
            else
            {
                SystemInfo systemInfo = await GetSystemInfoAsync(systemCapacity, moduleType, arrayType, systemLosses, tilt, azmuth, coords);

                sysInfoCache.TryAdd(cacheID, systemInfo);
                return(systemInfo);
            }
        }
예제 #6
0
 private string CreateWeatherQuery(GeoCoords coords)
 {
     return(CreateAuthenticatedQuery($"http://api.openweathermap.org/data/2.5/weather?lat={coords.Latitude}&lon={coords.Longitude}"));
 }
예제 #7
0
        private async Task <SystemInfo> GetSystemInfoAsync(
            string dataset,
            double systemCapacity,
            PanelModuleType moduleType,
            PanelArrayType arrayType,
            double systemLosses,
            double?tilt,
            double azmuth,
            GeoCoords coords)
        {
            using (var client = new HttpClient())
            {
                string query = CreateQueryParams(new Params
                {
                    { "api_key", apiKey },
                    { "timeframe", "hourly" },
                    { "dataset", dataset },
                    { "system_capacity", systemCapacity.ToString() },
                    { "module_type", ((int)moduleType).ToString() },
                    { "losses", systemLosses.ToString() },
                    { "array_type", ((int)arrayType).ToString() },
                    { "tilt", tilt.ToString() },
                    { "azimuth", azmuth.ToString() },
                    { "lat", coords.Latitude.ToString() },
                    { "lon", coords.Longitude.ToString() },
                    { "radius", "0" }
                });

                string requestUrl = $"{BaseUrl}?{query}";
                string json       = null;

                try
                {
                    json = await client.GetStringAsync(requestUrl);
                }
                catch (Exception e)
                {
                    throw e;
                }

                JObject data   = JObject.Parse(json);
                JArray  errors = data["errors"] as JArray;

                if (errors != null && errors.Count != 0)
                {
                    throw new AggregateException(errors.Select(error => new Exception(error.ToString())));
                }
                else
                {
                    JObject output = (JObject)data["outputs"];

                    List <double> dcArrayOutputMonthly  = ((JArray)output["dc_monthly"]).Values <double>().ToList();
                    double        dcArrayOutputAnnually = dcArrayOutputMonthly.Sum();

                    return(new SystemInfo
                    {
                        HourlyDCArrayOutput = ((JArray)output["dc"]).Values <double>().Select(x => x / 1000).ToList(),
                        MonthlyDCArrayOutput = dcArrayOutputMonthly.ToList(),
                        AnnualDCArrayOutput = dcArrayOutputAnnually
                    });
                }
            }
        }