public bool Run( ) { AzurePrepInputs inputs; if (!GetInputs(out inputs)) { Console.WriteLine("Error while getting inputs."); Console.WriteLine("Press Enter to continue..."); Console.ReadLine( ); return(false); } AzurePrepOutputs createResults = CreateEventHub(inputs); if (createResults == null) { Console.WriteLine("Error while creating Event Hubs."); Console.WriteLine("Press Enter to continue..."); Console.ReadLine( ); return(false); } for ( ;;) { Console.WriteLine("Do you want to create Stream Analytics jobs? (y/n)"); string answer = Console.ReadLine( ); bool create; string request; if (!string.IsNullOrEmpty(answer) && answer.ToLower( ).StartsWith("y")) { create = true; request = ""; } else { create = false; request = "do not "; } if (ConsoleHelper.Confirm("Are you sure you " + request + "want to create Stream Analytics jobs?")) { if (create) { CreateStreamAnalyticsJobs(inputs, createResults); } break; } } #region print results _ConsoleBuffer.Add(""); _ConsoleBuffer.Add("Service Bus management connection string (i.e. for use in Service Bus Explorer):"); _ConsoleBuffer.Add(createResults.nsConnectionString); _ConsoleBuffer.Add(""); _ConsoleBuffer.Add("Device AMQP address strings (for Raspberry PI/devices):"); for (int i = 1; i <= 4; i++) { var deviceKeyName = String.Format("D{0}", i); var deviceKey = (createResults.ehDevices.Authorization.First((d) => String.Equals(d.KeyName, deviceKeyName, StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey; _ConsoleBuffer.Add(string.Format("amqps://{0}:{1}@{2}.servicebus.windows.net", deviceKeyName, Uri.EscapeDataString(deviceKey), createResults.SBNamespace)); } Console.WriteLine(""); Console.WriteLine(""); string fileName = createResults.SBNamespace + DateTime.UtcNow.ToString("_d_MMM_h_mm") + ".log"; string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string fileFullName = filePath + @"\" + fileName; if (_ConsoleBuffer.FlushToFile(fileFullName)) { Console.WriteLine("Output was saved to your desktop, at " + fileFullName + " file."); } Console.WriteLine("Please hit enter to close."); Console.ReadLine( ); #endregion return(true); }
bool Run( ) { CloudWebDeployInputs inputs = null; if (!GetInputs(out inputs)) { return(false); } Console.WriteLine("Retrieving namespace metadata..."); // Create Namespace ServiceBusManagementClient sbMgmt = new ServiceBusManagementClient(inputs.Credentials); var nsDescription = sbMgmt.Namespaces.GetNamespaceDescription(inputs.SBNamespace); string nsConnectionString = nsDescription.NamespaceDescriptions.First( (d) => String.Equals(d.AuthorizationType, "SharedAccessAuthorization") ).ConnectionString; NamespaceManager nsManager = NamespaceManager.CreateFromConnectionString(nsConnectionString); EventHubDescription ehDevices = AzureConsoleHelper.SelectEventHub(nsManager, inputs.Credentials); var serviceNamespace = inputs.SBNamespace; var hubName = ehDevices.Path; var sharedAccessAuthorizationRule = ehDevices.Authorization.FirstOrDefault((d) => d.Rights.Contains(AccessRights.Listen)) as SharedAccessAuthorizationRule; if (sharedAccessAuthorizationRule == null) { Console.WriteLine("Cannot locate Authorization rule for WebSite key."); return(false); } var receiverKeyName = sharedAccessAuthorizationRule.KeyName; var receiverKey = sharedAccessAuthorizationRule.PrimaryKey; //Console.WriteLine("Starting temperature processor with {0} partitions.", partitionCount); CancellationTokenSource cts = new CancellationTokenSource( ); int closedReceivers = 0; AutoResetEvent receiversStopped = new AutoResetEvent(false); MessagingFactory factory = MessagingFactory.Create(ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, ""), new MessagingFactorySettings { TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(receiverKeyName, receiverKey), TransportType = TransportType.Amqp }); EventHubClient eventHubClient = factory.CreateEventHubClient(hubName); EventHubConsumerGroup eventHubConsumerGroup = eventHubClient.GetDefaultConsumerGroup( ); int partitionCount = ehDevices.PartitionCount; for (int i = 0; i < partitionCount; i++) { Task.Factory.StartNew((state) => { try { _ConsoleBuffer.Add(string.Format("Starting worker to process partition: {0}", state)); var receiver = eventHubConsumerGroup.CreateReceiver(state.ToString( ), DateTime.UtcNow); _ConsoleBuffer.Add(string.Format("Waiting for start receiving messages: {0} ...", state)); while (true) { // Receive could fail, I would need a retry policy etc... var messages = receiver.Receive(10); foreach (var message in messages) { //var eventBody = Newtonsoft.Json.JsonConvert.DeserializeObject<TemperatureEvent>(Encoding.Default.GetString(message.GetBytes())); //Console.WriteLine("{0} [{1}] Temperature: {2}", DateTime.Now, message.PartitionKey, eventBody.Temperature); _ConsoleBuffer.Add(message.PartitionKey + " sent message:" + Encoding.Default.GetString(message.GetBytes( ))); } if (cts.IsCancellationRequested) { Console.WriteLine("Stopping: {0}", state); receiver.Close( ); if (Interlocked.Increment(ref closedReceivers) >= partitionCount) { receiversStopped.Set(); } break; } } } catch (Exception ex) { _ConsoleBuffer.Add(ex.Message); } }, i); } Console.ReadLine( ); cts.Cancel( ); //waiting for all receivers to stop receiversStopped.WaitOne( ); bool saveToFile; for ( ;;) { Console.WriteLine("Do you want to save received data to file? (y/n)"); string answer = Console.ReadLine( ); string request = "do not"; saveToFile = false; if (!string.IsNullOrEmpty(answer) && answer.ToLower( ).StartsWith("y")) { saveToFile = true; request = ""; } if (ConsoleHelper.Confirm("Are you sure you " + request + " want to save received data?")) { break; } } if (saveToFile) { string fileName = inputs.SBNamespace + DateTime.UtcNow.ToString("_d_MMM_h_mm") + ".log"; string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string fileFullName = filePath + @"\" + fileName; if (_ConsoleBuffer.FlushToFile(fileFullName)) { Console.WriteLine("Output was saved to your desktop, at " + fileFullName + " file."); } } Console.WriteLine("Wait for all receivers to close and then press ENTER."); Console.ReadLine( ); return(true); }
public bool Run( ) { AzurePrepInputs inputs; if (!GetInputs(out inputs)) { Console.WriteLine("Error while getting inputs."); Console.WriteLine("Press Enter to continue..."); Console.ReadLine( ); return(false); } AzurePrepOutputs createResults = CreateEventHub(inputs); if (createResults == null) { Console.WriteLine("Error while creating Event Hubs."); Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); return(false); } #region print results _ConsoleBuffer.Add(""); _ConsoleBuffer.Add("Service Bus management connection string (i.e. for use in Service Bus Explorer):"); _ConsoleBuffer.Add(createResults.nsConnectionString); _ConsoleBuffer.Add(""); _ConsoleBuffer.Add("Device AMQP address strings (for Raspberry PI/devices):"); for (int i = 1; i <= 4; i++) { var deviceKeyName = String.Format("D{0}", i); var deviceKey = (createResults.ehDevices.Authorization.First((d) => String.Equals(d.KeyName, deviceKeyName, StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey; _ConsoleBuffer.Add(string.Format("amqps://{0}:{1}@{2}.servicebus.windows.net", deviceKeyName, Uri.EscapeDataString(deviceKey), createResults.SBNamespace)); } Console.WriteLine(""); Console.WriteLine(""); string fileName = createResults.SBNamespace + DateTime.UtcNow.ToString("_d_MMM_h_mm") + ".log"; string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string fileFullName = filePath + @"\" + fileName; if (_ConsoleBuffer.FlushToFile(fileFullName)) { Console.WriteLine("Output was saved to your desktop, at " + fileFullName + " file."); } Console.WriteLine("Please hit enter to close."); Console.ReadLine( ); #endregion #if AZURESTREAMANALYTICS // Create StreamAnalyticsJobs + inputs + outputs + enter keys // Untested code. May require AAD authentication, no support for management cert? // Create Resource Group for the Stream Analytics jobs var groupCreateRequest = WebRequest.Create(String.Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}?api-version=2014-04-01-preview", SubscriptionId, StreamAnalyticsGroup)) as HttpWebRequest; groupCreateRequest.ClientCertificates.Add(creds.ManagementCertificate); groupCreateRequest.ContentType = "application/json"; groupCreateRequest.Method = "PUT"; groupCreateRequest.KeepAlive = true; var bytesGroup = Encoding.UTF8.GetBytes("{\"location\":\"Central US\"}"); groupCreateRequest.ContentLength = bytesGroup.Length; groupCreateRequest.GetRequestStream().Write(bytesGroup, 0, bytesGroup.Length); var groupCreateResponse = groupCreateRequest.GetResponse(); //var streamMgmt = new ManagementClient(creds); //, new Uri("https://management.azure.com")); //HttpClient client = streamMgmt.HttpClient; var createJob = new StreamAnalyticsJob() { location = Location, inputs = new List <StreamAnalyticsEntity> { new StreamAnalyticsEntity { name = "devicesInput", properties = new Dictionary <string, object> { { "type", "stream" }, { "serialization", new Dictionary <string, object> { { "type", "JSON" }, { "properties", new Dictionary <string, object> { { "encoding", "UTF8" }, } } } }, { "datasource", new Dictionary <string, object> { { "type", "Microsoft.ServiceBus/EventHub" }, { "properties", new Dictionary <string, object> { { "eventHubNamespace", Namespace }, { "eventHubName", EventHubDevices }, { "sharedAccessPolicyName", "StreamingAnalytics" }, { "sharedAccessPolicyKey", (ehDevices.Authorization.First((d) => String.Equals(d.KeyName, "StreamingAnalytics", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey }, } } } } }, }, }, transformation = new StreamAnalyticsEntity() { name = "Aggregates", properties = new Dictionary <string, object> { { "streamingUnits", 1 }, { "query", "select * from devicesInput" }, } }, outputs = new List <StreamAnalyticsEntity> { new StreamAnalyticsEntity { name = "output", properties = new Dictionary <string, object> { { "datasource", new Dictionary <string, object> { { "type", "Microsoft.ServiceBus/EventHub" }, { "properties", new Dictionary <string, object> { { "eventHubNamespace", Namespace }, { "eventHubName", EventHubAlerts }, { "sharedAccessPolicyName", "StreamingAnalytics" }, { "sharedAccessPolicyKey", (ehAlerts.Authorization.First((d) => String.Equals(d.KeyName, "StreamingAnalytics", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey }, } } } }, { "serialization", new Dictionary <string, object> { { "type", "JSON" }, { "properties", new Dictionary <string, object> { { "encoding", "UTF8" }, } } } }, }, }, } }; var jobCreateRequest = WebRequest.Create(String.Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}/Microsoft.StreamAnalytics/streamingjobs/{2}?api-version=2014-10-01", SubscriptionId, StreamAnalyticsGroup, JobAggregates)) as HttpWebRequest; jobCreateRequest.ClientCertificates.Add(creds.ManagementCertificate); jobCreateRequest.ContentType = "application/json"; jobCreateRequest.Method = "PUT"; jobCreateRequest.KeepAlive = true; var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(createJob)); jobCreateRequest.ContentLength = bytes.Length; jobCreateRequest.GetRequestStream().Write(bytes, 0, bytes.Length); var jobCreateResponse = jobCreateRequest.GetResponse(); //var jobCreateTask = streamMgmt.HttpClient.PutAsync( // String.Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}/Microsoft.StreamAnalytics/streamingjobs/{2}?api-version=2014-10-01", // SubscriptionId, StreamAnalyticsGroup, JobAggregates), // new StringContent(JsonConvert.SerializeObject(createJob))); //jobCreateTask.Wait(); //var jobCreateResponse = jobCreateTask.Result; #endif return(true); }