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

            ITransportSettings[] settings = { mqttSetting };

            /*  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.");

            // Read Module Twin Desired Properties
            Console.WriteLine("Reading module Twin from IoT Hub.");
            var moduleTwin = await ioTHubModuleClient.GetTwinAsync();

            // Parse Twin Json and initialize gateway
            Console.WriteLine("Starting Gateway controller handler process.");
            ServiceBusClientModel gatewayConfigModel = ServiceBusClientModel.InitClientModel(moduleTwin.Properties.Desired);

            serviceBusClient = ServiceBusClient.Init(gatewayConfigModel, ioTHubModuleClient);


            // Attach callback for Twin desired properties updates
            await ioTHubModuleClient.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertiesUpdate, null);
        }
예제 #2
0
        internal static ServiceBusClient Init(ServiceBusClientModel gatewayDeviceConfig, IoT.ModuleClient ioTHubModuleClient)
        {
            if (gatewayDeviceConfig == null || string.IsNullOrEmpty(gatewayDeviceConfig.ConnectionString) ||
                string.IsNullOrEmpty(gatewayDeviceConfig.Topic) || string.IsNullOrEmpty(gatewayDeviceConfig.Subscription))
            {
                throw new ArgumentException("Gateway config in the module twin doesn't contain required properties");
            }
            ServiceBusClient gateway = new ServiceBusClient();

            ServiceBusClient.clientModel = gatewayDeviceConfig;
            Console.WriteLine($"Connectiong to ServiceBus with conn:{gatewayDeviceConfig.ConnectionString}  topic:{gatewayDeviceConfig.Topic} subscription:{gatewayDeviceConfig.Subscription}");
            ServiceBusClient.subscriptionClient = new SubscriptionClient(gatewayDeviceConfig.ConnectionString, gatewayDeviceConfig.Topic,
                                                                         gatewayDeviceConfig.Subscription);

            // Configure the MessageHandler Options in terms of exception handling, number of concurrent messages to deliver etc.
            var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
            {
                // Maximum number of Concurrent calls to the callback `ProcessMessagesAsync`, set to 1 for simplicity.
                // Set it according to how many messages the application wants to process in parallel.
                MaxConcurrentCalls = 1,
                // Indicates whether MessagePump should automatically complete the messages after returning from User Callback.
                // False below indicates the Complete will be handled by the User Callback as in `ProcessMessagesAsync` below.
                AutoComplete = false
            };

            // Register the function that will process messages
            ServiceBusClient.subscriptionClient.RegisterMessageHandler(ServiceBusClient.ProcessMessagesAsync, messageHandlerOptions);
            ServiceBusClient.ioTHubModuleClient = ioTHubModuleClient;

            // await gateway.Start();

            return(gateway);
        }
예제 #3
0
        public static ServiceBusClientModel InitClientModel(TwinCollection settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            string serializedStr = JsonConvert.SerializeObject(settings);

            if (string.IsNullOrEmpty(serializedStr))
            {
                throw new ArgumentOutOfRangeException("No configuration provided for the module Twin.");
            }
            else
            {
                Console.WriteLine(String.Format("Attempt to parse configuration JSON: {0}", serializedStr));
                ServiceBusClientModel model = JsonConvert.DeserializeObject <ServiceBusClientModel>(serializedStr);
                if (model == null)
                {
                    throw new ArgumentOutOfRangeException("Errorparsing gateway twin settings");
                }
                else
                {
                    return(model);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Callback to handle Twin desired properties updates�
        /// </summary>
        static async Task OnDesiredPropertiesUpdate(TwinCollection desiredProperties, object userContext)
        {
            if (Program.serviceBusClient == null)
            {
                throw new InvalidOperationException("ServiceBusClient context doesn't exist");
            }

            ModuleClient ioTHubModuleClient = ServiceBusClient.ioTHubModuleClient;
            await serviceBusClient.Stop();

            ServiceBusClientModel gatewayConfigModel = ServiceBusClientModel.InitClientModel(desiredProperties);

            serviceBusClient = ServiceBusClient.Init(gatewayConfigModel, ioTHubModuleClient);
        }