WindowsIoTCorePi2FezHat.ConnectTheDotsHelper.ReceiveMessage C# (CSharp) Method

ReceiveMessage() public method

public ReceiveMessage ( ) : Task
return Task
        public async Task<string> ReceiveMessage()
        {
            if (this.HubConnectionInitialized)
            {
                try
                {
                    var receivedMessage = await this.deviceClient.ReceiveAsync();

                    if (receivedMessage != null)
                    {
                        var messageData = Encoding.ASCII.GetString(receivedMessage.GetBytes());
                        this.deviceClient.CompleteAsync(receivedMessage);
                        return messageData;
                    }
                    else
                    {
                        return string.Empty;
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Exception when receiving message:" + e.Message);
                    return string.Empty;
                }
            }
            else
            {
                return string.Empty;
            }
        }

Usage Example

Example #1
0
        private async void CommandsTimer_Tick(object sender, object e)
        {
            string message = await ctdHelper.ReceiveMessage();

            if (message != string.Empty)
            {
                System.Diagnostics.Debug.WriteLine("Command Received: {0}", message);
                switch (message.ToUpperInvariant())
                {
                case "RED":
                    hat.D2.Color = new FEZHAT.Color(255, 0, 0);
                    break;

                case "GREEN":
                    hat.D2.Color = new FEZHAT.Color(0, 255, 0);
                    break;

                case "BLUE":
                    hat.D2.Color = new FEZHAT.Color(0, 0, 255);
                    break;

                case "OFF":
                    hat.D2.TurnOff();
                    break;

                default:
                    System.Diagnostics.Debug.WriteLine("Unrecognized command: {0}", message);
                    break;
                }
            }
        }