Esempio n. 1
0
    /// <summary>
    /// Called when a connection attempt complete, successfully or not.
    /// </summary>
    /// <param name="asyncInfo">Data about the async operation.</param>
    /// <param name="status">The status of the operation.</param>
    public void SocketOpenedHandler(IAsyncAction asyncInfo, AsyncStatus status)
    {
        // Status completed is successful.
        if (status == AsyncStatus.Completed)
        {
            socketOpen = true;
            Debug.Log("Connected! Ready to send and receive data");
            //socketDataWriter = new DataWriter(socketConnection.OutputStream);
            socketDataReader = new DataReader(socketConnection.InputStream);
            socketDataReader.UnicodeEncoding = UnicodeEncoding.Utf8;
            socketDataReader.ByteOrder       = ByteOrder.LittleEndian;

            //Begin reading data in the input stream async
            DataReaderLoadOperation outstandingRead    = socketDataReader.LoadAsync(4 * 16); // Try to load 53 bytes
            AsyncOperationCompletedHandler <uint> aoch = new AsyncOperationCompletedHandler <uint>(DataReadHandler);
            outstandingRead.Completed = aoch;
        }
        else
        {
            Debug.Log("Failed to establish connection. Error Code: " + asyncInfo.ErrorCode);
            // In the failure case we'll requeue the data and wait before trying again.
            socketConnection.Dispose();
            // Setup a callback function that will retry connection after 2 seconds
            if (!socketOpen)               // Redundant but to be safe
            {
                deferredConnection = true; // Defer the connection attempt
            }
        }
    }
Esempio n. 2
0
        /// <summary>
        /// Called when receive data has completed.
        /// </summary>
        /// <param name="operation">Data about the async operation.</param>
        /// <param name="status">The status of the operation.</param>
        public void DataReadHandler(IAsyncOperation <uint> operation, AsyncStatus status)
        {
            // If we failed, requeue the data and set the deferral time.
            if (status == AsyncStatus.Error)
            {
                // didn't load data
                Debug.Log("Failed to load new data");
            }
            else
            {
                ax = socketDataReader.ReadSingle();              // Substract the offset of 1 and scale from mm to meters
                bx = socketDataReader.ReadSingle();
                //cx = socketDataReader.ReadSingle();
                ay = socketDataReader.ReadSingle();
                by = socketDataReader.ReadSingle();
                //cy = socketDataReader.ReadSingle();
                //Debug.Log("ax " + ax + " bx " + bx + " cx " + cx + " ay " + ay + " by " + by + " cy " + cy);
                interlock = 1;


                //restart reading data in the input stream async
                DataReaderLoadOperation outstandingRead    = socketDataReader.LoadAsync(4 * 4);
                AsyncOperationCompletedHandler <uint> aoch = new AsyncOperationCompletedHandler <uint>(DataReadHandler);
                outstandingRead.Completed = aoch;
            }
        }
Esempio n. 3
0
 public static void TrackAsyncAction <T>(IAsyncOperation <T> action, AsyncOperationCompletedHandler <T> completed)
 {
     DisableView(null);
     action.Completed = (s, e) => root.Dispatcher.Begin(() =>
     {
         EnableView();
         completed(s, e);
     });
 }
Esempio n. 4
0
 public static void TrackAsyncAction <T>(IAsyncOperation <T> action, AsyncOperationCompletedHandler <T> completed)
 {
     DisableView(null);
     action.Completed = (s, e) => DispatcherHelper.BeginInvokeOnUIThread(() =>
     {
         EnableView();
         completed(s, e);
     });
 }
