Ejemplo n.º 1
0
        /// <summary>
        /// Called by the view when a value change occurs.  The view fires this for both duty cycle and frequency value changes and the event args let us
        /// tell which one was fired
        /// </summary>
        /// <param name="channelName">The name of the channel to set the value on.</param>
        /// <param name="channelValue">The new channel value.</param>
        public void SetChannelValue(string channelName, double channelValue)
        {
            // set the collator owner to be different for the different channel value change operations so a value change for one of the controls doesn't
            // erase the value change for the other one
            var collatorOwner = channelName == PulseWidthModulationControlModel.DutyCycleChannelName ? _dutyCycleCollatorOwner : _frequencyCollatorOwner;

            if (Host.ActiveRunTimeServiceProvider().Status == RunTimeProviderStatus.Connected)
            {
                ScreenModel screenModel = ScreenModel.GetScreen(this);
                if (screenModel != null)
                {
                    // Use the action collator to make sure we are not generating more set channel value calls than we can handle.
                    screenModel.ToGatewayActionCollator.AddAction(collatorOwner, async() =>
                    {
                        try
                        {
                            if (channelName == PulseWidthModulationControlModel.DutyCycleChannelName && !string.IsNullOrEmpty(DutyCycleChannel))
                            {
                                // set the channel value on the gateway, we are passing in empty labda expressions to the success and failure callbacks in this case
                                // if we wanted to report errors to the user we could add some handling code for the failure case
                                await Host.GetRunTimeService <ITagService>().SetTagValueAsync(DutyCycleChannel, TagFactory.CreateTag(channelValue));
                            }
                            if (channelName == PulseWidthModulationControlModel.FrequencyChannelName && !string.IsNullOrEmpty(FrequencyChannel))
                            {
                                await Host.GetRunTimeService <ITagService>().SetTagValueAsync(FrequencyChannel, TagFactory.CreateTag(channelValue));
                            }
                        }
                        catch (VeriStandException e)
                        {
                            Host.Dispatcher.InvokeIfNecessary(
                                PlatformDispatcher.AsyncOperationAlwaysValid,
#if MUTATE2021
                                () => this.SafeReportError(
#else
                                () => this.ReportError(
#endif
                                    PwmControlModelErrorString,
                                    null,
                                    MessageDescriptor.Empty,
                                    e));
                        }
                    });
Ejemplo n.º 2
0
        /// <summary>
        /// This method is called when the channel itself is changed
        /// </summary>
        /// <param name="transactionItem">Information about the change in the model</param>
        /// <param name="owningScreen">Screen which owns this control</param>
        /// <param name="channelChangeHandler">The handler to use for registering with the gateway. Since we have two different channels
        /// we must make sure to provide the gateway with the correct handler</param>
        private async Task HandleChannelChangeAsync(
            TransactionItem transactionItem,
            ScreenModel owningScreen,
            Action <ITagValue> channelChangeHandler)
        {
            var propertyExpressionTransactionItem =
                transactionItem as PropertyExpressionTransactionItem;

            // Lots of conditions where we ignore the channel change
            if (propertyExpressionTransactionItem == null ||
                Host.ActiveRunTimeServiceProvider().Status != RunTimeProviderStatus.Connected ||
                Equals(propertyExpressionTransactionItem.OldValue, propertyExpressionTransactionItem.NewValue) ||
                owningScreen == null)
            {
                return;
            }

            // The oldValue can be thought of as what the channel value currently is and the newValue is what it will become.
            string oldValue, newValue;

            // transaction state being rolled back means that we are undoing something. for our purposes this reverses the logic for which item in the transaction is the old value and which is the new value
            if (transactionItem.State == TransactionState.RolledBack)
            {
                oldValue = propertyExpressionTransactionItem.NewValue as string;
                newValue = propertyExpressionTransactionItem.OldValue as string;
            }
            else
            {
                oldValue = propertyExpressionTransactionItem.OldValue as string;
                newValue = propertyExpressionTransactionItem.NewValue as string;
            }

            if (CheckForVectorChannels(!string.IsNullOrEmpty(newValue) ? newValue : oldValue))
            {
                return;
            }

            // Depending on whether there are values for old value and new value, either register, unregister, or rebind (which is unregister and register). Rebind is used to avoid
            // some race conditions with registration when the same control is being unregistered and re-registered to something else
            if (!string.IsNullOrEmpty(oldValue))
            {
                try
                {
                    await Host.GetRunTimeService <ITagService>().UnregisterTagAsync(oldValue, channelChangeHandler);
                }
                catch (Exception ex) when(ShouldExceptionBeCaught(ex))
                {
                    ReportErrorToModel(ex);
                }
            }
            if (!string.IsNullOrEmpty(newValue))
            {
                try
                {
                    await Host.GetRunTimeService <ITagService>().UnregisterTagAsync(newValue, channelChangeHandler);
                }
                catch (Exception ex) when(ShouldExceptionBeCaught(ex))
                {
                    ReportErrorToModel(ex);
                }
            }
        }