ConnectionLogdbService.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using EVCB_OCPP.Domain;
  2. using EVCB_OCPP.WSServer.Helper;
  3. using Microsoft.Data.SqlClient;
  4. using Microsoft.EntityFrameworkCore;
  5. using Microsoft.Extensions.Configuration;
  6. using Microsoft.Extensions.Logging;
  7. using OCPPServer.Protocol;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using static System.Runtime.InteropServices.JavaScript.JSType;
  15. namespace EVCB_OCPP.WSServer.Service;
  16. public interface IConnectionLogdbService
  17. {
  18. void WarmUpLog();
  19. void WriteMachineLog(ClientData clientData, string data, string messageType, string errorMsg = "", bool isSent = false);
  20. }
  21. public class ConnectionLogdbService : IConnectionLogdbService
  22. {
  23. public const string LimitConfigKey = "ConnectionLogDbLimit";
  24. public ConnectionLogdbService(
  25. IDbContextFactory<ConnectionLogDBContext> connectionLogdbContextFactory,
  26. ILogger<ConnectionLogdbService> logger,
  27. IConfiguration configuration)
  28. {
  29. this.connectionLogdbContextFactory = connectionLogdbContextFactory;
  30. this.logger = logger;
  31. var opLimit = GetLimit(configuration);
  32. this.queueHandler = new(WriteMachineLog, opLimit);
  33. }
  34. private readonly IDbContextFactory<ConnectionLogDBContext> connectionLogdbContextFactory;
  35. private readonly ILogger<ConnectionLogdbService> logger;
  36. private readonly QueueHandler<MachineLog> queueHandler;
  37. public void WarmUpLog()
  38. {
  39. try
  40. {
  41. using (var log = connectionLogdbContextFactory.CreateDbContext())
  42. {
  43. log.MachineConnectionLog.ToList();
  44. }
  45. }
  46. catch (Exception ex)
  47. {
  48. Console.WriteLine(ex.ToString());
  49. }
  50. }
  51. public void WriteMachineLog(ClientData clientData, string data, string messageType, string errorMsg = "", bool isSent = false)
  52. {
  53. var log = new MachineLog(clientData, data, messageType, errorMsg, isSent);
  54. queueHandler.Enqueue(log);
  55. }
  56. private async Task WriteMachineLog(MachineLog log)
  57. {
  58. try
  59. {
  60. if (log.clientData == null || string.IsNullOrEmpty(log.data)) return;
  61. if (log.clientData.ChargeBoxId == null)
  62. {
  63. logger.LogCritical(log.clientData.Path + "]********************session ChargeBoxId null sessionId=" + log.clientData.SessionID);
  64. }
  65. using (var db = connectionLogdbContextFactory.CreateDbContext())
  66. {
  67. string sp = "[dbo].[uspInsertMachineConnectionLog] @CreatedOn," +
  68. "@ChargeBoxId,@MessageType,@Data,@Msg,@IsSent,@EVSEEndPoint,@Session";
  69. var dd = DateTime.UtcNow;
  70. SqlParameter[] parameter =
  71. {
  72. new SqlParameter("CreatedOn", SqlDbType.DateTime){ Value = dd },
  73. new SqlParameter("ChargeBoxId", SqlDbType.NVarChar, 50){ Value= log.clientData.ChargeBoxId==null?"unknown":log.clientData.ChargeBoxId.Replace("'","''") },
  74. new SqlParameter("MessageType", SqlDbType.NVarChar , 50){ Value = log.messageType.Replace("'","''")},
  75. new SqlParameter("Data", SqlDbType.NVarChar, -1) { Value = log.data.Replace("'", "''") },
  76. new SqlParameter("Msg", SqlDbType.NVarChar, 200) { Value = log.errorMsg.Replace("'", "''") },
  77. new SqlParameter("IsSent", SqlDbType.Bit) { Value = log.isSent },
  78. new SqlParameter("EVSEEndPoint", SqlDbType.NVarChar, 25) { Value = log.clientData.RemoteEndPoint == null ? "123" : log.clientData.RemoteEndPoint.ToString() },
  79. new SqlParameter("Session", SqlDbType.NVarChar, 36) { Value = log.clientData.SessionID == null ? "123" : log.clientData.SessionID }
  80. };
  81. await db.Database.ExecuteSqlRawAsync(sp, parameter);
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. Console.WriteLine(ex.ToString());
  87. }
  88. }
  89. private int GetLimit(IConfiguration configuration)
  90. {
  91. var limitConfig = configuration[LimitConfigKey];
  92. int limit = 10;
  93. if (limitConfig != default)
  94. {
  95. int.TryParse(limitConfig, out limit);
  96. }
  97. return limit;
  98. }
  99. }
  100. internal record MachineLog(ClientData clientData, string data, string messageType, string errorMsg, bool isSent);