Administrator
2021-04-27 63993d30a95fcb678768ce3da82b8f01c99223c7
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
 
namespace BroC.Comn.OEE
{
    public class OEEToolKit
    {
        private DateTime lastClearTime = DateTime.Now;
 
        private int totalCount = 0;
 
        private int okCount = 0;
 
        private int ngCount = 0;
 
        private double ct = 0;
 
        private Dictionary<string, int> ngDic = new Dictionary<string, int>();
 
        private List<DateTime> testEndTimeList = new List<DateTime>();
 
        public int TotalCount
        {
            get => totalCount;
        }
 
        public int OKCount
        {
            get => okCount;
        }
 
        public int NGCount
        {
            get => ngCount;
        }
 
        public double CT
        {
            get => ct;
            set => ct = value;
        }
 
        public double Rate
        {
            get
            {
                if (totalCount == 0)
                    return 0;
                else
                    return okCount * 1.0 / totalCount;
            }
        }
 
        public int UPHAverage
        {
            get
            {
                var timeOffset = DateTime.Now - lastClearTime;
                var averageUPH = timeOffset.TotalSeconds == 0 ? totalCount : totalCount / (timeOffset.TotalSeconds / 3600.0);
                return Convert.ToInt32(averageUPH);
            }
        }
 
        public int UPHReal
        {
            get => testEndTimeList.Count;
        }
 
        public OEEToolKit()
        {
 
        }
 
        public void AddNgType(string type)
        {
            ngDic.Add(type, 0);
        }
 
        public void Update(bool isok)
        {
            var time = DateTime.Now;
 
            testEndTimeList.Add(time);
 
            int removeCount = 0;
 
            for (int i = 0; i < testEndTimeList.Count; i++)
            {
 
                if (testEndTimeList[i].CompareTo(time.AddHours(-1)) < 0)
                {
                    removeCount++;
                }
                else
                {
                    break;
                }
            }
 
            testEndTimeList.RemoveRange(0, removeCount);
 
            totalCount++;
            if (isok)
                okCount++;
            else
                ngCount++;
 
        }
 
        public void Update(string ngType)
        {
            if (ngType != "" && ngDic.ContainsKey(ngType))
            {
                ngDic[ngType]++;
            }
        }
 
        public int GetNgTypeCount(string ngType)
        {
            if (!ngDic.Keys.Contains(ngType))
                return -1;
 
            return ngDic[ngType];
        }
 
        public void Clear()
        {
            lastClearTime = DateTime.Now;
 
            totalCount = 0;
 
            okCount = 0;
 
            ngCount = 0;
 
            var keys = ngDic.Keys.ToArray();
 
            foreach (var item in keys)
            {
                ngDic[item] = 0;
            }
 
            testEndTimeList.Clear();
        }
 
        public void Record(string fileName,string tag="-", string ct = "-")
        {
            FileInfo fileInfo = new FileInfo(fileName);
 
            if (!fileInfo.Directory.Exists)
                fileInfo.Directory.Create();
 
            if (!File.Exists(fileName))
            {
                var title = "开始时间,记录时间,运行时间,标签,产量,良率,OK,NG,";
 
                foreach (var item in ngDic)
                {
                    title += $"{item.Key},";
                }
 
                title += "CT,UPH(实时),UPH(平均),";
 
                File.AppendAllText(fileName, title, Encoding.UTF8);
                File.AppendAllText(fileName, "\r\n", Encoding.UTF8);
            }
 
            var recordTime = DateTime.Now;
            var timeSpan = recordTime - lastClearTime;
 
            var content = $"{lastClearTime:yyyy-MM-dd-HH-mm-ss}," +
                $"{recordTime:yyyy-MM-dd-HH-mm-ss}," +
                $"{timeSpan.Minutes / 60.0:f2}," +
                $"{tag}," +
                $"{TotalCount}," +
                $"{Rate * 100:f2}," +
                $"{OKCount}," +
                $"{NGCount},";
 
            foreach (var item in ngDic)
            {
                content += $"{item.Value},";
            }
 
            content += $"{ct},{UPHReal},{UPHAverage}";
 
            File.AppendAllText(fileName, content, Encoding.UTF8);
            File.AppendAllText(fileName, "\r\n", Encoding.UTF8);
 
            Clear();
        }
 
        public void RecordNewLine(string fileName)
        {
            FileInfo fileInfo = new FileInfo(fileName);
 
            if (!fileInfo.Directory.Exists)
                fileInfo.Directory.Create();
 
            if (!File.Exists(fileName))
            {
                var title = "开始时间,记录时间,运行时间,标签,产量,良率,OK,NG,";
 
                foreach (var item in ngDic)
                {
                    title += $"{item.Key},";
                }
 
                title += "CT,UPH(实时),UPH(平均),";
 
                File.AppendAllText(fileName, title, Encoding.UTF8);
                File.AppendAllText(fileName, "\r\n", Encoding.UTF8);
            }
 
            var content = $"-," +
                $"-," +
                $"-," +
                $"-," +
                $"-," +
                $"-," +
                $"-," +
                $"-,";
 
            File.AppendAllText(fileName, content, Encoding.UTF8);
            File.AppendAllText(fileName, "\r\n", Encoding.UTF8);
        }
 
        public void StartAutoRecord()
        {
 
        }
 
        private void AutoRecordThread()
        {
 
        }
 
    }
}