Exemple #1
0
        public async Task <Result <ushort[]> > GetUshortsOfProperty(object propertyMaybe, DeviceContext deviceContext,
                                                                    bool cacheAllowed, bool isLocal)
        {
            IProperty property = propertyMaybe as IProperty;

            if (property == null || !deviceContext.DataProviderContainer.DataProvider.IsSuccess)
            {
                return(Result <ushort[]> .Create(false));
            }

            if (cacheAllowed && deviceContext.DeviceMemory != null && MemoryAccessor.IsMemoryContainsAddresses(
                    deviceContext.DeviceMemory, property.Address,
                    property.NumberOfPoints, isLocal))
            {
                return(MemoryAccessor.GetUshortsFromMemory(deviceContext.DeviceMemory,
                                                           property.Address,
                                                           property.NumberOfPoints, isLocal));
            }

            var ushorts =
                await deviceContext.DataProviderContainer.DataProvider.Item.ReadHoldingResgistersAsync(property.Address,
                                                                                                       property.NumberOfPoints, "Read property");

            if (ushorts.IsSuccessful)
            {
                MemoryAccessor.SetUshortsInMemory(deviceContext.DeviceMemory, property.Address, ushorts.Result,
                                                  isLocal);
                return(ushorts.Result);
            }

            return(Result <ushort[]> .Create(false));
        }
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);
                }
            }
        }
        private static async Task <Result <ushort[]> > GetConditionPropertyUshort(string resourceName,
                                                                                  DeviceContext deviceContext, IFormattingService formattingService, bool isLocal, ushort addressOffset,
                                                                                  IProperty resourceProperty)
        {
            if (!MemoryAccessor.IsMemoryContainsAddresses(deviceContext.DeviceMemory,
                                                          (ushort)(resourceProperty.Address + addressOffset), resourceProperty.NumberOfPoints, isLocal))
            {
                return(Result <ushort[]> .Create(false));
            }


            var propertyUshorts = MemoryAccessor.GetUshortsFromMemory(
                deviceContext.DeviceMemory,
                (ushort)(resourceProperty.Address + addressOffset), resourceProperty.NumberOfPoints, isLocal);

            if (resourceProperty is ISubProperty subProperty)
            {
                var x = deviceContext.DeviceSharedResources.SharedResourcesInContainers.FirstOrDefault(
                    container =>
                    container.ResourceName == resourceName)
                        .Resource;

                var resultBitArray = new bool[16];
                var boolArray      = propertyUshorts.GetBoolArrayFromUshortArray();
                int counter        = 0;
                for (int i = 0; i < 16; i++)
                {
                    if (subProperty.BitNumbersInWord.Contains(i))
                    {
                        resultBitArray[counter] = boolArray[i];
                        counter++;
                    }
                }

                propertyUshorts = resultBitArray.BoolArrayToUshort().AsCollection();
            }

            if (resourceProperty.IsFromBits)
            {
                var x = deviceContext.DeviceSharedResources.SharedResourcesInContainers.FirstOrDefault(
                    container =>
                    container.ResourceName == resourceName)
                        .Resource;

                var resultBitArray = new bool[16];
                var boolArray      = propertyUshorts.GetBoolArrayFromUshortArray();
                int counter        = 0;
                for (int i = 0; i < 16; i++)
                {
                    if (resourceProperty.BitNumbers.Contains((ushort)i))
                    {
                        resultBitArray[counter] = boolArray[i];
                        counter++;
                    }
                }

                propertyUshorts = resultBitArray.BoolArrayToUshort().AsCollection();
            }

            return(Result <ushort[]> .Create(propertyUshorts, true));
        }