using BPA.Helper; using BPASmart.Model; using BPA.Communication; using Newtonsoft.Json; namespace ComputerTestDemo { public partial class Form1 : Form { List communicationModels = new List(); public Form1() { InitializeComponent(); textBox1.Enabled = false; button1.Enabled = false; Json.Read(); communicationModels = Json.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); }); } } } }