示例#1
0
 private void accountsList_CellClick(object sender, DataGridViewCellEventArgs e)
 {
     if (e.ColumnIndex == 0)
     {
         Account currAccount = ((ViewModel)accountsList.Rows[e.RowIndex].DataBoundItem).DataItem;
         currAccount.IsDefault = true;
         MQTTModel mqtt = new MQTTModel();
         mqtt.Accounts.Attach(currAccount);
         var entry = mqtt.Entry(currAccount);
         entry.Property(a => a.IsDefault).IsModified = true;
         mqtt.SaveChanges();
         this.DialogResult = DialogResult.OK;
         this.Close();
     }
     else
     {
         Account   currAccount = ((ViewModel)accountsList.Rows[e.RowIndex].DataBoundItem).DataItem;
         MQTTModel mqtt        = new MQTTModel();
         mqtt.Accounts.Attach(currAccount);
         mqtt.Accounts.Remove(currAccount);
         mqtt.SaveChanges();
         this.accountsList.DataSource = mqtt.Accounts.Select(o => new ViewModel()
         {
             DataItem = o
         }).ToList();
     }
 }
示例#2
0
        private void sendButton_Click(object sender, EventArgs e)
        {
            if (txtContent.Text.Length == 0)
            {
                MessageBox.Show("Content is required");
                return;
            }

            if (txtTopic.Text.Length == 0)
            {
                MessageBox.Show("Topic is required");
                return;
            }

            if (cmbQOS.SelectedIndex == -1)
            {
                MessageBox.Show("Please choose QOS");
                return;
            }

            QOS messageQOS = QOS.AT_MOST_ONCE;

            switch (cmbQOS.SelectedIndex)
            {
            case 1:
                messageQOS = QOS.AT_LEAST_ONCE;
                break;

            case 2:
                messageQOS = QOS.EXACTLY_ONCE;
                break;
            }

            MQTTModel mqtt     = new MQTTModel();
            var       accounts = from a in mqtt.Accounts where a.IsDefault == true select a;

            dal.Message newMessage = new dal.Message();
            newMessage.Content   = Encoding.UTF8.GetBytes(txtContent.Text);
            newMessage.Incoming  = false;
            newMessage.QOS       = messageQOS;
            newMessage.TopicName = txtTopic.Text;
            newMessage.Account   = accounts.First();

            _client.Publish(new mqtt.avps.Topic(newMessage.TopicName, newMessage.QOS), newMessage.Content, chkRetain.Checked, chkDuplicate.Checked);
            mqtt.Accounts.Attach(newMessage.Account);
            mqtt.Messages.Add(newMessage);
            mqtt.Entry(newMessage.Account).State = EntityState.Unchanged;
            mqtt.SaveChanges();

            txtContent.Text      = String.Empty;
            txtTopic.Text        = String.Empty;
            cmbQOS.SelectedIndex = -1;
            chkDuplicate.Checked = false;
            chkRetain.Checked    = false;
            MessageBox.Show("Message Sent Succesfully", "Message Sent", MessageBoxButtons.OK, MessageBoxIcon.Information);

            List <dal.Message> messages = (from m in mqtt.Messages.Include("Account") where m.Account.ID == _account.ID orderby m.ID descending select m).ToList();

            this.Invoke((MethodInvoker) delegate { this.messagesGrid.DataSource = messages; });
            mqtt.Dispose();
        }
        private void loginButton_Click(object sender, EventArgs e)
        {
            if (cmbProtocol.SelectedIndex == 0 || cmbProtocol.SelectedIndex == 3 || cmbProtocol.SelectedIndex == 4)
            {
                if (txtUsername.Text.Length == 0)
                {
                    MessageBox.Show("Please enter username");
                    return;
                }

                if (txtPassword.Text.Length == 0)
                {
                    MessageBox.Show("Please enter password");
                    return;
                }
            }

            if (txtClientID.Text.Length == 0)
            {
                MessageBox.Show("Please enter client ID");
                return;
            }

            if (txtServerHost.Text.Length == 0)
            {
                MessageBox.Show("Please enter server host");
                return;
            }

            if (txtServerPort.Text.Length == 0)
            {
                MessageBox.Show("Please enter server port");
                return;
            }

            Int32 serverPort = 0;

            if (!Int32.TryParse(txtServerPort.Text, out serverPort))
            {
                MessageBox.Show("Server port has invalid value");
                return;
            }

            if (serverPort <= 0 || serverPort > UInt16.MaxValue)
            {
                MessageBox.Show("Server port has invalid value");
                return;
            }

            Int32 keepalive = 0;

            if (txtKeepalive.Text.Length > 0)
            {
                if (!Int32.TryParse(txtKeepalive.Text, out keepalive))
                {
                    MessageBox.Show("Keepalive has invalid value");
                    return;
                }
            }

            if (keepalive <= 0)
            {
                MessageBox.Show("Keepalive has invalid value");
                return;
            }

            if (keepalive >= 65535)
            {
                MessageBox.Show("Keepalive has invalid value");
                return;
            }

            if (cmbProtocol.SelectedIndex == 0 || cmbProtocol.SelectedIndex == 4 || cmbProtocol.SelectedIndex == 1)
            {
                if (txtWill.Text.Length > 0 && txtWillTopic.Text.Length == 0)
                {
                    MessageBox.Show("Both will and will topic are required");
                    return;
                }

                if (txtWill.Text.Length == 0 && txtWillTopic.Text.Length > 0)
                {
                    MessageBox.Show("Both will and will topic are required");
                    return;
                }

                if (txtWill.Text.Length > 0 && cmbQOS.SelectedIndex == -1)
                {
                    MessageBox.Show("Please choose will QOS");
                    return;
                }

                if (txtWill.Text.Length > 1400 && cmbProtocol.SelectedIndex == 1)
                {
                    MessageBox.Show("Maximum Will length for SN could be 1400");
                    return;
                }
            }

            Account newAccount = new Account();

            newAccount.UserName        = txtUsername.Text;
            newAccount.Pass            = txtPassword.Text;
            newAccount.ClientID        = txtClientID.Text;
            newAccount.ServerHost      = txtServerHost.Text;
            newAccount.ServerPort      = serverPort;
            newAccount.CleanSession    = chkCleanSession.Checked;
            newAccount.IsRetain        = chkRetain.Checked;
            newAccount.KeepAlive       = keepalive;
            newAccount.Will            = Encoding.UTF8.GetBytes(txtWill.Text);
            newAccount.WillTopic       = txtWillTopic.Text;
            newAccount.IsSecured       = chkSecurity.Checked;
            newAccount.certificatePass = txtSecurityPassword.Text;
            newAccount.certificate     = txtCertificate.Text;

            switch (cmbProtocol.SelectedIndex)
            {
            case 0:
                newAccount.Protocol = iotbroker.dal.Protocols.MQTT_PROTOCOL;
                break;

            case 1:
                newAccount.Protocol = iotbroker.dal.Protocols.MQTT_SN_PROTOCOL;
                break;

            case 2:
                newAccount.Protocol = iotbroker.dal.Protocols.COAP_PROTOCOL;
                break;

            case 3:
                newAccount.Protocol = iotbroker.dal.Protocols.AMQP_PROTOCOL;
                break;

            case 4:
                newAccount.Protocol = iotbroker.dal.Protocols.WS_PROTOCOL;
                break;
            }

            switch (cmbQOS.SelectedIndex)
            {
            case 0:
                newAccount.QOS = QOS.AT_MOST_ONCE;
                break;

            case 1:
                newAccount.QOS = QOS.AT_LEAST_ONCE;
                break;

            case 2:
                newAccount.QOS = QOS.EXACTLY_ONCE;
                break;
            }

            MQTTModel mqtt     = new MQTTModel();
            var       accounts = from a in mqtt.Accounts where a.ClientID == newAccount.ClientID where a.ServerHost == newAccount.ServerHost where a.ServerPort == newAccount.ServerPort where a.UserName == newAccount.UserName select a;

            if (accounts.Count() > 0)
            {
                MessageBox.Show("This account already exists in list");
                return;
            }

            newAccount.IsDefault = true;
            mqtt.Accounts.Add(newAccount);
            mqtt.SaveChanges();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }