Beispiel #1
0
        // Async method to send simulated telemetry
        private static async Task SendDeviceToCloudMessagesAsync()
        {
            ContinueLoop = true;
            OnDeviceStatusUpdate?.Invoke("IoT Hub Telemetry - Device sending messages.");
            while (ContinueLoop)
            {
                Azure_IoTHub_Sensors.TelemetryDataPoint telemetryDataPoint;
                if (Azure_IoTHub_Sensors.Weather.CurrentWeather.DoAsync)
                {
                    telemetryDataPoint = await Azure_IoTHub_Sensors.Weather.CurrentWeather.GetWeatherAsync();
                }
                else
                {
                    telemetryDataPoint = Azure_IoTHub_Sensors.Weather.CurrentWeather.GetWeather();
                }

                MessageString = JsonConvert.SerializeObject(telemetryDataPoint);

                Message = new Message(Encoding.ASCII.GetBytes(MessageString));
                //Message.UserId = Azure_IoTHub_Connections.MyConnections.IoTHubName;
                Message.Properties.Add("temperatureAlert", (telemetryDataPoint.temperature > 30) ? "true" : "false");
                Message.Properties.Add("humidityAlert", (telemetryDataPoint.humidity > 80) ? "true" : "false");
                Message.Properties.Add("pressureAlert", (telemetryDataPoint.pressure > 1010) ? "true" : "false");
                Azure_IoTHub_Telemetry.SyntheticIoTMessage iotmessage = new Azure_IoTHub_Telemetry.SyntheticIoTMessage(Message);
                MessageString = iotmessage.Serialise();



                System.Diagnostics.Debug.WriteLine("{0} > Sending message: {1}", DateTime.Now, MessageString);
                SetDeviceSentMsg?.Invoke(string.Format("{0} > Sending message: {1}", DateTime.Now, MessageString));

                // Send the telemetry message
                if (!IsDeviceStreaming)
                {
                    await s_deviceClient.SendEventAsync(Message);

                    Delay = 1000 * Azure_IoTHub_Connections.MyConnections.TelemetryDelayBtwReadings;

                    await Task.Delay(Delay);

                    if (!ContinueLoop)
                    {
                        OnDeviceStatusUpdate?.Invoke("Cancelled Telemetry - Device end");
                    }
                }
                else
                {
                    ContinueLoop = false;
                }
            }
        }
        // Async method to send simulated telemetry
        private static async Task SendDeviceToCloudMessagesAsync()
        {
            // Initial telemetry values
            //double minTemperature = 20;
            //double minHumidity = 60;
            //Random rand = new Random();
            OnDeviceStatusUpdateD?.Invoke("IoT Hub Telemetry #2- Device sending messages.");
            ContinueLoop = true;
            while (ContinueLoop)
            {
                //double currentTemperature = minTemperature + rand.NextDouble() * 15;
                //double currentHumidity = minHumidity + rand.NextDouble() * 20;

                //// Create JSON message
                //var telemetryDataPoint = new
                //{
                //    temperature = currentTemperature,
                //    humidity = currentHumidity
                //};
                //var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
                //var message = new Message(Encoding.ASCII.GetBytes(messageString));

                //// Add a custom application property to the message.
                //// An IoT hub can filter on these properties without access to the message body.
                //message.Properties.Add("temperatureAlert", (currentTemperature > 30) ? "true" : "false");
                //await s_deviceClient.SendEventAsync(message);


                Azure_IoTHub_Sensors.TelemetryDataPoint telemetryDataPoint;

                if (Azure_IoTHub_Sensors.Weather.CurrentWeather.DoAsync)
                {
                    telemetryDataPoint = await Azure_IoTHub_Sensors.Weather.CurrentWeather.GetWeatherAsync();
                }
                else
                {
                    telemetryDataPoint = Azure_IoTHub_Sensors.Weather.CurrentWeather.GetWeather();
                }

                MessageString = JsonConvert.SerializeObject(telemetryDataPoint);

                Message = new Message(Encoding.ASCII.GetBytes(MessageString));
                //Message.UserId = Azure_IoTHub_Connections.MyConnections.IoTHubName;
                Message.Properties.Add("temperatureAlert", (telemetryDataPoint.temperature > 30) ? "true" : "false");
                Message.Properties.Add("humidityAlert", (telemetryDataPoint.humidity > 80) ? "true" : "false");
                Message.Properties.Add("pressureAlert", (telemetryDataPoint.pressure > 1010) ? "true" : "false");
                Azure_IoTHub_Telemetry.SyntheticIoTMessage iotmessage = new Azure_IoTHub_Telemetry.SyntheticIoTMessage(Message);
                MessageString = iotmessage.Serialise();

                SetDeviceSentMsg?.Invoke(string.Format("{0} > Sending message: {1}", DateTime.Now, MessageString));



                // Send the tlemetry message
                await s_deviceClient.SendEventAsync(Message);

                SetDeviceSentMsg?.Invoke(string.Format("{0} > Sending message: {1}", DateTime.Now, MessageString));

                s_telemetryInterval = Azure_IoTHub_Connections.MyConnections.TelemetryDelayBtwReadings;
                await Task.Delay(s_telemetryInterval * 1000);

                if (!ContinueLoop)
                {
                    OnDeviceStatusUpdateD?.Invoke("Cancelled Telemetry - Device end");
                }
            }
        }