Ejemplo n.º 1
0
        /// <summary>
        /// Sends Microsoft Band Telemetry in JSON format to Azure IoT Hub via AMQP
        /// </summary>
        /// <param name="bandTelemetry"></param>
        static public void SendMicrosoftBandTelemetry(MicrosoftBandTelemetry bandTelemetry)
        {
            string entity = Fx.Format("/devices/{0}/messages/events", DEVICE_ID);
            string eventData;

            SenderLink senderLink = new SenderLink(session, "sender-link", entity);

            if (String.IsNullOrEmpty(bandTelemetry.DeviceId))
            {
                bandTelemetry.DeviceId = DEVICE_ID;
            }

            if (serializer == null)
            {
                serializer = new JsonSerializer();
                serializer.NullValueHandling = NullValueHandling.Ignore;
            }


            using (StringWriter sw = new StringWriter())
                using (JsonWriter writer = new JsonTextWriter(sw))
                {
                    serializer.Serialize(writer, bandTelemetry);
                    eventData = sw.ToString();
                }


            var     messageValue = Encoding.UTF8.GetBytes(eventData);
            Message message      = new Message()
            {
                BodySection = new Data()
                {
                    Binary = messageValue
                }
            };

            senderLink.Send(message);
            senderLink.Close();
        }
        /// <summary>
        /// Sends Device-to-Cloud Message to IoT Hub. Defaults to HTTPS protocol
        /// </summary>
        /// <param name="body"></param>
        public async void SendIoTHubMessage(MicrosoftBandTelemetry bandTelemetry)
        {
            string eventData;

            if (String.IsNullOrEmpty(bandTelemetry.DeviceId))
            {
                bandTelemetry.DeviceId = this.DeviceID;
            }

            if (serializer == null)
            {
                serializer = new JsonSerializer();
                serializer.NullValueHandling = NullValueHandling.Ignore;
            }


            using (StringWriter sw = new StringWriter())
                using (JsonWriter writer = new JsonTextWriter(sw))
                {
                    serializer.Serialize(writer, bandTelemetry);
                    eventData = sw.ToString();
                }
            this.SendIoTHubMessage(eventData);
        }