Hands-on lab/lab-files/starter-project/SmartMeterSimulator/Sensor.cs
/// <summary>
/// Check for new messages sent to this device through IoT Hub.
/// </summary>
Check for new messages sent to this device through IoT Hub.
Check for new messages sent to this device through IoT Hub .
public async void ReceiveMessageAsync()
        {
            try
            {
                var receivedMessage = await _DeviceClient?.ReceiveAsync();
                if (receivedMessage == null)
                {
                    ReceivedMessage = null;
                    return;
                }
                if(double.TryParse(ReceivedMessage, out var requestedTemperature))
                {
                    ReceivedTemperatureSetting = requestedTemperature;
                }
                else
                {
                    ReceivedTemperatureSetting = null;
                }
            }
            catch (NullReferenceException ex)
            {
                System.Diagnostics.Debug.WriteLine("The DeviceClient is null. This is likely due to it being disconnected since the ReceiveMessageAsync message was called.");
            }
        }
public async void ReceiveMessageAsync ( ) { try { var receivedMessage = await _DeviceClient ? . ReceiveAsync ( ) ; if ( receivedMessage == null ) { ReceivedMessage = null ; return ; } if ( double . TryParse ( ReceivedMessage , out var requestedTemperature ) ) { ReceivedTemperatureSetting = requestedTemperature ; } else { ReceivedTemperatureSetting = null ; } } catch ( NullReferenceException ex ) { System . Diagnostics . Debug . WriteLine ( " " ) ; } }
Check for new messages sent to this device through IoT Hub.
Check for new messages sent to this device through IoT Hub .
// Receive a message from IoT Hub //TODO: 21.Set the received message for this sensor to the string value of the message byte array //ReceivedMessage = Encoding.ASCII.GetString(...); // Send acknowledgement to IoT Hub that the has been successfully processed. // The message can be safely removed from the device queue. If something happened // that prevented the device app from completing the processing of the message, // IoT Hub delivers it again. //TODO: 22.Send acknowledgement to IoT hub that the message was processed //await _DeviceClient?.CompleteAsync(...); // The device client is null, likely due to it being disconnected since this method was called.