using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using HZY.Framework.DependencyInjection; using Microsoft.Extensions.Logging; using Sunny.UI; using LogLevel = Microsoft.Extensions.Logging.LogLevel; namespace Scada.Page { public partial class PageTotalEquipmentControl : UIPage, ISingletonSelfDependency { private ILogger _logger; public PageTotalEquipmentControl(ILogger logger) { _logger = logger; InitializeComponent(); } private void btn_Start_Common_Click(object sender, EventArgs e) { UISymbolButton uISymbolButton = sender as UISymbolButton; if (!Globals.SiemensClient.Connected) { UIMessageTip.ShowWarning("请先连接到西门子PLC"); return; } if (Globals.PlcWrite(uISymbolButton.TagString, true)) { UIMessageTip.ShowOk("写入成功"); ShowLog($"{uISymbolButton.TagString} 写入成功"); } else { UIMessageTip.ShowError("写入失败"); ShowLog($"{uISymbolButton.TagString} 写入失败", LogLevel.Warning); } } private void ShowLog(string log, LogLevel logLevel = LogLevel.Information) { this.txt_Log.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "=>" + log + Environment.NewLine); switch (logLevel) { case LogLevel.Information: _logger.LogInformation(log); break; case LogLevel.Warning: _logger.LogWarning(log); break; case LogLevel.Error: _logger.LogError(log); break; default: break; } } private void btn_DryRun_ClickEvent(object sender, EventArgs e) { if (!Globals.SiemensClient.Connected) { UIMessageTip.ShowWarning("请先连接到西门子PLC"); return; } if (Globals.PlcWrite(this.btn_DryRun.VariableName, !this.btn_DryRun.CounterButtonState)) { this.btn_DryRun.CounterButtonState = !this.btn_DryRun.CounterButtonState; UIMessageTip.ShowOk("写入成功"); ShowLog($"{btn_DryRun.VariableName} 写入成功"); } else { UIMessageTip.ShowError("写入失败"); ShowLog($"{btn_DryRun.VariableName} 写入失败", LogLevel.Warning); } } private void device_Common_ClickEvent(object sender, EventArgs e) { UserDeviceUnitControl userDeviceUnit = sender as UserDeviceUnitControl; if (!Globals.SiemensClient.Connected) { userDeviceUnit.State = false; UIMessageTip.ShowWarning("请先连接到西门子PLC"); return; } bool state = userDeviceUnit.State; string variableName = state ? userDeviceUnit.OpenVariableName : userDeviceUnit.CloseVariableName; if (Globals.PlcWrite(variableName, state)) { UIMessageTip.ShowOk("写入成功"); ShowLog($"{variableName} 写入成功"); } else { UIMessageTip.ShowError("写入失败"); ShowLog($"{variableName} 写入失败", LogLevel.Warning); } } } }