123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- using Cksoft.Data;
- using Cksoft.Unity;
- using DllEapEntity;
- using DllEapEntity.Dtos;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- namespace DllEapDal
- {
- /// <summary>
- /// 机台日志处理
- /// </summary>
- public class MachineLogDal
- {
- private readonly IDatabase CurrDb = null;
- public MachineLogDal(IDatabase db)
- {
- CurrDb = db;
- }
- /// <summary>
- /// EAP接口调用30服务器上的日志查看服务获取日志
- /// </summary>
- /// <param name="macCode"></param>
- /// <param name="startTime"></param>
- /// <param name="endTime"></param>
- /// <param name="errorinfo"></param>
- /// <param name="total"></param>
- /// <param name="pageIndex"></param>
- /// <param name="pageSize"></param>
- /// <returns></returns>
- public LayuiModel<MachineLog> GetRemotePaged(string macCode, DateTime? startTime, DateTime? endTime,
- ref string errorinfo, out int total, int pageIndex = 1, int pageSize = 10)
- {
- total = 0;
- var sql = $@"select a.ipaddress from mqserver a
- left join mqserverdetail b on a.id=b.mstid
- left join eapappserver c on b.APServerID=c.id
- left join eapappservermac d on c.id=d.EapAppserverID
- left join machine e on d.macid=e.ID
- where e.fcode='{macCode}'";
- var mqAddress = CurrDb.FindList<string>(sql).FirstOrDefault();
- if (string.IsNullOrEmpty(mqAddress))
- {
- errorinfo = "设备不存在或未设置AP服务器";
- return null;
- }
- var url = $"http://{mqAddress}:7979/api/machinelog/get";
- var dic = new Dictionary<string, string>();
- dic.Add("macCode", macCode.ToUpper());
- dic.Add("startTime", startTime.Value.ToString("yyyy-MM-dd HH:mm:ss"));
- dic.Add("endTime", endTime.Value.ToString("yyyy-MM-dd HH:mm:ss"));
- dic.Add("pageIndex", pageIndex.ToString());
- dic.Add("pageSize", pageSize.ToString());
- var data = HttpRequestHelper<LayuiModel<MachineLog>>.Get(url, dic, ref errorinfo);
- if (data != null && data.count > 0)
- {
- foreach (var item in data.data)
- {
- item.Content = item.Content.Replace("<", "<").Replace(">", ">").Replace("\n", "<br/>");
- }
- }
- return data;
- }
- public IEnumerable<MachineLog> Get(string macCode, DateTime? startTime, DateTime? endTime,
- ref string errorinfo)
- {
- var machine = CurrDb.FindListForCondition<Machine>($" and a.FCode = '{macCode}'",
- ref errorinfo).FirstOrDefault();
- if (machine == null)
- {
- errorinfo = "机台不存在";
- return null;
- }
- //NetworkCredential credential = new NetworkCredential("baoadmin", "!@#Root202012");
- //WebClient webClient = new WebClient();
- //webClient.Credentials = credential;
- // var path = "/eap/services/Log";
- var path = AppConfigurtaionServices.Configuration["LogDir"];
- var days = Math.Ceiling((Convert.ToDateTime(endTime.Value.ToString("yyyy-MM-dd 23:59:59")) -
- Convert.ToDateTime(startTime.Value.ToString("yyyy-MM-dd 00:00:00"))).TotalDays);
- var start = Convert.ToDateTime(startTime.Value.ToString("yyyy-MM-dd"));
- var logAll = new List<MachineLog>();
- for (int i = 0; i < days; i++)
- {
- var curr = start.AddDays(i);
- var fullPath = path + $"/log{curr.ToString("yyyyMMdd")}/{macCode}_{machine.ID}.log";
- if (!File.Exists(fullPath))
- {
- continue;
- }
- var lines = File.ReadAllLines(fullPath);
- var logs = this.AnalysisBuffer(lines);
- logAll = logAll.Concat(logs).ToList();
- }
- return logAll;
- }
- public IEnumerable<MachineLog> GetPaged(string macCode, DateTime? startTime, DateTime? endTime,
- ref string errorinfo, out int total, int pageIndex = 1, int pageSize = 10)
- {
- var logs = Get(macCode, startTime, endTime, ref errorinfo);
- if (logs == null)
- {
- total = 0;
- return null;
- }
- logs = logs.Where(c => c.OccurTime >= startTime && c.OccurTime <= endTime);
- total = logs.Count();
- //return logs.OrderByDescending(c => c.OccurTime).Skip((pageIndex - 1) * pageSize).Take(pageSize);
- return logs.OrderBy(c => c.OccurTime).Skip((pageIndex - 1) * pageSize).Take(pageSize);
- }
- private IEnumerable<MachineLog> AnalysisBuffer(string[] lines)
- {
- var logs = new List<MachineLog>();
- if (lines == null || lines.Length <= 0 || lines.All(c => string.IsNullOrEmpty(c)))
- return null;
- var index = 0;
- var part = new List<string>();
- var occurTime = DateTime.Now;
- while (index < lines.Length)
- {
- if (!string.IsNullOrEmpty(lines[index]))
- {
- if (lines[index].Contains("发生时间"))
- {
- occurTime = Convert.ToDateTime(lines[index].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
- [0].Split(new char[] { ':' })[1]);
- }
- part.Add(lines[index]);
- if (index < lines.Length - 1)
- {
- if (lines[index + 1].Contains("发生时间"))
- {
- logs.Add(new MachineLog { OccurTime = occurTime, Content = string.Join("\n", part) });
- part = new List<string>();
- occurTime = DateTime.Now;
- }
- }
- }
- else
- {
- if (part.Count > 0)
- {
- logs.Add(new MachineLog { OccurTime = occurTime, Content = string.Join("\n", part) });
- part = new List<string>();
- occurTime = DateTime.Now;
- }
- }
- index++;
- }
- return logs;
- }
- /// <summary>
- /// 导出数据到Txt文件
- /// </summary>
- /// <param name="macCode"></param>
- /// <param name="startTime"></param>
- /// <param name="endTime"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public byte[] ExportTxt(string macCode, DateTime? startTime, DateTime? endTime,
- ref string errorinfo)
- {
- var pageIndex = 1;
- var pageSize = int.MaxValue;
- int total = 0;
- var data = this.GetRemotePaged(macCode, startTime, endTime, ref errorinfo,
- out total, pageIndex, pageSize);
- if (data == null || data.count <= 0)
- return null;
- foreach (var item in data.data)
- {
- item.Content = item.Content.Replace("<", "<").Replace(">", ">").Replace("<br/>", "\n");
- }
- var str = string.Join("", data.data.Select(c => c.Content));
- return Encoding.UTF8.GetBytes(str);
- //using (MemoryStream mem = new MemoryStream())
- //{
- // mem.Position = 0;
- // using (StreamWriter sw = new StreamWriter(mem))
- // {
- // sw.Write(str);
- // sw.Flush();
- // sw.Close();
- // }
- // return mem.ToArray();
- //}
- }
- }
- // 发生时间:2021-08-13 00:00:11.142 机台ID:837 机台编号:AA00013 信息:Send
- //Send 2021-08-13 00:00:11.160
- //原始数据: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
- //指令:S1F3 W 序号:41828
- //<L[3]
- // <U2[1] 19>
- // <U2[1] 143>
- // <U2[1] 39>
- //>
- /// <summary>
- /// 机台日志
- /// </summary>
- public class MachineLog
- {
- /// <summary>
- /// 发生时间
- /// </summary>
- public DateTime OccurTime { get; set; }
- /// <summary>
- /// 指令发送时间
- /// </summary>
- public DateTime SendTime { get; set; }
- /// <summary>
- /// 设备ID
- /// </summary>
- public int MacId { get; set; }
- /// <summary>
- /// 设备编号
- /// </summary>
- public string MacCode { get; set; }
- /// <summary>
- /// 日志类型(发送或接收)
- /// </summary>
- public string OperateType { get; set; }
- /// <summary>
- /// 原始数据
- /// </summary>
- public string OriginData { get; set; }
- /// <summary>
- /// 指令
- /// </summary>
- public string Order { get; set; }
- /// <summary>
- /// 序号
- /// </summary>
- public int No { get; set; }
- /// <summary>
- /// 日志内容
- /// </summary>
- public string Content { get; set; }
- }
- }
|