コード例 #1
0
        async Task InitCallBack()
        {
            try
            {
                ITransportSettings transportSettings = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only);

                ITransportSettings[] settings = { transportSettings };

                // if running as Edge module
                if (this.configuration.RunningAsIoTEdgeModule)
                {
                    this.ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);

                    Logger.Init(new LoggerConfiguration
                    {
                        ModuleClient    = this.ioTHubModuleClient,
                        LogLevel        = LoggerConfiguration.InitLogLevel(this.configuration.LogLevel),
                        LogToConsole    = this.configuration.LogToConsole,
                        LogToHub        = this.configuration.LogToHub,
                        LogToUdp        = this.configuration.LogToUdp,
                        LogToUdpPort    = this.configuration.LogToUdpPort,
                        LogToUdpAddress = this.configuration.LogToUdpAddress,
                    });

                    if (this.configuration.IoTEdgeTimeout > 0)
                    {
                        this.ioTHubModuleClient.OperationTimeoutInMilliseconds = this.configuration.IoTEdgeTimeout;
                        Logger.Log($"Changing timeout to {this.ioTHubModuleClient.OperationTimeoutInMilliseconds} ms", LogLevel.Debug);
                    }

                    Logger.Log("Getting properties from module twin...", LogLevel.Information);

                    var moduleTwin = await this.ioTHubModuleClient.GetTwinAsync();

                    var moduleTwinCollection = moduleTwin.Properties.Desired;
                    try
                    {
                        this.loRaDeviceAPIService.SetURL(moduleTwinCollection["FacadeServerUrl"].Value as string);
                        Logger.LogAlways($"Facade function url: {this.loRaDeviceAPIService.URL}");
                    }
                    catch (ArgumentOutOfRangeException e)
                    {
                        Logger.Log("Module twin FacadeServerUrl property does not exist", LogLevel.Error);
                        throw e;
                    }

                    try
                    {
                        this.loRaDeviceAPIService.SetAuthCode(moduleTwinCollection["FacadeAuthCode"].Value as string);
                    }
                    catch (ArgumentOutOfRangeException e)
                    {
                        Logger.Log("Module twin FacadeAuthCode does not exist", LogLevel.Error);
                        throw e;
                    }

                    await this.ioTHubModuleClient.SetDesiredPropertyUpdateCallbackAsync(this.OnDesiredPropertiesUpdate, null);

                    await this.ioTHubModuleClient.SetMethodDefaultHandlerAsync(this.OnDirectMethodCalled, null);
                }

                // running as non edge module for test and debugging
                else
                {
                    Logger.Init(new LoggerConfiguration
                    {
                        ModuleClient    = null,
                        LogLevel        = LoggerConfiguration.InitLogLevel(this.configuration.LogLevel),
                        LogToConsole    = this.configuration.LogToConsole,
                        LogToHub        = this.configuration.LogToHub,
                        LogToUdp        = this.configuration.LogToUdp,
                        LogToUdpPort    = this.configuration.LogToUdpPort,
                        LogToUdpAddress = this.configuration.LogToUdpAddress,
                    });
                }
            }
            catch (Exception ex)
            {
                Logger.Log($"Initialization failed with error: {ex.Message}", LogLevel.Error);
                throw ex;
            }

            // Report Log level
            Logger.LogAlways($"Log Level: {(LogLevel)Logger.LoggerLevel}");
        }