public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext executionContext) { log.LogInformation("C# HTTP trigger function processed a request."); var config = executionContext.BuildConfiguraion(); var account = AccountConfigration.Load().First(); var devicedefinitions = DeviceDefinition.Load(); var token = await Extensions.GetS2SAccessToken(account.ClientId, config[account.ClientSecretSettingName], account.Resource, account.Authority); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken); var fb = new FarmBeatsClient(account.Url, _httpClient); foreach (var farm in account.farms) { foreach (var device in farm.devices) { var deviceModel = fb.GetDeviceModel(device.deviceModel).Result; var deviceDetail = await fb.CreateDevice(new Device(device.hardwareId, deviceModel.id, farm.id, new Location(0, 0), device.name, 300)); var sensorModels = devicedefinitions.deviceModels.Single(x => x.name == deviceModel.name).sensorModels; foreach (var sensor in sensorModels) { var sensorModel = fb.GetSensorModel(sensor).Result; await fb.CreateSensor(new Sensor(Guid.NewGuid().ToString(), sensorModel.id, deviceDetail.id, device.name + sensorModel.name)); } } } return(new OkObjectResult("OK")); }
public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext executionContext) { log.LogInformation("C# HTTP trigger function processed a request."); var config = executionContext.BuildConfiguraion(); var account = AccountConfigration.Load().First(); var deviceDefinition = DeviceDefinition.Load(); var token = await Extensions.GetS2SAccessToken(account.ClientId, config[account.ClientSecretSettingName], account.Resource, account.Authority); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken); var fb = new FarmBeatsClient(account.Url, _httpClient); foreach (var dm in deviceDefinition.deviceModels) { await fb.CreateDeviceModel(dm); } foreach (var sm in deviceDefinition.sensorModels) { await fb.CreateSensorModel(sm); } return(new OkObjectResult("OK")); }
public static async Task Run([EventHubTrigger("Ingest", Connection = "EventHubInputConnectionString")] EventData[] events, [EventHub("sensor-partner-eh-00", Connection = "EventHubOutputConnectionString")] IAsyncCollector <string> outputEvents, ILogger logger, ExecutionContext executionContext) { // Initialize dependancies var config = executionContext.BuildConfiguraion(); var token = await Extensions.GetS2SAccessToken(config); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken); var farmBeatsClient = new FarmBeatsClient(config["FarmBeatsApiUrl"], _httpClient); // Execute var exceptions = new List <Exception>(); var targetSensorConfiguration = await farmBeatsClient.GetSensors(); foreach (EventData eventData in events) { try { // Parse message var messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count); logger.LogInformation($"Input Message: {messageBody} Property Keys: {string.Join(",", eventData.SystemProperties.Keys)} Property Values: { string.Join(",", eventData.SystemProperties.Values)}"); var message = JsonConvert.DeserializeObject <IndoorM1Telemetry>(messageBody); var deviceId = (string)eventData.SystemProperties["iothub-connection-device-id"]; var deviceDefinition = await farmBeatsClient.GetDeviceByHardwareId(deviceId); // Contextualize - resolve device type and associated farmbeats device instance if (deviceDefinition != null) { // Convert to FarmBeats Telemetry Message var mapper = new IndoorM1DeviceInstanceDefinition(targetSensorConfiguration); var fbTelemetry = mapper.MapToFarmBeatsTelemetryModel(deviceDefinition.name, message); var telemetry = new FarmBeatsTelemetryModel(deviceDefinition.id, fbTelemetry); // Send to FarmBeats EventHub var outMessage = JsonConvert.SerializeObject(telemetry, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); logger.LogInformation($"Output Message: {outMessage}"); await outputEvents.AddAsync(outMessage); } else { logger.LogWarning($"Unsuppored device - Id: {deviceId}"); } } catch (Exception e) { // We need to keep processing the rest of the batch - capture this exception and continue. // Also, consider capturing details of the message that failed processing so it can be processed again later. exceptions.Add(e); } } // Once processing of the batch is complete, if any messages in the batch failed processing throw an exception so that there is a record of the failure. if (exceptions.Count > 1) { throw new AggregateException(exceptions); } if (exceptions.Count == 1) { throw exceptions.Single(); } }