Exemple #1
0
        async Task SendEvent()
        {
#if (ACCESS_IOT_HUB)
            List <SensorReadingBuffer> currentReadings = new List <SensorReadingBuffer>();
            lock (this)
            {
                foreach (var r in lastSensorReading)
                {
                    currentReadings.Add(new SensorReadingBuffer()
                    {
                        AccelX      = r.AccelX,
                        AccelY      = r.AccelY,
                        AccelZ      = r.AccelZ,
                        Temperature = r.Temperature,
                        Timestamp   = r.Timestamp
                    });
                }
                lastSensorReading.Clear();
            }
            Debug.WriteLine("Device sending {0} messages to IoTHub...\n", currentReadings.Count);

            try
            {
                List <Models.SensorReading> sendingBuffers = new List <Models.SensorReading>();
                for (int count = 0; count < currentReadings.Count; count++)
                {
                    var sensorReading = new Models.SensorReading()
                    {
                        accelx    = currentReadings[count].AccelX,
                        accely    = currentReadings[count].AccelY,
                        accelz    = currentReadings[count].AccelZ,
                        temp      = currentReadings[count].Temperature,
                        time      = currentReadings[count].Timestamp,
                        Longitude = MSIoTKiTHoLJP.IoTHoLConfig.Longitude,
                        Latitude  = MSIoTKiTHoLJP.IoTHoLConfig.Latitude
                    };
                    sendingBuffers.Add(sensorReading);
                }
                var     payload      = JsonConvert.SerializeObject(sendingBuffers);
                Message eventMessage = new Message(Encoding.UTF8.GetBytes(payload));
                Debug.WriteLine("\t{0}> Sending message: {1}, Data: [{2}]", DateTime.Now.ToLocalTime(), currentReadings.Count, payload);

                await deviceClient.SendEventAsync(eventMessage);

                tbSendStatus.Text = "Send[" + sendCount++ + "]@" + DateTime.Now.ToString();
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
            }
#endif
        }
        async Task SendEvent()
        {
#if (ACCESS_IOT_HUB)
            List<SensorReadingBuffer> currentReadings = new List<SensorReadingBuffer>();
            lock (this)
            {
                foreach (var r in lastSensorReading)
                {
                    currentReadings.Add(new SensorReadingBuffer()
                    {
                        AccelX = r.AccelX,
                        AccelY = r.AccelY,
                        AccelZ = r.AccelZ,
                        Temperature = r.Temperature,
                        Timestamp = r.Timestamp
                    });
                }
                lastSensorReading.Clear();
            }
            Debug.WriteLine("Device sending {0} messages to IoTHub...\n", currentReadings.Count);

            try
            {
                List<Models.SensorReading> sendingBuffers = new List<Models.SensorReading>();
                for (int count = 0; count < currentReadings.Count; count++)
                {
                    var sensorReading = new Models.SensorReading()
                    {
                        msgId = deviceId.ToString() + currentReadings[count].Timestamp.ToString("yyyyMMddHHmmssfff"),
                        accelx = currentReadings[count].AccelX,
                        accely = currentReadings[count].AccelY,
                        accelz = currentReadings[count].AccelZ,
                        deviceId = deviceId.ToString(),
                        temp = currentReadings[count].Temperature,
                        time = currentReadings[count].Timestamp,
                        Longitude = MSIoTKiTHoLJP.IoTHoLConfig.Longitude,
                        Latitude = MSIoTKiTHoLJP.IoTHoLConfig.Latitude
                    };
                    sendingBuffers.Add(sensorReading);
                }
                var payload = JsonConvert.SerializeObject(sendingBuffers);
                Message eventMessage = new Message(Encoding.UTF8.GetBytes(payload));
                Debug.WriteLine("\t{0}> Sending message: {1}, Data: [{2}]", DateTime.Now.ToLocalTime(), currentReadings.Count, payload);

                await deviceClient.SendEventAsync(eventMessage);
                tbSendStatus.Text = "Send[" + sendCount++ + "]@" + DateTime.Now.ToString();
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
            }
#endif
        }