Esempio n. 1
0
        public async Task <IFormattedValue> VisitDirectUshortFormatter(IUshortsFormatter formatter)
        {
            INumericValue numericValue = _typesContainer.Resolve <INumericValue>();

            numericValue.NumValue = _ushortsPayload[0];
            return(numericValue);
        }
        public async Task <ushort[]> VisitAsciiStringFormatter(IUshortsFormatter formatter)
        {
            byte[] bytes  = Encoding.ASCII.GetBytes((_formattedValue as IStringValue).StrValue);
            var    result = new ushort[bytes.Length / 2];

            Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
            return(result);
        }
Esempio n. 3
0
        private async Task ApplyUshortOnAnalog(ushort[] result, IUshortsFormatter formatter)
        {
            IFormattedValue value =
                await this._formattingService.FormatValueAsync(formatter, result,
                                                               new FormattingContext(_analogMeasuringElementViewModel, _deviceContext, false));

            ApplyValue(value);
        }
        public IUshortsFormatterViewModel VisitBitMaskFormatter(IUshortsFormatter formatter)
        {
            var vm = new DefaultBitMaskFormatterViewModel();

            vm.BitSignatures =
                new ObservableCollection <StringWrapper>(
                    (formatter as IBitMaskFormatter).BitSignatures.Select(s => new StringWrapper(s)));
            return(vm);
        }
        public async Task <ushort[]> VisitBoolFormatter(IUshortsFormatter boolFormatter)
        {
            if (_formattedValue is IBoolValue)
            {
                return(((IBoolValue)_formattedValue).BoolValueProperty ? new[] { (ushort)1 } : new[] { (ushort)0 });
            }

            throw new ArgumentException();
        }
Esempio n. 6
0
        private Task <IFormattedValue> TryFormatAsync(IUshortsFormatter ushortsFormatter, ushort[] ushorts, FormattingContext formattingContext)
        {
            if (ushortsFormatter == null)
            {
                return(null);
            }

            return(ushortsFormatter.Accept(new FormatterFormatVisitor(ushorts, _typesContainer,
                                                                      _iterationDefinitionsCache, formattingContext)));
        }
        public async Task <ushort[]> VisitCodeFormatter(IUshortsFormatter formatter)
        {
            var service       = StaticContainer.Container.Resolve <ICodeFormatterService>();
            var codeFormatter = formatter as ICodeFormatter;
            var fun           = service.GetFormatBackUshortsFunc(codeFormatter.CodeExpression, _formattingContext.DeviceContext,
                                                                 _formattingContext.IsLocal, _formattingContext.ValueOwner);
            var res = await fun.Item.Invoke(_formattedValue);

            return(res);
        }
        public IUshortsFormatterViewModel VisitCodeFormatter(IUshortsFormatter formatter)
        {
            var codeFormatter = formatter as ICodeFormatter;

            return(new CodeFormatterViewModel(StaticContainer.Container.Resolve <ICodeFormatterService>())
            {
                FormatBackCodeString = codeFormatter.FormatBackCodeString,
                FormatCodeString = codeFormatter.FormatCodeString
            });
        }
Esempio n. 9
0
 public FormattedValueInfo(IFormattedValue formattedValue, IMeasurable measurable, IUshortsFormatter formatter,
                           IRangeable rangeable, bool isEditingEnabled = true, bool isChangedByDefault = false)
 {
     FormattedValue     = formattedValue;
     Measurable         = measurable;
     Formatter          = formatter;
     Rangeable          = rangeable;
     IsEditingEnabled   = isEditingEnabled;
     IsChangedByDefault = isChangedByDefault;
 }
Esempio n. 10
0
        public async Task <IFormattedValue> VisitUshortToIntegerFormatter(IUshortsFormatter formatter)
        {
            if (_ushortsPayload.Length != 2)
            {
                throw new ArgumentException("Number of words must be equal 2");
            }
            INumericValue numValue = _typesContainer.Resolve <INumericValue>();

            numValue.NumValue = _ushortsPayload.GetIntFromTwoUshorts();
            return(numValue);
        }
