Beispiel #1
0
        private async Task Listen()
        {
            while (isListening)
            {
                Message receivedMessage = await deviceClient.ReceiveAsync();

                if (receivedMessage != null)
                {
                    var message = Encoding.UTF8.GetString(receivedMessage.GetBytes());

                    if (message.ToLower().Contains("ON"))
                    {
                        // ToggleCoffeeMakerRelay();
                    }
                    else if (message.ToLower().Contains("OFF"))
                    {
                        // ToggleCoffeeMakerRelay();
                    }

                    var offset       = receivedMessage.EnqueuedTimeUtc.DateTime - DateTime.Now;
                    var cloudMessage = new CloudMessage
                    {
                        Body      = message,
                        Timestamp = receivedMessage.EnqueuedTimeUtc.DateTime.Subtract(offset)
                    };
                    Messages.Add(cloudMessage);

                    await deviceClient.CompleteAsync(receivedMessage);
                }

                await Task.Delay(1000);
            }
        }
        private async Task Listen()
        {
			coffeeMakerRelay.Write(GpioPinValue.Low);
            coffeeMakerLED.Write(GpioPinValue.Low);

            while (isListening)
            {
                Message receivedMessage = await deviceClient.ReceiveAsync();
                if (receivedMessage != null)
                {
                    var message = Encoding.UTF8.GetString(receivedMessage.GetBytes());

                    if (message.ToLower().Contains("turn coffee maker on"))
                    {
                        ToggleCoffeeMakerRelay();
                    }
                    else if (message.ToLower().Contains("turn coffee maker off"))
                    {
                        ToggleCoffeeMakerRelay();
                    }

                    var offset = receivedMessage.EnqueuedTimeUtc.DateTime - DateTime.Now;
                    var cloudMessage = new CloudMessage
                    {
                        Body = message,
                        Timestamp = receivedMessage.EnqueuedTimeUtc.DateTime.Subtract(offset)
                    };
                    Messages.Add(cloudMessage);

                    await deviceClient.CompleteAsync(receivedMessage);
                }

                await Task.Delay(1000);
            }
        }