using BPA.Helper; using BPASmart.Model; using BPA.Communication; using Newtonsoft.Json; using System.Diagnostics; namespace ComputerTestDemo { public partial class Form1 : Form { List communicationModels = new List(); public Form1() { InitializeComponent(); bool[] cct = new bool[120]; GetData(cct); 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(); })); } private TResult GetData(TResult tc) { if (tc is Array tt) { var len = tt.Length; var va = tt.GetValue(0); } Type type = typeof(TResult); var name = type.Name; Debug.WriteLine(name); return default; } 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); }); } } } }