MachineLogDal.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using Cksoft.Data;
  2. using Cksoft.Unity;
  3. using DllEapEntity;
  4. using DllEapEntity.Dtos;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace DllEapDal
  13. {
  14. /// <summary>
  15. /// 机台日志处理
  16. /// </summary>
  17. public class MachineLogDal
  18. {
  19. private readonly IDatabase CurrDb = null;
  20. public MachineLogDal(IDatabase db)
  21. {
  22. CurrDb = db;
  23. }
  24. /// <summary>
  25. /// EAP接口调用30服务器上的日志查看服务获取日志
  26. /// </summary>
  27. /// <param name="macCode"></param>
  28. /// <param name="startTime"></param>
  29. /// <param name="endTime"></param>
  30. /// <param name="errorinfo"></param>
  31. /// <param name="total"></param>
  32. /// <param name="pageIndex"></param>
  33. /// <param name="pageSize"></param>
  34. /// <returns></returns>
  35. public LayuiModel<MachineLog> GetRemotePaged(string macCode, DateTime? startTime, DateTime? endTime,
  36. ref string errorinfo, out int total, int pageIndex = 1, int pageSize = 10)
  37. {
  38. total = 0;
  39. var sql = $@"select a.ipaddress from mqserver a
  40. left join mqserverdetail b on a.id=b.mstid
  41. left join eapappserver c on b.APServerID=c.id
  42. left join eapappservermac d on c.id=d.EapAppserverID
  43. left join machine e on d.macid=e.ID
  44. where e.fcode='{macCode}'";
  45. var mqAddress = CurrDb.FindList<string>(sql).FirstOrDefault();
  46. if (string.IsNullOrEmpty(mqAddress))
  47. {
  48. errorinfo = "设备不存在或未设置AP服务器";
  49. return null;
  50. }
  51. var url = $"http://{mqAddress}:7979/api/machinelog/get";
  52. var dic = new Dictionary<string, string>();
  53. dic.Add("macCode", macCode.ToUpper());
  54. dic.Add("startTime", startTime.Value.ToString("yyyy-MM-dd HH:mm:ss"));
  55. dic.Add("endTime", endTime.Value.ToString("yyyy-MM-dd HH:mm:ss"));
  56. dic.Add("pageIndex", pageIndex.ToString());
  57. dic.Add("pageSize", pageSize.ToString());
  58. var data = HttpRequestHelper<LayuiModel<MachineLog>>.Get(url, dic, ref errorinfo);
  59. if (data != null && data.count > 0)
  60. {
  61. foreach (var item in data.data)
  62. {
  63. item.Content = item.Content.Replace("<", "&lt;").Replace(">", "&gt;").Replace("\n", "<br/>");
  64. }
  65. }
  66. return data;
  67. }
  68. public IEnumerable<MachineLog> Get(string macCode, DateTime? startTime, DateTime? endTime,
  69. ref string errorinfo)
  70. {
  71. var machine = CurrDb.FindListForCondition<Machine>($" and a.FCode = '{macCode}'",
  72. ref errorinfo).FirstOrDefault();
  73. if (machine == null)
  74. {
  75. errorinfo = "机台不存在";
  76. return null;
  77. }
  78. //NetworkCredential credential = new NetworkCredential("baoadmin", "!@#Root202012");
  79. //WebClient webClient = new WebClient();
  80. //webClient.Credentials = credential;
  81. // var path = "/eap/services/Log";
  82. var path = AppConfigurtaionServices.Configuration["LogDir"];
  83. var days = Math.Ceiling((Convert.ToDateTime(endTime.Value.ToString("yyyy-MM-dd 23:59:59")) -
  84. Convert.ToDateTime(startTime.Value.ToString("yyyy-MM-dd 00:00:00"))).TotalDays);
  85. var start = Convert.ToDateTime(startTime.Value.ToString("yyyy-MM-dd"));
  86. var logAll = new List<MachineLog>();
  87. for (int i = 0; i < days; i++)
  88. {
  89. var curr = start.AddDays(i);
  90. var fullPath = path + $"/log{curr.ToString("yyyyMMdd")}/{macCode}_{machine.ID}.log";
  91. if (!File.Exists(fullPath))
  92. {
  93. continue;
  94. }
  95. var lines = File.ReadAllLines(fullPath);
  96. var logs = this.AnalysisBuffer(lines);
  97. logAll = logAll.Concat(logs).ToList();
  98. }
  99. return logAll;
  100. }
  101. public IEnumerable<MachineLog> GetPaged(string macCode, DateTime? startTime, DateTime? endTime,
  102. ref string errorinfo, out int total, int pageIndex = 1, int pageSize = 10)
  103. {
  104. var logs = Get(macCode, startTime, endTime, ref errorinfo);
  105. if (logs == null)
  106. {
  107. total = 0;
  108. return null;
  109. }
  110. logs = logs.Where(c => c.OccurTime >= startTime && c.OccurTime <= endTime);
  111. total = logs.Count();
  112. //return logs.OrderByDescending(c => c.OccurTime).Skip((pageIndex - 1) * pageSize).Take(pageSize);
  113. return logs.OrderBy(c => c.OccurTime).Skip((pageIndex - 1) * pageSize).Take(pageSize);
  114. }
  115. private IEnumerable<MachineLog> AnalysisBuffer(string[] lines)
  116. {
  117. var logs = new List<MachineLog>();
  118. if (lines == null || lines.Length <= 0 || lines.All(c => string.IsNullOrEmpty(c)))
  119. return null;
  120. var index = 0;
  121. var part = new List<string>();
  122. var occurTime = DateTime.Now;
  123. while (index < lines.Length)
  124. {
  125. if (!string.IsNullOrEmpty(lines[index]))
  126. {
  127. if (lines[index].Contains("发生时间"))
  128. {
  129. occurTime = Convert.ToDateTime(lines[index].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
  130. [0].Split(new char[] { ':' })[1]);
  131. }
  132. part.Add(lines[index]);
  133. if (index < lines.Length - 1)
  134. {
  135. if (lines[index + 1].Contains("发生时间"))
  136. {
  137. logs.Add(new MachineLog { OccurTime = occurTime, Content = string.Join("\n", part) });
  138. part = new List<string>();
  139. occurTime = DateTime.Now;
  140. }
  141. }
  142. }
  143. else
  144. {
  145. if (part.Count > 0)
  146. {
  147. logs.Add(new MachineLog { OccurTime = occurTime, Content = string.Join("\n", part) });
  148. part = new List<string>();
  149. occurTime = DateTime.Now;
  150. }
  151. }
  152. index++;
  153. }
  154. return logs;
  155. }
  156. /// <summary>
  157. /// 导出数据到Txt文件
  158. /// </summary>
  159. /// <param name="macCode"></param>
  160. /// <param name="startTime"></param>
  161. /// <param name="endTime"></param>
  162. /// <param name="errorinfo"></param>
  163. /// <returns></returns>
  164. public byte[] ExportTxt(string macCode, DateTime? startTime, DateTime? endTime,
  165. ref string errorinfo)
  166. {
  167. var pageIndex = 1;
  168. var pageSize = int.MaxValue;
  169. int total = 0;
  170. var data = this.GetRemotePaged(macCode, startTime, endTime, ref errorinfo,
  171. out total, pageIndex, pageSize);
  172. if (data == null || data.count <= 0)
  173. return null;
  174. foreach (var item in data.data)
  175. {
  176. item.Content = item.Content.Replace("&lt;", "<").Replace("&gt;", ">").Replace("<br/>", "\n");
  177. }
  178. var str = string.Join("", data.data.Select(c => c.Content));
  179. return Encoding.UTF8.GetBytes(str);
  180. //using (MemoryStream mem = new MemoryStream())
  181. //{
  182. // mem.Position = 0;
  183. // using (StreamWriter sw = new StreamWriter(mem))
  184. // {
  185. // sw.Write(str);
  186. // sw.Flush();
  187. // sw.Close();
  188. // }
  189. // return mem.ToArray();
  190. //}
  191. }
  192. }
  193. // 发生时间:2021-08-13 00:00:11.142 机台ID:837 机台编号:AA00013 信息:Send
  194. //Send 2021-08-13 00:00:11.160
  195. //原始数据:00 00 00 18 00 00 81 03 00 00 00 00 A3 64 01 03 A9 02 00 13 A9 02 00 8F A9 02 00 27
  196. //指令:S1F3 W 序号:41828
  197. //<L[3]
  198. // <U2[1] 19>
  199. // <U2[1] 143>
  200. // <U2[1] 39>
  201. //>
  202. /// <summary>
  203. /// 机台日志
  204. /// </summary>
  205. public class MachineLog
  206. {
  207. /// <summary>
  208. /// 发生时间
  209. /// </summary>
  210. public DateTime OccurTime { get; set; }
  211. /// <summary>
  212. /// 指令发送时间
  213. /// </summary>
  214. public DateTime SendTime { get; set; }
  215. /// <summary>
  216. /// 设备ID
  217. /// </summary>
  218. public int MacId { get; set; }
  219. /// <summary>
  220. /// 设备编号
  221. /// </summary>
  222. public string MacCode { get; set; }
  223. /// <summary>
  224. /// 日志类型(发送或接收)
  225. /// </summary>
  226. public string OperateType { get; set; }
  227. /// <summary>
  228. /// 原始数据
  229. /// </summary>
  230. public string OriginData { get; set; }
  231. /// <summary>
  232. /// 指令
  233. /// </summary>
  234. public string Order { get; set; }
  235. /// <summary>
  236. /// 序号
  237. /// </summary>
  238. public int No { get; set; }
  239. /// <summary>
  240. /// 日志内容
  241. /// </summary>
  242. public string Content { get; set; }
  243. }
  244. }