Ejemplo n.º 1
0
        private CdsInfo loadEnvironmentVariables()
        {
            CdsInfo cdsInfo               = new CdsInfo();
            string  companyId             = Environment.GetEnvironmentVariable("input_CompanyId");
            string  iotHubId              = Environment.GetEnvironmentVariable("input_IoTHubId");
            string  ioTHubPartitionNumber = Environment.GetEnvironmentVariable("input_Partition");
            string  label = Environment.GetEnvironmentVariable("input_Label");

            if (string.IsNullOrEmpty(companyId))
            {
                throw new ArgumentException("Can't find CompandId from SystemEnvironment");
            }

            if (string.IsNullOrEmpty(iotHubId))
            {
                throw new ArgumentException("Can't find IoTHubId from SystemEnvironment");
            }

            if (string.IsNullOrEmpty(ioTHubPartitionNumber))
            {
                throw new ArgumentException("Can't find IoTHubPartitionNumber from SystemEnvironment");
            }

            if (string.IsNullOrEmpty(label))
            {
                throw new ArgumentException("Can't find Label from SystemEnvironment");
            }

            cdsInfo.CompanyId    = companyId;
            cdsInfo.IoTHubId     = iotHubId;
            cdsInfo.PartitionNum = ioTHubPartitionNumber;
            cdsInfo.Label        = label;

            return(cdsInfo);
        }
Ejemplo n.º 2
0
        private ConsoleLog setupConsoleLog(CdsInfo cdsInfo)
        {
            CdsBackendSetting cdsBackendSetting = cdsInfo.cdsBackendSetting;

            return(new ConsoleLog(this.Context,
                                  new LogHelper(
                                      cdsBackendSetting.LogStorageConnectionString,
                                      cdsBackendSetting.LogStorageContainer,
                                      cdsBackendSetting.LogLevel),
                                  cdsInfo));
        }
Ejemplo n.º 3
0
        public IoTHubReceiver(StatelessServiceContext context)
            : base(context)
        {
            _cdsInfo = loadEnvironmentVariables();

            _cdsInfo.cdsBackendSetting = loadCdsBackendSettingFromDB();

            _consoleLog = setupConsoleLog(_cdsInfo);

            _consoleLog.Info("CompandId: " + _cdsInfo.CompanyId + ", IoTHubId: " + _cdsInfo.IoTHubId + ", PartitionNumber: " + _cdsInfo.PartitionNum + ", Label: " + _cdsInfo.Label);
            _consoleLog.BlobLogInfo("CompandId: " + _cdsInfo.CompanyId + ", IoTHubId: " + _cdsInfo.IoTHubId + ", PartitionNumber: " + _cdsInfo.PartitionNum + ", Label: " + _cdsInfo.Label);
        }
Ejemplo n.º 4
0
        public async Task <bool> Stop()
        {
            if (!_isRunning)
            {
                return(true);
            }

            await _eventProcessorHost.UnregisterEventProcessorAsync();

            _isRunning = false;

            CdsInfo cdsInfo = _epfm.CdsInfo;

            _consoleLog.Info("Unregistering IoTHubAliasEventMessageReceiver on {0} - partition {1}", cdsInfo.IoTHubAlias, cdsInfo.PartitionNum);
            _consoleLog.BlobLogInfo("Unregistering IoTHubAliasEventMessageReceiver on {0} - partition {1}", cdsInfo.IoTHubAlias, cdsInfo.PartitionNum);
            return(true);
        }
Ejemplo n.º 5
0
        public async Task <bool> Start()
        {
            if (_isRunning)
            {
                return(true);
            }

            _runStatus = "Initial";
            EventProcessorFactoryModel epfm = _epfm;
            CdsInfo cdsInfo = epfm.CdsInfo;

            await loadConfigurationFromDB(epfm);

            string leaseName = getLeaseName(cdsInfo.CompanyId, cdsInfo.IoTHubId, cdsInfo.IoTHubAlias);
            string hostName  = getHostName(cdsInfo.CompanyId, cdsInfo.IoTHubId, cdsInfo.PartitionNum, cdsInfo.IoTHubAlias);

            _consoleLog.Info("hostName= {0}, leaseName= {1}", hostName, leaseName);

            _eventProcessorHost = new EventProcessorHost(
                hostName,                                   // Task ID
                _CompatibleEventHub.EndpointName,           // Endpoint: messages/events
                _CompatibleEventHub.ConsumerGroup,          // Consumer Group
                _CompatibleEventHub.IoTHubConnectionString, // IoT Hub Connection String
                _CompatibleEventHub.StorageConnectionString,
                leaseName,
                leaseName);

            _consoleLog.Info("Registering IoTHubAliasEventMessageReceiver on {0} - partition {1}", cdsInfo.IoTHubAlias, cdsInfo.PartitionNum);

            var options = new EventProcessorOptions
            {
                InitialOffsetProvider = (partitionId) => DateTime.UtcNow
            };

            options.ExceptionReceived += (sender, e) =>
            {
                if (sender == null)
                {
                    return; // ignore others processor with un-tracked partitions.
                }
                _consoleLog.Error("EventProcessorOptions Exception:{0}", e.Exception);
                _consoleLog.BlobLogError("EventProcessorOptions Exception:{0}", e.Exception);
            };

            try
            {
                await _eventProcessorHost.RegisterEventProcessorFactoryAsync(new IoTHubMessageProcessorFactory(epfm), options);

                _isRunning = true;
                _runStatus = "Good";
            }
            catch (Exception ex)
            {
                _isRunning = false;
                _consoleLog.Error("RegisterEventProcessorFactoryAsync Fail. IoTHubId: {0}, PartitionNum: {1}, Exception: {2}", cdsInfo.IoTHubId, cdsInfo.PartitionNum, ex.Message);
                _consoleLog.BlobLogError("RegisterEventProcessorFactoryAsync Fail. IoTHubId: {0}, PartitionNum: {1}, Exception: {2}", cdsInfo.IoTHubId, cdsInfo.PartitionNum, ex.Message);
                _runStatus = "Error";
                throw ex;
            }

            return(true);
        }
Ejemplo n.º 6
0
 public IoTHubMessageReceiver(CdsInfo cdsInfo)
 {
     _consoleLog   = IoTHubReceiver._consoleLog;
     _epfm         = new EventProcessorFactoryModel();
     _epfm.CdsInfo = cdsInfo;
 }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        public ConsoleLog(StatelessServiceContext serviceContext, LogHelper appLogger, CdsInfo cdsInfo)
        {
            this._context   = serviceContext;
            this._appLogger = appLogger;

            _iotHubReceiverLongName = "C" + cdsInfo.CompanyId + "_I" + cdsInfo.IoTHubId + "_P" + cdsInfo.PartitionNum + "_" + cdsInfo.Label;
            if (_iotHubReceiverLongName.Length > 35)
            {
                _iotHubReceiverLongName = _iotHubReceiverLongName.Substring(0, 35) + "..";
            }
        }