终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

346 lines
15 KiB

  1. using BPASmartClient.Academy._50L;
  2. using BPASmartClient.CustomResource.UserControls.MessageShow;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Controls.Primitives;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace BPASmartClient.Academy.View
  19. {
  20. /// <summary>
  21. /// ReactionKettle500LView.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class ReactionKettle50LView : UserControl
  24. {
  25. private volatile static ReactionKettle50LView _Instance;
  26. public static ReactionKettle50LView GetInstance => _Instance ?? (_Instance = new ReactionKettle50LView());
  27. private ReactionKettle50LView()
  28. {
  29. InitializeComponent();
  30. this.DataContext = ReactionKettle50LViewModel.GetInstance;
  31. }
  32. private void open_Click(object sender, RoutedEventArgs e)
  33. {
  34. if (sender is ButtonBase button && !string.IsNullOrWhiteSpace(button.Tag.ToString().Trim()))
  35. {
  36. if (PlcControl.GetInstance.IsConnect)
  37. {
  38. var tag = button.Tag.ToString().Trim();
  39. //BoolAddEnum addr = (BoolAddEnum)Enum.Parse(typeof(BoolAddEnum), tag);
  40. //var result = PlcControl.GetInstance.Write(addr, true);
  41. //if (result != null && result.IsSuccess)
  42. //{
  43. // Notify(EnumPromptType.Success, $"写入成功", $"成功:写入{tag}:True");
  44. //}
  45. //else
  46. //{
  47. // Notify(EnumPromptType.Error, $"写入失败", $"失败:写入{tag}:True\n{result.Message}");
  48. //}
  49. WriteBooleanAddr(tag,true);
  50. }
  51. else
  52. {
  53. Notify(EnumPromptType.Error, "失败", "设备未连接");
  54. }
  55. }
  56. }
  57. private void close_Click(object sender, RoutedEventArgs e)
  58. {
  59. if (sender is ButtonBase button && !string.IsNullOrWhiteSpace(button.Tag.ToString().Trim()))
  60. {
  61. if (PlcControl.GetInstance.IsConnect)
  62. {
  63. var tag = button.Tag.ToString().Trim();
  64. //BoolAddEnum addr = (BoolAddEnum)Enum.Parse(typeof(BoolAddEnum), tag);
  65. //var result = PlcControl.GetInstance.Write(addr, false);
  66. //if (result != null && result.IsSuccess)
  67. //{
  68. // Notify(EnumPromptType.Success, $"写入成功", $"成功:写入{tag}:False");
  69. //}
  70. //else
  71. //{
  72. // Notify(EnumPromptType.Error, $"写入失败", $"失败:写入{tag}:False\n{result.Message}");
  73. //}
  74. WriteBooleanAddr(tag, false);
  75. }
  76. else
  77. {
  78. Notify(EnumPromptType.Error, "失败", "设备未连接");
  79. }
  80. }
  81. }
  82. private void Notify(EnumPromptType promptType,string title,string msg)
  83. {
  84. App.Current.Dispatcher.Invoke(new Action(() =>
  85. {
  86. NoticeDemoViewModel.OpenMsg(promptType, App.Current.MainWindow, title, msg);
  87. }));
  88. MessageNotify.GetInstance.ShowRunLog($"人工介入:"+msg);
  89. }
  90. private void auto_click(object sender, RoutedEventArgs e)
  91. {
  92. }
  93. private void TempSet_CLick(object sender, RoutedEventArgs e)
  94. {
  95. if (sender is ButtonBase button && !string.IsNullOrWhiteSpace(button.Tag.ToString().Trim()))
  96. {
  97. if (PlcControl.GetInstance.IsConnect)
  98. {
  99. var tag = button.Tag.ToString().Trim();
  100. switch (tag)
  101. {
  102. case "反应釜升温":
  103. if (float.TryParse(reactorHeatUpTemp.Text,out float reactorHeatTemp) && reactorHeatTemp <= 200f && reactorHeatTemp >= 0f)
  104. {
  105. PlcControl.GetInstance.Write(FloatAddEnum.反应釜设定升温温度, MathF.Round(reactorHeatTemp, 2)).OnSuccess((s) =>
  106. {
  107. PlcControl.GetInstance.Write(BoolAddEnum.反应釜升温, true).OnSuccess((s2) =>
  108. {
  109. Notify(EnumPromptType.Success, $"写入成功", $"成功:写入反应釜升温:{reactorHeatTemp}");
  110. }).OnFailure((s) => {
  111. Notify(EnumPromptType.Error, "写入失败", $"{s.Message}。");
  112. });
  113. }).OnFailure((s) => {
  114. Notify(EnumPromptType.Error, "写入失败", $"{s.Message}。");
  115. });
  116. }
  117. else
  118. {
  119. Notify(EnumPromptType.Error, "写入失败", $"输入的温度不是一个有效的数字,范围0-200℃。");
  120. }
  121. break;
  122. case "反应釜降温":
  123. if (float.TryParse(reactorCoolDownTemp.Text, out float reactorCoolTemp) && reactorCoolTemp<=200f && reactorCoolTemp>=0f)
  124. {
  125. PlcControl.GetInstance.Write(FloatAddEnum.反应釜设定降温温度, MathF.Round(reactorCoolTemp, 2)).OnSuccess((s) =>
  126. {
  127. PlcControl.GetInstance.Write(BoolAddEnum.反应釜降温, true).OnSuccess((s2) =>
  128. {
  129. Notify(EnumPromptType.Success, $"写入成功", $"成功:写入反应釜降温:{reactorCoolTemp}");
  130. }).OnFailure((s) => {
  131. Notify(EnumPromptType.Error, "写入失败", $"{s.Message}。");
  132. });
  133. }).OnFailure((s) => {
  134. Notify(EnumPromptType.Error, "写入失败", $"{s.Message}。");
  135. });
  136. }
  137. else
  138. {
  139. Notify(EnumPromptType.Error, "写入失败", $"输入的温度不是一个有效的数字,范围0-200℃。");
  140. }
  141. break;
  142. case "冷凝水罐降温":
  143. if (float.TryParse(ColdWaterTankCoolDownTemp.Text, out float CWTTemp) && CWTTemp <= 200f && CWTTemp >= 0f)
  144. {
  145. PlcControl.GetInstance.Write(FloatAddEnum.冷凝水罐设定降温温度, MathF.Round(CWTTemp, 2)).OnSuccess((s) =>
  146. {
  147. PlcControl.GetInstance.Write(BoolAddEnum.冷凝水罐降温, true).OnSuccess((s2) =>
  148. {
  149. Notify(EnumPromptType.Success, $"写入成功", $"成功:写入冷凝水罐降温:{CWTTemp}");
  150. }).OnFailure((s) => {
  151. Notify(EnumPromptType.Error, "写入失败", $"{s.Message}。");
  152. });
  153. }).OnFailure((s) => {
  154. Notify(EnumPromptType.Error, "写入失败", $"{s.Message}。");
  155. });
  156. }
  157. else
  158. {
  159. Notify(EnumPromptType.Error, "写入失败", $"输入的温度不是一个有效的数字,范围0-200℃。");
  160. }
  161. break;
  162. }
  163. }
  164. else
  165. {
  166. Notify(EnumPromptType.Error, "失败", "设备未连接");
  167. }
  168. }
  169. }
  170. private void TempCLose_CLick(object sender, RoutedEventArgs e)
  171. {
  172. close_Click(sender, e);
  173. }
  174. private void TempAdjust_Click(object sender, RoutedEventArgs e)
  175. {
  176. if (PlcControl.GetInstance.IsConnect)
  177. {
  178. if (float.TryParse(reactorTempAdjustValue.Text, out float reactorAdjustTemp))
  179. {
  180. PlcControl.GetInstance.Write(FloatAddEnum.反应釜温度校准, MathF.Round(reactorAdjustTemp, 2)).OnSuccess((s) =>
  181. {
  182. Notify(EnumPromptType.Success, $"写入校准成功", $"成功:写入反应釜温度校准:{reactorAdjustTemp}");
  183. }).OnFailure((s) =>
  184. {
  185. Notify(EnumPromptType.Error, "写入校准失败", $"{s.Message}。");
  186. });
  187. }
  188. else
  189. {
  190. Notify(EnumPromptType.Error, "写入校准失败", $"【反应釜温度校准】输入的温度不是一个有效的数字");
  191. }
  192. if (float.TryParse(coldWaterTankTempAdjustValue.Text, out float CWTAdjustValue))
  193. {
  194. PlcControl.GetInstance.Write(FloatAddEnum.冷凝水罐温度校准, MathF.Round(CWTAdjustValue, 2)).OnSuccess((s) =>
  195. {
  196. Notify(EnumPromptType.Success, $"写入校准成功", $"成功:写入冷凝水罐温度校准:{CWTAdjustValue}");
  197. }).OnFailure((s) =>
  198. {
  199. Notify(EnumPromptType.Error, "写入校准失败", $"{s.Message}。");
  200. });
  201. }
  202. else
  203. {
  204. Notify(EnumPromptType.Error, "写入校准失败", $"【冷凝水罐温度校准】输入的温度不是一个有效的数字");
  205. }
  206. }
  207. else
  208. {
  209. Notify(EnumPromptType.Error, "校准失败", "设备未连接");
  210. }
  211. }
  212. private void SetReactorSpeed_Click(object sender, RoutedEventArgs e)
  213. {
  214. if (PlcControl.GetInstance.IsConnect)
  215. {
  216. SetParamValue(reactorRotateStirSpeedValue.Text, FloatAddEnum.反应釜旋转炒制速度, 0, 100);
  217. SetParamValue(reactorRotateSlowSpeedValue.Text, FloatAddEnum.反应釜旋转慢速度, 0, 100);
  218. }
  219. else
  220. {
  221. Notify(EnumPromptType.Error, "写入失败", "设备未连接");
  222. }
  223. }
  224. private void SetParamValue(string text,FloatAddEnum floatAdd,float minValue,float maxValue)
  225. {
  226. if (float.TryParse(text, out float tempValue) && tempValue>=minValue && tempValue<=maxValue)
  227. {
  228. PlcControl.GetInstance.Write(floatAdd, MathF.Round(tempValue, 2)).OnSuccess((s) =>
  229. {
  230. Notify(EnumPromptType.Success, $"写入成功", $"成功:写入【{floatAdd.ToString()}】:{tempValue}");
  231. }).OnFailure((s) =>
  232. {
  233. Notify(EnumPromptType.Error, "写入失败", $"{s.Message}。");
  234. });
  235. }
  236. else
  237. {
  238. Notify(EnumPromptType.Error, "写入失败", $"【{floatAdd.ToString()}】输入的温度不是一个有效的数字,限值{minValue} -- {maxValue}。");
  239. }
  240. }
  241. private void steamParamsSet_Click(object sender, RoutedEventArgs e)
  242. {
  243. if (PlcControl.GetInstance.IsConnect)
  244. {
  245. SetParamValue(steamValveApertureValve.Text, FloatAddEnum.蒸汽比例阀开度, 0, 100);
  246. SetParamValue(reactorSteamPressureLimitValue.Text, FloatAddEnum.反应釜蒸汽压力上限, -0.1F, 0.6F);
  247. SetParamValue(reactorPressureLimitValue.Text, FloatAddEnum.反应釜压力上限, 0F, 1F);
  248. }
  249. else
  250. {
  251. Notify(EnumPromptType.Error, "写入失败", "设备未连接");
  252. }
  253. }
  254. private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
  255. {
  256. if (e.Source is HandValve valve)
  257. {
  258. if (PlcControl.GetInstance.IsConnect && valve.Tag != null && !string.IsNullOrWhiteSpace(valve.Tag.ToString().Trim()))
  259. {
  260. var tag = valve.Tag.ToString().Trim();
  261. BoolAddEnum addr = (BoolAddEnum)Enum.Parse(typeof(BoolAddEnum), tag);
  262. DeviceControlView.Show(tag, "未知", valve.OpenEnable ? "打开" : "关闭", new Action(() =>
  263. {
  264. var result = PlcControl.GetInstance.Write(addr, true);
  265. if (result != null && result.IsSuccess)
  266. {
  267. Notify(EnumPromptType.Success, $"写入成功", $"成功:写入{tag}:True");
  268. }
  269. else
  270. {
  271. Notify(EnumPromptType.Error, $"写入失败", $"失败:写入{tag}:True\n{result.Message}");
  272. }
  273. }), new Action(() =>
  274. {
  275. var result = PlcControl.GetInstance.Write(addr, false);
  276. if (result != null && result.IsSuccess)
  277. {
  278. Notify(EnumPromptType.Success, $"写入成功", $"成功:写入{tag}:False");
  279. }
  280. else
  281. {
  282. Notify(EnumPromptType.Error, $"写入失败", $"失败:写入{tag}:False\n{result.Message}");
  283. }
  284. }));
  285. }
  286. else
  287. {
  288. Notify(EnumPromptType.Error, "失败", "设备未连接,不可进行控制。");
  289. }
  290. }
  291. }
  292. private void WriteBooleanAddr(string tagName,bool value)
  293. {
  294. BoolAddEnum addr = BoolAddEnum.反应釜旋转电机;
  295. try
  296. {
  297. addr = (BoolAddEnum)Enum.Parse(typeof(BoolAddEnum), tagName);
  298. }
  299. catch (Exception)
  300. {
  301. Notify(EnumPromptType.Error, "失败", $"未找到该地址[{tagName}]。");
  302. return;
  303. }
  304. var result = PlcControl.GetInstance.Write(addr, value);
  305. if (result != null && result.IsSuccess)
  306. {
  307. Notify(EnumPromptType.Success, $"写入成功", $"成功:写入{tagName}:{value}");
  308. }
  309. else
  310. {
  311. Notify(EnumPromptType.Error, $"写入失败", $"失败:写入{tagName}:{value}\n{result.Message}");
  312. }
  313. }
  314. private void Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  315. {
  316. open_Click(sender,e);
  317. }
  318. private void Button_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  319. {
  320. close_Click(sender,e);
  321. }
  322. }
  323. }