private ConsoleLog setupConsoleLog(CdsInfo cdsInfo) { CdsBackendSetting cdsBackendSetting = cdsInfo.cdsBackendSetting; return(new ConsoleLog(this.Context, new LogHelper( cdsBackendSetting.LogStorageConnectionString, cdsBackendSetting.LogStorageContainer, cdsBackendSetting.LogLevel), cdsInfo)); }
private CdsBackendSetting loadCdsBackendSettingFromDB() { CdsBackendSetting cdsBackendSetting = new CdsBackendSetting(); try { if (_ignoreCDS20DB) { cdsBackendSetting.LogStorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=cds20systemstoragedev;AccountKey=64SCgh/hP9PjCCY6YljAcf4E/kCCh58pUoscfzxnUYfNSwRw8ZYLgN+5wVgWUC5njqax6A1c1BK/GT45hNEwWQ==;EndpointSuffix=core.windows.net"; // "sfwebadminstorage" cdsBackendSetting.LogStorageContainer = "log-backend-iothubeventprocessor"; cdsBackendSetting.LogLevel = LogLevel.Info; cdsBackendSetting.ServiceBusConnectionString = "Endpoint=sb://sfservicebus.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=pO5mNKGGhR06BPKXhf8Dk7chLXm7TfjoQjF+8Zvw5XY="; cdsBackendSetting.ServiceBusEventActionQueue = "alarmops"; cdsBackendSetting.ServiceBusProvisionQueue = "infraops"; cdsBackendSetting.ServiceBusProcessCommandTopic = "processcommand"; cdsBackendSetting.RTMessageFeedInURL = "http://admin.dev.iot-cds.net/Monitor/RTMessageFeedIn"; cdsBackendSetting.SuperAdminHeartbeatURL = "http://superadmin.dev.iot-cds.net/Monitor/RTMessageFeedIn"; cdsBackendSetting.AdminHeartbeatURL = "http://admin.dev.iot-cds.net/Monitor/RTMessageFeedIn"; } else { /* Log */ cdsBackendSetting.LogStorageConnectionString = AzureSQLHelper.SystemConfigurationModel.GetCDSConfigValueByKey("SystemStorageConnectionString"); cdsBackendSetting.LogStorageContainer = AzureSQLHelper.SystemConfigurationModel.GetCDSConfigValueByKey("IoTHubReceiverLogStorageContainerApp"); cdsBackendSetting.LogLevel = (LogLevel)Enum.Parse(typeof(LogLevel), AzureSQLHelper.SystemConfigurationModel.GetCDSConfigValueByKey("IoTHubReceiverLogLevel")); /* Service Bus */ cdsBackendSetting.ServiceBusConnectionString = AzureSQLHelper.SystemConfigurationModel.GetCDSConfigValueByKey("ServiceBusConnectionString"); cdsBackendSetting.ServiceBusEventActionQueue = AzureSQLHelper.SystemConfigurationModel.GetCDSConfigValueByKey("EventActionQueue"); cdsBackendSetting.ServiceBusProvisionQueue = AzureSQLHelper.SystemConfigurationModel.GetCDSConfigValueByKey("ProvisionQueue"); cdsBackendSetting.ServiceBusProcessCommandTopic = AzureSQLHelper.SystemConfigurationModel.GetCDSConfigValueByKey("IoTHubReceiverTopic"); /* Feed In URL */ cdsBackendSetting.RTMessageFeedInURL = AzureSQLHelper.SystemConfigurationModel.GetCDSConfigValueByKey("RTMessageFeedInURL"); /* Heartbeat */ cdsBackendSetting.SuperAdminHeartbeatURL = AzureSQLHelper.SystemConfigurationModel.GetCDSConfigValueByKey("SuperAdminHeartbeatURL"); cdsBackendSetting.AdminHeartbeatURL = AzureSQLHelper.SystemConfigurationModel.GetCDSConfigValueByKey("AdminHeartbeatURL"); } cdsBackendSetting.HeartbeatIntervalInSec = getHeartbeatIntervalInSec(); } catch (Exception ex) { throw new Exception("Cannot load the backend setting from database: " + ex.Message); } return(cdsBackendSetting); }
private async Task loadConfigurationFromDB(EventProcessorFactoryModel epfm) { CdsInfo cdsInfo = epfm.CdsInfo; IoTHub iotHub; CompanyInSubscriptionPlan cisp; string telemetryStorageContainer; string eventStorageContainer; if (_ignoreCDS20DB) { /* For Test */ TestSampleHelper tsHelper = new TestSampleHelper(); iotHub = tsHelper.generateIoTHubForTest(cdsInfo.IoTHubId); cisp = new CompanyInSubscriptionPlan(); cisp.ExpiredDate = DateTime.UtcNow.AddMonths(1);// Test cisp.StoreColdMessage = true; cisp.StoreHotMessage = true; /* Walker's Cosmos DB */ cisp.CosmosDBConnectionString = "https://sfdev.documents.azure.com:443/;AccountKey=PHsydcvXyVdELDtWTgLvlbrP5ohuaJbMKQNNCxZKR1UPwS45qVkYiTuXR6wTm9PhnqIDe5IwUoQ0fqmk28CJww==;";// "https://sfdocumentdb.documents.azure.com:443/;AccountKey=uscYe8taxXEtIIzQjCM47T3y3F53wMn2QOPUOnZu55oBClFnzOzfd5UDSlMixgCR6aqBNbHebJmIgoSmdk2MxQ==;"; cisp.CosmosDBName = "db69"; cisp.CosmosDBCollectionID = "69"; telemetryStorageContainer = "telemetry"; eventStorageContainer = "event"; } else { /* For CDS 2.0 DB */ AzureSQLHelper.IoTHubModel ioTHubModel = new AzureSQLHelper.IoTHubModel(); iotHub = ioTHubModel.GetById(Int32.Parse(cdsInfo.IoTHubId)); AzureSQLHelper.CompanyModel companyModel = new AzureSQLHelper.CompanyModel(); cisp = companyModel.GetActiveSubscriptionPlanByCompanyId(Int32.Parse(cdsInfo.CompanyId)); telemetryStorageContainer = AzureSQLHelper.SystemConfigurationModel.GetCDSConfigValueByKey("TelemetryStorageContainer"); telemetryStorageContainer = telemetryStorageContainer == null ? "telemetry" : telemetryStorageContainer; eventStorageContainer = AzureSQLHelper.SystemConfigurationModel.GetCDSConfigValueByKey("EventStorageContainer"); eventStorageContainer = eventStorageContainer == null ? "event" : eventStorageContainer; } if (iotHub == null) { _consoleLog.Info("IoTHub Not Found. IoTHubId:{0}", cdsInfo.IoTHubId); _consoleLog.BlobLogError("IoTHub Not Found. IoTHubId:{0}", cdsInfo.IoTHubId); throw new Exception("IoTHub Not Found"); } if (cisp == null) { _consoleLog.Info("NO Actived CompanyInSubscriptionPlan. CompanyId:{0}", cdsInfo.CompanyId); _consoleLog.BlobLogError("NO Actived CompanyInSubscriptionPlan. CompanyId:{0}", cdsInfo.CompanyId); throw new Exception("NO Actived CompanyInSubscriptionPlan"); } /* IoTHubAlias */ cdsInfo.IoTHubAlias = iotHub.IoTHubName; /* CompanyInSubscriptionPlan */ cdsInfo.CompanyInSubscriptionPlan = cisp; /* Load the message schema from IoT Hub devices */ epfm.SimpleIoTDeviceMessageCatalogList = findAllMessageCatalogIdInIoTDevices(iotHub.IoTDevice); /* Load the message JSON transformer from IoT Hub devices */ epfm.MessageTransformerInDeviceId = findAllMessageTransformers(iotHub.IoTDevice); /* Load Alarm Rules */ epfm.EventRulesInMessageId = findAllEventRules(iotHub); /* Load Monitor Frequence In MinSec By MessageId */ epfm.MonitorFrequenceInMinSecByMessageId = findMonitorFrequenceInMinSecByMessageId(iotHub.IoTDevice); /* Load IoT Hub configuration */ _CompatibleEventHub = findCompatibleEventHub(iotHub); /* Cosmos DB Helper */ epfm.docDBHelper = await createDocDBHelper(cdsInfo.CompanyInSubscriptionPlan); /* Blob Helper */ epfm.TelemetryBlobStorageHelper = new BlobStorageHelper(_CompatibleEventHub.StorageConnectionString, telemetryStorageContainer); epfm.EventBlobStorageHelper = new BlobStorageHelper(_CompatibleEventHub.StorageConnectionString, eventStorageContainer); /* Service Bus Helper */ CdsBackendSetting cdsBackendSetting = cdsInfo.cdsBackendSetting; epfm.EventQueueClient = QueueClient.CreateFromConnectionString(cdsBackendSetting.ServiceBusConnectionString, cdsBackendSetting.ServiceBusEventActionQueue); epfm.InfraQueueClient = QueueClient.CreateFromConnectionString(cdsBackendSetting.ServiceBusConnectionString, cdsBackendSetting.ServiceBusProvisionQueue); }
private void ListenOnServiceBusTopic(CdsInfo cdsInfo) { CdsBackendSetting azureCS = cdsInfo.cdsBackendSetting; /* Create Topic Subscription Client, and bind with Message Property on companyid = xx */ var namespaceManager = NamespaceManager.CreateFromConnectionString(azureCS.ServiceBusConnectionString); string subscriptionName = "C_" + cdsInfo.CompanyId + "_IoTHubId_" + cdsInfo.IoTHubId + "_P_" + cdsInfo.PartitionNum; SqlFilter messageFilter = new SqlFilter("Process = 'IoTHubReceiver' AND IoTHubId = '" + cdsInfo.IoTHubId + "'"); /* If the subscription not exist, create it. */ if (!namespaceManager.SubscriptionExists(azureCS.ServiceBusProcessCommandTopic, subscriptionName)) { namespaceManager.CreateSubscription(azureCS.ServiceBusProcessCommandTopic, subscriptionName, messageFilter); } /* Create subscription client and listen on message */ _sbSubscriptionClient = SubscriptionClient.CreateFromConnectionString(azureCS.ServiceBusConnectionString, azureCS.ServiceBusProcessCommandTopic, subscriptionName); OnMessageOptions options = new OnMessageOptions(); options.AutoComplete = true; IoTHubReceiverModel _IoTHubReceiverMsg = null; _sbSubscriptionClient.OnMessage(async(message) => { AzureSQLHelper.OperationTaskModel operationTask = new AzureSQLHelper.OperationTaskModel(); try { string messagePayload = message.GetBody <string>(); _IoTHubReceiverMsg = JsonConvert.DeserializeObject <IoTHubReceiverModel>(messagePayload); // Process message from subscription. _consoleLog.Info("onMessage: {0}", messagePayload); _consoleLog.BlobLogInfo("onMessage: {0}", messagePayload); _consoleLog.Info("Received Task:" + _IoTHubReceiverMsg.task); _consoleLog.BlobLogInfo("Received Task:" + _IoTHubReceiverMsg.task); switch (_IoTHubReceiverMsg.task) { //case "start": // await _IoTHubMessageReceiver.Start(); // break; //case "stop": // await _IoTHubMessageReceiver.Stop(); // break; case TaskName.IoTHubReceiver_Restart: reloadHeartbeatInterval(); _IoTHubMessageReceiver.Stop().Wait(); _IoTHubMessageReceiver.Start().Wait(); break; //case "shutdown": // message.Complete(); // await _IoTHubMessageReceiver.Stop(); // operationTask.UpdateTaskBySuccess(_IoTHubReceiverMsg.taskId); // Environment.Exit(0); // break; } operationTask.UpdateTaskBySuccess(_IoTHubReceiverMsg.taskId); } catch (Exception ex) { // Indicates a problem, unlock message in subscription. _consoleLog.Error("Exception: {0}", ex.Message); _consoleLog.BlobLogError("Exception: {0}", ex.Message); operationTask.UpdateTaskByFail(_IoTHubReceiverMsg.taskId, ex.Message); } }, options); }