VendorIdUpdateService.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using EVCB_OCPP.Packet.Features;
  2. using EVCB_OCPP.Packet.Messages;
  3. using EVCB_OCPP.Packet.Messages.Core;
  4. using EVCB_OCPP.WSServer.Message;
  5. using EVCB_OCPP.WSServer.Service.WsService;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Hosting;
  8. using Microsoft.Extensions.Logging;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace EVCB_OCPP.WSServer.Service
  15. {
  16. public static class VendorIdUpdateServiceExt
  17. {
  18. public static Task InitVendorIdReplaceService(this IHost host)
  19. {
  20. var server = host.Services.GetRequiredService<ProtalServer>();
  21. var vendorIdReplaceService = host.Services.GetRequiredService<VendorIdUpdateService>();
  22. server.InitActions.Add(vendorIdReplaceService.GetAndRecordVendorId);
  23. return host.Services.GetRequiredService<StationConfigService>().Init();
  24. }
  25. }
  26. public class VendorIdUpdateService
  27. {
  28. public VendorIdUpdateService(
  29. ProtalServer protalServer
  30. , ServerMessageService messageService
  31. , ConfirmWaitingMessageSerevice confirmWaitingMessageSerevice
  32. , ILogger<VendorIdUpdateService> logger
  33. )
  34. {
  35. this.protalServer = protalServer;
  36. this.messageService = messageService;
  37. this.confirmWaitingMessageSerevice = confirmWaitingMessageSerevice;
  38. this.logger = logger;
  39. }
  40. private static string Session_VID_Key = "StationConfigService_Station";
  41. private readonly ProtalServer protalServer;
  42. private readonly ServerMessageService messageService;
  43. private readonly ConfirmWaitingMessageSerevice confirmWaitingMessageSerevice;
  44. private readonly ILogger<VendorIdUpdateService> logger;
  45. internal async Task GetAndRecordVendorId(WsClientData wsClient, CancellationToken token)
  46. {
  47. var getEvseCurrentConfigResponse = await GetEvseCurrentConfig(wsClient.ChargeBoxId, token);
  48. if (!getEvseCurrentConfigResponse.Success ||
  49. getEvseCurrentConfigResponse.Message is not GetConfigurationConfirmation confirmation)
  50. {
  51. logger.LogWarning("{chargeBoxId} get config from evse failed", wsClient.ChargeBoxId);
  52. return;
  53. }
  54. var vendorId = confirmation.configurationKey?.FirstOrDefault(x => x.key == "ChargePointVendorId");
  55. if (vendorId is null)
  56. {
  57. logger.LogWarning("{chargeBoxId} get vendorId from evse failed", wsClient.ChargeBoxId);
  58. return;
  59. }
  60. SetSessionVendorId(wsClient, vendorId.value);
  61. }
  62. internal void ReplaceVID(WsClientData session, Actions action, IRequest request)
  63. {
  64. if (action != Actions.DataTransfer ||
  65. request is not DataTransferRequest dataTransferRequest)
  66. {
  67. return;
  68. }
  69. string vid = GetSessionVendorId(session);
  70. if (string.IsNullOrEmpty(vid))
  71. {
  72. return;
  73. }
  74. dataTransferRequest.vendorId = vid;
  75. }
  76. private async Task<MessageResult> GetEvseCurrentConfig(string chargeBoxId, CancellationToken token = default)
  77. {
  78. var sendTask = async () => await messageService.SendGetEVSEConfigureRequest(chargeBoxId);
  79. var response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token);
  80. return response;
  81. }
  82. private string GetChargeBoxIdVendorId(string chargeBoxId)
  83. {
  84. var clients = protalServer.GetClientDic();
  85. if (!clients.ContainsKey(chargeBoxId))
  86. {
  87. return null;
  88. }
  89. var session = clients[chargeBoxId];
  90. return GetSessionVendorId(session);
  91. }
  92. private string GetSessionVendorId(WsClientData session)
  93. {
  94. if (session is null ||
  95. !session.Data.ContainsKey(Session_VID_Key))
  96. {
  97. return null;
  98. }
  99. return (string)session.Data[Session_VID_Key];
  100. }
  101. private void SetSessionVendorId(WsClientData session, string vid)
  102. {
  103. if (session is null)
  104. {
  105. return;
  106. }
  107. session.Data[Session_VID_Key] = vid;
  108. }
  109. }
  110. }