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; } }