public IoTHubReceiverThread(string messagePayload) { _IoTHubReceiverMsg = JsonConvert.DeserializeObject <IoTHubReceiverModel>(messagePayload); if (_IoTHubReceiverMsg.content.iotHubId == 0) { throw new Exception("iotHubId can't be null"); } }
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); }