/// <summary> /// Initializes the DeviceClient and sets up the callback to receive /// messages containing temperature information /// </summary> static async Task Init(string connectionString, bool bypassCertVerification = false) { Console.WriteLine("Connection String: {0}", connectionString); var mqttSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only); // During dev you might want to bypass the cert verification. It is highly recommended to verify certs systematically in production if (bypassCertVerification) { Console.WriteLine("Certificate verification is bypassed."); mqttSetting.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; } ITransportSettings[] settings = { mqttSetting }; // Open a connection to the Edge runtime //var ioTHubModuleClient = DeviceClient.CreateFromConnectionString(connectionString, settings); //Andrey Fedorov ModuleClient ioTHubModuleClient = null; //Andrey Fedorov if (string.IsNullOrEmpty(connectionString)) //Andrey Fedorov { ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings); //Andrey Fedorov } else //Andrey Fedorov { ioTHubModuleClient = ModuleClient.CreateFromConnectionString(connectionString, settings); } await ioTHubModuleClient.OpenAsync(); Console.WriteLine("IoT Hub module client initialized."); var moduleTwin = await ioTHubModuleClient.GetTwinAsync(); var moduleTwinCollection = moduleTwin.Properties.Desired; desiredPropertiesData = new DesiredPropertiesData(moduleTwinCollection); // callback for updating desired properties through the portal or rest api await ioTHubModuleClient.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertiesUpdate, null); // this direct method will allow to reset the temperature sensor values back to their initial state await ioTHubModuleClient.SetMethodHandlerAsync("reset", ResetMethod, null); // we don't pass ioTHubModuleClient as we're not sending any messages out to the message bus // Register callback to be called when a message is received by the module await ioTHubModuleClient.SetInputMessageHandlerAsync("control", ControlMessageHandler, null); // as this runs in a loop we don't await SendSimulationData(ioTHubModuleClient); }
/// <summary> /// Initializes the DeviceClient and sets up the callback to receive /// messages containing temperature information /// </summary> static async Task Init() { // Do not do this in production // After creating Edge certs, I was not able to fix validation, I keep getting "The remote certificate is invalid according to the validation procedure." bool DisableCertCheck = false; //DEBUG: print all environment variables Console.WriteLine("DEBUG: Printing environment variables:"); foreach (DictionaryEntry e in Environment.GetEnvironmentVariables()) { Console.WriteLine(e.Key + ":" + e.Value); } Console.WriteLine("---"); // From https://docs.microsoft.com/en-us/azure/iot-edge/how-to-create-transparent-gateway-linux#installation-on-the-downstream-device Console.WriteLine("DEBUG: installing root CA certificate..."); string certPath = "/app/azure-iot-test-only.root.ca.cert.pem"; if (!File.Exists(certPath)) { Console.WriteLine($"Missing path to root CA certificate file: {certPath}"); throw new InvalidOperationException("Missing certificate file."); } else { X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadWrite); store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(certPath))); store.Close(); } // Create iot hub client // Code copied over from FilterModule AmqpTransportSettings amqpSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only); if (DisableCertCheck) { Console.WriteLine("DEBUG: disabling certificate validation. DO NOT USE IN PRODUCTION!"); amqpSetting.RemoteCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); } ITransportSettings[] settings = { amqpSetting }; // Open a connection to the Edge runtime ModuleClient ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings); await ioTHubModuleClient.OpenAsync(); Console.WriteLine("IoT Hub module client initialized."); /* Original code, part of it was in Main() * * // The Edge runtime gives us the connection string we need -- it is injected as an environment variable * var connectionString = Environment.GetEnvironmentVariable("EdgeHubConnectionString"); * * // Cert verification is not yet fully functional when using Windows OS for the container * var bypassCertVerification = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); * if (!bypassCertVerification) InstallCert(); * await Init(connectionString, bypassCertVerification); * * Console.WriteLine("Connection String {0}", connectionString); * * var mqttSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only); * // During dev you might want to bypass the cert verification. It is highly recommended to verify certs systematically in production * if (bypassCertVerification) * { * mqttSetting.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; * } * ITransportSettings[] settings = { mqttSetting }; * * // Open a connection to the Edge runtime * var ioTHubModuleClient = DeviceClient.CreateFromConnectionString(connectionString, settings); * await ioTHubModuleClient.OpenAsync(); * Console.WriteLine("IoT Hub module client initialized."); */ var moduleTwin = await ioTHubModuleClient.GetTwinAsync(); var moduleTwinCollection = moduleTwin.Properties.Desired; desiredPropertiesData = new DesiredPropertiesData(moduleTwinCollection); // callback for updating desired properties through the portal or rest api await ioTHubModuleClient.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertiesUpdate, null); // this direct method will allow to reset the temperature sensor values back to their initial state await ioTHubModuleClient.SetMethodHandlerAsync("reset", ResetMethod, null); // we don't pass ioTHubModuleClient as we're not sending any messages out to the message bus await ioTHubModuleClient.SetInputMessageHandlerAsync("control", ControlMessageHandler, null); // as this runs in a loop we don't await SendSimulationData(ioTHubModuleClient); }
private static Task OnDesiredPropertiesUpdate(TwinCollection twinCollection, object userContext) { desiredPropertiesData = new DesiredPropertiesData(twinCollection); return(Task.CompletedTask); }