Esempio n. 11
0
        public async Task <IFormattedValue> VisitAsciiStringFormatter(IUshortsFormatter formatter)
        {
            IStringValue stringValue = _typesContainer.Resolve <IStringValue>();

            byte[] bytes = new byte[_ushortsPayload.Length * 2];
            Buffer.BlockCopy(_ushortsPayload, 0, bytes, 0, _ushortsPayload.Length * 2);
            string formattedString = Encoding.ASCII.GetString(bytes);

            stringValue.StrValue = formattedString;
            return(stringValue);
        }
        public async Task <ushort[]> VisitDirectUshortFormatter(IUshortsFormatter boolFormatter)
        {
            if (_formattedValue is INumericValue)
            {
                if ((_formattedValue as INumericValue).NumValue % 1 != 0)
                {
                    throw new ArgumentException();
                }
                return(new[] { (ushort)(_formattedValue as INumericValue).NumValue });
            }

            throw new ArgumentException();
        }
Esempio n. 13
0
 public async Task <IFormattedValue> FormatValueAsync(IUshortsFormatter ushortsFormatter, ushort[] ushorts, FormattingContext formattingContext)
 {
     try
     {
         return(await TryFormatAsync(ushortsFormatter, ushorts, formattingContext));
     }
     catch (Exception e)
     {
         var error = _typesContainer.Resolve <IErrorValue>();
         error.ErrorMessage = e.Message;
         return(error);
     }
 }
Esempio n. 14
0
 public LocalMemorySubscription(IUshortsFormatter ushortsFormatter, DeviceContext deviceContext,
                                IRuntimePropertyViewModel runtimePropertyViewModel,
                                IProperty property, IFormattingService formattingService, int offset, ushort[] prevUshorts)
 {
     _ushortsFormatter         = ushortsFormatter;
     _deviceContext            = deviceContext;
     _runtimePropertyViewModel = runtimePropertyViewModel;
     _property            = property;
     _formattingService   = formattingService;
     _offset              = offset;
     _prevUshorts         = prevUshorts;
     _prevUshortFormatter = property.UshortsFormatter;
 }
Esempio n. 15
0
 public async Task <Result <ushort[]> > FormatBackAsync(IUshortsFormatter ushortsFormatter, IFormattedValue formattedValue, FormattingContext formattingContext)
 {
     try
     {
         return(Result <ushort[]> .Create(await ushortsFormatter.Accept(new FormatterFormatBackVisitor(formattedValue, formattingContext)), true));
     }
     catch (Exception e)
     {
         var error = _typesContainer.Resolve <IErrorValue>();
         error.ErrorMessage = e.Message;
         return(Result <ushort[]> .CreateWithException(e));
     }
 }
Esempio n. 16
0
        public async Task <IFormattedValue> VisitCodeFormatter(IUshortsFormatter formatter)
        {
            var service       = _typesContainer.Resolve <ICodeFormatterService>();
            var codeFormatter = formatter as ICodeFormatter;


            var value = service.GetFormatUshortsFunc(codeFormatter.CodeExpression);

            var res = (await value.Item.Invoke(new BuiltExpressionFormatContext(_formattingContext.DeviceContext,
                                                                                _ushortsPayload, _formattingContext.IsLocal, _formattingContext.ValueOwner)));

            return(res);
        }