Esempio n. 5
0
        private void SendBytes(byte[] reg, AsyncOperationCompletedHandler <uint> completed)
        {
            Writer.WriteBytes(reg);
            var storeResult = Writer.StoreAsync();

            if (completed != null)
            {
                storeResult.Completed += completed;
            }
        }
        private async void InitializeRecognizer()
        {
            this.Recognizer = new SpeechRecognizer();
            this.Recognizer.Grammars.AddGrammarFromPredefinedType("search", SpeechPredefinedGrammar.WebSearch);

            //note used, but here's an example of the other predefined grammer collections:
            //this.Recognizer.Grammars.AddGrammarFromPredefinedType("dictation", SpeechPredefinedGrammar.Dictation);

            await this.Recognizer.PreloadGrammarsAsync();

            recoCompletedAction = new AsyncOperationCompletedHandler <SpeechRecognitionResult>((operation, asyncStatus) =>
            {
                Dispatcher.BeginInvoke(() =>
                {
                    this.CurrentRecognizerOperation = null;

                    switch (asyncStatus)
                    {
                    case AsyncStatus.Completed:
                        SpeechRecognitionResult result = operation.GetResults();
                        if (!String.IsNullOrEmpty(result.Text))
                        {
                            //execute my search here!
                            LaunchSearch(result.Text);
                        }
                        break;

                    case AsyncStatus.Error:
                        if (operation == null)
                        {
                            MessageBox.Show("Sorry, you haven't accepted the privacy policy for voice recognition yet " +
                                            "so I can't help you.");
                        }
                        else
                        {
                            MessageBox.Show("Sorry, there was a problem recognizing your search request: " +
                                            operation.ErrorCode.Message);
                        }
                        break;

                    default:
                        break;
                    }
                });
            });
        }
Esempio n. 7
0
        /// <summary>
        /// Initializes the Speech Recognizer object and its completion handler, used for subsequent reco operations.
        /// </summary>
        private async void InitializeRecognizer()
        {
            this.Recognizer = new SpeechRecognizer();
            this.Recognizer.Grammars.AddGrammarFromPredefinedType("search", SpeechPredefinedGrammar.WebSearch);
            await this.Recognizer.PreloadGrammarsAsync();

            recoCompletedAction = new AsyncOperationCompletedHandler <SpeechRecognitionResult>((operation, asyncStatus) =>
            {
                Dispatcher.BeginInvoke(() =>
                {
                    this.CurrentRecognizerOperation = null;
                    bool recognitionSuccessful      = false;

                    switch (asyncStatus)
                    {
                    case AsyncStatus.Completed:
                        SpeechRecognitionResult result = operation.GetResults();
                        if (!String.IsNullOrEmpty(result.Text))
                        {
                            recognitionSuccessful = true;
                            StartSearchQueryNavigation(result.Text, true);
                        }
                        break;

                    case AsyncStatus.Error:
                        MessageBox.Show(String.Format(
                                            AppResources.SpeechRecognitionErrorTemplate,
                                            operation.ErrorCode.HResult,
                                            operation.ErrorCode.Message));
                        break;

                    default:
                        break;
                    }

                    if (!recognitionSuccessful)
                    {
                        // For errors and cancellations, we'll revert back to the starting state
                        RestoreDefaultSearchText();
                        SetSearchState(SearchState.ReadyForInput);
                    }
                });
            });
        }
Esempio n. 8
0
        internal static Task <T> AsTask <T>(this IAsyncOperation <T> operation, CancellationToken cancellationToken = default(CancellationToken))
        {
            var registration = cancellationToken.Register(operation.Cancel);
            var tcs          = new TaskCompletionSource <T>();
            AsyncOperationCompletedHandler <T> completionHandler = (op, status) =>
            {
                registration.Dispose();
                switch (status)
                {
                case AsyncStatus.Canceled:
                    tcs.TrySetCanceled();
                    break;

                case AsyncStatus.Completed:
                    tcs.TrySetResult(op.GetResults());
                    break;

                case AsyncStatus.Error:
                    tcs.TrySetException(op.ErrorCode);
                    break;

                default:
                    tcs.TrySetException(new InvalidOperationException("Unexpected async operation transition"));
                    break;
                }
            };

            var oldCompleted = operation.Completed;

            operation.Completed = new AsyncOperationCompletedHandler <T>((op, status) =>
            {
                completionHandler(op, status);
                oldCompleted?.Invoke(op, status);
            });

            if (operation.Status != AsyncStatus.Started)
            {
                completionHandler(operation, operation.Status);
            }

            return(tcs.Task);
        }
