public async Task <SessionView> CreateSessionChannelAsync(int indexEndpoint, string username, string password) { SessionView sessionView; UserIdentity userI = new UserIdentity(username, password); var endpoint = new ConfiguredEndpoint(null, endpoints[indexEndpoint]); try { session = await Task.Run(() => { return(Session.Create(config, endpoint, false, "OPC Client", 60000, userI, null)); }); if (session == null) { sessionView = null; } else { session.KeepAlive += OnKeepAliveHandler; EndpointView endpointView = new EndpointView(session.Endpoint.EndpointUrl, session.Endpoint.SecurityMode.ToString(), session.Endpoint.TransportProfileUri, 0, session.Endpoint.SecurityPolicyUri.Split('#')[1]); sessionView = new SessionView(session.SessionId.Identifier.ToString(), session.SessionId.NamespaceIndex.ToString(), session.SessionName, endpointView); } return(sessionView); } catch (ServiceResultException p) { throw new BadUserException(p.Message, p); } }
async void OnSelected(object sender, ItemTappedEventArgs e) { EndpointView selected = e.Item as EndpointView; int i = storedList.endpointList.IndexOf(selected); string action = null; try { action = await DisplayActionSheet("Select authentication mode: ", "cancel", null, "Anonymous", "Username & Password"); if (action.Equals("Anonymous")) { Device.BeginInvokeOnMainThread(() => { UserDialogs.Instance.ShowLoading(); }); try { sessionView = await Task.Run(() => client.CreateSessionChannelAsync(i)); } catch (NotImplementedException) { Device.BeginInvokeOnMainThread(() => { UserDialogs.Instance.HideLoading(); }); await DisplayAlert("Error", "The Endpoint is not supported!", "ok"); return; } catch (UnsupportedEndpointException p) { Device.BeginInvokeOnMainThread(() => { UserDialogs.Instance.HideLoading(); }); await DisplayAlert("Error", p.Message, "ok"); return; } if (sessionView == null) { Device.BeginInvokeOnMainThread(() => { UserDialogs.Instance.HideLoading(); }); await DisplayAlert("Error", "Cannot connect to an OPC UA Server!", "OK"); } else { Device.BeginInvokeOnMainThread(() => { UserDialogs.Instance.HideLoading(); }); await DisplayAlert("Info", "Session created successfully!", "Ok"); ContentPage sessionPage = new SessionPage(client, sessionView); sessionPage.Title = "OPC Session Services"; await Navigation.PushAsync(sessionPage); } } else if (action.Equals("Username & Password")) { var _loginPopup = new LoginPopupPage(client, i, sessionView); await Navigation.PushPopupAsync(_loginPopup); } } catch (NullReferenceException) { } }