public async Task Send(string jsonPayload) { using (X509Certificate2 cert = new X509Certificate2(this.path)) { if (!Program.GetInfo(in cert, out string deviceName, out string vehicleUuid)) { return; } // Retrieves vehicle with Authentication Info from CVS. This step is required to parse out the service authentication key needed // to retrieve the vehicle connection information using (VehicleConnectionClient vc = new VehicleConnectionClient(new Uri(String.Format("https://{0}:8510", this.domain)), cert)) { DeviceCredentialInformation deviceInfo = new DeviceCredentialInformation { VehicleUuid = vehicleUuid }; DeviceConnectionInformation connectionInfo = vc.GetDeviceConnectionInfoAsync(deviceInfo, deviceName).Result; if (connectionInfo == null) { Console.WriteLine("Vehicle ID or device name not found. Please try again"); return; } DeviceClient deviceClient = DeviceClient.Create( connectionInfo.PrimaryIoTHubName, new DeviceAuthenticationWithRegistrySymmetricKey( connectionInfo.IoTDeviceId, connectionInfo.IoTAuthenticationInformation.SymmetricKey.PrimaryKey), TransportType.Mqtt_Tcp_Only); Console.WriteLine("------------------------"); // construct a basic Device Telemetry Message DeviceTelemetryMessage telemetryMessage = new DeviceTelemetryMessage { TelemetryName = this.telemName, // needs to match the prefix of our telemetry extension Payload = jsonPayload, Priority = MessagePriority.Normal }; // construct the actual IoT message. Setting the content type is necessary for prioritization to function properly within CVS. DeviceMessage deviceMessage = new DeviceMessage(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(telemetryMessage))); deviceMessage.ContentType = "application/json"; await deviceClient.SendEventAsync(deviceMessage); Console.WriteLine($"Sent telemetry message to IoT hub!"); Console.WriteLine("------------------------"); } } }
/// <summary> /// This function is from the perspective of the vehicle where the command is pulled from IoT hub. /// Next the vehicle turns around and send telemetry back up to the cloud in response /// Note that it is necessary to have run the Provisioning Training Kit first, in order to have a vehicle to work with. /// </summary> public async Task ReceiveRespondCmdAsync() { using (X509Certificate2 cert = new X509Certificate2(this.path)) { if (!Program.GetInfo(in cert, out string deviceName, out string vehicleUuid)) { return; } // Retrieves connection information from CVS for the device. Connection information tells the device how to connect to IoTHub, including // authN and which endpoint to communicate with. using (IVehicleConnectionClient connectionClient = new VehicleConnectionClient(new Uri($"https://{this.domain}:8510"), cert)) { DeviceCredentialInformation deviceCredInfo = new DeviceCredentialInformation() { VehicleUuid = vehicleUuid }; DeviceConnectionInformation connectionInfo = await connectionClient.GetDeviceConnectionInfoAsync(deviceCredInfo, deviceName); DeviceClient primaryDeviceClient = DeviceClient.Create( connectionInfo.PrimaryIoTHubName, new DeviceAuthenticationWithRegistrySymmetricKey(connectionInfo.IoTDeviceId, connectionInfo.IoTAuthenticationInformation.SymmetricKey.PrimaryKey), TransportType.Mqtt_Tcp_Only); DeviceClient secondaryDeviceClient = DeviceClient.Create( connectionInfo.SecondaryIoTHubName, new DeviceAuthenticationWithRegistrySymmetricKey(connectionInfo.IoTDeviceId, connectionInfo.IoTAuthenticationInformation.SymmetricKey.PrimaryKey), TransportType.Mqtt_Tcp_Only); // If the message was timed out prior to running this function, this receive will fail. Console.WriteLine("Start retrieving device command message."); bool received = false; DateTimeOffset bailoutTime = DateTimeOffset.UtcNow.AddSeconds(60); while (!received && DateTimeOffset.UtcNow < bailoutTime) { List <Task <Tuple <DeviceClient, DeviceMessage> > > tasks = new List <Task <Tuple <DeviceClient, DeviceMessage> > >(); tasks.Add(ReceiveDeviceMessage(primaryDeviceClient)); tasks.Add(ReceiveDeviceMessage(secondaryDeviceClient)); while (tasks.Count > 0) { Task <Tuple <DeviceClient, DeviceMessage> > finishedTask = await Task.WhenAny(tasks); tasks.Remove(finishedTask); Tuple <DeviceClient, DeviceMessage> msg = await finishedTask; if (msg.Item2 != null) { received = true; string hubName = msg.Item1 == primaryDeviceClient ? "primary" : "secondary"; Console.WriteLine($"Retrieved device command message from {hubName} IoTHub."); if (msg.Item1 == primaryDeviceClient) { await HandleDeviceMessage(msg.Item2, primaryDeviceClient, secondaryDeviceClient, "primary", "secondary"); } else { await HandleDeviceMessage(msg.Item2, secondaryDeviceClient, primaryDeviceClient, "secondary", "primary"); } } else if (!received) { string hubName = msg.Item1 == primaryDeviceClient ? "primary" : "secondary"; Console.WriteLine($"Unable to retrieve device command message from {hubName} IoTHubs."); Thread.Sleep(2000); } } } Console.WriteLine("------------------------"); } } }