RWAlarmFile.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. using System.Globalization;
  6. namespace BaseLibRWFile
  7. {
  8. public struct AlarmMsg
  9. {
  10. public string ErrorCode;
  11. public int Level;
  12. public bool Enable;//表示报警是否启用
  13. public string Content;
  14. public string Exception;
  15. public string Solution;
  16. public string PictureName;
  17. public string FileName;
  18. public string PageNo;
  19. public string StartTime;
  20. public string EndTime;
  21. public string TimeSpan;
  22. public string MachineMode;
  23. public string Operator;
  24. public bool IsShow;
  25. public string AlarmPos;
  26. public string CMErrorCode;//CM 客户要求统一代码
  27. }
  28. public struct ErrorMessage
  29. {
  30. public string ErrorCode;
  31. public string Level;
  32. public string Enable;//屏蔽
  33. public string ContentChinese;
  34. public string ContentEnglish;
  35. public string ContentOther;
  36. public string SolutionChinese;
  37. public string SolutionEnglish;
  38. public string SolutionOther;
  39. public string PictureName;
  40. public string PDFFileName;
  41. public string PDFPageNo;
  42. public string SpecifiedErrorCode;
  43. }
  44. public struct AlarmLogSum
  45. {
  46. public string ErrorCode;
  47. public string Level;
  48. public string Enable;
  49. public string Content;
  50. public string Solution;
  51. public double TimeSum;
  52. public int Count;
  53. public string SpecifiedErrorCode;//客户指定ErrCode
  54. }
  55. public class RWAlarmFile
  56. {
  57. static readonly Excel newExcel = new Excel();
  58. public static Dictionary<string, ErrorMessage> ErrorCodeDic = new Dictionary<string, ErrorMessage>();
  59. public static Dictionary<string, AlarmLogSum> AlarmLogSumDic = new Dictionary<string, AlarmLogSum>();
  60. public static void ReadErrorCodeExcel()
  61. {
  62. try
  63. {
  64. ErrorCodeDic.Clear();
  65. string[] strSheetNameArray = new string[1] { "Alarm" };
  66. string strPath = GlobalPara.BaseFilePath + "\\Excel\\ErrorCode.xlsx";
  67. if (!File.Exists(strPath))
  68. {
  69. newExcel.CreatExcel(strPath, strSheetNameArray);
  70. newExcel.SetCellValue(strPath, strSheetNameArray[0], 0, 0, "ErrorCode,Level,Enable,ContentChinese,ContentEnglish,ContentOther," +
  71. "SolutionChinese,SolutionEnglish,SolutionOther,PictureName,PDFFileName,PDFPageNo,SpecifiedErrorCode");
  72. }
  73. string[,] strErrorCode = newExcel.ReadSheetData(strPath, strSheetNameArray[0]);
  74. ErrorMessage ErMsg = new ErrorMessage();
  75. for (int i = 1; i < strErrorCode.GetLongLength(0); i++)
  76. {
  77. ErMsg.ErrorCode = strErrorCode[i, 0];
  78. ErMsg.Level = strErrorCode[i, 1];
  79. ErMsg.Enable = strErrorCode[i, 2];
  80. ErMsg.ContentChinese = strErrorCode[i, 3];
  81. ErMsg.ContentEnglish = strErrorCode[i, 4];
  82. ErMsg.ContentOther = strErrorCode[i, 5];
  83. ErMsg.SolutionChinese = strErrorCode[i, 6];
  84. ErMsg.SolutionEnglish = strErrorCode[i, 7];
  85. ErMsg.SolutionOther = strErrorCode[i, 8];
  86. ErMsg.PictureName = strErrorCode[i, 9];
  87. ErMsg.PDFFileName = strErrorCode[i, 10];
  88. ErMsg.PDFPageNo = strErrorCode[i, 11];
  89. ErMsg.SpecifiedErrorCode = strErrorCode[i, 12];
  90. ErrorCodeDic.Add(ErMsg.ErrorCode, ErMsg);
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. //读取ErrorCode表格失败
  96. //AlarmManager.TriggerAlarm("Err0061", ex.Message, "RWAlarmFile.ReadErrorCodeExcel");
  97. MessageBox.Show($"读取ErrorCode文件出错\r\nError reading ErrorCode file\r\n{ex.Message}", "Warning");
  98. }
  99. }
  100. public static void WriteAlarmFile(AlarmMsg stTemp)
  101. {
  102. string[] StrSheetNameArray = new string[1] { "AlarmLog" };
  103. string strPath = $"{GlobalPara.ProDataFilePath}\\AlarmLog\\{DateTime.Now.ToString("yyyyMMdd")}.xlsx";
  104. string strValue = $"{stTemp.ErrorCode},{stTemp.Level},{stTemp.Enable},{stTemp.Content},{stTemp.Exception},{stTemp.Solution}," +
  105. $"{stTemp.StartTime},{stTemp.EndTime},{stTemp.TimeSpan},{stTemp.MachineMode},{stTemp.Operator},{stTemp.CMErrorCode}";
  106. if (!Directory.Exists($"{GlobalPara.ProDataFilePath}\\AlarmLog"))
  107. {
  108. Directory.CreateDirectory($"{GlobalPara.ProDataFilePath}\\AlarmLog");
  109. }
  110. if (!File.Exists(strPath))
  111. {
  112. newExcel.CreatExcel(strPath, StrSheetNameArray);
  113. newExcel.SetCellValue(strPath, StrSheetNameArray[0], 0, 0, "ErrorCode,Level,Enable,Content,Exception,Solution,StartTime,EndTime,TimeSpan,MachineMode,Operator,SpecifiedErrorCode");
  114. }
  115. newExcel.InsertRow(strPath, 1, strValue, StrSheetNameArray[0]);
  116. }
  117. public static void ReadAlarmFile(int iDays = 30)
  118. {
  119. AlarmLogSum almlog = new AlarmLogSum();
  120. string strPath = $"{GlobalPara.ProDataFilePath}\\AlarmLog";
  121. try
  122. {
  123. FileSystemInfo info = new DirectoryInfo(strPath);
  124. if (!info.Exists) return;
  125. //不是目录
  126. if (!(info is DirectoryInfo dir)) return;//DirectoryInfo dir = info as DirectoryInfo;
  127. FileSystemInfo[] files = dir.GetFileSystemInfos();
  128. AlarmLogSumDic.Clear();
  129. for (int i = 0; i < files.Length; i++)
  130. {
  131. if (files[i] is FileInfo file)//FileInfo file = files[i] as FileInfo; //if (file != null)
  132. {
  133. DateTime fileDate = DateTime.ParseExact(file.Name.Split('.')[0], "yyyyMMdd", CultureInfo.CurrentCulture);
  134. if ((DateTime.Now - fileDate).Days < iDays)
  135. {
  136. string[,] strAlarmLog = newExcel.ReadSheetData(file.FullName, "AlarmLog");
  137. for (int j = 1; j < strAlarmLog.GetLongLength(0); j++)
  138. {
  139. if (!AlarmLogSumDic.ContainsKey(strAlarmLog[j, 0]))
  140. {
  141. //almlog.SpecifiedErrorCode = strAlarmLog[j, 0];
  142. almlog.ErrorCode = strAlarmLog[j, 0];
  143. almlog.Level = strAlarmLog[j, 1];
  144. // almlog.Enable = strAlarmLog[j, 3];
  145. almlog.Content = strAlarmLog[j, 3];
  146. almlog.Solution = strAlarmLog[j, 5];
  147. almlog.TimeSum = Convert.ToDouble(strAlarmLog[j, 8]);
  148. almlog.Count = 1;
  149. AlarmLogSumDic.Add(strAlarmLog[j, 0], almlog);
  150. }
  151. else
  152. {
  153. //almlog.SpecifiedErrorCode = strAlarmLog[j, 0];
  154. almlog.ErrorCode = strAlarmLog[j, 0];
  155. almlog.Level = strAlarmLog[j, 1];
  156. // almlog.Enable = strAlarmLog[j, 3];
  157. almlog.Content = strAlarmLog[j, 3];
  158. almlog.Solution = strAlarmLog[j, 5];
  159. almlog.Count = AlarmLogSumDic[strAlarmLog[j, 0]].Count + 1;
  160. almlog.TimeSum = AlarmLogSumDic[strAlarmLog[j, 0]].TimeSum + Convert.ToDouble(strAlarmLog[j, 8]);
  161. AlarmLogSumDic.Remove(strAlarmLog[j, 0]);
  162. AlarmLogSumDic.Add(strAlarmLog[j, 0], almlog);
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. catch (Exception ex)
  170. {
  171. //读取报警文件出错,请检查文件格式
  172. //AlarmManager.TriggerAlarm("Err0062", ex.Message, "RWAlarmFile.ReadAlarmFile");
  173. MessageBox.Show($"读取报警文件出错,请检查文件格式\r\nRead the alarm file failed,please check file format\r\n{ex.Message}", "Warning");
  174. }
  175. }
  176. }
  177. }