VendorIdUpdateService.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 confirmation = await GetEvseCurrentConfig(wsClient.ChargeBoxId, token);
  48. if (confirmation is null)
  49. {
  50. logger.LogWarning("{chargeBoxId} get config from evse failed", wsClient.ChargeBoxId);
  51. return;
  52. }
  53. var vendorId = confirmation.configurationKey?.FirstOrDefault(x => x.key == "ChargePointVendorId");
  54. if (vendorId is null)
  55. {
  56. logger.LogWarning("{chargeBoxId} get vendorId from evse failed", wsClient.ChargeBoxId);
  57. return;
  58. }
  59. SetSessionVendorId(wsClient, vendorId.value);
  60. }
  61. internal void ReplaceVID(WsClientData session, Actions action, IRequest request)
  62. {
  63. if (action != Actions.DataTransfer ||
  64. request is not DataTransferRequest dataTransferRequest)
  65. {
  66. return;
  67. }
  68. string vid = GetSessionVendorId(session);
  69. if (string.IsNullOrEmpty(vid))
  70. {
  71. return;
  72. }
  73. dataTransferRequest.vendorId = vid;
  74. }
  75. private async Task<GetConfigurationConfirmation> GetEvseCurrentConfig(string chargeBoxId, CancellationToken token = default)
  76. {
  77. var sendTask = async () => await messageService.SendGetEVSEConfigureRequest(chargeBoxId);
  78. var response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token);
  79. if (response is GetConfigurationConfirmation confirm)
  80. {
  81. return confirm;
  82. }
  83. return null;
  84. }
  85. private string GetChargeBoxIdVendorId(string chargeBoxId)
  86. {
  87. var clients = protalServer.GetClientDic();
  88. if (!clients.ContainsKey(chargeBoxId))
  89. {
  90. return null;
  91. }
  92. var session = clients[chargeBoxId];
  93. return GetSessionVendorId(session);
  94. }
  95. private string GetSessionVendorId(WsClientData session)
  96. {
  97. if (session is null ||
  98. !session.Data.ContainsKey(Session_VID_Key))
  99. {
  100. return null;
  101. }
  102. return (string)session.Data[Session_VID_Key];
  103. }
  104. private void SetSessionVendorId(WsClientData session, string vid)
  105. {
  106. if (session is null)
  107. {
  108. return;
  109. }
  110. session.Data[Session_VID_Key] = vid;
  111. }
  112. }
  113. }