示例#1
0
        private void TDRawDeviceEvent(IntPtr data, int controllerId, int callbackId, IntPtr context)
        {
            string parsedData = Marshal.PtrToStringAnsi(data);

            if (string.IsNullOrWhiteSpace(parsedData))
            {
                return;
            }

            RawCommandReceivedEventArgs eventArgs = new RawCommandReceivedEventArgs(controllerId, parsedData);

            RawCommandReceived.Trigger(this, eventArgs);
        }
        /// <summary>
        /// Runs the Communication Session
        /// </summary>
        /// <param name="cancellationToken">The Cancellation Token</param>
        /// <returns>The executing task</returns>
        public async Task RunAsync(CancellationToken cancellationToken)
        {
            // Make sure the session is not a closed session
            if (this.Status == SessionStatus.Stopped)
            {
                throw new InvalidOperationException("Session is already stopped and cannot be reused");
            }

            // Set the Status to Running
            this.Status = SessionStatus.Running;

            // Output Welcome Message
            await ReplyAsync(cancellationToken, "Welcome!");

            if (ShowPrompt)
            {
                await PrintPromptAsync(cancellationToken);
            }

            // Listen to following session requests
            while (cancellationToken.IsCancellationRequested == false)
            {
                // Retrieve the command input
                var command = await ReadCommandAsync(cancellationToken).ConfigureAwait(false);

                if (command == null)
                {
                    break;
                }

                // Process Internal Commands (since there are very few commands, they are hard coded)
                var commandUpper = command.ToUpper();

                if (commandUpper == "SET TIMER ON")
                {
                    ShowTimer = true;
                }
                else if (commandUpper == "SET TIMER OFF")
                {
                    ShowTimer = false;
                }
                else if (commandUpper == "SET PROMPT ON")
                {
                    ShowPrompt = true;
                }
                else if (commandUpper == "SET PROMPT OFF")
                {
                    ShowPrompt = false;
                }
                else if (commandUpper == "QUIT")
                {
                    break;
                }
                else
                {
                    // Other commands, process properly

                    // Raise Command Received Event
                    var commandEventArgs = new RawCommandInputReceivedEventArgs(command);

                    // Create the Processing Signal
                    CancellationTokenSource tokenSource = new CancellationTokenSource();
                    CancellationToken       token       = tokenSource.Token;

                    var taskProcessing = new Task(async() =>
                    {
                        var capturedToken = token;
                        var characters    = new char[] { '|', '/', '-', '\\' };
                        int currentChar   = 0;

                        while (token.IsCancellationRequested == false)
                        {
                            await ReplyAsync(cancellationToken, "\r" + characters[currentChar], true, false);
                            currentChar++;
                            if (currentChar == 4)
                            {
                                currentChar = 0;
                            }

                            await Task.Delay(250);
                        }
                    }, token);

                    if (ShowTimer)
                    {
                        taskProcessing.Start();
                    }

                    RawCommandReceived?.Invoke(this, commandEventArgs);

                    // Cancel the processing thing
                    if (ShowTimer)
                    {
                        tokenSource.Cancel();
                    }

                    // Reply to command
                    if (String.IsNullOrEmpty(commandEventArgs.Response))
                    {
                        await ReplyAsync(cancellationToken, "\rNo Response");
                    }
                    else
                    {
                        await ReplyAsync(cancellationToken, "\r" + commandEventArgs.Response);
                    }
                }

                // Display Prompt for Next Command
                if (ShowPrompt)
                {
                    await PrintPromptAsync(cancellationToken);
                }
            }

            // Stop Session
            this.Stop();
        }