Ejemplo n.º 1
0
        private void SendData()
        {
            try
            {
                string SharedAccessKeyName = "RootManageSharedAccessKey";
                string SharedAccessKey = "5E314QltYvjm8uWcy13CT/mMllKgvfnI2mq0WOr0gNw=";
                string Namespace = "MyIoTDemo-ns";
                string EventHubName = "myiotdemoeventhub";

                //ResourceUtilizationMessage rm = new ResourceUtilizationMessage();
                //rm.CPULoad = GetCPUInfo();
                //rm.MemoryLoad = GetMemoryInfo();
                //rm.MessageNumber = MessageCounter++;

                //MemoryStream _Stream = new MemoryStream();
                //var _Serializer = new DataContractJsonSerializer(rm.GetType());
                //_Serializer.WriteObject(_Stream, rm);
                //_Stream.Position = 0;
                //StreamReader _Reader = new StreamReader(_Stream);
                //string jsonBody = _Reader.ReadToEnd();

                string jsonBody = "hello";

                string url = String.Format("https://{0}.servicebus.windows.net/{1}/messages?timeout=60", Namespace, EventHubName);
                string token = EHOpTokenProvider.GetSasToken(url, SharedAccessKeyName, SharedAccessKey);
                HttpClientUtility client = new HttpClientUtility(token);

                Uri uri = new Uri(url);
                client.SendMessage(uri, jsonBody);
            }

            catch (Exception ex)
            {
                string text = ex.ToString() + ex.InnerException.ToString();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Send data secure to the event hub by means of Shared Access Signature Authentication (SAS) Authentication.
        /// SAS authentication uses the following elements:
        /// <list type = "bullet" >
        ///     <item>
        ///         <term>SharedAccessAuthorizationRule</term>
        ///         <description>A 256-bit primary cryptographic key in Base64 representation, an optional secondary key, and a key name and associated rights(a collection of Listen, Send, or Manage rights)</description>
        ///     </item>
        /// </list>
        /// <list type = "bullet" >
        ///     <item>
        ///         <term>SharedAccessSignature token</term>
        ///         <description>Generated using the HMAC-SHA256 of a resource string, consisting of the URI of the resource that is accessed and an expiry, with the cryptographic key.The signature and other elements described in the following sections are formatted into a string to form the SharedAccessSignature token.</description>
        ///     </item>
        /// </list>
        /// </summary>
        private void SendData()
        {
            if (!ApiInformation.IsTypePresent(GpioPresentNS))
            {
                return;
            }

            ledPin?.Write(GpioPinValue.Low);

            try
            {

                // Setup variables for SAS Authentication - Retrieved from Azure Event Hub properties
                string SharedAccessKeyName = "Send";
                string SharedAccessKey = "SYP/xaZteVLAIzE9Nvh303fWODlibpIdT5Mj6njyd7E=";
                string Namespace = "MyIoTDemo-ns";
                string EventHubName = "myiotdemoeventhub";

                var payload = new Payload();
                var jsonBody = payload.GetAsJson();

                string url = String.Format("https://{0}.servicebus.windows.net/{1}/messages?timeout=60", Namespace, EventHubName);
                string token = SasTokenProvider.GetSasToken(url, SharedAccessKeyName, SharedAccessKey);
                HttpClientUtility client = new HttpClientUtility(token);

                Uri uri = new Uri(url);
                client.SendMessage(uri, jsonBody);
                Debug.WriteLine("Event published: " + jsonBody);
            }

            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString() + ex.InnerException.ToString());
            }

            ledPin?.Write(GpioPinValue.High);
        }