Example #1
0
        /// <summary>
        /// Запуск задачи соединения с облаком
        /// </summary>
        /// <param name="connection_string"></param>
        /// <param name="client_id"></param>
        private async Task <bool> Start(MQTTClient client)
        {
            return(await Task.Run(() =>
            {
                try
                {
                    var configuration = new MqttConfiguration
                    {
                        //BufferSize = 128 * 1024,
                        Port = client.Port
                               //KeepAliveSecs = 10,
                               //WaitTimeoutSecs = 12,
                               //MaximumQualityOfService = MqttQualityOfService.AtMostOnce
                               //AllowWildcardsInTopicFilters = true
                    };

                    //System.Net.Sockets.Socket.
                    CloudConnectionResult.Message = "В ожидании соединения";
                    _client = MqttClient.CreateAsync(client.Server, configuration).Result;
                    _client.Disconnected += _client_Disconnected;
                    var sessionState = _client.ConnectAsync(new MqttClientCredentials(clientId: client.ClientId, userName: client.UserName, password: client.Password), cleanSession: false).Result;
                    CloudConnectionResult.Message = "Соединение установлено";
                    return true;
                }
                catch (System.Exception ex)
                {
                    CloudConnectionResult.Message = ex.Message;
                    Log.Debug(MQTT_TAG, ex.Message);
                    Toast.MakeText(_context, ex.Message, ToastLength.Long);
                    return false;
                }
            }));
        }
        public override void OnViewCreated(View view, Bundle savedInstanceState)
        {
            base.OnViewCreated(view, savedInstanceState);
            listView = view.FindViewById <ListView>(Resource.Id.ListViewOrders);

            var client_data = new MQTTClient()
            {
                ClientId = "ESP32AndroidReleaseClient",
                Password = mqttPassword,
                Port     = mqttPort,
                Server   = mqttServer,
                UserName = mqttUserName
            };

            mQTT = new MQTTService(client_data, Activity);

            BoxStateAdapter adapter = new BoxStateAdapter(Activity, this.FragmentManager, mQTT);

            listView.Adapter = adapter;

            //firstBox = view.FindViewById<RadioButton>(Resource.Id.RbtnFirstOrder);
            //secondBox = view.FindViewById<RadioButton>(Resource.Id.RbtnSecondOrder);
            //CloudAccessText = view.FindViewById<EditText>(Resource.Id.TextCloudAccessOptions);
            //CommandText = view.FindViewById<EditText>(Resource.Id.TextCommand);
            //Btn_Sending_Command = view.FindViewById<Button>(Resource.Id.ButtonSendCommand);

            //CloudAccessText.Text = topic;
            //CommandText.Text = "0";

            //firstBox.Click += delegate
            //{
            //    topic = "mashtgbr/esp/led";
            //    CloudAccessText.Text = topic;
            //    CommandText.Text = "0";
            //};

            //secondBox.Click += delegate
            //{
            //    topic = "mashtgbr/esp/led2";
            //    CloudAccessText.Text = topic;
            //    CommandText.Text = "0";
            //};

            //Btn_Sending_Command.Click += BtnSendingCommand;

            //AlertDialog.Builder alert = new AlertDialog.Builder(Activity);
            //alert.SetTitle("Статус соединения с облаком");
            //alert.SetMessage(CloudConnectionResult.Message);
            //alert.SetNeutralButton("Закрыть", (senderAlert, args) =>
            //{

            //});
            //Dialog dialog = alert.Create();
            //dialog.Show();
        }
Example #3
0
        private async void StartAsync(MQTTClient client)
        {
            bool result = await Start(client);

            CreateAlertResult();
        }
Example #4
0
 public MQTTService(MQTTClient client, Context context)
 {
     this._context = context;
     StartAsync(client);
 }