Пример #1
0
        /// <inheritdoc/>
        public async Task SetEndpointAsync(EndpointModel endpoint)
        {
            if (endpoint.IsSameAs(Endpoint))
            {
                return;
            }

            // Unregister old endpoint
            if (Endpoint != null)
            {
                await _client.UnregisterAsync(Endpoint);

                _logger.Information("Endpoint {@endpoint} unregistered.", Endpoint);
            }

            // Set new endpoint
            Endpoint = endpoint;

            // Register callback to report endpoint state property
            if (Endpoint != null)
            {
                await _client.RegisterAsync(Endpoint,
                                            state => _events?.SendAsync("State", state));

                _logger.Information("Endpoint {@endpoint} registered.", Endpoint);
            }
        }
Пример #2
0
        /// <inheritdoc/>
        public Task SetEndpointAsync(EndpointModel endpoint)
        {
            if (!endpoint.IsSameAs(Endpoint))
            {
                // Unregister old endpoint
                if (Endpoint != null)
                {
                    _callback?.Dispose();
                    _callback = null;
                    _session?.Dispose();
                    _session = null;
                }

                // Set new endpoint
                Endpoint = endpoint;

                // Register callback to report endpoint state property
                if (Endpoint != null)
                {
                    var connection = new ConnectionModel {
                        Endpoint = Endpoint
                    };
                    _session  = _client.GetSessionHandle(connection);
                    _callback = _client.RegisterCallback(connection,
                                                         state => _events?.ReportAsync("State", state));
                }
            }
            return(Task.CompletedTask);
        }
Пример #3
0
        /// <inheritdoc/>
        public Task SetEndpointAsync(EndpointModel endpoint)
        {
            if (endpoint.IsSameAs(Endpoint))
            {
                return(Task.CompletedTask);
            }

            // Unregister old endpoint
            if (Endpoint != null)
            {
                _client.Unregister(Endpoint);
            }

            // Set new endpoint
            Endpoint = endpoint;

            // Register callback to report endpoint state property
            if (Endpoint != null)
            {
                _client.Register(Endpoint,
                                 state => _events?.SendAsync("State", state));
            }
            return(Task.CompletedTask);
        }
Пример #4
0
        /// <inheritdoc/>
        public async Task SetEndpointAsync(EndpointModel endpoint)
        {
            await _lock.WaitAsync();

            try {
                var previous = _endpoint.Task.IsCompleted ? _endpoint.Task.Result : null;
                if (!endpoint.IsSameAs(previous))
                {
                    _logger.Debug(
                        "Updating twin {device} endpoint from {previous} to {endpoint}...",
                        _events?.DeviceId, previous?.Url, endpoint?.Url);

                    if (_endpoint.Task.IsCompleted)
                    {
                        _endpoint = new TaskCompletionSource <EndpointModel>();
                    }

                    // Unregister old endpoint
                    if (previous != null)
                    {
                        _callback?.Dispose();
                        _callback = null;
                        _session?.Dispose();
                        _session = null;

                        // Clear state
                        State = EndpointConnectivityState.Disconnected;
                    }

                    // Register callback to report endpoint state property
                    if (endpoint != null)
                    {
                        var connection = new ConnectionModel {
                            Endpoint = endpoint
                        };
                        _session = _client.GetSessionHandle(connection);

                        // Set initial state
                        State = _session.State;

                        // update reported state
                        _callback = _client.RegisterCallback(connection,
                                                             state => {
                            State = state;
                            return(_events?.ReportAsync("State",
                                                        state));
                        });
                        _logger.Information("Endpoint {endpoint} ({device}, {module}) updated.",
                                            endpoint?.Url, _events.DeviceId, _events.ModuleId);

                        // ready to use
                        _endpoint.TrySetResult(endpoint);
                    }

                    _logger.Information("Twin {device} endpoint updated to {endpoint}.",
                                        _events?.DeviceId, endpoint?.Url);
                }
            }
            finally {
                _lock.Release();
            }
        }