}                          //Prevent instantiation of helper.

        public static AstronomyHelper Instance()
        {
            if (_helper is null && _repository is null)
            {
                _helper     = new AstronomyHelper();                         //Create new helper.
                _repository = new AstroRepository(new AstronomyDbContext()); //Inject repository.
            }
            return(_helper);
        }
Beispiel #2
0
        private string GetConditionIconResource(WeatherCodes?codes, string weatherUnrecognizedCode, bool useTwilight,
                                                DateTime?dateTime, IAstronomy astronomy)
        {
            if (!codes.HasValue || codes == WeatherCodes.Undefined)
            {
                Log.DebugFormat("Undefined condition icon code '{0}'.", codes);
                return(null);
            }

            if (codes == WeatherCodes.Error)
            {
                Log.ErrorFormat("Unrecognized condition icon code '{0}'.", weatherUnrecognizedCode);
                return(null);
            }

            var needTwilight = false;

            var group = "_ovc";

            if (codes == WeatherCodes.ClearSky)
            {
                group        = "_skc";
                needTwilight = true;
            }
            else if (codes.Value.HasFlag(WeatherCodes.ClearSky))
            {
                group        = "_bkn";
                needTwilight = true;
            }
            else if (codes.Value.HasFlag(WeatherCodes.Fog))
            {
                group        = "_fg";
                needTwilight = true;
            }

            var precipitation = string.Empty;

            if (codes.Value.HasFlag(WeatherCodes.Heavy) || codes.Value.HasFlag(WeatherCodes.Storm))
            {
                precipitation += "_max";
            }
            else if (codes.Value.HasFlag(WeatherCodes.Light))
            {
                precipitation += "_min";
            }

            if (codes.Value.HasFlag(WeatherCodes.Thunderstorm))
            {
                precipitation += "_ts";
            }
            if (codes.Value.HasFlag(WeatherCodes.Rain))
            {
                precipitation += "_ra";
            }
            if (codes.Value.HasFlag(WeatherCodes.Snow))
            {
                precipitation += "_sn";
            }
            if (codes.Value.HasFlag(WeatherCodes.Hail))
            {
                precipitation = "_gr";
            }

            var result = string.Format("{0}{1}", group, precipitation);

            if (needTwilight)
            {
                var twilightCalculatorState = "_d";

                if (useTwilight && dateTime.HasValue && astronomy?.Sunrise != null && astronomy.Sunset.HasValue)
                {
                    if ((dateTime <= astronomy.Sunrise || dateTime >= astronomy.Sunset) &&
                        (
                            dateTime <= astronomy.NextInfo?.Sunrise || dateTime >= astronomy.NextInfo?.Sunset ||
                            dateTime <= astronomy.PreviousInfo?.Sunrise || dateTime >= astronomy.PreviousInfo?.Sunset))
                    {
                        twilightCalculatorState = "_n";
                    }
                }

                result += twilightCalculatorState;
            }

            return(string.IsNullOrEmpty(group) ? null : result);
        }