wenxiangchao
2026-11-02 9d1660270272180cd71147d6ddd53d1382643d2e
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
using Panuon.WPF.UI;
using PLCHelper;
using System;
using System.Collections.Generic;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
 
namespace Plugin.TestResult
{
    /// <summary>
    /// CoolerStatus.xaml 的交互逻辑
    /// </summary>
    public partial class CoolerStatus : WindowX
    {
        private int ovenIndex;
        private int columns;
        private Timer timer;
        private List<Cooler> coolers = new List<Cooler>();
 
        public CoolerStatus(int ovenIndex, int columns, string title)
        {
            DataContext = this;
            this.ovenIndex = ovenIndex;
            this.columns = columns;
            Title = title;
            timer = new Timer();
            timer.Interval = 1000;
            timer.Elapsed += Timer_Elapsed;
            InitializeComponent();
            RefreshUI();
            RefreshStatus();
        }
 
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            RefreshStatus();
        }
 
        private void RefreshStatus()
        {
            foreach (var cooler in coolers)
            {
                try
                {
                    if (S7Context.GetData("", $"Cooler{cooler.index + 1}_Status", out object val))
                    {
                        cooler.Dispatcher.Invoke(() =>
                        {
                            cooler.Status = (ushort)val;
                        });
                    }
                }
                catch (Exception ex)
                {
 
                }
            }
        }
 
        private void RefreshUI()
        {
            mainGrid.Children.Clear();
            mainGrid.ColumnDefinitions.Clear();
 
            for (int i = 0; i < this.columns; i++)
            {
                ColumnDefinition newCol = new ColumnDefinition();
                newCol.Width = new GridLength(1, GridUnitType.Star);
                mainGrid.ColumnDefinitions.Add(newCol);
                var cooler = new Cooler(ovenIndex * columns + i);
                Grid.SetColumn(cooler, i);
                mainGrid.Children.Add(cooler);
                coolers.Add(cooler);
            }
        }
 
        private void WindowX_Closed(object sender, EventArgs e)
        {
            timer.Enabled = false;
            timer.Dispose();
 
            foreach (var cooler in coolers)
            {
                cooler.Release();
            }
        }
 
        private void WindowX_Loaded(object sender, RoutedEventArgs e)
        {
            timer.Enabled = true;
            this.Loaded -= WindowX_Loaded;
        }
    }
}