GlobalConfig.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. WS_Port = Int32.TryParse(value, out WS_Port) ? WS_Port : 0;
  60. }
  61. break;
  62. case "WSSPort":
  63. {
  64. var value = configuration[key];
  65. var ports = value.Split(',');
  66. WSS_Ports = new List<int>();
  67. foreach (var port in ports)
  68. {
  69. WSS_Ports.Add(Convert.ToInt32(port));
  70. }
  71. }
  72. break;
  73. case "OCPP20_WSUrl":// convert to int type
  74. {
  75. var value = configuration[key];
  76. OCPP20_WSUrl = value;
  77. }
  78. break;
  79. case "OCPP20_WSSUrl":// convert to int type
  80. {
  81. var value = configuration[key];
  82. OCPP20_WSSUrl = value;
  83. }
  84. break;
  85. case "SecurityProfileLevel":
  86. {
  87. var value = configuration[key];
  88. SecurityProfileLevel = Convert.ToInt32(value);
  89. }
  90. break;
  91. case "SecurityPassword":
  92. {
  93. var value = configuration[key];
  94. SecurityPassword = value;
  95. }
  96. break;
  97. default://convert to string type
  98. break;
  99. }
  100. }
  101. result = true;
  102. }
  103. catch (Exception ex)
  104. {
  105. Console.WriteLine(key + " Load from APPConfig " + ex.ToString());
  106. }
  107. return result;
  108. }
  109. public static int GetWS_Port()
  110. {
  111. return WS_Port;
  112. }
  113. public static List<int> GetWSS_Ports()
  114. {
  115. return WSS_Ports;
  116. }
  117. public static int GetHEARTBEAT_INTERVAL()
  118. {
  119. return DEFAULT_HEARTBEAT_INTERVAL;
  120. }
  121. public static readonly int DB_DefaultConnectionTimeout = 60;
  122. /// <summary>
  123. /// 預設 Null的 DateTime
  124. /// </summary>
  125. public static DateTime DefaultNullTime = new DateTime(1991, 1, 1);
  126. }
  127. }