Example #1
0
        public static UserUnit operator *(UserUnit uu1, UserUnit uu2)
        {
            var uu = new UserUnit();

            if (!String.IsNullOrWhiteSpace(uu1.NumeratorName))
            {
                uu.NumeratorName = String.IsNullOrWhiteSpace(uu.NumeratorName)? uu1.NumeratorName : uu.NumeratorName + "*" + uu1.NumeratorName;
            }
            if (!String.IsNullOrWhiteSpace(uu2.NumeratorName))
            {
                uu.NumeratorName = String.IsNullOrWhiteSpace(uu.NumeratorName) ? uu2.NumeratorName : uu.NumeratorName + "*" + uu2.NumeratorName;
            }
            if (!String.IsNullOrWhiteSpace(uu1.DenominatorName))
            {
                uu.DenominatorName = String.IsNullOrWhiteSpace(uu.DenominatorName) ? uu1.DenominatorName : uu.DenominatorName + "*" + uu1.DenominatorName;
            }
            if (!String.IsNullOrWhiteSpace(uu2.DenominatorName))
            {
                uu.DenominatorName = String.IsNullOrWhiteSpace(uu.DenominatorName) ? uu2.DenominatorName : uu.DenominatorName + "*" + uu2.DenominatorName;
            }
            uu.Numerator.AddRange(uu1.Numerator);
            uu.Numerator.AddRange(uu2.Numerator);
            uu.Denominator.AddRange(uu1.Denominator);
            uu.Denominator.AddRange(uu2.Denominator);
            uu.Multiplier        = uu1.Multiplier * uu2.Multiplier;
            uu.NumeratorPrefix   = uu1.NumeratorPrefix * uu2.NumeratorPrefix;
            uu.DenominatorPrefix = uu1.DenominatorPrefix * uu2.DenominatorPrefix;
            uu.DimensionVector   = uu1.DimensionVector + uu2.DimensionVector;
            return(uu);
        }
Example #2
0
        public async Task Subscribe(string topic, string toUnit, Func <string, object[], CancellationToken, Task> answerCallback)
        {
            this.topic  = topic;
            this.toUnit = await unitFactory.Parse(toUnit);

            this.answerCallback = answerCallback;
            FromUnit            = await unitFactory.Parse(topic.Split("/").Last().Replace("%F2", "/"));

            if (this.toUnit.DimensionVector != FromUnit.DimensionVector)
            {
                throw new InvalidOperationException("Units do not have the same dimension vector");
            }
            subscribtionId = await mqttClientManager.Subscribe(topic, HandleNewMessage);
        }
Example #3
0
        private async Task <UserUnit> ParseSimpleUnit(string input)
        {
            FindPrefix(input, out var hasPrefix, out var prefixFactor);
            if (hasPrefix)
            {
                input = input.Replace(prefixFactor?.Item1, "");
            }
            var dbUnit = await db.SelectUnit(input);

            var uu = new UserUnit(name: input,
                                  numeratorPrefixes: hasPrefix ? (prefixFactor?.Item2) : ConversionFactor.One,
                                  denominatorPrefixes: ConversionFactor.One,
                                  multiplier: dbUnit.ConversionMultiplier,
                                  offset: dbUnit.ConversionOffset,
                                  dv: dbUnit.DimensionVector);

            return(uu);
        }
Example #4
0
        private async Task <UserUnit> ParseFractionPart(string input, UserUnit uu, bool isNumerator)
        {
            var parts = input.Split("*");

            foreach (var part in parts)
            {
                FindPrefix(part, out var hasPrefix, out var prefixFactor);

                if (part.Contains("^"))
                {
                    return(await SeperatePowers(uu, part, hasPrefix, prefixFactor, isNumerator));
                }
                else
                {
                    string partToAdd = part;
                    if (hasPrefix)
                    {
                        partToAdd = part.Replace(prefixFactor?.Item1, "");
                    }
                    var dbUnit = await db.SelectUnit(partToAdd);

                    UserUnit newUU = new UserUnit(name: partToAdd,
                                                  numeratorPrefixes:    hasPrefix ? (prefixFactor?.Item2) : ConversionFactor.One,
                                                  denominatorPrefixes:  ConversionFactor.One,
                                                  multiplier:           dbUnit.ConversionMultiplier,
                                                  offset:               0,
                                                  dv:                   dbUnit.DimensionVector);

                    if (isNumerator)
                    {
                        uu *= newUU;
                    }
                    else
                    {
                        uu /= newUU;
                    }
                }
            }

            return(uu);
        }
Example #5
0
        public async Task <UserUnit> Parse(string input)
        {
            var uu = new UserUnit();

            input = input.Replace(" ", "").Replace("\t", "").ToUpperInvariant();

            if (!input.Contains("/") && !input.Contains("*") && !input.Contains("^"))
            {
                uu = await ParseSimpleUnit(input);
            }
            else
            {
                uu = await ParseFractionPart(input.Split("/")[0], uu, true);

                if (input.Contains("/"))
                {
                    uu = await ParseFractionPart(input.Split("/")[1], uu, false);
                }
            }
            return(uu);
        }
Example #6
0
 private async Task <UserUnit> SeperatePowers(UserUnit uu, string part, bool hasPrefix, (string, ConversionFactor)?prefixFactor, bool isNumerator)