public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, [Queue("highbush-automation-queue", Connection = "AzureWebJobsStorage")] IAsyncCollector <IotEvent> queueCollector, ILogger log) { IActionResult result; //string name = req.Query["name"]; string requestBody = new StreamReader(req.Body).ReadToEnd(); IotEvent iotEvent = JsonConvert.DeserializeObject <IotEvent>(requestBody); //name = name ?? data?.name; if (iotEvent == null) { log.LogInformation("IOT event not provided."); result = new BadRequestObjectResult("Please pass IOT event in the request body."); } else { string message = "IOT event was triggered."; await queueCollector.AddAsync(iotEvent); log.LogInformation(message); result = new OkObjectResult(message); } return(result); }
/// <summary> /// This starts waiting for messages from the IoT Hub. /// </summary> /// <returns></returns> internal async Task StartAsync() { while (true) { try { var receivedMessage = await _deviceClient.ReceiveAsync(new TimeSpan(int.MaxValue)); if (receivedMessage == null) { continue; } foreach (var prop in receivedMessage.Properties) { await _display.WriteAsync($"{prop.Key} {prop.Value}"); } await _deviceClient.CompleteAsync(receivedMessage); var messageData = Encoding.ASCII.GetString(receivedMessage.GetBytes()); IotEvent?.Invoke(null, new IotEventArgs { EventData = receivedMessage.Properties, MessageData = messageData }); } catch { //Write out to the display perhaps } } }
public static async Task Run( [QueueTrigger("highbush-automation-queue", Connection = "AzureWebJobsStorage")] IotEvent iotEvent, [Inject] IEventSender eventSender, ILogger log) { log.LogInformation($"C# Queue trigger function processed: {iotEvent}"); log.LogInformation($"Event type: {iotEvent.DoorEvent.DoorEventType}"); string topicName = Environment.GetEnvironmentVariable("EventGridTopicEndpoint"); string topicKey = Environment.GetEnvironmentVariable("EventGridTopicKey"); await eventSender.SendAsync(iotEvent.DoorEvent, topicName, topicKey); }