public async Task StartAsync(CancellationToken cancellationToken) { var client = new HttpClient { BaseAddress = new Uri(producerOptions.ApiBaseAddress) }; await Task.Factory.StartNew(async() => { var random = new Random(); var stopwatch = new Stopwatch(); while (!cancellationToken.IsCancellationRequested) { stopwatch.Restart(); var model = new SensorTelemetryModel { Id = producerOptions.SensorId, Timestamp = DateTimeOffset.UtcNow, Measurement = random.Next(0, 1000) }; var content = JsonContent.Create(model); await client.PostAsync("/telemetry", content); stopwatch.Stop(); logger.LogInformation($"Sensor ID: {model.Id} | Measurement: {model.Measurement} | {stopwatch.ElapsedMilliseconds}ms"); await Task.Delay(producerOptions.MillisecondsDelay); } }, cancellationToken); }
public async Task <IActionResult> Post([FromBody] SensorTelemetryModel model) { // Validate model. // Convert to entity. var entity = new SensorTelemetryEntity { Id = model.Id, Measurement = model.Measurement, Timestamp = model.Timestamp }; // Get Grain and process event. var sensor = grainFactory.GetGrain <ISensorTypeAIntakeGrain>(Guid.Parse(model.Id)); var result = await sensor.RecordMeasurementAsync(entity); return(result ? Ok() : StatusCode(500)); }