Esempio n. 9
0
    /// <summary>
    /// Called when receive data has completed.
    /// </summary>
    /// <param name="operation">Data about the async operation.</param>
    /// <param name="status">The status of the operation.</param>
    public void DataReadHandler(IAsyncOperation <uint> operation, AsyncStatus status)
    {
        // If we failed, requeue the data and set the deferral time.
        if (status == AsyncStatus.Error)
        {
            // didn't load data
            Debug.Log("Failed to load new data");
        }
        else
        {
            // Read from the stream all the entries of the transform Matrix H.
            H.m00 = socketDataReader.ReadSingle();
            H.m01 = socketDataReader.ReadSingle();
            H.m02 = socketDataReader.ReadSingle();
            H.m03 = socketDataReader.ReadSingle();
            H.m10 = socketDataReader.ReadSingle();
            H.m11 = socketDataReader.ReadSingle();
            H.m12 = socketDataReader.ReadSingle();
            H.m13 = socketDataReader.ReadSingle();
            H.m20 = socketDataReader.ReadSingle();
            H.m21 = socketDataReader.ReadSingle();
            H.m22 = socketDataReader.ReadSingle();
            H.m23 = socketDataReader.ReadSingle();
            H.m30 = socketDataReader.ReadSingle();
            H.m31 = socketDataReader.ReadSingle();
            H.m32 = socketDataReader.ReadSingle();
            H.m33 = socketDataReader.ReadSingle();

            objAlg.updatePoseMatrix(H);

            interlock = 1;


            //restart reading data in the input stream async
            DataReaderLoadOperation outstandingRead    = socketDataReader.LoadAsync(4 * 16);
            AsyncOperationCompletedHandler <uint> aoch = new AsyncOperationCompletedHandler <uint>(DataReadHandler);
            outstandingRead.Completed = aoch;
        }
    }
 internal override void OnCompleted(AsyncOperationCompletedHandler <TResult> userCompletionHandler, AsyncStatus asyncStatus)
 {
     Debug.Assert(userCompletionHandler != null);
     userCompletionHandler(this, asyncStatus);
 }
        /// <summary>
        /// Initializes the Speech Recognizer object and its completion handler, used for subsequent reco operations.
        /// </summary>
        private async void InitializeRecognizer()
        {
            this.Recognizer = new SpeechRecognizer();
            this.Recognizer.Grammars.AddGrammarFromPredefinedType("search", SpeechPredefinedGrammar.WebSearch);
            await this.Recognizer.PreloadGrammarsAsync();

            recoCompletedAction = new AsyncOperationCompletedHandler<SpeechRecognitionResult>((operation, asyncStatus) =>
            {
                Dispatcher.BeginInvoke(() =>
                {
                    this.CurrentRecognizerOperation = null;
                    bool recognitionSuccessful = false;
                    try
                    {

                        switch (asyncStatus)
                        {
                            case AsyncStatus.Completed:
                                SpeechRecognitionResult result = operation.GetResults();
                                if (!String.IsNullOrEmpty(result.Text))
                                {
                                    recognitionSuccessful = true;
                                    StartSearchQueryNavigation(result.Text, true);
                                }
                                break;
                            case AsyncStatus.Error:
                                MessageBox.Show(String.Format(
                                    AppResources.SpeechRecognitionErrorTemplate,
                                    operation.ErrorCode.HResult,
                                    operation.ErrorCode.Message));
                                break;
                            default:
                                break;
                        }
                    }
                    catch (Exception)
                    {
                    }
                    if (!recognitionSuccessful)
                    {
                        // For errors and cancellations, we'll revert back to the starting state
                        RestoreDefaultSearchText();
                        SetSearchState(SearchState.ReadyForInput);
                    }
                });
            });

        }
Esempio n. 12
0
 private void SendBytes(byte[] reg, AsyncOperationCompletedHandler<uint> completed)
 {
     Writer.WriteBytes(reg);
     var storeResult = Writer.StoreAsync();
     if (completed != null)
     {
         storeResult.Completed += completed;
     }
 }