コード例 #1
0
        public async Task DisconnectAsync()
        {
            switch (Model.DefaultTransport.Value)
            {
            /*case TransportTypes.AMQP:
             *  ConnectButtonVisible = true;
             *  break;*/
            case TransportTypes.AzureIoTHub:
#if IOTHUB
                if (_azureIoTHubClient != null)
                {
                    await _azureIoTHubClient.CloseAsync();

                    _azureIoTHubClient.Dispose();
                    _azureIoTHubClient = null;
                }
#endif

                ConnectButtonVisible = true;
                break;

            case TransportTypes.MQTT:
                if (_mqttClient != null)
                {
                    _mqttClient.Disconnect();
                    _mqttClient = null;
                }

                ConnectButtonVisible = true;
                break;

            case TransportTypes.TCP:
                if (_tcpClient != null)
                {
                    await _tcpClient.DisconnectAsync();

                    _tcpClient.Dispose();
                }

                ConnectButtonVisible = true;
                break;

            case TransportTypes.UDP:
                if (_udpClient != null)
                {
                    await _udpClient.DisconnectAsync();

                    _udpClient.Dispose();
                }
                ConnectButtonVisible = true;
                break;
            }

            SetDisconnectedState();;

            RightMenuIcon = Client.Core.ViewModels.RightMenuIcon.None;
        }
コード例 #2
0
        public async Task <InvokeResult> DisconnectAsync()
        {
            switch (_simulator.DefaultTransport.Value)
            {
            case TransportTypes.AMQP:
                break;

            case TransportTypes.AzureIoTHub:

                if (_azureIoTHubClient != null)
                {
                    await _notificationPublisher.PublishTextAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Disconnecting from {_simulator.DefaultTransport.Text}.");

                    await _azureIoTHubClient.CloseAsync();

                    _azureIoTHubClient.Dispose();
                    _azureIoTHubClient = null;
                }
                else
                {
                    await _notificationPublisher.PublishTextAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Disconnecting called for {_simulator.DefaultTransport.Text} but not connected.");

                    return(InvokeResult.FromError($"Disconnecting called for {_simulator.DefaultTransport.Text} but not connected."));
                }

                break;

            case TransportTypes.MQTT:
                if (_mqttClient != null)
                {
                    await _notificationPublisher.PublishTextAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Disconnecting from {_simulator.DefaultTransport.Text}.");

                    _mqttClient.Disconnect();
                    _mqttClient = null;
                }
                else
                {
                    await _notificationPublisher.PublishTextAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Disconnecting called for {_simulator.DefaultTransport.Text} but not connected.");

                    return(InvokeResult.FromError($"Disconnecting called for {_simulator.DefaultTransport.Text} but not connected."));
                }

                break;

            case TransportTypes.TCP:
                if (_tcpClient != null)
                {
                    await _notificationPublisher.PublishTextAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Disconnecting from {_simulator.DefaultTransport.Text}.");

                    await _tcpClient.DisconnectAsync();

                    _tcpClient.Dispose();
                }
                else
                {
                    await _notificationPublisher.PublishTextAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Disconnecting called for {_simulator.DefaultTransport.Text} but not connected.");

                    return(InvokeResult.FromError($"Disconnecting called for {_simulator.DefaultTransport.Text} but not connected."));
                }

                break;

            case TransportTypes.UDP:
                if (_udpClient != null)
                {
                    await _notificationPublisher.PublishTextAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Disconnecting from {_simulator.DefaultTransport.Text}.");

                    await _udpClient.DisconnectAsync();

                    _udpClient.Dispose();
                }
                else
                {
                    await _notificationPublisher.PublishTextAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Disconnecting called for {_simulator.DefaultTransport.Text} but not connected.");

                    return(InvokeResult.FromError($"Disconnecting called for {_simulator.DefaultTransport.Text} but not connected."));
                }
                break;

            default:
                await _notificationPublisher.PublishTextAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Disconnecting called for {_simulator.DefaultTransport.Text} but not a connection to be closed.");

                return(InvokeResult.FromError($"Disconnecting called for {_simulator.DefaultTransport.Text} but not a connection to be closed."));
            }

            if (_receivingTask != null)
            {
            }

            SetDisconnectedState();

            return(InvokeResult.Success);
        }