终端一体化运控平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

94 rindas
3.2 KiB

  1. using BPA.Helper;
  2. using BPASmart.Model;
  3. using BPA.Communication;
  4. using Newtonsoft.Json;
  5. using System.Diagnostics;
  6. namespace ComputerTestDemo
  7. {
  8. public partial class Form1 : Form
  9. {
  10. List<CommunicationModel> communicationModels = new List<CommunicationModel>();
  11. public Form1()
  12. {
  13. InitializeComponent();
  14. bool[] cct = new bool[120];
  15. GetData(cct);
  16. textBox1.Enabled = false;
  17. button1.Enabled = false;
  18. Json<CommunicationPar>.Read();
  19. communicationModels = Json<CommunicationPar>.Data.CommunicationDevices.Where(p => p.DeviceName.Length > 0).ToList();
  20. communicationModels.ForEach(item => { comboBox1.Items.Add(item.DeviceName); });
  21. Task.Run(new Action(() =>
  22. {
  23. MqttInit();
  24. }));
  25. }
  26. private TResult GetData<TResult>(TResult tc)
  27. {
  28. if (tc is Array tt)
  29. {
  30. var len = tt.Length;
  31. var va = tt.GetValue(0);
  32. }
  33. Type type = typeof(TResult);
  34. var name = type.Name;
  35. Debug.WriteLine(name);
  36. return default;
  37. }
  38. MqttHelper mqttHelper = new MqttHelper();
  39. private void MqttInit()
  40. {
  41. mqttHelper.Connect("admin", "fengyoufu067101!@#", "124.222.238.75", 61613, $"·Ö²¼Ê½ÉÏλ»ú:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
  42. mqttHelper.ConnectOk = new Action(() =>
  43. {
  44. this.Invoke(new Action(() => { textBox1.Enabled = true; button1.Enabled = true; }));
  45. });
  46. }
  47. private void Publish(PublishInfo publishInfo)
  48. {
  49. mqttHelper.Publish("DistributedHostComputer/Control", JsonConvert.SerializeObject(publishInfo));
  50. }
  51. private void button1_Click(object sender, EventArgs e)
  52. {
  53. string DeviceName = comboBox1.Text.Trim();
  54. string VarName = comboBox2.Text.Trim();
  55. var resDevices = communicationModels.FirstOrDefault(p => p.DeviceName == DeviceName);
  56. if (resDevices != null)
  57. {
  58. var resVars = resDevices.VarTableModels.FirstOrDefault(p => p.VarName == VarName);
  59. if (resVars != null)
  60. {
  61. PublishInfo publishInfo = new PublishInfo();
  62. publishInfo.PublishModels.Add(new PublishModel()
  63. {
  64. DataType = (EDataType)Enum.Parse(typeof(EDataType), resVars.DataType),
  65. DeviceName = DeviceName,
  66. RealAddress = resVars.RealAddress,
  67. Value = textBox1.Text.Trim(),
  68. });
  69. Publish(publishInfo);
  70. }
  71. }
  72. }
  73. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  74. {
  75. string selectName = comboBox1.Text.Trim();
  76. var res = communicationModels.FirstOrDefault(p => p.DeviceName == selectName);
  77. if (res != null)
  78. {
  79. comboBox2.Items.Clear();
  80. res.VarTableModels.ToList()?.ForEach(item => { comboBox2.Items.Add(item.VarName); });
  81. }
  82. }
  83. }
  84. }