PageTotalEquipmentControl.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using HZY.Framework.DependencyInjection;
  12. using Microsoft.Extensions.Logging;
  13. using Sunny.UI;
  14. using LogLevel = Microsoft.Extensions.Logging.LogLevel;
  15. namespace Scada.Page
  16. {
  17. public partial class PageTotalEquipmentControl : UIPage, ISingletonSelfDependency
  18. {
  19. private ILogger<PageTotalEquipmentControl> _logger;
  20. public PageTotalEquipmentControl(ILogger<PageTotalEquipmentControl> logger)
  21. {
  22. _logger = logger;
  23. InitializeComponent();
  24. LogExtension.ShowMessage = ShowLog;
  25. }
  26. private void btn_Start_Common_Click(object sender, EventArgs e)
  27. {
  28. UISymbolButton uISymbolButton = sender as UISymbolButton;
  29. if (!Globals.SiemensClient.Connected)
  30. {
  31. UIMessageTip.ShowWarning("请先连接到西门子PLC");
  32. return;
  33. }
  34. if (Globals.PlcWrite(uISymbolButton.TagString, true))
  35. {
  36. UIMessageTip.ShowOk("写入成功");
  37. ShowLog($"{uISymbolButton.TagString} 写入成功");
  38. }
  39. else
  40. {
  41. UIMessageTip.ShowError("写入失败");
  42. ShowLog($"{uISymbolButton.TagString} 写入失败", LogLevel.Warning);
  43. }
  44. }
  45. private void ShowLog(string log, LogLevel logLevel = LogLevel.Information)
  46. {
  47. this.txt_Log.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "=>" + log + Environment.NewLine);
  48. switch (logLevel)
  49. {
  50. case LogLevel.Information:
  51. _logger.LogInformation(log);
  52. break;
  53. case LogLevel.Warning:
  54. _logger.LogWarning(log);
  55. break;
  56. case LogLevel.Error:
  57. _logger.LogError(log);
  58. break;
  59. default:
  60. break;
  61. }
  62. }
  63. private void btn_DryRun_ClickEvent(object sender, EventArgs e)
  64. {
  65. if (!Globals.SiemensClient.Connected)
  66. {
  67. UIMessageTip.ShowWarning("请先连接到西门子PLC");
  68. return;
  69. }
  70. if (Globals.PlcWrite(this.btn_DryRun.VariableName, !this.btn_DryRun.CounterButtonState))
  71. {
  72. this.btn_DryRun.CounterButtonState = !this.btn_DryRun.CounterButtonState;
  73. UIMessageTip.ShowOk("写入成功");
  74. ShowLog($"{btn_DryRun.VariableName} 写入成功");
  75. }
  76. else
  77. {
  78. UIMessageTip.ShowError("写入失败");
  79. ShowLog($"{btn_DryRun.VariableName} 写入失败", LogLevel.Warning);
  80. }
  81. }
  82. private void device_Common_ClickEvent(object sender, EventArgs e)
  83. {
  84. UserDeviceUnitControl userDeviceUnit = sender as UserDeviceUnitControl;
  85. if (!Globals.SiemensClient.Connected)
  86. {
  87. userDeviceUnit.State = false;
  88. UIMessageTip.ShowWarning("请先连接到西门子PLC");
  89. return;
  90. }
  91. bool state = userDeviceUnit.State;
  92. string variableName = state ? userDeviceUnit.OpenVariableName : userDeviceUnit.CloseVariableName;
  93. if (Globals.PlcWrite(variableName, state))
  94. {
  95. UIMessageTip.ShowOk("写入成功");
  96. ShowLog($"{variableName} 写入成功");
  97. }
  98. else
  99. {
  100. UIMessageTip.ShowError("写入失败");
  101. ShowLog($"{variableName} 写入失败", LogLevel.Warning);
  102. }
  103. }
  104. }
  105. }