|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using BPA.Helper;
- using BPASmart.Model;
- using BPA.Communication;
- using Newtonsoft.Json;
-
- namespace ComputerTestDemo
- {
- public partial class Form1 : Form
- {
- List<CommunicationModel> communicationModels = new List<CommunicationModel>();
- public Form1()
- {
- InitializeComponent();
- textBox1.Enabled = false;
- button1.Enabled = false;
- Json<CommunicationPar>.Read();
- communicationModels = Json<CommunicationPar>.Data.CommunicationDevices.Where(p => p.DeviceName.Length > 0).ToList();
- communicationModels.ForEach(item => { comboBox1.Items.Add(item.DeviceName); });
- Task.Run(new Action(() =>
- {
- MqttInit();
- }));
- }
-
- MqttHelper mqttHelper = new MqttHelper();
- private void MqttInit()
- {
- mqttHelper.Connect("admin", "fengyoufu067101!@#", "124.222.238.75", 61613, $"ֲʽλ:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
- mqttHelper.ConnectOk = new Action(() =>
- {
- this.Invoke(new Action(() => { textBox1.Enabled = true; button1.Enabled = true; }));
- });
- }
-
- private void Publish(PublishInfo publishInfo)
- {
- mqttHelper.Publish("DistributedHostComputer/Control", JsonConvert.SerializeObject(publishInfo));
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- string DeviceName = comboBox1.Text.Trim();
- string VarName = comboBox2.Text.Trim();
- var resDevices = communicationModels.FirstOrDefault(p => p.DeviceName == DeviceName);
- if (resDevices != null)
- {
- var resVars = resDevices.VarTableModels.FirstOrDefault(p => p.VarName == VarName);
- if (resVars != null)
- {
- PublishInfo publishInfo = new PublishInfo();
- publishInfo.PublishModels.Add(new PublishModel()
- {
- DataType = (EDataType)Enum.Parse(typeof(EDataType), resVars.DataType),
- DeviceName = DeviceName,
- RealAddress = resVars.RealAddress,
- Value = textBox1.Text.Trim(),
- });
- Publish(publishInfo);
- }
- }
- }
-
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- string selectName = comboBox1.Text.Trim();
- var res = communicationModels.FirstOrDefault(p => p.DeviceName == selectName);
- if (res != null)
- {
- comboBox2.Items.Clear();
- res.VarTableModels.ToList()?.ForEach(item => { comboBox2.Items.Add(item.VarName); });
- }
- }
- }
- }
|