版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

193 líneas
6.2 KiB

  1. using Advantech.Motion;
  2. using Newtonsoft.Json;
  3. using AssistClient.Device;
  4. using AssistClient.Utils;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. using System.Xml.Linq;
  17. namespace AssistClient.UI
  18. {
  19. public partial class UIHeightDev : UserControl
  20. {
  21. SynchronizationContext SyncContext = null;
  22. public Action<string> GetParamsEvent;
  23. //
  24. private HeightDevProp prop = new HeightDevProp(Config.HeightDev_IP,Config.HeightDev_Port);
  25. private HeightDev dev;
  26. public UIHeightDev()
  27. {
  28. InitializeComponent();
  29. propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
  30. refreshUIState();
  31. //获取UI线程同步上下文
  32. SyncContext = SynchronizationContext.Current;
  33. //init();
  34. }
  35. private void refreshUIState()
  36. {
  37. foreach (ToolStripItem item in this.toolStrip1.Items)
  38. {
  39. if (item.Text == "打开设备")
  40. item.Visible = (dev == null || !dev.IsInit);
  41. else
  42. this.propertyGrid1.Enabled = item.Enabled = !(dev == null || !dev.IsInit);
  43. }
  44. }
  45. public void init()
  46. {
  47. this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
  48. this.propertyGrid1.SelectedObject = prop;
  49. dev = new HeightDev(true);
  50. dev.WarningEvent = (level, info) =>
  51. {
  52. txtLog.Text = $"({level}){info}";
  53. };
  54. //DATA
  55. dev.HeightEvent += new System.Action<double>((value) =>
  56. {
  57. prop.HeightValue = value;
  58. this.refreshUI();
  59. });
  60. if (!dev.start(prop.IP, prop.Port))
  61. {
  62. this.closeDev();
  63. return;
  64. }
  65. refreshUIState();
  66. }
  67. public string getParamsData()
  68. {
  69. return prop.serialize();
  70. }
  71. public void setParamsData(string json)
  72. {
  73. if (json == "") return;
  74. prop.deserialize(json);
  75. this.propertyGrid1.Refresh();
  76. }
  77. private void refreshUI()
  78. {
  79. SyncContext.Post(m =>
  80. {
  81. var result = m as string;
  82. propertyGrid1.Refresh();
  83. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  84. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  85. }, "异步操作完成结果");
  86. }
  87. private void closeDev()
  88. {
  89. try
  90. {
  91. if (dev == null)
  92. return;
  93. dev.stop();
  94. }
  95. catch (Exception ex)
  96. {
  97. refreshUIState();
  98. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  99. }
  100. }
  101. protected override void OnHandleDestroyed(EventArgs e)
  102. {
  103. base.OnHandleDestroyed(e);
  104. // 在此添加需要手动释放资源的代码
  105. this.closeDev();
  106. }
  107. private void tbtnRun_Click(object sender, EventArgs e)
  108. {
  109. try
  110. {
  111. //var res=dev.setDigitalValue(prop.ChannelIndex, prop.DigitalValue);
  112. //if (!res)
  113. // throw new Exception("设置失败!");
  114. }
  115. catch(Exception ex)
  116. {
  117. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  118. }
  119. }
  120. private void tbtnExport_Click(object sender, EventArgs e)
  121. {
  122. string filePath = FileUtil.saveAsFile($"Light配置.json", "JSON|*.json");
  123. if (filePath != "")
  124. {
  125. string jsonText = prop.serialize();
  126. File.WriteAllText(filePath, jsonText);
  127. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  128. }
  129. }
  130. private void tbtnImport_Click(object sender, EventArgs e)
  131. {
  132. string filePath = FileUtil.openFile("JSON|*.json");
  133. if (filePath != "")
  134. {
  135. string jsonText = File.ReadAllText(filePath);
  136. prop.deserialize(jsonText);
  137. this.propertyGrid1.Refresh();
  138. //this.propertyGrid1.SelectedObject= prop;
  139. //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  140. }
  141. }
  142. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  143. {
  144. //其中包含了两个重要的属性:OldValue和ChangeItem。
  145. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  146. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  147. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  148. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  149. var oldValue = e.OldValue;
  150. var newValue = e.ChangedItem.Value;
  151. switch (propertyName)
  152. {
  153. //case "ChannelIndex":
  154. // if((int)newValue <1 || (int)newValue >4)
  155. // {
  156. // prop.ChannelIndex = (int)oldValue;
  157. // this.refreshUI();
  158. // }
  159. // else
  160. // {
  161. // dev.getDigitalValue((int)newValue);
  162. // }
  163. // break;
  164. }
  165. }
  166. private void tbtnSave_Click(object sender, EventArgs e)
  167. {
  168. GetParamsEvent?.Invoke(prop.serialize());
  169. }
  170. private void tsbtnOpenDev_Click(object sender, EventArgs e)
  171. {
  172. this.init();
  173. }
  174. }
  175. }