/// <summary> /// Initializes the ModuleClient and sets up the callback to receive /// messages /// </summary> static async Task Init() { AmqpTransportSettings amqpSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only); 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."); 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); // Register callback to be called when a message is received by the module await ioTHubModuleClient.SetInputMessageHandlerAsync("input1", PipeMessage, ioTHubModuleClient); // 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); }