Exemplo n.º 1
0
        private async Task Initialize()
        {
            if (_isInitialized)
            {
                return;
            }

            await _initializeSemaphoreSlim.WaitAsync();

            if (_isInitialized)
            {
                return;
            }

            try
            {
                /* Unfortunately the protocol changes based on what type of hardware we're using... */
                HardwareVersion = await GetHardwareVersion();

                _connection.CoordinatorHardwareVersion = HardwareVersion;

                /* We want the receiver to have the hardware version in context so cycle the connection */
                _connection.Close();

                // This is to address a .net (windows?) serial port management issue
                await TaskExtensions.Retry(() => _connection.Open(),
                                           TimeSpan.FromSeconds(3), typeof(UnauthorizedAccessException));

                // ReSharper disable PossibleInvalidOperationException
                Local = CreateNode(HardwareVersion.Value);
                // ReSharper restore PossibleInvalidOperationException

                _isInitialized = true;
            }
            finally
            {
                _initializeSemaphoreSlim.Release();
            }
        }