Ejemplo n.º 1
0
        public async void Execute()
        {
            if (_property.IsFromBits)
            {
                //var resultBitArray=new bool[16];
                var resultBitArray = _deviceContext.DeviceMemory.LocalMemoryValues[(ushort)(_property.Address + _offset)].GetBoolArrayFromUshort();


                IFormattingService formattingService = StaticContainer.Container.Resolve <IFormattingService>();
                IEditableValueFetchingFromViewModelVisitor fetchingFromViewModelVisitor =
                    StaticContainer.Container.Resolve <IEditableValueFetchingFromViewModelVisitor>();


                var formatterForDependentProperty = _property.UshortsFormatter;

                if (_property?.Dependencies?.Count > 0)
                {
                    formatterForDependentProperty = await
                                                    DependentSubscriptionHelpers.GetFormatterConsideringDependencies(_property.Dependencies,
                                                                                                                     _deviceContext, formattingService, _property.UshortsFormatter, (ushort)_offset, true);
                }


                var ushorts = await formattingService.FormatBackAsync(formatterForDependentProperty,
                                                                      EditableValueViewModel.Accept(fetchingFromViewModelVisitor), new FormattingContext(_runtimePropertyViewModel, _deviceContext, true));

                var ushortOfSubProperty = ushorts.Item.First();
                var boolArray           = ushorts.Item.GetBoolArrayFromUshortArray();
                int counter             = 0;
                for (int i = 0; i < 16; i++)
                {
                    if (_property.BitNumbers.Contains((ushort)i))
                    {
                        resultBitArray[i] = boolArray[counter];
                        counter++;
                    }
                }



                resultBitArray = resultBitArray.ToArray();
                var resUshorts = new ushort[] { resultBitArray.BoolArrayToUshort() };
                MemoryAccessor.SetUshortsInMemory(_deviceContext.DeviceMemory, (ushort)(_property.Address + _offset), resUshorts, true);
                _deviceContext.DeviceEventsDispatcher.TriggerLocalAddressSubscription(
                    (ushort)(_property.Address + _offset), (ushort)resUshorts.Length);
            }
            else
            {
                IFormattingService formattingService = StaticContainer.Container.Resolve <IFormattingService>();
                IEditableValueFetchingFromViewModelVisitor fetchingFromViewModelVisitor =
                    StaticContainer.Container.Resolve <IEditableValueFetchingFromViewModelVisitor>();


                var formatterForDependentProperty = _property.UshortsFormatter;

                if (_property?.Dependencies?.Count > 0)
                {
                    formatterForDependentProperty = await
                                                    DependentSubscriptionHelpers.GetFormatterConsideringDependencies(_property.Dependencies,
                                                                                                                     _deviceContext, formattingService, _property.UshortsFormatter, (ushort)_offset, true);
                }


                var ushorts = await formattingService.FormatBackAsync(formatterForDependentProperty,
                                                                      EditableValueViewModel.Accept(fetchingFromViewModelVisitor), new FormattingContext(_runtimePropertyViewModel, _deviceContext, true));


                if (!ushorts.IsSuccess)
                {
                    if (EditableValueViewModel is ValidatableBindableBase validatableBindableBase)
                    {
                        validatableBindableBase.AddError("NumValue", ushorts.Exception.Message);
                    }
                }
                else
                {
                    MemoryAccessor.SetUshortsInMemory(_deviceContext.DeviceMemory, (ushort)(_property.Address + _offset),
                                                      ushorts.Item, true);
                    _deviceContext.DeviceEventsDispatcher.TriggerLocalAddressSubscription(
                        (ushort)(_property.Address + _offset), (ushort)ushorts.Item.Length);
                }
            }
        }