public IWeatherViewModel Create(
     WeatherContract weatherContract,
     TemperatureUnit temperatureUnit,
     WindSpeedUnit windSpeedUnit)
 {
     return(new WeatherViewModel(weatherContract, temperatureUnit, windSpeedUnit));
 }
        public double Convert(double sourceValue, WindSpeedUnit sourceUnit, WindSpeedUnit targetUnit)
        {
            double targetValue = double.NaN;

            IWindSpeedUnitConverter windSpeedUnitConverter;
            if (_windSpeedConverters.TryGetValue(sourceUnit, out windSpeedUnitConverter))
            {
                try
                {
                    targetValue = windSpeedUnitConverter.Convert(sourceValue, sourceUnit, targetUnit);
                }
                catch (ArgumentException ex)
                {
                    // OK the above sourceUnit-related converter could not convert to teh target unit,
                    // so try the targetUnit-related converter instead
                    if (_windSpeedConverters.TryGetValue(targetUnit, out windSpeedUnitConverter))
                    {
                        try
                        {
                            targetValue = windSpeedUnitConverter.Convert(sourceValue, sourceUnit, targetUnit);
                        }
                        catch (ArgumentException ex2)
                        {
                            LoggerManager.WriteError(typeof(UnitConverter), ex2.Message);
                        }
                    }
                }
            }

            return targetValue;
        }
        public static float windSpeed(this Map map, WindSpeedUnit unit = WindSpeedUnit.KilometersPerHour)
        {
            // on the basis that map.windManager returns a value that is normally somewhere between 0-2,
            // and can go over 2 in extreme cases. Translated to a 1-12 scale, that's a factor of 6ish.
            var beaufort = map.windManager.WindSpeed * 5.6f;

            if (unit == WindSpeedUnit.Beaufort)
            {
                return(beaufort);
            }

            var kph = WindSpeedCurve.Evaluate(Mathf.Clamp(beaufort, 0, 13));

            if (unit == WindSpeedUnit.KilometersPerHour)
            {
                return(kph);
            }

            return(unit switch
            {
                WindSpeedUnit.MetersPerSecond => kph / 3.6f,
                WindSpeedUnit.MilesPerHour => kph / 1.609f,
                WindSpeedUnit.Knots => kph / 1.852f,
                WindSpeedUnit.FeetPerSecond => kph / 1.097f,
                _ => throw new ArgumentOutOfRangeException(nameof(unit), unit, null)
            });
        /// <summary>
        /// This first "converter" only knows about km/h and therefore no actual conversion is needed.
        /// </summary>
        /// <param name="sourceValue"></param>
        /// <param name="sourceUnit"></param>
        /// <param name="targetUnit"></param>
        /// <returns></returns>
        public double Convert(double sourceValue, WindSpeedUnit sourceUnit, WindSpeedUnit targetUnit)
        {
            if (sourceUnit != WindSpeedUnit.Kph) throw new ArgumentException("Unexpected source unit");

            if (targetUnit != WindSpeedUnit.Kph) throw new ArgumentException("Unexpected target unit");

            return sourceValue;
        }
        public IWindSpeedViewModel Create(WindSpeedUnit windSpeedUnit)
        {
            switch (windSpeedUnit)
            {
            case WindSpeedUnit.MetersPerSecond:
                return(new WindSpeedViewModel("м/с", windSpeedUnit));

            case WindSpeedUnit.MilesPerHour:
                return(new WindSpeedViewModel("М/ч", windSpeedUnit));

            case WindSpeedUnit.KilometersPerHour:
                return(new WindSpeedViewModel("Км/с", windSpeedUnit));

            default:
                throw new ArgumentOutOfRangeException(nameof(windSpeedUnit), windSpeedUnit, null);
            }
        }
Beispiel #6
0
        public WeatherViewModel(
            WeatherContract weatherContract,
            TemperatureUnit temperatureUnit,
            WindSpeedUnit windSpeedUnit)
        {
            Time        = $"{weatherContract.DateTime:HH:mm}";
            Description = weatherContract.Description;

            switch (temperatureUnit)
            {
            case TemperatureUnit.Celsius:
                Temperature = $"{weatherContract.Celsius} °C";
                break;

            case TemperatureUnit.Fahrenheit:
                Temperature = $"{weatherContract.Fahrenheit} °F";
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(temperatureUnit), temperatureUnit, null);
            }

            switch (windSpeedUnit)
            {
            case WindSpeedUnit.MetersPerSecond:
                WindSpeed = $"{weatherContract.MetersPerSecond} м/с";
                break;

            case WindSpeedUnit.MilesPerHour:
                WindSpeed = $"{weatherContract.MilesPerHour} М/ч";
                break;

            case WindSpeedUnit.KilometersPerHour:
                WindSpeed = $"{weatherContract.KilometersPerHour} Км/с";
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(windSpeedUnit), windSpeedUnit, null);
            }

            Precipitation = weatherContract.Precipitation == null
                ? "Без осдаков"
                : $"{weatherContract.Precipitation.Value}";
        }
Beispiel #7
0
        public WeatherDataBuilder SetWindSpeed(float value, WindSpeedUnit unit)
        {
            if (value < 0)
            {
                throw new InvalidOperationException("Wind speed cannot be smaller than 0");
            }

            switch (unit)
            {
            case WindSpeedUnit.KilometersPerHour:
                return(Do(x => x.WindSpeed = value));

            case WindSpeedUnit.MetersPerSecond:
                return(Do(x => x.WindSpeed = value * 3.6f));

            case WindSpeedUnit.MilesPerHour:
                return(Do(x => x.WindSpeed = value * 1.609f));

            default:
                throw new ArgumentOutOfRangeException(nameof(unit), unit, $"Windspeed unit {unit} is not yet implemented");
            }
        }
        public void DoWindowContents(Rect canvas)
        {
            var options = new Listing_Standard();

            options.Begin(canvas);
            if (options.ButtonTextLabeled(I18n.Settings_WindSpeedUnit, unit.ToString()))
            {
                var unitOptions = Enum.GetValues(typeof(WindSpeedUnit)) as WindSpeedUnit[];
                Find.WindowStack.Add(new FloatMenu(unitOptions
                                                   .Select(option =>
                                                           new FloatMenuOption(
                                                               option.ToString(), () => unit = option))
                                                   .ToList()));
            }

            options.End();
        }
Beispiel #9
0
 public WindSpeedViewModel(string name, WindSpeedUnit windSpeedUnit)
 {
     Name          = name;
     WindSpeedUnit = windSpeedUnit;
 }