public async Task StopAsync()
        {
            lock (_msgSendTimers)
            {
                if (Status != null && !Status.IsRunning)
                {
                    return;
                }


                foreach (var tmr in _msgSendTimers)
                {
                    tmr.Change(Timeout.Infinite, Timeout.Infinite);
                    tmr.Dispose();
                }

                _msgSendTimers.Clear();

                _receiveTaskCancelTokenSource?.Cancel();
                _receiveTaskCancelTokenSource = null;
            }

            switch (_simulator.DefaultTransport.Value)
            {
            case TransportTypes.MQTT:
                DisconnectMQTT();
                break;

            case TransportTypes.AzureIoTHub:
                await DisconnectAzureIoTHubAsync();

                break;

            case TransportTypes.TCP:
                _tcpClient?.DisconnectAsync();
                _tcpClient.Dispose();
                _tcpClient = null;
                break;

            case TransportTypes.UDP:
                await _udpClient?.DisconnectAsync();

                _udpClient.Dispose();
                _udpClient = null;
                break;
            }


            Status = new SimulatorStatus()
            {
                IsRunning = false,
                Text      = "Stopped"
            };

            await _notificationPublisher.PublishAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Simulator Stopped", CurrentState);

            await _notificationPublisher.PublishAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Status Update", Status);
        }
 public void Run(IPAddress address, int port)
 {
     try{
         _TCPClient = new TCPClient(address, port);
         _TCPClient.Connect();
         _TCPClient.RecieveDataEvent += RecieveResponse;
     }catch (SocketException) {
         _TCPClient.Dispose();
         throw new CRClientException("Cant connect to server with this ip:" + address.ToString());
     }
     catch (TCPProtocolException exc)
     {
         _TCPClient.Dispose();
         throw new CRClientException("Cant connect to server with this ip:" + address.ToString(), exc);
     }
 }
Exemplo n.º 3
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;
        }
