GlobalConfig.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using Microsoft.Extensions.Configuration;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. namespace EVCB_OCPP.WSServer
  7. {
  8. public static class GlobalConfig
  9. {
  10. public static List<string> ConfigKeys = new List<string>()
  11. {
  12. "WSPort",
  13. "WSSPort",
  14. "OCPP20_WSUrl",
  15. "OCPP20_WSSUrl",
  16. "SecurityProfileLevel",
  17. "SecurityPassword"
  18. };
  19. public static List<string> DenyModelNames = new List<string>();
  20. public static string UTC_DATETIMEFORMAT = "yyyy-MM-dd'T'HH':'mm':'ss'Z'";
  21. public static string TCC_API_URL = string.Empty;
  22. public static string TCC_SALTKEY = string.Empty;
  23. public static string OCPP20_WSUrl = string.Empty;
  24. public static string OCPP20_WSSUrl = string.Empty;
  25. public static int SecurityProfileLevel = 0;
  26. public static string SecurityPassword = string.Empty;
  27. public static JsonSerializerSettings JSONSERIALIZER_FORMAT = new JsonSerializerSettings()
  28. {
  29. NullValueHandling = NullValueHandling.Ignore,
  30. Formatting = Formatting.None,
  31. DateFormatString = UTC_DATETIMEFORMAT
  32. };
  33. /// <summary>
  34. /// 預設心跳間隔時間 單位:秒
  35. /// </summary>
  36. private static int DEFAULT_HEARTBEAT_INTERVAL = 60;
  37. /// <summary>
  38. ///WS Port
  39. /// </summary>
  40. private static int WS_Port = 80;
  41. /// <summary>
  42. ///WSS Port
  43. /// </summary>
  44. private static List<int> WSS_Ports = null;
  45. public static bool LoadAPPConfig(IConfiguration configuration)
  46. {
  47. bool result = false;
  48. string key = string.Empty;
  49. try
  50. {
  51. for (int i = 0; i < ConfigKeys.Count; i++)
  52. {
  53. key = ConfigKeys[i];
  54. switch (key)
  55. {
  56. case "WSPort":// convert to int type
  57. {
  58. var value = configuration[key];
  59. if (string.IsNullOrEmpty(value))
  60. {
  61. WS_Port = 80;
  62. break;
  63. }
  64. WS_Port = Int32.TryParse(value, out WS_Port) ? WS_Port : 80;
  65. }
  66. break;
  67. case "WSSPort":
  68. {
  69. var value = configuration[key];
  70. if (string.IsNullOrEmpty(value))
  71. {
  72. WSS_Ports = new List<int>();
  73. break;
  74. }
  75. var ports = value.Split(',');
  76. WSS_Ports = new List<int>();
  77. foreach (var port in ports)
  78. {
  79. WSS_Ports.Add(Convert.ToInt32(port));
  80. }
  81. }
  82. break;
  83. case "OCPP20_WSUrl":// convert to int type
  84. {
  85. var value = configuration[key];
  86. OCPP20_WSUrl = value;
  87. }
  88. break;
  89. case "OCPP20_WSSUrl":// convert to int type
  90. {
  91. var value = configuration[key];
  92. OCPP20_WSSUrl = value;
  93. }
  94. break;
  95. case "SecurityProfileLevel":
  96. {
  97. var value = configuration[key];
  98. SecurityProfileLevel = Convert.ToInt32(value);
  99. }
  100. break;
  101. case "SecurityPassword":
  102. {
  103. var value = configuration[key];
  104. SecurityPassword = value;
  105. }
  106. break;
  107. default://convert to string type
  108. break;
  109. }
  110. }
  111. result = true;
  112. }
  113. catch (Exception ex)
  114. {
  115. Console.WriteLine(key + " Load from APPConfig " + ex.ToString());
  116. }
  117. return result;
  118. }
  119. public static int GetWS_Port()
  120. {
  121. return WS_Port;
  122. }
  123. public static List<int> GetWSS_Ports()
  124. {
  125. return WSS_Ports;
  126. }
  127. public static int GetHEARTBEAT_INTERVAL()
  128. {
  129. return DEFAULT_HEARTBEAT_INTERVAL;
  130. }
  131. public static readonly int DB_DefaultConnectionTimeout = 60;
  132. /// <summary>
  133. /// 預設 Null的 DateTime
  134. /// </summary>
  135. public static DateTime DefaultNullTime = new DateTime(1991, 1, 1);
  136. public const string BootData_EVSEConfig_Key = "BootData_EVSEConfig_Key";
  137. }
  138. }