ClientData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using OCPPPacket.Packet.Messages.Basic;
  2. using Packet.Cmd;
  3. using SuperSocket.SocketBase;
  4. using SuperWebSocket;
  5. using System;
  6. using System.Collections.Generic;
  7. namespace OCPPServer.Protocol
  8. {
  9. public class ClientData : WebSocketSession<ClientData>
  10. {
  11. public string ocppmessage = String.Empty;
  12. public delegate void OCPPClientDataEventHandler<ClientData, String>(ClientData clientdata, String msg);
  13. public event OCPPClientDataEventHandler<ClientData, String> m_ReceiveData;
  14. public CmdHelper CmdHelper;
  15. public bool IsCheckIn { get; set; }
  16. public string MachineId { get; set; }
  17. public Guid CustomerId { get; set; }
  18. public string CustomerName { get; set; }
  19. /// <summary>
  20. /// 每個client有他自己的指令序號 2 byte
  21. /// </summary>
  22. private ushort _cmdSerNum = 0;
  23. /// <summary>
  24. /// 傳送的封包序號 1 byte
  25. /// </summary>
  26. private byte _serNum = 0;
  27. /// <summary>
  28. /// 傳送的chargingProfileId 1 byte
  29. /// </summary>
  30. private byte _chargingProfileId = 0;
  31. /// <summary>
  32. /// 取得指令序號 0-65535
  33. /// </summary>
  34. /// <returns></returns>
  35. public ushort GetCmdSerNum()
  36. {
  37. if (_cmdSerNum == ushort.MaxValue)
  38. _cmdSerNum = 0;
  39. return ++_cmdSerNum;
  40. }
  41. /// <summary>
  42. /// 取得封包序號 1-255
  43. /// </summary>
  44. /// <returns></returns>
  45. public byte GetSerNum()
  46. {
  47. if (_serNum == byte.MaxValue)
  48. {
  49. _serNum = 0;
  50. }
  51. return ++_serNum;
  52. }
  53. public byte GetCurrentSerNum()
  54. {
  55. return _serNum;
  56. }
  57. /// <summary>
  58. /// 取得封包序號 1-255
  59. /// </summary>
  60. /// <returns></returns>
  61. public byte GetChargingProfileId()
  62. {
  63. if (_chargingProfileId == byte.MaxValue)
  64. {
  65. _chargingProfileId = 0;
  66. }
  67. return ++_chargingProfileId;
  68. }
  69. public byte GetCurrentChargingProfileId()
  70. {
  71. return _chargingProfileId;
  72. }
  73. /// <summary>
  74. /// 客戶自訂的樁id
  75. /// </summary>
  76. public string MachineCustomId
  77. {
  78. get; set;
  79. }
  80. /// <summary>
  81. /// 客戶自訂的樁id
  82. /// </summary>
  83. public string MachineCustomIdTemp
  84. {
  85. get; set;
  86. }
  87. /// <summary>
  88. /// 根據unique id來儲存.取出OCPP Request
  89. /// </summary>
  90. public Queue queue;
  91. /// <summary>
  92. /// 根據 GetConfiguration Key來儲存.
  93. /// </summary>
  94. public List<KeyValueType> configurationKey;
  95. /// <summary>
  96. /// 車輛類別
  97. /// 0:默認樁體設定 , 1:小車(12V) , 2:大車(24V)
  98. /// </summary>
  99. public int CarType;
  100. /// <summary>
  101. /// 上一個Heartbeat日期封包
  102. /// </summary>
  103. public DateTime? heartbeatDate { get; set; }
  104. /// <summary>
  105. /// 當 Heartbeat 與 上一個 Heartbeat相差值小於2秒,判斷是否送出ChangeConfiguration.req(HeartbeatInterval: 15) 給電樁
  106. /// </summary>
  107. public bool IsSendHeartbeatChangeConfiguration { get; set; }
  108. /// <summary>
  109. /// Gets the app server.
  110. /// </summary>
  111. public new OCPPWSServer AppServer
  112. {
  113. get { return (OCPPWSServer)base.AppServer; }
  114. }
  115. /// <summary>
  116. /// Sends the raw binary data to client.
  117. /// </summary>
  118. /// <param name="data">The data.</param>
  119. /// <param name="offset">The offset.</param>
  120. /// <param name="length">The length.</param>
  121. public void SendRawData(byte[] data, int offset, int length)
  122. {
  123. base.Send(data, offset, length);
  124. }
  125. //receive data event trigger
  126. public void ReceiveData(ClientData clientdata, string msg)
  127. {
  128. if (m_ReceiveData != null)
  129. m_ReceiveData(clientdata, msg);
  130. }
  131. /// <summary>
  132. /// Called when [session closed].
  133. /// </summary>
  134. /// <param name="reason">The reason.</param>
  135. protected override void OnSessionClosed(CloseReason reason)
  136. {
  137. }
  138. public void InitialPileData()
  139. {
  140. this.queue = new Queue();
  141. this.MachineCustomId = Guid.NewGuid().ToString();
  142. this.MachineCustomIdTemp = Guid.NewGuid().ToString();
  143. this.IsCheckIn = false;
  144. this.configurationKey = new List<KeyValueType>();
  145. this.CarType = 0; // 默認樁體設定
  146. heartbeatDate = null;
  147. IsSendHeartbeatChangeConfiguration = false;
  148. #region Core
  149. configurationKey.Add(new KeyValueType { key = "AllowOfflineTxForUnknownId", keyreadonly = false, value = "false" });
  150. configurationKey.Add(new KeyValueType { key = "AuthorizationCacheEnabled", keyreadonly = false, value = "false" });
  151. configurationKey.Add(new KeyValueType { key = "AuthorizeRemoteTxRequests", keyreadonly = false, value = "false" });
  152. configurationKey.Add(new KeyValueType { key = "BlinkRepeat", keyreadonly = false, value = "0" });
  153. configurationKey.Add(new KeyValueType { key = "ClockAlignedDataInterval", keyreadonly = false, value = "0" });
  154. configurationKey.Add(new KeyValueType { key = "ConnectionTimeOut", keyreadonly = false, value = "0" });
  155. configurationKey.Add(new KeyValueType { key = "GetConfigurationMaxKeys", keyreadonly = true, value = "0" });
  156. configurationKey.Add(new KeyValueType { key = "HeartbeatInterval", keyreadonly = false, value = "0" });
  157. configurationKey.Add(new KeyValueType { key = "LightIntensity", keyreadonly = false, value = "0" });
  158. configurationKey.Add(new KeyValueType { key = "LocalAuthorizeOffline", keyreadonly = false, value = "false" });
  159. configurationKey.Add(new KeyValueType { key = "LocalPreAuthorize", keyreadonly = false, value = "false" });
  160. configurationKey.Add(new KeyValueType { key = "MaxEnergyOnInvalidId", keyreadonly = false, value = "0" });
  161. configurationKey.Add(new KeyValueType { key = "MeterValuesAlignedData", keyreadonly = false, value = "0" });
  162. configurationKey.Add(new KeyValueType { key = "MeterValuesAlignedDataMaxLength", keyreadonly = true, value = "0" });
  163. configurationKey.Add(new KeyValueType { key = "MeterValuesSampledData", keyreadonly = false, value = "0" });
  164. configurationKey.Add(new KeyValueType { key = "MeterValuesSampledDataMaxLength", keyreadonly = true, value = "0" });
  165. configurationKey.Add(new KeyValueType { key = "MeterValueSampleInterval", keyreadonly = false, value = "0" });
  166. configurationKey.Add(new KeyValueType { key = "MinimumStatusDuration", keyreadonly = false, value = "0" });
  167. configurationKey.Add(new KeyValueType { key = "NumberOfConnectors", keyreadonly = true, value = "0" });
  168. configurationKey.Add(new KeyValueType { key = "ResetRetries", keyreadonly = false, value = "0" });
  169. configurationKey.Add(new KeyValueType { key = "ConnectorPhaseRotation", keyreadonly = false, value = "0" });
  170. configurationKey.Add(new KeyValueType { key = "ConnectorPhaseRotationMaxLength", keyreadonly = true, value = "0" });
  171. configurationKey.Add(new KeyValueType { key = "StopTransactionOnEVSideDisconnect", keyreadonly = false, value = "false" });
  172. configurationKey.Add(new KeyValueType { key = "StopTransactionOnInvalidId", keyreadonly = false, value = "false" });
  173. configurationKey.Add(new KeyValueType { key = "StopTxnAlignedData", keyreadonly = false, value = "0" });
  174. configurationKey.Add(new KeyValueType { key = "StopTxnAlignedDataMaxLength", keyreadonly = true, value = "0" });
  175. configurationKey.Add(new KeyValueType { key = "StopTxnSampledData", keyreadonly = false, value = "0" });
  176. configurationKey.Add(new KeyValueType { key = "StopTxnSampledDataMaxLength", keyreadonly = true, value = "0" });
  177. configurationKey.Add(new KeyValueType { key = "SupportedFeatureProfiles", keyreadonly = true, value = "0" });
  178. configurationKey.Add(new KeyValueType { key = "SupportedFeatureProfilesMaxLength", keyreadonly = true, value = "0" });
  179. configurationKey.Add(new KeyValueType { key = "TransactionMessageAttempts", keyreadonly = false, value = "0" });
  180. configurationKey.Add(new KeyValueType { key = "TransactionMessageRetryInterval", keyreadonly = false, value = "0" });
  181. configurationKey.Add(new KeyValueType { key = "UnlockConnectorOnEVSideDisconnect", keyreadonly = false, value = "false" });
  182. configurationKey.Add(new KeyValueType { key = "WebSocketPingInterval", keyreadonly = false, value = "0" });
  183. #endregion Core
  184. #region Local Auth List Management
  185. configurationKey.Add(new KeyValueType { key = "LocalAuthListEnabled", keyreadonly = false, value = "false" });
  186. configurationKey.Add(new KeyValueType { key = "LocalAuthListMaxLength", keyreadonly = true, value = "0" });
  187. configurationKey.Add(new KeyValueType { key = "SendLocalListMaxLength", keyreadonly = true, value = "0" });
  188. #endregion Local Auth List Management
  189. #region Reservation Profile
  190. configurationKey.Add(new KeyValueType { key = "ReserveConnectorZeroSupported", keyreadonly = true, value = "false" });
  191. #endregion Reservation Profile
  192. #region Smart Charging Profile
  193. configurationKey.Add(new KeyValueType { key = "ChargeProfileMaxStackLevel", keyreadonly = true, value = "0" });
  194. configurationKey.Add(new KeyValueType { key = "ChargingScheduleAllowedChargingRateUnit", keyreadonly = true, value = "0" });
  195. configurationKey.Add(new KeyValueType { key = "ChargingScheduleMaxPeriods", keyreadonly = true, value = "0" });
  196. configurationKey.Add(new KeyValueType { key = "ConnectorSwitch3to1PhaseSupported", keyreadonly = true, value = "false" });
  197. configurationKey.Add(new KeyValueType { key = "MaxChargingProfilesInstalled", keyreadonly = true, value = "0" });
  198. #endregion Smart Charging Profile
  199. }
  200. }
  201. }