Ejemplo n.º 1
0
        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}

        private async void ConnectButton_OnClick(object sender, RoutedEventArgs e)
        {
            if (App.Instance.MqttClient != null && App.Instance.MqttClient.IsConnected)
            {
                NavigateToChildPage();
            }

            MqttConnectionInfo connectionInfo = GetConnectionInfo();

            App.Instance.SaveState(App.Instance.MqttConnectionSettingString, connectionInfo);

            if (UseSsl.IsChecked == true)
            {
                App.Instance.MqttClient = MqttClient.CreateSecureClient(SocketEncryption.Ssl);
            }
            else
            {
                App.Instance.MqttClient = MqttClient.CreateClient();
            }

            await App.Instance.ConnectToMqtt(connectionInfo);

            if (App.Instance.MqttClient.IsConnected)
            {
                AppendLogMessage(MessageLevel.Info, "Connection successful.");
                App.Instance.SaveState(App.Instance.MqttConnectionSettingString, connectionInfo);
                App.Instance.SaveState(App.Instance.MqttConnectedSettingString, true);
                NavigateToChildPage();
            }
            else
            {
                AppendLogMessage(MessageLevel.Error, "ERROR: Connection failed.");
                App.Instance.SaveState(App.Instance.MqttConnectedSettingString, false);
            }
        }
Ejemplo n.º 2
0
        internal IAsyncOperation <MqttConnectAckMessage> ConnectToMqtt(MqttConnectionInfo connectionInfo)
        {
            var builder = new MqttConnectMessageBuilder
            {
                ClientId = connectionInfo.ClientName,
                UserName = connectionInfo.Username,
                Password = connectionInfo.Password
            };

            try
            {
                return(MqttClient.ConnectWithMessageAsync(builder, connectionInfo.HostName));
            }
            catch (Exception ex)
            {
                Debug.WriteLine("ERROR: Connection to MQTT broker failed: {0}", ex.Message);
            }
            return(null);
        }