public static async Task Run([EventHubTrigger( Constants.EVENT_HUB_QA_TELEMETRY_PROTOBUF, ConsumerGroup = Constants.EVENT_HUB_CONSUMER_GROUP, Connection = "PACKAGING_QA_TELEMETRY_PROTOBUF_EVENTHUB" )] EventData[] events, [CosmosDB( databaseName: Constants.COSMOS_DB_DATABASE_NAME, collectionName: Constants.COSMOS_DB_CONTAINER_NAME, ConnectionStringSetting = "FACTORY_COSMOS_DB" )] IAsyncCollector <object> items, ILogger log) { var exceptions = new List <Exception>(); TelemetryProtobuf deserialized = null; foreach (EventData eventData in events) { try { deserialized = TelemetryProtobuf.Parser.ParseFrom(eventData.Body.Array); deserialized.DeviceId = eventData.SystemProperties["iothub-connection-device-id"].ToString(); await items.AddAsync(deserialized); } 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(); } }
static async Task Main(string[] args) { double temperature; var rand = new Random(); using (var security = new SecurityProviderSymmetricKey(registrationId, primaryKey, secondaryKey)) using (var transport = new ProvisioningTransportHandlerMqtt()) { ProvisioningDeviceClient provClient = ProvisioningDeviceClient.Create(GlobalDeviceEndpoint, idScope, security, transport); DeviceRegistrationResult result = await provClient.RegisterAsync(); IAuthenticationMethod auth = new DeviceAuthenticationWithRegistrySymmetricKey(result.DeviceId, (security as SecurityProviderSymmetricKey).GetPrimaryKey()); using (DeviceClient iotClient = DeviceClient.Create(result.AssignedHub, auth, TransportType.Mqtt)) { while (true) { if (_temperature.IsAvailable) { temperature = Math.Round(_temperature.Temperature.Celsius, 2); } else { temperature = rand.Next(15, 35); } try { msgId++; Console.WriteLine($"The CPU temperature is {temperature}"); Image img = Image.FromFile(filename); // simulate camera captured image using (MemoryStream ms = new MemoryStream()) { img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); TelemetryProtobuf tpb = new TelemetryProtobuf() { Temperature = temperature, Humidity = 50, Pressure = 1100, MsgId = msgId, Label = "Orange", Probability = 1.0, Image = ByteString.CopyFrom(ms.ToArray()), DeviceId = string.Empty // this is set in the azure function when writing to cosmosdb }; using (MemoryStream memStream = new MemoryStream()) { tpb.WriteTo(memStream); await SendMsgIotHub(iotClient, memStream.ToArray()); // Deserialise Protobuf // var newTpb = TelemetryProtobuf.Parser.ParseFrom(memStream.ToArray()); } await SendMsgIotHub(iotClient, temperature); } } catch (Exception ex) { Console.WriteLine("exception msg: " + ex.Message); } Thread.Sleep(4000); // sleep for 10 seconds } } } }