Example #1
0
        private static Task <MethodResponse> SetInnerDevice(MethodRequest methodRequest, object userContext)
        {
            var data = Encoding.UTF8.GetString(methodRequest.Data);

            Console.WriteLine("SetInnerDevice Called...");
            mInnerdevice = JsonConvert.DeserializeObject <MythicalInnerDevice>(data);
            string result = "{\"result\":\"Executed SetInnerDevice method. InnerDevice set to: " + mInnerdevice.DeviceName + "\"}";

            innerDeviceName       = mInnerdevice.DeviceName;
            innerDeviceConnString = mInnerdevice.ConnString;
            //mInnerdevice.ConnectInnerDevice();
            //Acknowledge the receipt of the direct method call
            return(Task.FromResult(new  MethodResponse(Encoding.UTF8.GetBytes(result), 200)));
        }
Example #2
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();
                    }
                }
            }
        }