Пример #1
0
        // Async method to send simulated telemetry
        private static async Task SendDeviceToCloudMessagesAsync()
        {
            // Initial telemetry values
            double minTemperature = 20;
            double minHumidity    = 60;
            Random rand           = new Random();

            ContinueLoop = true;
            while (ContinueLoop)
            {
                double currentTemperature = minTemperature + rand.NextDouble() * 15;
                double currentHumidity    = minHumidity + rand.NextDouble() * 20;

                var telemetryDataPoint = await Weather.GetWeatherObj();

                //Create JSON message
                //var telemetryDataPoint = new
                //{
                //    temperature = currentTemperature,
                //    humidity = currentHumidity
                //};
                MessageString = JsonConvert.SerializeObject(telemetryDataPoint);

                Message = new Message(Encoding.ASCII.GetBytes(MessageString));

                //Stuff:
                //var mess2 = Encoding.ASCII.GetBytes(MessageString);
                //var qwe = Message.GetBytes();
                //string MessageString2= Encoding.UTF8.GetString(qwe, 0, qwe.Length);
                // 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");
                Message.Properties.Add("temperatureAlert2", (currentTemperature > 40) ? "true" : "false");
                AzIoTHubModules.SyntheticIoTMessage iotmessage = new AzIoTHubModules.SyntheticIoTMessage(Message);
                MessageString = iotmessage.Serialise();



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

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

                    await Task.Delay(Delay);
                }
                else
                {
                    ContinueLoop = false;
                }
                Weather.GetNextCity();
            }
        }
Пример #2
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);
     }
 }