public static void Run([ServiceBusTrigger("sensors", "all", Connection = "ServiceBusConnection")] string mySbMsg, ILogger log)
        {
            try
            {
                log.LogInformation($"SensorsSB received message: {mySbMsg}");

                SensorEntity lSensor = JsonConvert.DeserializeObject <SensorEntity>(mySbMsg);

                if (lSensor.Temperature > 10)
                {
                    string lMSg = $"Sensor {lSensor.Name} temp is {lSensor.Temperature} C - threshold is 10 C";

                    string      ServiceBusConnectionString = "<TODO: Service Bus Connection String>";
                    string      TopicName    = "notifications";
                    TopicClient _topicClient = new TopicClient(ServiceBusConnectionString, TopicName);

                    var message = new Message(Encoding.UTF8.GetBytes(lMSg));

                    // Send the message to the queue.
                    _topicClient.SendAsync(message);
                }
            }
            catch (Exception ex)
            {
                log.LogError($"Failed to proccess message: {mySbMsg}", ex);
            }
        }
Beispiel #2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
            ExecutionContext ctx,
            ILogger log)
        {
            string lAlertBody = await new StreamReader(req.Body).ReadToEndAsync();

            log.LogInformation($"NotificationsAPI called with payload: {lAlertBody}");

            string      ServiceBusConnectionString = "<TODO: Service Bus Connection String>";
            string      TopicName    = "sensors";
            TopicClient _topicClient = new TopicClient(ServiceBusConnectionString, TopicName);

            SensorEntity lSensor = new SensorEntity();

            lSensor.Name        = $"Sensor {lAlertBody}";
            lSensor.Temperature = 98;

            string messageBody = JsonConvert.SerializeObject(lSensor);
            var    message     = new Message(Encoding.UTF8.GetBytes(messageBody));

            await _topicClient.SendAsync(message);

            return(new OkObjectResult(lAlertBody));
        }