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);
                }
            }
        }
Exemple #2
0
        public async void Execute()
        {
            if (!MemoryAccessor.IsMemoryContainsAddresses(_deviceContext.DeviceMemory,
                                                          (ushort)(_property.Address + _offset),
                                                          _property.NumberOfPoints, true))
            {
                return;
            }

            var newUshorts = GetLocalUshortsForProp();


            if (_property?.Dependencies?.Count > 0)
            {
                bool isHidden = false;

                bool isInteractionBlocked          = false;
                var  formatterForDependentProperty = _property.UshortsFormatter;

                foreach (var dependency in _property.Dependencies)
                {
                    if (dependency is IConditionResultDependency conditionResultDependency)
                    {
                        var checkResult = await DependentSubscriptionHelpers.CheckCondition(
                            conditionResultDependency.Condition,
                            _deviceContext, _formattingService, true, (ushort)_offset);

                        if (checkResult.IsSuccess)
                        {
                            if (checkResult.Item)
                            {
                                switch (conditionResultDependency.Result)
                                {
                                case IApplyFormatterResult applyFormatterResult:
                                    formatterForDependentProperty = applyFormatterResult.UshortsFormatter;
                                    break;

                                case IBlockInteractionResult blockInteractionResult:
                                    isInteractionBlocked = checkResult.Item;
                                    break;

                                case IHidePropertyResult hidePropertyResult:
                                    isHidden = checkResult.Item;
                                    break;
                                }
                            }
                            else
                            {
                                switch (conditionResultDependency.Result)
                                {
                                case IBlockInteractionResult blockInteractionResult:
                                    isInteractionBlocked = checkResult.Item;
                                    break;

                                case IHidePropertyResult hidePropertyResult:
                                    isHidden = checkResult.Item;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                }

                if (_prevUshorts.IsEqual(newUshorts) && _prevIsHidden == isHidden &&
                    formatterForDependentProperty == _prevUshortFormatter &&
                    _prevIsInteractionBlocked == isInteractionBlocked)
                {
                    return;
                }

                _prevIsHidden = isHidden;

                _prevIsInteractionBlocked = isInteractionBlocked;
                _prevUshorts         = newUshorts;
                _prevUshortFormatter = formatterForDependentProperty;
                if (_runtimePropertyViewModel?.LocalValue != null)
                {
                    _deviceContext.DeviceEventsDispatcher.RemoveSubscriptionById(_runtimePropertyViewModel
                                                                                 .LocalValue.Id);
                    _runtimePropertyViewModel.LocalValue.Dispose();
                }


                _runtimePropertyViewModel.IsHidden = _property.IsHidden || isHidden;

                var localValue = await _formattingService.FormatValueAsync(formatterForDependentProperty,
                                                                           newUshorts,
                                                                           new FormattingContext(_runtimePropertyViewModel, _deviceContext, true));

                var editableValue = StaticContainer.Container.Resolve <IValueViewModelFactory>()
                                    .CreateEditableValueViewModel(new FormattedValueInfo(localValue, _property,
                                                                                         formatterForDependentProperty,
                                                                                         _property, !isInteractionBlocked, !_prevUshorts.IsEqual(newUshorts)));
                var editSubscription =
                    new LocalDataEditedSubscription(_runtimePropertyViewModel, editableValue, _deviceContext,
                                                    _property, _offset);
                _runtimePropertyViewModel.LocalValue = editableValue;
                editableValue?.InitDispatcher(_deviceContext.DeviceEventsDispatcher);
                if (_runtimePropertyViewModel.LocalValue != null)
                {
                    _deviceContext.DeviceEventsDispatcher.AddSubscriptionById(editSubscription
                                                                              , _runtimePropertyViewModel.LocalValue.Id);
                }



                editableValue.InitDispatcher(_deviceContext.DeviceEventsDispatcher);
            }
            else
            {
                if (!MemoryAccessor.IsMemoryContainsAddresses(_deviceContext.DeviceMemory,
                                                              (ushort)(_property.Address + _offset), _property.NumberOfPoints, true))
                {
                    return;
                }

                if (!newUshorts.IsEqual(_prevUshorts))
                {
                    _prevUshorts = newUshorts;

                    var subPropertyValue = await StaticContainer.Container.Resolve <IFormattingService>()
                                           .FormatValueAsync(
                        _property.UshortsFormatter,
                        newUshorts,
                        new FormattingContext(_runtimePropertyViewModel, _deviceContext, true));


                    _runtimePropertyViewModel.LocalValue.Accept(
                        new EditableValueSetFromLocalVisitor(subPropertyValue));
                }
            }
        }
        public async void Execute()
        {
            if (_property.IsFromBits)
            {
                IFormattingService formattingService = StaticContainer.Container.Resolve <IFormattingService>();

                var formatterForProperty = _property?.UshortsFormatter;
                if (!MemoryAccessor.IsMemoryContainsAddresses(_deviceContext.DeviceMemory,
                                                              (ushort)(_property.Address + _offset), _property.NumberOfPoints, false))
                {
                    return;
                }

                var ushortsFromDevice = MemoryAccessor.GetUshortsFromMemory(
                    _deviceContext.DeviceMemory,
                    (ushort)(_property.Address + _offset), _property.NumberOfPoints, false);

                var         boolArray        = ushortsFromDevice.GetBoolArrayFromUshortArray().ToArray();
                List <bool> subPropertyBools = new List <bool>();
                foreach (var bitNumber in _property.BitNumbers)
                {
                    subPropertyBools.Add(boolArray[bitNumber]);
                }

                subPropertyBools.Reverse();
                var subPropertyUshort = subPropertyBools.BoolArrayToUshort();
                if (_property?.Dependencies?.Count > 0)
                {
                    formatterForProperty = await DependentSubscriptionHelpers.GetFormatterConsideringDependencies(
                        _property.Dependencies, _deviceContext, formattingService,
                        _property?.UshortsFormatter, _offset, false);
                }

                var value = await formattingService.FormatValueAsync(formatterForProperty, subPropertyUshort.AsCollection(), new FormattingContext(_localAndDeviceValueContainingViewModel, _deviceContext, false));

                _localAndDeviceValueContainingViewModel.DeviceValue =
                    _valueViewModelFactory.CreateFormattedValueViewModel(value);
            }
            else
            {
                IFormattingService formattingService = StaticContainer.Container.Resolve <IFormattingService>();

                var formatterForProperty = _property?.UshortsFormatter;
                if (_property?.Dependencies?.Count > 0)
                {
                    formatterForProperty = await DependentSubscriptionHelpers.GetFormatterConsideringDependencies(
                        _property.Dependencies, _deviceContext, formattingService,
                        _property?.UshortsFormatter, _offset, false);
                }

                if (MemoryAccessor.IsMemoryContainsAddresses(
                        _deviceContext.DeviceMemory,
                        (ushort)(_property.Address + _offset), _property.NumberOfPoints, false))
                {
                    var value = await formattingService.FormatValueAsync(formatterForProperty,
                                                                         MemoryAccessor.GetUshortsFromMemory(
                                                                             _deviceContext.DeviceMemory,
                                                                             (ushort)(_property.Address + _offset), _property.NumberOfPoints, false), new FormattingContext(_localAndDeviceValueContainingViewModel, _deviceContext, false));

                    _localAndDeviceValueContainingViewModel.DeviceValue =
                        _valueViewModelFactory.CreateFormattedValueViewModel(value);
                }
            }
        }