Exemple #1
0
 public static SyntheticIoTMessage Deserialize(string msg)
 {
     try
     {
         object obj = JsonConvert.DeserializeObject(msg);
         SyntheticIoTMessage iMsg = JsonConvert.DeserializeObject <SyntheticIoTMessage>(msg);
         return((SyntheticIoTMessage)iMsg);
     }
     catch (Exception)
     {
         return(null);
     }
 }
Exemple #2
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;
                }
            }
        }