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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using Bro.Common.Helper;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing.Design;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Bro.Common.Model
{
    public class OperationCTCollection : IComplexDisplay
    {
        [Category("CT配置")]
        [Description("CT动作名称")]
        public string Name { get; set; }
 
        [Category("CT配置")]
        [Description("PLC触发索引,从0开始,例如30,表示触发地址是45030")]
        public int TriggerIndex { get; set; }
 
        [Category("CT配置")]
        [Description("操作CT配置集合")]
        [TypeConverter(typeof(CollectionCountConvert))]
        [Editor(typeof(ComplexCollectionEditor<OperationCT>), typeof(UITypeEditor))]
        public ObservableCollection<OperationCT> CTList { get; set; } = new ObservableCollection<OperationCT>();
 
        [Category("CT时间")]
        [Description("操作CT合计")]
        [JsonIgnore]
        [ReadOnly(true)]
        public int CTSum { get; set; } = 0;
 
        [Category("CSV输出")]
        [Description("是否做CSV文件输出,true:将CT统计的CSV文件输出到全局配置CSV的文件夹目录下;false:不做CSV文件输出")]
        public bool IsCSVOutput { get; set; } = false;
 
        public string GetDisplayText()
        {
            return TriggerIndex + "--" + Name;
        }
 
        public OperationCTCollection()
        {
            CTList.CollectionChanged += CTList_CollectionChanged;
        }
 
        private void CTList_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            foreach (var ct in CTList)
            {
                ct.PropertyChanged += CTChanged;
            }
 
            CTChanged(null, null);
        }
 
        private void CTChanged(object sender, PropertyChangedEventArgs args)
        {
            CTSum = CTList.Sum(p => p.OpCT);
        }
 
        #region CSV输出
        static object csvLock = new object();
        public void CSVOutput(string foldPath)
        {
            if (!IsCSVOutput)
            {
                return;
            }
 
            DirectoryInfo dir = new DirectoryInfo(foldPath);
            if (!dir.Exists)
            {
                dir.Create();
            }
 
            string filePath = Path.Combine(foldPath, Name + ".csv");
            bool fileExist = File.Exists(filePath);
 
            lock (csvLock)
            {
                using (StreamWriter writer = new StreamWriter(filePath))
                {
                    if (!fileExist)
                    {
                        writer.WriteLine(CSVHeadOutput());
                        writer.WriteLine(CSVDataOutput());
                    }
                    else
                    {
                        writer.WriteLine(CSVDataOutput());
                    }
 
                    writer.Flush();
                    writer.Close();
                }
            }
        }
 
        private string CSVHeadOutput()
        {
            return String.Join(",", CTList.Select(c => c.OpCode));
        }
 
        private string CSVDataOutput()
        {
            return String.Join(",", CTList.Select(c => c.OpCT));
        }
        #endregion
    }
 
    /// <summary>
    /// 工位动作CT
    /// </summary>
    public class OperationCT : IComplexDisplay, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
 
        [Category("动作CT配置")]
        [Description("PLC触发CT的索引值")]
        public int OpTrigger { get; set; }
 
        [Category("动作CT配置")]
        [Description("动作代号")]
        public string OpCode { get; set; }
 
        [Category("动作CT配置")]
        [Description("动作描述")]
        public string OpDesc { get; set; }
 
        private DateTime? startTime = null;
 
        [Category("CT时间")]
        [Description("动作开始时间")]
        [ReadOnly(true)]
        [TypeConverter(typeof(ShortTimeConverter))]
        [JsonIgnore]
        public DateTime? StartTime
        {
            get => startTime;
            set
            {
                startTime = value;
                CalculateCT();
            }
        }
 
        private DateTime? endTime = null;
 
        [Category("CT时间")]
        [Description("动作结束时间")]
        [ReadOnly(true)]
        [TypeConverter(typeof(ShortTimeConverter))]
        [JsonIgnore]
        public DateTime? EndTime
        {
            get => endTime;
            set
            {
                endTime = value;
                CalculateCT();
            }
        }
 
        private int opCT = 0;
 
        [Category("CT统计")]
        [Description("动作CT时间,单位:ms")]
        [ReadOnly(true)]
        [JsonIgnore]
        public int OpCT
        {
            get
            {
                return opCT;
            }
            set
            {
                if (value > 0)
                {
                    opCT = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("OpCT"));
                }
            }
        }
 
        private void CalculateCT()
        {
            if (StartTime == null || EndTime == null)
            {
                OpCT = 0;
            }
            else
            {
                OpCT = (int)(EndTime.Value - StartTime.Value).TotalMilliseconds;
            }
        }
 
        public string GetDisplayText()
        {
            if (OpCT == 0)
            {
                return OpTrigger + "--" + OpCode + "--" + OpDesc;
            }
            else
            {
                return OpTrigger + "--" + OpCode + ":" + OpCT;
            }
        }
    }
}