using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; using System.Globalization; namespace BaseLibRWFile { public struct AlarmMsg { public string ErrorCode; public int Level; public bool Enable;//表示报警是否启用 public string Content; public string Exception; public string Solution; public string PictureName; public string FileName; public string PageNo; public string StartTime; public string EndTime; public string TimeSpan; public string MachineMode; public string Operator; public bool IsShow; public string AlarmPos; public string CMErrorCode;//CM 客户要求统一代码 } public struct ErrorMessage { public string ErrorCode; public string Level; public string Enable;//屏蔽 public string ContentChinese; public string ContentEnglish; public string ContentOther; public string SolutionChinese; public string SolutionEnglish; public string SolutionOther; public string PictureName; public string PDFFileName; public string PDFPageNo; public string SpecifiedErrorCode; } public struct AlarmLogSum { public string ErrorCode; public string Level; public string Enable; public string Content; public string Solution; public double TimeSum; public int Count; public string SpecifiedErrorCode;//客户指定ErrCode } public class RWAlarmFile { static readonly Excel newExcel = new Excel(); public static Dictionary ErrorCodeDic = new Dictionary(); public static Dictionary AlarmLogSumDic = new Dictionary(); public static void ReadErrorCodeExcel() { try { ErrorCodeDic.Clear(); string[] strSheetNameArray = new string[1] { "Alarm" }; string strPath = GlobalPara.BaseFilePath + "\\Excel\\ErrorCode.xlsx"; if (!File.Exists(strPath)) { newExcel.CreatExcel(strPath, strSheetNameArray); newExcel.SetCellValue(strPath, strSheetNameArray[0], 0, 0, "ErrorCode,Level,Enable,ContentChinese,ContentEnglish,ContentOther," + "SolutionChinese,SolutionEnglish,SolutionOther,PictureName,PDFFileName,PDFPageNo,SpecifiedErrorCode"); } string[,] strErrorCode = newExcel.ReadSheetData(strPath, strSheetNameArray[0]); ErrorMessage ErMsg = new ErrorMessage(); for (int i = 1; i < strErrorCode.GetLongLength(0); i++) { ErMsg.ErrorCode = strErrorCode[i, 0]; ErMsg.Level = strErrorCode[i, 1]; ErMsg.Enable = strErrorCode[i, 2]; ErMsg.ContentChinese = strErrorCode[i, 3]; ErMsg.ContentEnglish = strErrorCode[i, 4]; ErMsg.ContentOther = strErrorCode[i, 5]; ErMsg.SolutionChinese = strErrorCode[i, 6]; ErMsg.SolutionEnglish = strErrorCode[i, 7]; ErMsg.SolutionOther = strErrorCode[i, 8]; ErMsg.PictureName = strErrorCode[i, 9]; ErMsg.PDFFileName = strErrorCode[i, 10]; ErMsg.PDFPageNo = strErrorCode[i, 11]; ErMsg.SpecifiedErrorCode = strErrorCode[i, 12]; ErrorCodeDic.Add(ErMsg.ErrorCode, ErMsg); } } catch (Exception ex) { //读取ErrorCode表格失败 //AlarmManager.TriggerAlarm("Err0061", ex.Message, "RWAlarmFile.ReadErrorCodeExcel"); MessageBox.Show($"读取ErrorCode文件出错\r\nError reading ErrorCode file\r\n{ex.Message}", "Warning"); } } public static void WriteAlarmFile(AlarmMsg stTemp) { string[] StrSheetNameArray = new string[1] { "AlarmLog" }; string strPath = $"{GlobalPara.ProDataFilePath}\\AlarmLog\\{DateTime.Now.ToString("yyyyMMdd")}.xlsx"; string strValue = $"{stTemp.ErrorCode},{stTemp.Level},{stTemp.Enable},{stTemp.Content},{stTemp.Exception},{stTemp.Solution}," + $"{stTemp.StartTime},{stTemp.EndTime},{stTemp.TimeSpan},{stTemp.MachineMode},{stTemp.Operator},{stTemp.CMErrorCode}"; if (!Directory.Exists($"{GlobalPara.ProDataFilePath}\\AlarmLog")) { Directory.CreateDirectory($"{GlobalPara.ProDataFilePath}\\AlarmLog"); } if (!File.Exists(strPath)) { newExcel.CreatExcel(strPath, StrSheetNameArray); newExcel.SetCellValue(strPath, StrSheetNameArray[0], 0, 0, "ErrorCode,Level,Enable,Content,Exception,Solution,StartTime,EndTime,TimeSpan,MachineMode,Operator,SpecifiedErrorCode"); } newExcel.InsertRow(strPath, 1, strValue, StrSheetNameArray[0]); } public static void ReadAlarmFile(int iDays = 30) { AlarmLogSum almlog = new AlarmLogSum(); string strPath = $"{GlobalPara.ProDataFilePath}\\AlarmLog"; try { FileSystemInfo info = new DirectoryInfo(strPath); if (!info.Exists) return; //不是目录 if (!(info is DirectoryInfo dir)) return;//DirectoryInfo dir = info as DirectoryInfo; FileSystemInfo[] files = dir.GetFileSystemInfos(); AlarmLogSumDic.Clear(); for (int i = 0; i < files.Length; i++) { if (files[i] is FileInfo file)//FileInfo file = files[i] as FileInfo; //if (file != null) { DateTime fileDate = DateTime.ParseExact(file.Name.Split('.')[0], "yyyyMMdd", CultureInfo.CurrentCulture); if ((DateTime.Now - fileDate).Days < iDays) { string[,] strAlarmLog = newExcel.ReadSheetData(file.FullName, "AlarmLog"); for (int j = 1; j < strAlarmLog.GetLongLength(0); j++) { if (!AlarmLogSumDic.ContainsKey(strAlarmLog[j, 0])) { //almlog.SpecifiedErrorCode = strAlarmLog[j, 0]; almlog.ErrorCode = strAlarmLog[j, 0]; almlog.Level = strAlarmLog[j, 1]; // almlog.Enable = strAlarmLog[j, 3]; almlog.Content = strAlarmLog[j, 3]; almlog.Solution = strAlarmLog[j, 5]; almlog.TimeSum = Convert.ToDouble(strAlarmLog[j, 8]); almlog.Count = 1; AlarmLogSumDic.Add(strAlarmLog[j, 0], almlog); } else { //almlog.SpecifiedErrorCode = strAlarmLog[j, 0]; almlog.ErrorCode = strAlarmLog[j, 0]; almlog.Level = strAlarmLog[j, 1]; // almlog.Enable = strAlarmLog[j, 3]; almlog.Content = strAlarmLog[j, 3]; almlog.Solution = strAlarmLog[j, 5]; almlog.Count = AlarmLogSumDic[strAlarmLog[j, 0]].Count + 1; almlog.TimeSum = AlarmLogSumDic[strAlarmLog[j, 0]].TimeSum + Convert.ToDouble(strAlarmLog[j, 8]); AlarmLogSumDic.Remove(strAlarmLog[j, 0]); AlarmLogSumDic.Add(strAlarmLog[j, 0], almlog); } } } } } } catch (Exception ex) { //读取报警文件出错,请检查文件格式 //AlarmManager.TriggerAlarm("Err0062", ex.Message, "RWAlarmFile.ReadAlarmFile"); MessageBox.Show($"读取报警文件出错,请检查文件格式\r\nRead the alarm file failed,please check file format\r\n{ex.Message}", "Warning"); } } } }