예제 #1
0
        /// <summary>
        /// Initializes the ModuleClient and sets up the callback to receive
        /// messages containing temperature information
        /// </summary>
        static async Task Init()
        {
            MqttTransportSettings mqttSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only);

            ITransportSettings[] settings = { mqttSetting };

            // Open a connection to the Edge runtime
            ModuleClient ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);

            await ioTHubModuleClient.OpenAsync();

            Console.WriteLine("IoT Hub module client initialized.");

            // Register callback to be called when a message is received by the module
            await ioTHubModuleClient.SetInputMessageHandlerAsync("input1", PipeMessage, ioTHubModuleClient);

            //Register callback to be called when a direct method is received
            await ioTHubModuleClient.SetMethodHandlerAsync("setinnerdevice", SetInnerDevice, ioTHubModuleClient);


            while (true)
            {
                if (innerDeviceName != null)
                {
                    Console.WriteLine("{0},{1}", innerDeviceName, innerDeviceConnString);
                    MythicalInnerDevice mid = new MythicalInnerDevice(innerDeviceName, innerDeviceConnString);
                    mid.ConnectInnerDevice();
                    {
                        Console.WriteLine("Start Receiving Messages...");
                        //The innerdevice is connected.....
                        //Flush all the messages and start receiving new messages
                        await mid.ReceiveC2dAsync();
                    }
                }
            }
        }