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 ngDic = new Dictionary(); private List testEndTimeList = new List(); 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() { } } }