private static async Task ReceiveMessagesFromDeviceAsync(string partition, CancellationToken ct) { var eventHubReceiver = eventHubClient.GetDefaultConsumerGroup().CreateReceiver(partition, DateTime.UtcNow); while (true) { if (ct.IsCancellationRequested) { break; } EventData eventData = await eventHubReceiver.ReceiveAsync(TimeSpan.FromSeconds(2)); if (eventData == null) { continue; } var deviceId = eventData.SystemProperties["iothub-connection-device-id"] as string; deviceId = deviceId.Substring("dispositivo".Length); string data = Encoding.UTF8.GetString(eventData.GetBytes()); Debug.WriteLine("Message received. Partition: {0} Data: '{1}'", partition, data); var telemetria = JsonConvert.DeserializeObject <Telemetria>(data); telemetria.IdDispositivo = int.Parse(deviceId); TelemetriaHub.InviaMessaggio(telemetria); } }
public async Task <IHttpActionResult> Post(Telemetria telemetria) { telemetria.DataSalvataggio = DateTime.UtcNow; using (var contesto = new Contesto()) { contesto.Set <Telemetria>().Add(telemetria); await contesto.SaveChangesAsync(); } TelemetriaHub.InviaMessaggio(telemetria); return(Ok(telemetria)); }