Skip to content
Permalink
8f83027905
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
596 lines (495 sloc) 24.8 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Client.Options;
using MQTTnet.Extensions.ManagedClient;
using MQTTnet.Client.Connecting;
using System.Threading;
namespace Virtual_Farm
{
public partial class Form1 : Form
{
private object a, b, c, d=0;
private List<Control> originalControls;
private TextBox userInputTextBox;
string broker = "9f9b90d624bc4ae8b7864e07d117aecc.s1.eu.hivemq.cloud";
int port = 8883;
string TOPIC1 = "client/operation1";
string TOPIC2 = "client/operation2";
string TOPIC3 = "broker/operation1";
string TOPIC4 = "broker/operation2";
private readonly MqttFactory factory = new MqttFactory();
private IManagedMqttClient mqttClient;
public Form1()
{
InitializeComponent();
InitializeMqttClient();
originalControls = new List<Control>();
foreach (Control control in Controls)
{
originalControls.Add(control);
}
}
public void ConnectAndSubscribe()
{
var options = new MqttClientOptionsBuilder()
.WithClientId("clientId")
.WithTcpServer("9f9b90d624bc4ae8b7864e07d117aecc.s1.eu.hivemq.cloud", 8883)
.WithCredentials("username", "password")
.Build();
mqttClient = (IManagedMqttClient)new MqttFactory().CreateMqttClient();
mqttClient.ConnectAsync(options).Wait();
mqttClient.UseApplicationMessageReceivedHandler(HandleReceivedMessage);
mqttClient.SubscribeAsync("broker/operation1");
mqttClient.SubscribeAsync("broker/operation2");
}
public void HandleReceivedMessage(MqttApplicationMessageReceivedEventArgs e)
{
string topic = e.ApplicationMessage.Topic;
string payload = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
if (topic == "broker/operation1")
{
Console.WriteLine(payload);
}
else if (topic == "broker/operation2")
{
Console.WriteLine("Plant successfully!");
}
}
public void ClientLoop()
{
while (true)
{
Thread.Sleep(1000); // Adjust sleep time as needed
}
}
public void SendOrder(string order, string topic)
{
MqttApplicationMessage message = new MqttApplicationMessageBuilder()
.WithTopic(topic)
.WithPayload(order)
.WithExactlyOnceQoS()
.WithRetainFlag()
.Build();
mqttClient.PublishAsync(message);
}
private async void InitializeMqttClient()
{
var options = new MqttClientOptionsBuilder()
.WithClientId("fertilizer_sensor")
.WithTcpServer(broker)
.WithCredentials("fertilizer_sensor", "Daleidelei1")
.WithTls(new MqttClientOptionsBuilderTlsParameters
{
UseTls = true,
SslProtocol = System.Security.Authentication.SslProtocols.Tls, // 定义要使用的 TLS 版本
CertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true
})
.Build();
var managedOptions = new ManagedMqttClientOptionsBuilder()
.WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
.WithClientOptions(options)
.Build();
mqttClient = factory.CreateManagedMqttClient();
mqttClient.ConnectedHandler = new MqttClientConnectedHandlerDelegate(async e =>
{
Console.WriteLine("Connected to MQTT broker.");
await mqttClient.SubscribeAsync(TOPIC1);
});
mqttClient.UseDisconnectedHandler(async e =>
{
Console.WriteLine($"Connecting to MQTT broker failed: {e.Exception?.Message}");
});
await mqttClient.StartAsync(managedOptions);
}
private async Task PublishMessageToBrokerAsync(string message)
{
if (mqttClient != null && mqttClient.IsConnected)
{
var messageBuilder = new MqttApplicationMessageBuilder()
.WithTopic(TOPIC1)
.WithPayload(Encoding.UTF8.GetBytes(message))
.WithExactlyOnceQoS()
.WithRetainFlag();
await mqttClient.PublishAsync(messageBuilder.Build());
}
else
{
MessageBox.Show("Error: MQTT client is not connected.");
}
}
private void UserInputTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void GetInputButton_Click(object sender, EventArgs e)
{
System.Windows.Forms.Label textLabel3 = new System.Windows.Forms.Label();
textLabel3.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel3.Text = "The list of states: " + "\n" +
"1. K, 2. N, 3. P, 4. Temperature, 5. Humidity, 6. pH, 7. Rainfall, 8. Pest Situation";
textLabel3.AutoSize = true;
textLabel3.Location = new System.Drawing.Point(20, 250);
Controls.Add(textLabel3);
System.Windows.Forms.Label textLabel4 = new System.Windows.Forms.Label();
textLabel4.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel4.Text = "Please select a state: ";
textLabel4.AutoSize = true;
textLabel4.Location = new System.Drawing.Point(20, 300);
Controls.Add(textLabel4);
TextBox userInputTextBox2 = new TextBox();
userInputTextBox2.Location = new System.Drawing.Point(220, 300);
userInputTextBox2.Size = new System.Drawing.Size(80, 40);
userInputTextBox2.ReadOnly = false;
userInputTextBox2.KeyPress += UserInputTextBox_KeyPress;
Controls.Add(userInputTextBox2);
string userInput2 = userInputTextBox2.Text;
object c = userInput2;
Button getInputButton2 = new Button();
getInputButton2.Text = "Get Input";
getInputButton2.Location = new System.Drawing.Point(240, 330);
getInputButton2.Click += GetInputButton2_Click;
Controls.Add(getInputButton2);
Operation operation = new Operation();
Dictionary<string, object> data = operation.OperationCode(a, b, c, d);
uPLibrary.Networking.M2Mqtt.MqttClient client = new uPLibrary.Networking.M2Mqtt.MqttClient(broker);
client.Connect(clientId);
string dataToSend = $"Operation result: {data}";
client.Publish(TOPIC1, Encoding.UTF8.GetBytes(dataToSend));
client.Disconnect();
}
private void GetInputButton2_Click(object sender, EventArgs e)
{
}
private void GetInputButton3_Click(object sender, EventArgs e)
{
System.Windows.Forms.Label textLabel3 = new System.Windows.Forms.Label();
textLabel3.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel3.Text = "Please enter the number of plants: ";
textLabel3.AutoSize = true;
textLabel3.Location = new System.Drawing.Point(20, 250);
Controls.Add(textLabel3);
TextBox userInputTextBox2 = new TextBox();
userInputTextBox2.Location = new System.Drawing.Point(330, 250);
userInputTextBox2.Size = new System.Drawing.Size(80, 40);
userInputTextBox2.ReadOnly = false;
userInputTextBox2.KeyPress += UserInputTextBox_KeyPress;
Controls.Add(userInputTextBox2);
string userInput2 = userInputTextBox2.Text;
object c = userInput2;
Button getInputButton2 = new Button();
getInputButton2.Text = "Get Input";
getInputButton2.Location = new System.Drawing.Point(320, 280);
getInputButton2.Click += GetInputButton2_Click;
Controls.Add(getInputButton2);
Operation operation = new Operation();
Dictionary<string, object> data = operation.OperationCode(a, b, c, d);
}
private void GetInputButton4_Click(object sender, EventArgs e)
{
System.Windows.Forms.Label textLabel3 = new System.Windows.Forms.Label();
textLabel3.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel3.Text = "Please select a plant to move out: ";
textLabel3.AutoSize = true;
textLabel3.Location = new System.Drawing.Point(20, 250);
Controls.Add(textLabel3);
TextBox userInputTextBox2 = new TextBox();
userInputTextBox2.Location = new System.Drawing.Point(320, 250);
userInputTextBox2.Size = new System.Drawing.Size(80, 40);
userInputTextBox2.ReadOnly = false;
userInputTextBox2.KeyPress += UserInputTextBox_KeyPress;
Controls.Add(userInputTextBox2);
string userInput2 = userInputTextBox2.Text;
object c = userInput2;
Button getInputButton2 = new Button();
getInputButton2.Text = "Get Input";
getInputButton2.Location = new System.Drawing.Point(350, 280);
getInputButton2.Click += GetInputButton2_Click;
Controls.Add(getInputButton2);
Operation operation = new Operation();
Dictionary<string, object> data = operation.OperationCode(a, b, c, d);
}
private void GetInputButton5_Click(object sender, EventArgs e)
{
System.Windows.Forms.Label textLabel3 = new System.Windows.Forms.Label();
textLabel3.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel3.Text = "The list of states: " + "\n" +
"1. K, 2. N, 3. P, 4. Temperature, 5. Humidity, 6. pH, 7. Rainfall, 8. Pest Situation";
textLabel3.AutoSize = true;
textLabel3.Location = new System.Drawing.Point(20, 230);
Controls.Add(textLabel3);
System.Windows.Forms.Label textLabel4 = new System.Windows.Forms.Label();
textLabel4.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel4.Text = "Please select a state: ";
textLabel4.AutoSize = true;
textLabel4.Location = new System.Drawing.Point(20, 280);
Controls.Add(textLabel4);
TextBox userInputTextBox2 = new TextBox();
userInputTextBox2.Location = new System.Drawing.Point(220, 280);
userInputTextBox2.Size = new System.Drawing.Size(80, 40);
userInputTextBox2.ReadOnly = false;
userInputTextBox2.KeyPress += UserInputTextBox_KeyPress;
Controls.Add(userInputTextBox2);
string userInput2 = userInputTextBox2.Text;
object c = userInput2;
Button getInputButton6 = new Button();
getInputButton6.Text = "Get Input";
getInputButton6.Location = new System.Drawing.Point(240, 310);
getInputButton6.Click += GetInputButton6_Click;
Controls.Add(getInputButton6);
Operation operation = new Operation();
Dictionary<string, object> data = operation.OperationCode(a, b, c, d);
}
private void GetInputButton6_Click(object sender, EventArgs e)
{
System.Windows.Forms.Label textLabel3 = new System.Windows.Forms.Label();
textLabel3.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel3.Text = "Please choose '1. increase' or '2. decrease':";
textLabel3.AutoSize = true;
textLabel3.Location = new System.Drawing.Point(20, 340);
Controls.Add(textLabel3);
TextBox userInputTextBox2 = new TextBox();
userInputTextBox2.Location = new System.Drawing.Point(410, 340);
userInputTextBox2.Size = new System.Drawing.Size(80, 40);
userInputTextBox2.ReadOnly = false;
userInputTextBox2.KeyPress += UserInputTextBox_KeyPress;
Controls.Add(userInputTextBox2);
string userInput2 = userInputTextBox2.Text;
object d = userInput2;
Button getInputButton6 = new Button();
getInputButton6.Text = "Get Input";
getInputButton6.Location = new System.Drawing.Point(430, 370);
getInputButton6.Click += GetInputButton2_Click;
Controls.Add(getInputButton6);
Operation operation = new Operation();
Dictionary<string, object> data = operation.OperationCode(a, b, c, d);
}
private void NewButton_Click(object sender, EventArgs e)
{
Controls.Clear();
foreach (Control control in originalControls)
{
Controls.Add(control);
}
}
private void button1_Click_1(object sender, EventArgs e)
{
Controls.Clear();
Button newButton = new Button();
newButton.Text = "Back";
newButton.Location = new System.Drawing.Point(700, 420);
newButton.Click += NewButton_Click;
Controls.Add(newButton);
///Form2 mainform2= new Form2();
//mainform2.Show();
System.Windows.Forms.Label textLabel = new System.Windows.Forms.Label();
textLabel.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel.Text = "The list of plant:" + "\n" + "1. rice, 2. maize, 3. chickpea, 4. kidneybeans, 5. pigeonpeas, " + "\n" + "6. mothbeans, 7. mungbean, 8. blackgram, 9. lentil, 10. pomegranate, " + "\n" +
"11. banana, 12. mango, 13. grapes, 14. watermelon, 15. muskmelon, " + "\n" + "16. apple, 17. orange, 18. papaya, 19. coconut, 20. cotton, " + "\n" + "21. jute, 22. coffee";
textLabel.AutoSize = true;
textLabel.Location = new System.Drawing.Point(20, 10);
Controls.Add(textLabel);
object a = 1;
System.Windows.Forms.Label textLabel2 = new System.Windows.Forms.Label();
textLabel2.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel2.Text = "Please select one of this Plant:";
textLabel2.AutoSize = true;
textLabel2.Location = new System.Drawing.Point(20, 170);
Controls.Add(textLabel2);
TextBox userInputTextBox = new TextBox();
userInputTextBox.Location = new System.Drawing.Point(300, 170);
userInputTextBox.Size = new System.Drawing.Size(80, 40);
userInputTextBox.ReadOnly = false;
userInputTextBox.KeyPress += UserInputTextBox_KeyPress;
Controls.Add(userInputTextBox);
string userInput = userInputTextBox.Text;
object b = userInput;
Button getInputButton = new Button();
getInputButton.Text = "Get Input";
getInputButton.Location = new System.Drawing.Point(350, 200);
getInputButton.Click += GetInputButton_Click;
Controls.Add(getInputButton);
}
private void button2_Click(object sender, EventArgs e)
{
Controls.Clear();
Button newButton = new Button();
newButton.Text = "Back";
newButton.Location = new System.Drawing.Point(700, 420);
newButton.Click += NewButton_Click;
Controls.Add(newButton);
///Form2 mainform2= new Form2();
//mainform2.Show();
object a = 2;
System.Windows.Forms.Label textLabel = new System.Windows.Forms.Label();
textLabel.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel.Text = "The list of plant:" + "\n" + "1. rice, 2. maize, 3. chickpea, 4. kidneybeans, 5. pigeonpeas, " + "\n" + "6. mothbeans, 7. mungbean, 8. blackgram, 9. lentil, 10. pomegranate, " + "\n" +
"11. banana, 12. mango, 13. grapes, 14. watermelon, 15. muskmelon, " + "\n" + "16. apple, 17. orange, 18. papaya, 19. coconut, 20. cotton, " + "\n" + "21. jute, 22. coffee";
textLabel.AutoSize = true;
textLabel.Location = new System.Drawing.Point(20, 10);
Controls.Add(textLabel);
System.Windows.Forms.Label textLabel2 = new System.Windows.Forms.Label();
textLabel2.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel2.Text = "Please select one of this Plant:";
textLabel2.AutoSize = true;
textLabel2.Location = new System.Drawing.Point(20, 170);
Controls.Add(textLabel2);
TextBox userInputTextBox = new TextBox();
userInputTextBox.Location = new System.Drawing.Point(300, 170);
userInputTextBox.Size = new System.Drawing.Size(80, 40);
userInputTextBox.ReadOnly = false;
userInputTextBox.KeyPress += UserInputTextBox_KeyPress;
Controls.Add(userInputTextBox);
string userInput = userInputTextBox.Text;
object b = userInput;
Button getInputButton3 = new Button();
getInputButton3.Text = "Get Input";
getInputButton3.Location = new System.Drawing.Point(350, 200);
getInputButton3.Click += GetInputButton3_Click;
Controls.Add(getInputButton3);
}
private void button3_Click(object sender, EventArgs e)
{
Controls.Clear();
Button newButton = new Button();
newButton.Text = "Back";
newButton.Location = new System.Drawing.Point(700, 420);
newButton.Click += NewButton_Click;
Controls.Add(newButton);
///Form2 mainform2= new Form2();
//mainform2.Show();
object a = 3;
System.Windows.Forms.Label textLabel = new System.Windows.Forms.Label();
textLabel.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel.Text = "The list of plant:" + "\n" + "1. rice, 2. maize, 3. chickpea, 4. kidneybeans, 5. pigeonpeas, " + "\n" + "6. mothbeans, 7. mungbean, 8. blackgram, 9. lentil, 10. pomegranate, " + "\n" +
"11. banana, 12. mango, 13. grapes, 14. watermelon, 15. muskmelon, " + "\n" + "16. apple, 17. orange, 18. papaya, 19. coconut, 20. cotton, " + "\n" + "21. jute, 22. coffee";
textLabel.AutoSize = true;
textLabel.Location = new System.Drawing.Point(20, 10);
Controls.Add(textLabel);
System.Windows.Forms.Label textLabel2 = new System.Windows.Forms.Label();
textLabel2.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel2.Text = "Please select one of this Plant:";
textLabel2.AutoSize = true;
textLabel2.Location = new System.Drawing.Point(20, 170);
Controls.Add(textLabel2);
TextBox userInputTextBox = new TextBox();
userInputTextBox.Location = new System.Drawing.Point(300, 170);
userInputTextBox.Size = new System.Drawing.Size(80, 40);
userInputTextBox.ReadOnly = false;
userInputTextBox.KeyPress += UserInputTextBox_KeyPress;
Controls.Add(userInputTextBox);
string userInput = userInputTextBox.Text;
object b = userInput;
Button getInputButton3 = new Button();
getInputButton3.Text = "Get Input";
getInputButton3.Location = new System.Drawing.Point(350, 200);
getInputButton3.Click += GetInputButton3_Click;
Controls.Add(getInputButton3);
}
private void button4_Click(object sender, EventArgs e)
{
Controls.Clear();
Button newButton = new Button();
newButton.Text = "Back";
newButton.Location = new System.Drawing.Point(700, 420);
newButton.Click += NewButton_Click;
Controls.Add(newButton);
///Form2 mainform2= new Form2();
//mainform2.Show();
object a = 4;
System.Windows.Forms.Label textLabel = new System.Windows.Forms.Label();
textLabel.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel.Text = "The list of plant:" + "\n" + "1. rice, 2. maize, 3. chickpea, 4. kidneybeans, 5. pigeonpeas, " + "\n" + "6. mothbeans, 7. mungbean, 8. blackgram, 9. lentil, 10. pomegranate, " + "\n" +
"11. banana, 12. mango, 13. grapes, 14. watermelon, 15. muskmelon, " + "\n" + "16. apple, 17. orange, 18. papaya, 19. coconut, 20. cotton, " + "\n" + "21. jute, 22. coffee";
textLabel.AutoSize = true;
textLabel.Location = new System.Drawing.Point(20, 10);
Controls.Add(textLabel);
System.Windows.Forms.Label textLabel2 = new System.Windows.Forms.Label();
textLabel2.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel2.Text = "Please select a plant to move in:";
textLabel2.AutoSize = true;
textLabel2.Location = new System.Drawing.Point(20, 170);
Controls.Add(textLabel2);
TextBox userInputTextBox = new TextBox();
userInputTextBox.Location = new System.Drawing.Point(320, 170);
userInputTextBox.Size = new System.Drawing.Size(80, 40);
userInputTextBox.ReadOnly = false;
userInputTextBox.KeyPress += UserInputTextBox_KeyPress;
Controls.Add(userInputTextBox);
string userInput = userInputTextBox.Text;
object b = userInput;
Button getInputButton4 = new Button();
getInputButton4.Text = "Get Input";
getInputButton4.Location = new System.Drawing.Point(350, 200);
getInputButton4.Click += GetInputButton4_Click;
Controls.Add(getInputButton4);
}
private void button5_Click(object sender, EventArgs e)
{
Controls.Clear();
Button newButton = new Button();
newButton.Text = "Back";
newButton.Location = new System.Drawing.Point(700, 420);
newButton.Click += NewButton_Click;
Controls.Add(newButton);
///Form2 mainform2= new Form2();
//mainform2.Show();
object a = 5;
System.Windows.Forms.Label textLabel = new System.Windows.Forms.Label();
textLabel.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel.Text = "The list of plant:" + "\n" + "1. rice, 2. maize, 3. chickpea, 4. kidneybeans, 5. pigeonpeas, " + "\n" + "6. mothbeans, 7. mungbean, 8. blackgram, 9. lentil, 10. pomegranate, " + "\n" +
"11. banana, 12. mango, 13. grapes, 14. watermelon, 15. muskmelon, " + "\n" + "16. apple, 17. orange, 18. papaya, 19. coconut, 20. cotton, " + "\n" + "21. jute, 22. coffee";
textLabel.AutoSize = true;
textLabel.Location = new System.Drawing.Point(20, 10);
Controls.Add(textLabel);
System.Windows.Forms.Label textLabel2 = new System.Windows.Forms.Label();
textLabel2.Font = new Font("Arial", 14, FontStyle.Regular);
textLabel2.Text = "Please select one of this Plant:";
textLabel2.AutoSize = true;
textLabel2.Location = new System.Drawing.Point(20, 170);
Controls.Add(textLabel2);
TextBox userInputTextBox = new TextBox();
userInputTextBox.Location = new System.Drawing.Point(300, 170);
userInputTextBox.Size = new System.Drawing.Size(80, 40);
userInputTextBox.ReadOnly = false;
userInputTextBox.KeyPress += UserInputTextBox_KeyPress;
Controls.Add(userInputTextBox);
string userInput = userInputTextBox.Text;
object b = userInput;
Button getInputButton5 = new Button();
getInputButton5.Text = "Get Input";
getInputButton5.Location = new System.Drawing.Point(350, 200);
getInputButton5.Click += GetInputButton5_Click;
Controls.Add(getInputButton5);
}
private void button9_Click(object sender, EventArgs e)
{
this.Close();
}
}
public class Operation
{
public Dictionary<string, object> OperationCode(object a, object b, object c, object d)
{
Dictionary<string, object> data = new Dictionary<string, object>
{
{ "operation parameter1", a },
{ "operation parameter2", b },
{ "operation parameter3", c },
{ "operation parameter4", d }
};
return data;
}
}
}