LogHub.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using Cksoft.Unity;
  4. using DllEapBll.Services;
  5. using DllEapDal;
  6. using DllEapEntity;
  7. using DllHsms;
  8. using log4net.Core;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.SignalR;
  11. using Microsoft.Extensions.Configuration;
  12. using Microsoft.Extensions.Logging;
  13. using RabbitMQ.Client;
  14. using RabbitMQ.Client.Events;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.ComponentModel.DataAnnotations;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Threading;
  22. using System.Threading.Tasks;
  23. namespace DllEapBll.SignalR
  24. {
  25. public class LogHub : Hub
  26. {
  27. public LogHandler LogHandler { get; set; }
  28. public LogHub(LogHandler logHandler)
  29. {
  30. this.LogHandler = logHandler;
  31. }
  32. /// <summary>
  33. /// 定时发送信息
  34. /// </summary>
  35. /// <param name="filter"></param>
  36. /// <param name="recipe"></param>
  37. /// <returns></returns>
  38. [AllowAnonymous]
  39. public async Task GetLogStatus(string id)
  40. {
  41. var response = new EapResponse { Code = 1, Msg = string.Empty };
  42. string connectionId = Context.ConnectionId;
  43. string errorinfo = string.Empty;
  44. try
  45. {
  46. while (true)
  47. {
  48. }
  49. }
  50. catch (Exception e)
  51. {
  52. await Clients.Client(connectionId).SendAsync("Error", e.Message);
  53. }
  54. finally
  55. {
  56. await Clients.Client(connectionId).SendAsync("Finished");
  57. // Dispose();
  58. }
  59. }
  60. public EapResponse GetEap()
  61. {
  62. var res = new EapResponse { Code = 1, Msg = string.Empty };
  63. res.Data = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
  64. return res;
  65. }
  66. public async Task SendMessage(string message)
  67. {
  68. await Clients.All.SendAsync("ReceiveUpdate", message);
  69. }
  70. /// <summary>
  71. /// 第一次连接发送信息
  72. /// </summary>
  73. /// <returns></returns>
  74. public override async Task OnConnectedAsync()
  75. {
  76. var connectionId = Context.ConnectionId;
  77. var context = Context.GetHttpContext();
  78. var macCode = Context.GetHttpContext().Request.Query["macCode"].FirstOrDefault();
  79. var macId = Context.GetHttpContext().Request.Query["macId"].FirstOrDefault();
  80. await Clients.Client(connectionId).SendAsync("BeginConnection", new EapResponse { Code = 1, Data = new { MacCode = macCode, Log = "开始接收日志....<br><br>" } });
  81. string errorinfo = string.Empty;
  82. this.LogHandler.Startdll(Convert.ToInt32(macId), connectionId, errorinfo);
  83. }
  84. public override async Task OnDisconnectedAsync(Exception exception)
  85. {
  86. this.LogHandler.Dispose(Context.ConnectionId);
  87. await Clients.Client(Context.ConnectionId).SendAsync("Finished", "停止连接");
  88. }
  89. public EapResponse GetMachine(string filter)
  90. {
  91. var res = new EapResponse { Code = 1, Msg = string.Empty };
  92. string errorinfo = string.Empty;
  93. HsmsLog hsmsLog = new HsmsLog();
  94. hsmsLog.FType = 2;
  95. hsmsLog.MacID = 325;
  96. hsmsLog.Log = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "-->asdgqwec2131231231234231dsfwaeqw123";
  97. hsmsLog.MacCode = "WB00012";
  98. res.Data = hsmsLog;
  99. return res;
  100. //using (IDatabase db = DbFactory.Base("eapslave"))
  101. //{
  102. // db.BeginTrans();
  103. // var dal = new MachineDal(db);
  104. // var entity = dal.Get(Convert.ToInt32(filter));
  105. // if (entity == null)
  106. // {
  107. // res.Code = -1;
  108. // res.Msg = errorinfo;
  109. // return res;
  110. // }
  111. // res.Data = entity;
  112. // return res;
  113. //}
  114. }
  115. }
  116. }