Esempio n. 1
0
        static void ReceiveCommands(DeviceClient deviceClient)
        {
            Debug.Print("Device waiting for commands from IoTHub...");
            Message receivedMessage;
            string  messageData;

            while (true)
            {
                receivedMessage = deviceClient.Receive();

                if (receivedMessage != null)
                {
                    StringBuilder sb = new StringBuilder();

                    foreach (byte b in receivedMessage.GetBytes())
                    {
                        sb.Append((char)b);
                    }

                    messageData = sb.ToString();

                    // dispose string builder
                    sb = null;

                    Debug.Print(DateTime.Now.ToLocalTime() + "> Received message: " + messageData);

                    deviceClient.Complete(receivedMessage);
                }

                Thread.Sleep(10000);
            }
        }
Esempio n. 2
0
        private void ReceiveCommands()
        {
            new Thread(() =>
            {
                Hashtable commandColors = new Hashtable();
                commandColors.Add("black", PinKit.BoardFullColorLED.Colors.Black);
                commandColors.Add("red", PinKit.BoardFullColorLED.Colors.Red);
                commandColors.Add("green", PinKit.BoardFullColorLED.Colors.Green);
                commandColors.Add("yellow", PinKit.BoardFullColorLED.Colors.Yellow);
                commandColors.Add("blue", PinKit.BoardFullColorLED.Colors.Blue);
                commandColors.Add("magenta", PinKit.BoardFullColorLED.Colors.Magenta);
                commandColors.Add("cyan", PinKit.BoardFullColorLED.Colors.Cyan);
                commandColors.Add("white", PinKit.BoardFullColorLED.Colors.White);

                Debug.Print("Start to receive command...");
                while (true)
                {
                    try
                    {
                        var receivedMessage = deviceClient.Receive();
                        if (receivedMessage != null)
                        {
                            var buf = receivedMessage.GetBytes();
                            if (buf != null && buf.Length > 0)
                            {
                                var content = new string(System.Text.UTF8Encoding.UTF8.GetChars(buf));
                                Debug.Print(DateTime.Now.ToLocalTime() + "> Received Message:" + content);

                                deviceClient.Complete(receivedMessage);

                                string lContent = content.ToLower();
                                foreach (var ccKey in commandColors.Keys)
                                {
                                    if (lContent.IndexOf((string)ccKey) > 0)
                                    {
                                        pinkit.LED.SetColor((PinKit.BoardFullColorLED.Colors)commandColors[ccKey]);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.Print("Receive Error - " + ex.Message);
                        BlinkPinKitLED(PinKit.BoardFullColorLED.Colors.Yellow, 1000, 500, 5);
                        break;
                    }
                }
            }).Start();
        }
Esempio n. 3
0
        static void ReceiveCommands(DeviceClient deviceClient)
        {
            Debug.Print("Device waiting for commands from IoTHub...");
            Message receivedMessage;
            string  messageData;

            while (true)
            {
                receivedMessage = deviceClient.Receive();

                if (receivedMessage != null)
                {
                    StringBuilder sb = new StringBuilder();

                    foreach (byte b in receivedMessage.GetBytes())
                    {
                        sb.Append((char)b);
                    }

                    messageData = sb.ToString();

                    // dispose string builder
                    sb = null;

                    Debug.Print(DateTime.Now.ToLocalTime() + "> Received message: " + messageData);

                    deviceClient.Complete(receivedMessage);
                }

                //  Note: In this sample, the polling interval is set to
                //  10 seconds to enable you to see messages as they are sent.
                //  To enable an IoT solution to scale, you should extend this //  interval. For example, to scale to 1 million devices, set
                //  the polling interval to 25 minutes.
                //  For further information, see
                //  https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging
                Thread.Sleep(10000);
            }
        }