BasicMessageHandler.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using EVCB_OCPP.Packet.Features;
  2. using EVCB_OCPP.Packet.Messages;
  3. using EVCB_OCPP.Packet.Messages.Basic;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using NLog;
  7. using OCPPServer.Protocol;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using Packet20 = EVCB_OCPP.Packet20;
  14. namespace EVCB_OCPP.WSServer.Message
  15. {
  16. /// <summary>
  17. /// 實現 OCPP 基本傳送規範,
  18. /// 1.訊息 基本格式,將訊息包裝成 Call 、CallResult、CallError 三種格式
  19. /// 2.OCPP 定義的傳送規則:交易相關的訊息必須依照時序性傳送,一個傳完才能接著送下一個(忽略規則 由Center System定義)
  20. /// </summary>
  21. internal class BasicMessageHandler
  22. {
  23. static protected ILogger logger = NLog.LogManager.GetCurrentClassLogger();
  24. #region 傳送 or 解析訊息需要欄位
  25. private const int INDEX_MESSAGEID = 0;
  26. private const int INDEX_UNIQUEID = 1;
  27. internal const int TYPENUMBER_CALL = 2;
  28. private const int INDEX_CALL_ACTION = 2;
  29. private const int INDEX_CALL_PAYLOAD = 3;
  30. internal const int TYPENUMBER_CALLRESULT = 3;
  31. private const int INDEX_CALLRESULT_PAYLOAD = 2;
  32. internal const int TYPENUMBER_CALLERROR = 4;
  33. private const int INDEX_CALLERROR_ERRORCODE = 2;
  34. private const int INDEX_CALLERROR_DESCRIPTION = 3;
  35. private const int INDEX_CALLERROR_PAYLOAD = 4;
  36. private const string CALL_FORMAT = "[2,\"{0}\",\"{1}\",{2}]";
  37. private const string CALLRESULT_FORMAT = "[3,\"{0}\",{1}]";
  38. private const string CALLERROR_FORMAT = "[4,\"{0}\",\"{1}\",\"{2}\",{3}]";
  39. private const string DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
  40. private const string DATE_FORMAT_WITH_MS = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
  41. #endregion
  42. private OCPP16MessageHandler _ocpp16Handler = new OCPP16MessageHandler();
  43. private OCPP20MessageHandler _ocpp20Handler = new OCPP20MessageHandler();
  44. /// <summary>
  45. /// 將收到的封包做基本的拆解分成 Call 、CallResult、CallError
  46. /// </summary>
  47. /// <param name="client"></param>
  48. /// <param name="data"></param>
  49. /// <returns></returns>
  50. internal MessageResult AnalysisReceiveData(ClientData client, string data)
  51. {
  52. MessageResult result = null;
  53. if (client.IsOCPP16)
  54. {
  55. result = _ocpp16Handler.AnalysisReceiveData(client, data);
  56. }
  57. else
  58. {
  59. result = _ocpp20Handler.AnalysisReceiveData(client, data);
  60. }
  61. return result;
  62. }
  63. static internal string GenerateCallError(string uniqueId, string errorCode, string errorDescription)
  64. {
  65. string msg = string.Format(CALLERROR_FORMAT, uniqueId, errorCode, errorDescription, "{}");
  66. return msg;
  67. }
  68. static internal string GenerateConfirmation(string uniqueId,IConfirmation confirmation)
  69. {
  70. string msg = string.Empty;
  71. if (confirmation != null && confirmation.Validate())
  72. {
  73. msg = string.Format(CALLRESULT_FORMAT, uniqueId, JsonConvert.SerializeObject(confirmation, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }));
  74. }
  75. else
  76. {
  77. logger.Error(string.Format("confirmation is null or InVaild in GenerateConfirmation Method"), "Warning");
  78. }
  79. return msg;
  80. }
  81. static internal string GenerateConfirmationofOCPP20(string uniqueId, Packet20.Messages.IConfirmation confirmation)
  82. {
  83. string msg = string.Empty;
  84. if (confirmation != null && confirmation.Validate())
  85. {
  86. msg = string.Format(CALLRESULT_FORMAT, uniqueId, JsonConvert.SerializeObject(confirmation, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }));
  87. }
  88. else
  89. {
  90. logger.Error(string.Format("confirmation is null or InVaild in GenerateConfirmation Method"), "Warning");
  91. }
  92. return msg;
  93. }
  94. static internal string GenerateRequest(string uniqueId, string action,IRequest request)
  95. {
  96. string msg = string.Empty;
  97. if (request != null && request.Validate())
  98. {
  99. msg = string.Format(CALL_FORMAT, uniqueId, action, JsonConvert.SerializeObject(request, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }));
  100. }
  101. else
  102. {
  103. logger.Error(string.Format("confirmation is null or InVaild in GenerateRequest Method"), "Warning");
  104. }
  105. return msg;
  106. }
  107. static internal string GenerateRequestofOCPP20(string uniqueId, string action, Packet20.Messages.IRequest request)
  108. {
  109. string msg = string.Empty;
  110. if (request != null && request.Validate())
  111. {
  112. msg = string.Format(CALL_FORMAT, uniqueId, action, JsonConvert.SerializeObject(request, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }));
  113. }
  114. else
  115. {
  116. logger.Error(string.Format("confirmation is null or InVaild in GenerateRequest Method"), "Warning");
  117. }
  118. return msg;
  119. }
  120. static internal string GenerateDestroyRequest(string uniqueId, string action, string request)
  121. {
  122. return string.Format(CALL_FORMAT, uniqueId, action, request);
  123. }
  124. }
  125. }