Esempio n. 17
0
        public static async Task <IUshortsFormatter> GetAnalogElementFormatting(this IAnalogMeasuringElement analogMeasuringElement,
                                                                                DeviceContext deviceContext)
        {
            IUshortsFormatter formatter = analogMeasuringElement.UshortsFormatter;
            var deps = analogMeasuringElement.Dependencies.OfType <IBoolToAddressDependency>();

            foreach (var dependency in deps)
            {
                (await IsDependencyTrue(dependency, deviceContext)).OnSuccess(b =>
                                                                              formatter = b ? dependency.FormatterIfTrue : dependency.FormatterIfFalse);
            }

            return(formatter);
        }
        private void OnOkExecute(object obj)
        {
            if (SelectedUshortsFormatterViewModel == null)
            {
                return;
            }
            if (SelectedUshortsFormatterViewModel is IDynamicFormatterViewModel)
            {
                if (!((IDynamicFormatterViewModel)SelectedUshortsFormatterViewModel).IsValid)
                {
                    return;
                }
            }

            if (CurrentResourceString != null)
            {
                ISaveFormatterService saveFormatterService = _container.Resolve <ISaveFormatterService>();

                IUshortsFormatter resourceUshortsFormatter =
                    saveFormatterService.CreateUshortsParametersFormatter(SelectedUshortsFormatterViewModel);
                resourceUshortsFormatter.Name = CurrentResourceString;
                _sharedResourcesGlobalViewModel.UpdateSharedResource(resourceUshortsFormatter);


                _ushortFormattableViewModel.ForEach(model => model.FormatterParametersViewModel =
                                                        _container.Resolve <IFormatterViewModelFactory>().CreateFormatterViewModel(resourceUshortsFormatter));
            }
            else
            {
                _ushortFormattableViewModel.ForEach(model => model.FormatterParametersViewModel =
                                                        _container.Resolve <IFormatterParametersViewModel>());
                _ushortFormattableViewModel.ForEach(model => model.FormatterParametersViewModel.RelatedUshortsFormatterViewModel =
                                                        SelectedUshortsFormatterViewModel);
            }


            if (_ushortFormattableViewModel.Count == 1)
            {
                var ushortFormattableViewModel = _ushortFormattableViewModel.First();

                if (ushortFormattableViewModel is IBitsConfigViewModel bitsConfigViewModel)
                {
                    this.CopyBitsTo(bitsConfigViewModel);
                    IsBitsEditingEnabled = true;
                }
            }

            (obj as Window)?.Close();
        }
        public async Task <ushort[]> VisitFormulaFormatter(IUshortsFormatter formatter)
        {
            try
            {
                IFormulaFormatter formulaFormatter = formatter as IFormulaFormatter;

                (_formattedValue as INumericValue).NumValue = Math.Round((_formattedValue as INumericValue).NumValue,
                                                                         formulaFormatter.NumberOfSimbolsAfterComma);
                string     numstr     = (_formattedValue as INumericValue).NumValue.ToString().Replace(',', '.');
                Expression expression = new Expression("solve(" + "(" + formulaFormatter.FormulaString + ")" + "-" +
                                                       numstr +
                                                       ",x,0," + ushort.MaxValue + 1 + ")");

                if (formulaFormatter.UshortFormattableResources != null)
                {
                    //  int index = 1;
                    //   foreach (IUshortFormattable formattableUshortResource in formulaFormatter.UshortFormattableResources)
                    //    {
                    // if (formattableUshortResource is IDeviceValueContaining)
                    //{
                    //    IFormattedValue value =
                    //       formattableUshortResource.UshortsFormatter.Format(
                    //           (formattableUshortResource as IDeviceValueContaining).DeviceUshortsValue);
                    //   if (value is INumericValue)
                    //   {
                    //       double num = (value as INumericValue).NumValue;
                    //       expression.addArguments(new Argument("x" + index++, num));
                    //  }
                    //}
                    //   }
                }

                double x = expression.calculate();
                if (double.IsNaN(x))
                {
                    throw new ArgumentException();
                }
                if (x == (double)(ushort.MaxValue + 1))
                {
                    x = ushort.MaxValue;
                }
                return(new[] { (ushort)x });
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Esempio n. 20
0
        public async Task <IFormattedValue> VisitBoolFormatter(IUshortsFormatter boolFormatter)
        {
            IBoolValue boolValue = _typesContainer.Resolve <IBoolValue>();

            if (_ushortsPayload[0] == 0)
            {
                boolValue.BoolValueProperty = false;
            }
            else
            {
                boolValue.BoolValueProperty = true;
            }

            return(boolValue);
        }
Esempio n. 21
0
        public IUshortsFormatterViewModel VisitTimeFormatter(IUshortsFormatter formatter)
        {
            var viewModel = new DefaultTimeFormatterViewModel();
            IDefaultTimeFormatter defaultTimeFormatter = formatter as IDefaultTimeFormatter;

            viewModel.MillisecondsDecimalsPlaces = defaultTimeFormatter.MillisecondsDecimalsPlaces.ToString();
            viewModel.NumberOfPointsInUse        = defaultTimeFormatter.NumberOfPointsInUse.ToString();
            viewModel.YearPointNumber            = defaultTimeFormatter.YearPointNumber.ToString();
            viewModel.MonthPointNumber           = defaultTimeFormatter.MonthPointNumber.ToString();
            viewModel.DayInMonthPointNumber      = defaultTimeFormatter.DayInMonthPointNumber.ToString();
            viewModel.HoursPointNumber           = defaultTimeFormatter.HoursPointNumber.ToString();
            viewModel.MinutesPointNumber         = defaultTimeFormatter.MinutesPointNumber.ToString();
            viewModel.SecondsPointNumber         = defaultTimeFormatter.SecondsPointNumber.ToString();
            viewModel.MillisecondsPointNumber    = defaultTimeFormatter.MillisecondsPointNumber.ToString();
            return(viewModel);
        }
Esempio n. 22
0
        IFormatterParametersViewModel IFormatterViewModelFactory.CreateFormatterViewModel(
            IUshortsFormatter ushortsFormatter)
        {
            if (ushortsFormatter == null)
            {
                return(null);
            }
            var formatterViewModel           = ushortsFormatter.Accept(this);
            var formatterParametersViewModel = new FormatterParametersViewModel()
            {
                Name = ushortsFormatter.Name,
                IsFromSharedResources            = ushortsFormatter.Name != null,
                RelatedUshortsFormatterViewModel = formatterViewModel
            };

            return(formatterParametersViewModel);
        }
Esempio n. 23
0
        public IUshortsFormatterViewModel VisitDictionaryMatchFormatter(IUshortsFormatter formatter)
        {
            var vm = new DictionaryMatchingFormatterViewModel();

            if (!(formatter is IDictionaryMatchingFormatter dictionaryMatchingFormatter))
            {
                return(null);
            }
            foreach (KeyValuePair <ushort, string> kvp in dictionaryMatchingFormatter.StringDictionary)
            {
                vm.KeyValuesDictionary.Add(new BindableKeyValuePair <ushort, string>(kvp.Key, kvp.Value));
            }

            vm.IsKeysAreNumbersOfBits = dictionaryMatchingFormatter.IsKeysAreNumbersOfBits;
            vm.DefaultMessage         = dictionaryMatchingFormatter.DefaultMessage;
            vm.UseDefaultMessage      = dictionaryMatchingFormatter.UseDefaultMessage;
            return(vm);
        }
Esempio n. 24
0
        public async Task <IFormattedValue> VisitDictionaryMatchFormatter(IUshortsFormatter formatter)
        {
            IChosenFromListValue         chosenFromListValue         = _typesContainer.Resolve <IChosenFromListValue>();
            IDictionaryMatchingFormatter dictionaryMatchingFormatter = formatter as IDictionaryMatchingFormatter;

            chosenFromListValue.InitList(dictionaryMatchingFormatter.StringDictionary.Values);

            if (!dictionaryMatchingFormatter.StringDictionary.Any((pair => pair.Key == _ushortsPayload[0])))
            {
                if (dictionaryMatchingFormatter.UseDefaultMessage)
                {
                    chosenFromListValue.SelectedItem = dictionaryMatchingFormatter.DefaultMessage;
                    return(chosenFromListValue);
                }

                chosenFromListValue.SelectedItem = this._ushortsPayload[0].ToString();
                return(chosenFromListValue);
            }

            if (dictionaryMatchingFormatter.IsKeysAreNumbersOfBits)
            {
                BitArray bitArray      = new BitArray(new int[] { _ushortsPayload[0] });
                bool     isValueExists = false;
                for (ushort i = 0; i < bitArray.Length; i++)
                {
                    if ((bitArray[i]) && (dictionaryMatchingFormatter.StringDictionary.ContainsKey(i)))
                    {
                        chosenFromListValue.SelectedItem = dictionaryMatchingFormatter.StringDictionary[i];
                        isValueExists = true;
                    }
                }

                if ((!isValueExists) && (dictionaryMatchingFormatter.StringDictionary.ContainsKey(0)))
                {
                    chosenFromListValue.SelectedItem = dictionaryMatchingFormatter.StringDictionary[0];
                }
            }
            else
            {
                chosenFromListValue.SelectedItem = dictionaryMatchingFormatter.StringDictionary[_ushortsPayload[0]];
            }

            return(chosenFromListValue);
        }
Esempio n. 25
0
        public async Task <IFormattedValue> VisitBitMaskFormatter(IUshortsFormatter formatter)
        {
            IBitMaskFormatter bitMaskFormatter = formatter as IBitMaskFormatter;
            IBitMaskValue     bitMaskValue     = _typesContainer.Resolve <IBitMaskValue>();

            foreach (ushort uUshort in _ushortsPayload)
            {
                List <bool> bools    = new List <bool>();
                BitArray    bitArray = new BitArray(new[] { (int)uUshort });
                foreach (bool o in bitArray)
                {
                    bools.Add(o);
                }

                bitMaskValue.BitArray.Add(bools.Take(16).Reverse().ToList());
            }

            bitMaskValue.BitSignatures.AddRange(bitMaskFormatter.BitSignatures);
            return(bitMaskValue);
        }
Esempio n. 26
0
        public static void CheckFormatterResult(int identity, IUshortsFormatter formatter)
        {
            switch (identity)
            {
            case 1:
                Assert.True(formatter is BoolFormatter);
                break;

            case 2:
                Assert.True(formatter is AsciiStringFormatter);
                break;

            case 3:
                Assert.True(formatter is DictionaryMatchingFormatter);
                var dictMatchFormatter = (DictionaryMatchingFormatter)formatter;
                Assert.True(dictMatchFormatter.UseDefaultMessage);
                Assert.True(dictMatchFormatter.IsKeysAreNumbersOfBits);
                Assert.AreEqual(dictMatchFormatter.DefaultMessage, "jopa");
                Assert.AreEqual(dictMatchFormatter.StringDictionary.Count, 3);
                Assert.AreEqual(dictMatchFormatter.StringDictionary[0], "jopa0");
                Assert.AreEqual(dictMatchFormatter.StringDictionary[1], "jopa1");
                Assert.AreEqual(dictMatchFormatter.StringDictionary[2], "jopa2");
                break;

            case 4:
                Assert.True(formatter is DirectUshortFormatter);
                break;

            case 5:
                Assert.True(formatter is StringFormatter1251);
                break;

            case 6:
                Assert.True(formatter is FormulaFormatter);
                var formulaFormatter = (FormulaFormatter)formatter;
                Assert.AreEqual(formulaFormatter.FormulaString, $"x*2+{identity}");
                Assert.AreEqual(formulaFormatter.UshortFormattableResources.First(), "testRes" + identity);

                break;
            }
        }
Esempio n. 27
0
        public async Task <IFormattedValue> VisitTimeFormatter(IUshortsFormatter formatter)
        {
            ITimeValue            value         = _typesContainer.Resolve <ITimeValue>();
            IDefaultTimeFormatter timeFormatter = formatter as IDefaultTimeFormatter;

            value.NumberOfPointsInUse        = timeFormatter.NumberOfPointsInUse;
            value.MillisecondsDecimalsPlaces = timeFormatter.MillisecondsDecimalsPlaces;
            for (int i = 0; i < timeFormatter.NumberOfPointsInUse; i++)
            {
                if (timeFormatter.YearPointNumber == i)
                {
                    value.YearValue = _ushortsPayload[i];
                }
                else if (timeFormatter.MonthPointNumber == i)
                {
                    value.MonthValue = _ushortsPayload[i];
                }
                else if (timeFormatter.DayInMonthPointNumber == i)
                {
                    value.DayInMonthValue = _ushortsPayload[i];
                }
                else if (timeFormatter.HoursPointNumber == i)
                {
                    value.HoursValue = _ushortsPayload[i];
                }
                else if (timeFormatter.MinutesPointNumber == i)
                {
                    value.MinutesValue = _ushortsPayload[i];
                }
                else if (timeFormatter.SecondsPointNumber == i)
                {
                    value.SecondsValue = _ushortsPayload[i];
                }
                else if (timeFormatter.MillisecondsPointNumber == i)
                {
                    value.MillisecondsValue = _ushortsPayload[i];
                }
            }

            return(value);
        }
Esempio n. 28
0
        private void CreateFormatterParametersForResourcesViewModel(int identity)
        {
            ISharedResourcesGlobalViewModel sharedResourcesGlobalViewModel =
                _typesContainer.Resolve <ISharedResourcesGlobalViewModel>();
            var formatterViewModel = CreateFormatterViewModel(identity, _typesContainer);

            sharedResourcesGlobalViewModel.AddAsSharedResource(new FormatterParametersViewModel()
            {
                IsFromSharedResources = false,
                Name = "formatter" + identity,
                RelatedUshortsFormatterViewModel = formatterViewModel
            }, false);

            ISaveFormatterService saveFormatterService = _typesContainer.Resolve <ISaveFormatterService>();

            IUshortsFormatter resourceUshortsFormatter =
                saveFormatterService.CreateUshortsParametersFormatter(formatterViewModel);

            resourceUshortsFormatter.Name = "formatter" + identity;
            sharedResourcesGlobalViewModel.UpdateSharedResource(resourceUshortsFormatter);
        }
Esempio n. 29
0
        public IUshortsFormatterViewModel VisitFormulaFormatter(IUshortsFormatter formatter)
        {
            IUshortsFormatterViewModel formatterViewModel =
                StaticContainer.Container.Resolve <IUshortsFormatterViewModel>(
                    StringKeys.FORMULA_FORMATTER + ApplicationGlobalNames.CommonInjectionStrings.VIEW_MODEL);
            var formulaFormatter = formatter as IFormulaFormatter;

            (formatterViewModel as IFormulaFormatterViewModel).NumberOfSimbolsAfterComma =
                formulaFormatter.NumberOfSimbolsAfterComma;
            (formatterViewModel as IFormulaFormatterViewModel).FormulaString =
                formulaFormatter.FormulaString;
            (formatterViewModel as IFormulaFormatterViewModel).ArgumentViewModels.Clear();
            (formatterViewModel as IFormulaFormatterViewModel).ArgumentViewModels.AddCollection(formulaFormatter
                                                                                                .UshortFormattableResources.Select(s => new ArgumentViewModel()
            {
                ResourceNameString = s,
                ArgumentName       = s,
                TestValue          = 1,
            }).ToList());
            return(formatterViewModel);
        }
        public async Task <ushort[]> VisitDictionaryMatchFormatter(IUshortsFormatter formatter)
        {
            IChosenFromListValue         chosenFromListValue         = _formattedValue as IChosenFromListValue;
            IDictionaryMatchingFormatter dictionaryMatchingFormatter = formatter as IDictionaryMatchingFormatter;

            ushort[] resultedUshorts = new ushort[1];
            if (dictionaryMatchingFormatter.IsKeysAreNumbersOfBits)
            {
                BitArray bitArray  = new BitArray(16);
                ushort   numberKey = dictionaryMatchingFormatter.StringDictionary
                                     .First((pair => pair.Value == chosenFromListValue.SelectedItem)).Key;
                bitArray[numberKey] = true;
                resultedUshorts[0]  = (ushort)bitArray.GetIntFromBitArray();
            }
            else
            {
                resultedUshorts[0] = dictionaryMatchingFormatter.StringDictionary
                                     .First((pair => pair.Value == chosenFromListValue.SelectedItem))
                                     .Key;
            }

            return(resultedUshorts);
        }