public static string ToString(object value, Unit unit, CustomUnits customUnits)
        {
            var type = value.GetType();

            if (type == typeof(bool))
            {
                var boolean = Convert.ToBoolean(value);
                if (!unit.IsDigital())
                {
                    throw new ArgumentException($@"Bool value acceptably only for digital unit.
Value: {boolean}, Unit: {unit}");
                }

                return(BooleanToDigitalValue(boolean, unit, customUnits));
            }
            else if (type == typeof(TimeSpan))
            {
                var span = (TimeSpan)value;
                return(span.ToString(
                           $@"{(span.Days == 0 ? string.Empty : @"d\.")}" +
                           @"hh\:mm\:ss" +
                           $@"{(span.Milliseconds == 0 ? string.Empty : @"\.fff")}"));
            }
            else if (type == typeof(double))
            {
                return(Convert.ToDouble(value).ToString("F3"));
            }
            else
            {
                throw new NotImplementedException($@"Type not supported.
Type: {type}, Value: {value}, Unit: {unit}
Supported types: bool, float, TimeSpan");
            }
        }
        public static string ConvertValue(string value, Unit fromUnit, Unit toUnit,
                                          CustomUnits fromCustomUnits = null,
                                          CustomUnits toCustomUnits   = null)
        {
            if (fromUnit.IsDigital())
            {
                var boolean = DigitalValueToBoolean(value, fromUnit, fromCustomUnits);

                return(toUnit.IsDigital()
                    ? BooleanToDigitalValue(boolean, toUnit, toCustomUnits)
                    : boolean.ToByte().ToString());
            }

            if (toUnit.IsDigital())
            {
                double doubleValue;
                if (!double.TryParse(value, out doubleValue))
                {
                    BooleanToDigitalValue(true, toUnit, toCustomUnits);
                }

                return(BooleanToDigitalValue(doubleValue != 0.0, toUnit, toCustomUnits));
            }

            return(value);
        }
        public static UnitsNames GetUnitsNames(this Unit unit, CustomUnits customUnits = null)
        {
            var names = UnitsNamesUtilities.GetNames(customUnits);

            if (!names.ContainsKey(unit))
            {
                throw new ArgumentException($@"Unit name not exists.
Unit: {unit}", nameof(unit));
            }

            return(names[unit]);
        }
 public VariableValue(int value, Unit unit, CustomUnits customUnits = null,
                      int maxRange = -1)
 {
     Value       = value;
     Unit        = unit;
     CustomUnits = customUnits;
     MaxRange    = !unit.IsDigital()
         ? -1
         : maxRange == -1
             ? value
             : maxRange;
 }
        public static object ToObject(string value, Unit unit, CustomUnits customUnits)
        {
            switch (unit)
            {
            case Unit.Time:
                return(TimeSpan.Parse(value));

            default:
                return(unit.IsDigital()
                        ? DigitalValueToBoolean(value, unit, customUnits)
                        : (object)Convert.ToDouble(value));
            }
        }
        public static bool DigitalValueToBoolean(string value, Unit unit,
                                                 CustomUnits customUnits = null)
        {
            var onName  = unit.GetOnName(customUnits);
            var offName = unit.GetOffName(customUnits);

            if (!value.Equals(onName, StringComparison.OrdinalIgnoreCase) &&
                !value.Equals(offName, StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException($@"Value not valid.
Value: {value}, Unit: {unit}.
Supporting values: {onName}, {offName}
CustomUnits: {customUnits}");
            }

            return(value.Equals(onName, StringComparison.OrdinalIgnoreCase));
        }
        private static Dictionary <Unit, UnitsNames> FillDictionary(
            CustomUnits customUnits     = null,
            Func <Unit, bool> predicate = null)
        {
            var names = new Dictionary <Unit, UnitsNames>();

            foreach (Unit units in Enum.GetValues(typeof(Unit)))
            {
                if (predicate != null && !predicate(units))
                {
                    continue;
                }

                names.Add(units, GetNameCollection(units, customUnits));
            }

            return(names);
        }
        private static UnitsNames GetNameCollection(Unit unit,
                                                    CustomUnits customUnits = null)
        {
            switch (unit)
            {
            //Custom digital part
            case Unit.CustomDigital1:
            case Unit.CustomDigital2:
            case Unit.CustomDigital3:
            case Unit.CustomDigital4:
            case Unit.CustomDigital5:
            case Unit.CustomDigital6:
            case Unit.CustomDigital7:
            case Unit.CustomDigital8:
                return(GetCustomDigitalName(unit - Unit.CustomDigital1, customUnits?.Digital, unit.ToString()));

            //Custom analog part
            case Unit.Custom1:
            case Unit.Custom2:
            case Unit.Custom3:
            case Unit.Custom4:
            case Unit.Custom5:
                return(GetCustomAnalogName(unit - Unit.Custom1, customUnits?.Analog, unit.ToString()));

            //Custom input analog part
            case Unit.InputAnalogCustom1:
            case Unit.InputAnalogCustom2:
            case Unit.InputAnalogCustom3:
            case Unit.InputAnalogCustom4:
            case Unit.InputAnalogCustom5:
                return(GetCustomAnalogName(unit - Unit.InputAnalogCustom1, customUnits?.Analog, unit.ToString()));

            //Custom output analog part
            case Unit.OutputAnalogCustom1:
            case Unit.OutputAnalogCustom2:
            case Unit.OutputAnalogCustom3:
            case Unit.OutputAnalogCustom4:
            case Unit.OutputAnalogCustom5:
                return(GetCustomAnalogName(unit - Unit.OutputAnalogCustom1, customUnits?.Analog, unit.ToString()));

            default:
                return(unit.GetUnitsNames()?.UnitsNames ?? new UnitsNames(unit.ToString()));
            }
        }
        public static Dictionary <Unit, UnitsNames> GetNames(
            CustomUnits customUnits     = null,
            Func <Unit, bool> predicate = null)
        {
            if (customUnits != null)
            {
                return(FillDictionary(customUnits, predicate));
            }

            var names = BaseDictionary;

            if (predicate == null)
            {
                return(names);
            }

            return(names.Where(pair => predicate(pair.Key))
                   .ToDictionary(pair => pair.Key, pair => pair.Value));
        }
 public static string GetUnitName(this Unit unit, CustomUnits customUnits = null) =>
 unit.IsDigital()
         ? unit.GetOffOnName(customUnits)
         : unit.GetOffName(customUnits);
 public static string GetOffOnName(this Unit unit, CustomUnits customUnits = null) =>
 GetUnitsNames(unit, customUnits).OffOnName;
 public string ConvertValue(Unit unit, CustomUnits customUnits = null) =>
 ConvertValue(ToString(), Unit, unit, CustomUnits, customUnits);
 public VariableValue(string value, Unit unit, CustomUnits customUnits = null,
                      int maxRange = -1)
     : this(ToObject(value, unit, customUnits), unit, customUnits, maxRange)
 {
 }
 public static string BooleanToDigitalValue(bool value, Unit unit,
                                            CustomUnits customUnits = null) =>
 value?unit.GetOnName(customUnits) : unit.GetOffName(customUnits);
 public VariableValue(object value, Unit unit, CustomUnits customUnits = null,
                      int maxRange = -1)
     : this(ToInt(value, unit, maxRange), unit, customUnits, maxRange)
 {
 }