private void sendCommandToServiceBusQueue(EventProcessorFactoryModel epfm, string deviceId, JObject command) { QueueClient queueClient = epfm.InfraQueueClient; string iothubAlias = epfm.CdsInfo.IoTHubAlias; try { JObject msgObj = new JObject(); msgObj.Add("job", "device management"); msgObj.Add("entity", "iotdevice"); msgObj.Add("entityId", deviceId); msgObj.Add("task", "update device reported property to db"); msgObj.Add("iothubDeviceId", deviceId); msgObj.Add("requester", "IoTHubReceiver_" + iothubAlias); msgObj.Add("requestDateTime", DateTime.UtcNow); msgObj.Add("iothubAlias", iothubAlias); msgObj.Add("iothubIsPrimary", true); JObject payload = new JObject(); payload.Add(SF_LASTUPDATED_TIMESTAMP, command[SF_LASTUPDATED_TIMESTAMP]); msgObj.Add("deviceConfiguration", payload); var messageString = JsonConvert.SerializeObject(msgObj); var msg = new BrokeredMessage(messageString); queueClient.Send(msg); } catch (Exception ex) { _consoleLog.MessageEventError("sendCommandToServiceBusQueue Exception: payload={0} ex={1}", command, ex.ToString()); _consoleLog.BlobLogError("sendCommandToServiceBusQueue Exception: payload={0} ex={1}", command, ex.ToString()); } }
public IoTHubMessageEventProcessor(EventProcessorFactoryModel epfm) { _consoleLog = IoTHubReceiver._consoleLog; _epfm = epfm; _companyId = Int32.Parse(epfm.CdsInfo.CompanyId); _cachekey_message_quota_per_day = "C_" + _companyId + "_MessageQuotaPerDay"; _cachekey_message_total_consumed_count = "C_" + _companyId + "_MessageConsumed"; _cachekey_message_consumed_date = "C_" + _companyId + "_MessageConsumedDate"; _msgSizeInBytes = 4096;// Hard Code now. _storeHotMessageEnabled = epfm.CdsInfo.CompanyInSubscriptionPlan.StoreHotMessage; _storeColdMessageEnabled = epfm.CdsInfo.CompanyInSubscriptionPlan.StoreColdMessage; //_RTMessageSender = new RealTimeMessageSender(epfm.CdsInfo.cdsBackendSetting.RTMessageFeedInURL); _RTMessageFeedInURL = epfm.CdsInfo.cdsBackendSetting.RTMessageFeedInURL; }
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); }
public IoTHubMessageReceiver(CdsInfo cdsInfo) { _consoleLog = IoTHubReceiver._consoleLog; _epfm = new EventProcessorFactoryModel(); _epfm.CdsInfo = cdsInfo; }
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 async Task runEventRules(EventProcessorFactoryModel epfm, string deviceId, int messageCatalogId, JObject payload, string messageDocumentId) { try { var ts = payload["msgTimestamp"]; DateTime msgTimestamp = DateTime.Parse(ts.ToString()); Dictionary <int, List <EventRuleCatalogEngine> > eventRulesInMessageId = epfm.EventRulesInMessageId; if (eventRulesInMessageId.ContainsKey(messageCatalogId) == true) { List <EventRuleCatalogEngine> eventRuleCatalogEngineList = eventRulesInMessageId[messageCatalogId]; foreach (EventRuleCatalogEngine eventRuleCatalogEngine in eventRuleCatalogEngineList) { if (eventRuleCatalogEngine.RuleEngineItems.Count > 0) { // Get all results of equations foreach (KeyValuePair <string, RuleEngineItem> ruleEngineItem in eventRuleCatalogEngine.RuleEngineItems) { runSingleRuleItem(ruleEngineItem.Value, payload); } // Get the result of bitwise operation bool eventTriggered = compileBitWiseRules(eventRuleCatalogEngine.RuleEngineItems.Count - 1, eventRuleCatalogEngine.RuleEngineItems); if (eventTriggered) { string now = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"); bool eventSent = checkEventTimeWindow(eventRuleCatalogEngine, msgTimestamp); //_consoleLog.MessageEventDebug("EventRuleCatalogId={0}, eventSent={1}, LastTriggerTime={2}", // eventRuleCatalogEngine.EventRuleCatalogId, eventSent, eventRuleCatalogEngine.LastTriggerTime.ToString("yyyy-MM-ddTHH:mm:ss")); //_consoleLog.BlobLogInfo("EventRuleCatalogId={0}, eventSent={1}, LastTriggerTime={2}", // eventRuleCatalogEngine.EventRuleCatalogId, eventSent, eventRuleCatalogEngine.LastTriggerTime.ToString("yyyy-MM-ddTHH:mm:ss")); EventMessageModel eventMessageModel = new EventMessageModel(eventRuleCatalogEngine.EventRuleCatalog, now, eventSent, messageDocumentId, payload); // Send the Event to Service Bus if it matchs the time window if (eventSent) { sendEventToServiceBusQueue(deviceId, epfm.EventQueueClient, eventMessageModel); } // Put the event to Cosmos DB (Hot Data) string eventDocumentId = await putEvent2CosmosDb(deviceId, epfm.docDBHelper, eventMessageModel); // Put the event to Blob (Cold Data) await putEvent2BlobStorage(eventDocumentId, deviceId, eventMessageModel); } } } } } catch (Exception ex) { _consoleLog.Error("runEventRules Exception: {0}", ex.ToString()); _consoleLog.BlobLogError("runEventRules Exception: {0}", ex.ToString()); return; } }
public IoTHubMessageProcessorFactory(EventProcessorFactoryModel epfm) { this.epfm = epfm; }