Exemplo n.º 4
0
        public async void Connect()
        {
            try
            {
                IsBusy = true;
                switch (Model.DefaultTransport.Value)
                {
                /*                    case TransportTypes.AMQP:
                 *                      {
                 *                          var connectionString = $"Endpoint=sb://{Model.DefaultEndPoint}.servicebus.windows.net/;SharedAccessKeyName={Model.AccessKeyName};SharedAccessKey={Model.AccessKey}";
                 *                          var bldr = new EventHubsConnectionStringBuilder(connectionString)
                 *                          {
                 *                              EntityPath = Model.HubName
                 *                          };
                 *
                 *                          _isConnected = true;
                 *                      }
                 *
                 *                      break;*/

                case TransportTypes.AzureIoTHub:
#if IOTHUB
                    var connectionString = $"HostName={Model.DefaultEndPoint};DeviceId={Model.DeviceId};SharedAccessKey={Model.AccessKey}";
                    _azureIoTHubClient = DeviceClient.CreateFromConnectionString(connectionString, Microsoft.Azure.Devices.Client.TransportType.Amqp_Tcp_Only);
                    await _azureIoTHubClient.OpenAsync();

                    ReceivingTask = Task.Run(ReceiveDataFromAzure);
                    SetConnectedState();
#endif
                    break;

                case TransportTypes.MQTT:
                    _mqttClient = SLWIOC.Create <IMQTTDeviceClient>();
                    _mqttClient.ShowDiagnostics = true;
                    _mqttClient.BrokerHostName  = Model.DefaultEndPoint;
                    _mqttClient.BrokerPort      = Model.DefaultPort;
                    _mqttClient.DeviceId        = Model.UserName;
                    _mqttClient.Password        = Model.Password;
                    var result = await _mqttClient.ConnectAsync();

                    if (result.Result == ConnAck.Accepted)
                    {
                        _isConnected = true;
                        if (!String.IsNullOrEmpty(Model.Subscription))
                        {
                            var subscription = new MQTTSubscription()
                            {
                                Topic = Model.Subscription.Replace("~deviceid~", Model.DeviceId),
                                QOS   = EntityHeader <QOS> .Create(QOS.QOS2)
                            };
                            await _mqttClient.SubscribeAsync(subscription);

                            _mqttClient.MessageReceived += _mqttClient_CommandReceived;
                        }

                        SetConnectedState();
                    }
                    else
                    {
                        await Popups.ShowAsync($"{Resources.SimulatorCoreResources.Simulator_ErrorConnecting}: {result.Result.ToString()}");
                    }

                    break;

                case TransportTypes.TCP:
                    _tcpClient = SLWIOC.Create <ITCPClient>();
                    await _tcpClient.ConnectAsync(Model.DefaultEndPoint, Model.DefaultPort);

                    StartReceiveThread();
                    SetConnectedState();
                    break;

                case TransportTypes.UDP:
                    _udpClient = SLWIOC.Create <IUDPClient>();
                    await _udpClient.ConnectAsync(Model.DefaultEndPoint, Model.DefaultPort);

                    StartReceiveThread();
                    SetConnectedState();
                    break;
                }

                RightMenuIcon = Client.Core.ViewModels.RightMenuIcon.None;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                await Popups.ShowAsync(ex.Message);

                if (_mqttClient != null)
                {
                    _mqttClient.Dispose();
                    _mqttClient = null;
                }

#if IOTHUB
                if (_azureIoTHubClient != null)
                {
                    await _azureIoTHubClient.CloseAsync();

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

                if (_tcpClient != null)
                {
                    await _tcpClient.DisconnectAsync();

                    _tcpClient.Dispose();
                    _tcpClient = null;
                }

                if (_udpClient != null)
                {
                    await _udpClient.DisconnectAsync();

                    _udpClient.Dispose();
                    _udpClient = null;
                }

                SetDisconnectedState();
            }
            finally
            {
                IsBusy = false;
            }
        }
        public async Task <InvokeResult> ConnectAsync()
        {
            try
            {
                IsBusy = true;
                switch (_simulator.DefaultTransport.Value)
                {
                /*                    case TransportTypes.AMQP:
                 *                      {
                 *                          var connectionString = $"Endpoint=sb://{Model.DefaultEndPoint}.servicebus.windows.net/;SharedAccessKeyName={Model.AccessKeyName};SharedAccessKey={Model.AccessKey}";
                 *                          var bldr = new EventHubsConnectionStringBuilder(connectionString)
                 *                          {
                 *                              EntityPath = Model.HubName
                 *                          };
                 *
                 *                          _isConnected = true;
                 *                      }
                 *
                 *                      break;*/

                case TransportTypes.AzureIoTHub:
                    await ConnectAzureIoTHubAsync();

                    break;

                case TransportTypes.MQTT:
                    await MQTTConnectAsync();

                    break;

                case TransportTypes.TCP:
                    await _notificationPublisher.PublishTextAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Connecting to {_simulator.DefaultTransport.Text} - {_simulator.DefaultEndPoint} on {_simulator.DefaultPort}.");

                    _tcpClient = _runtimeService.GetTCPClient();
                    await _tcpClient.ConnectAsync(_simulator.DefaultEndPoint, _simulator.DefaultPort);

                    StartReceiveThread();
                    SetConnectedState();

                    await _notificationPublisher.PublishTextAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Connected to {_simulator.DefaultTransport.Text} - {_simulator.DefaultEndPoint} on {_simulator.DefaultPort}.");

                    break;

                case TransportTypes.UDP:
                    await _notificationPublisher.PublishTextAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Connecting to {_simulator.DefaultTransport.Text} - {_simulator.DefaultEndPoint} on {_simulator.DefaultPort}.");

                    _udpClient = _runtimeService.GetUDPCLient();
                    await _udpClient.ConnectAsync(_simulator.DefaultEndPoint, _simulator.DefaultPort);

                    StartReceiveThread();
                    SetConnectedState();

                    await _notificationPublisher.PublishTextAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Connected to {_simulator.DefaultTransport.Text} - {_simulator.DefaultEndPoint} on {_simulator.DefaultPort}.");

                    break;

                case TransportTypes.RestHttp:
                case TransportTypes.RestHttps:
                    break;

                default:
                    await _notificationPublisher.PublishTextAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Attempt to connect to {_simulator.DefaultTransport.Text} that does not allow connections..");

                    break;
                }

                return(InvokeResult.Success);
            }
            catch (Exception ex)
            {
                await _notificationPublisher.PublishTextAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Error connecting to {_simulator.DefaultTransport.Text} - {ex.Message}.");

                if (_mqttClient != null)
                {
                    _mqttClient.Dispose();
                    _mqttClient = null;
                }

                if (_azureIoTHubClient != null)
                {
                    await _azureIoTHubClient.CloseAsync();

                    _azureIoTHubClient.Dispose();
                    _azureIoTHubClient = null;
                }

                if (_tcpClient != null)
                {
                    await _tcpClient.DisconnectAsync();

                    _tcpClient.Dispose();
                    _tcpClient = null;
                }

                if (_udpClient != null)
                {
                    await _udpClient.DisconnectAsync();

                    _udpClient.Dispose();
                    _udpClient = null;
                }

                SetDisconnectedState();

                return(InvokeResult.FromException("ConnectAsync", ex));
            }
            finally
            {
                IsBusy = false;
            }
        }
 public void Dispose()
 {
     _TCPClient.Dispose();
 }