wenxiangchao
3 天以前 b544d5cccd76735e034c8055918a50927b437474
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using log4net;
using Plugin.DeviceDebug.Entity;
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
 
namespace Plugin.DeviceDebug.Panels
{
    /// <summary>
    /// IO 点位状态显示模型
    /// </summary>
    public class IoPointStatus
    {
        public string Address { get; set; }
        public bool Value { get; set; }
        public DateTime LastUpdate { get; set; }
    }
 
    /// <summary>
    /// 互锁状态显示模型
    /// </summary>
    public class InterlockStatus
    {
        public string Name { get; set; }
        public string Status { get; set; } // "满足" / "不满足"
    }
 
    /// <summary>
    /// PlcDebugPanel.xaml 的交互逻辑
    /// </summary>
    public partial class PlcDebugPanel : UserControl
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(PlcDebugPanel));
 
        /// <summary>
        /// 是否为维护模式(控制输入可用性)
        /// </summary>
        public static readonly DependencyProperty IsMaintenanceModeProperty =
            DependencyProperty.Register(nameof(IsMaintenanceMode), typeof(bool), typeof(PlcDebugPanel),
                new PropertyMetadata(false));
 
        public bool IsMaintenanceMode
        {
            get => (bool)GetValue(IsMaintenanceModeProperty);
            set => SetValue(IsMaintenanceModeProperty, value);
        }
 
        /// <summary>
        /// 命令执行事件
        /// </summary>
        public event EventHandler<DebugCommand> OnCommandExecute;
 
        /// <summary>
        /// IO 点位状态列表
        /// </summary>
        public ObservableCollection<IoPointStatus> IoStatuses { get; } = new();
 
        /// <summary>
        /// 互锁状态列表
        /// </summary>
        public ObservableCollection<InterlockStatus> Interlocks { get; } = new();
 
        public PlcDebugPanel()
        {
            InitializeComponent();
            ioStatusList.ItemsSource = IoStatuses;
            interlockList.ItemsSource = Interlocks;
 
            // 初始化示例数据
            InitSampleData();
        }
 
        /// <summary>
        /// 初始化示例数据(实际应从 PLC 读取)
        /// </summary>
        private void InitSampleData()
        {
            try
            {
                // 添加示例 IO 点位
                IoStatuses.Add(new IoPointStatus { Address = "M0.0", Value = false, LastUpdate = DateTime.Now });
                IoStatuses.Add(new IoPointStatus { Address = "M0.1", Value = true, LastUpdate = DateTime.Now });
                IoStatuses.Add(new IoPointStatus { Address = "M0.2", Value = false, LastUpdate = DateTime.Now });
                IoStatuses.Add(new IoPointStatus { Address = "I0.0", Value = true, LastUpdate = DateTime.Now });
                IoStatuses.Add(new IoPointStatus { Address = "Q0.0", Value = false, LastUpdate = DateTime.Now });
 
                // 添加示例互锁
                Interlocks.Add(new InterlockStatus { Name = "安全门关闭", Status = "满足" });
                Interlocks.Add(new InterlockStatus { Name = "急停未触发", Status = "满足" });
                Interlocks.Add(new InterlockStatus { Name = "气压正常", Status = "不满足" });
                Interlocks.Add(new InterlockStatus { Name = "温度正常", Status = "满足" });
            }
            catch (Exception ex)
            {
                log.Error("初始化示例数据失败", ex);
            }
        }
 
        /// <summary>
        /// 更新 IO 点位状态
        /// </summary>
        public void UpdateIoStatus(string address, bool value)
        {
            try
            {
                var existing = IoStatuses.FirstOrDefault(s => s.Address == address);
                if (existing != null)
                {
                    existing.Value = value;
                    existing.LastUpdate = DateTime.Now;
                }
                else
                {
                    IoStatuses.Add(new IoPointStatus
                    {
                        Address = address,
                        Value = value,
                        LastUpdate = DateTime.Now
                    });
                }
            }
            catch (Exception ex)
            {
                log.Error($"更新 IO 状态失败: {address}", ex);
            }
        }
 
        /// <summary>
        /// 更新互锁状态
        /// </summary>
        public void UpdateInterlock(string name, bool satisfied)
        {
            try
            {
                var existing = Interlocks.FirstOrDefault(i => i.Name == name);
                if (existing != null)
                {
                    existing.Status = satisfied ? "满足" : "不满足";
                }
                else
                {
                    Interlocks.Add(new InterlockStatus
                    {
                        Name = name,
                        Status = satisfied ? "满足" : "不满足"
                    });
                }
            }
            catch (Exception ex)
            {
                log.Error($"更新互锁状态失败: {name}", ex);
            }
        }
 
        private void WriteBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsMaintenanceMode)
                {
                    MessageBox.Show("当前为只读模式,无法执行写入操作", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
 
                string address = addressInput.Text.Trim();
                if (string.IsNullOrEmpty(address))
                {
                    MessageBox.Show("请输入 IO 地址", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
 
                bool value = (valueSelect.SelectedItem as ComboBoxItem)?.Content?.ToString() == "True";
 
                // 创建命令
                var command = new PlcWriteCommand
                {
                    IoAddress = address,
                    Value = value
                };
 
                // 触发命令执行事件
                OnCommandExecute?.Invoke(this, command);
 
                // 更新本地显示
                UpdateIoStatus(address, value);
 
                log.Info($"写入 PLC IO: {address} = {value}");
            }
            catch (Exception ex)
            {
                log.Error("写入 PLC IO 失败", ex);
                MessageBox.Show($"写入失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}