终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

74 行
2.8 KiB

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