GlobalConfig.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. };
  17. public static string UTC_DATETIMEFORMAT = "yyyy-MM-dd'T'HH':'mm':'ss'Z'";
  18. public static string TCC_API_URL = string.Empty;
  19. public static string TCC_SALTKEY = string.Empty;
  20. public static string OCPP20_WSUrl = string.Empty;
  21. public static string OCPP20_WSSUrl = string.Empty;
  22. public static JsonSerializerSettings JSONSERIALIZER_FORMAT = new JsonSerializerSettings()
  23. {
  24. NullValueHandling = NullValueHandling.Ignore,
  25. Formatting = Formatting.None,
  26. DateFormatString = UTC_DATETIMEFORMAT
  27. };
  28. /// <summary>
  29. /// 預設心跳間隔時間 單位:秒
  30. /// </summary>
  31. private static int DEFAULT_HEARTBEAT_INTERVAL = 60;
  32. /// <summary>
  33. ///WS Port
  34. /// </summary>
  35. private static int WS_Port = 80;
  36. /// <summary>
  37. ///WSS Port
  38. /// </summary>
  39. private static int WSS_Port = 443;
  40. public static bool LoadAPPConfig(IConfiguration configuration)
  41. {
  42. bool result = false;
  43. string key = string.Empty;
  44. try
  45. {
  46. for (int i = 0; i < ConfigKeys.Count; i++)
  47. {
  48. key = ConfigKeys[i];
  49. switch (key)
  50. {
  51. case "WSPort":// convert to int type
  52. {
  53. var value = configuration[key];
  54. WS_Port = string.IsNullOrEmpty(value) ? WS_Port : Convert.ToInt32(value);
  55. }
  56. break;
  57. case "WSSPort":
  58. {
  59. var value = configuration[key];
  60. WSS_Port = string.IsNullOrEmpty(value) ? WS_Port : Convert.ToInt32(value);
  61. }
  62. break;
  63. case "OCPP20_WSUrl":// convert to int type
  64. {
  65. var value = configuration[key];
  66. OCPP20_WSUrl = value;
  67. }
  68. break;
  69. case "OCPP20_WSSUrl":// convert to int type
  70. {
  71. var value = configuration[key];
  72. OCPP20_WSSUrl = value;
  73. }
  74. break;
  75. default://convert to string type
  76. break;
  77. }
  78. }
  79. result = true;
  80. }
  81. catch (Exception ex)
  82. {
  83. Console.WriteLine(key + " Load from APPConfig " + ex.ToString());
  84. }
  85. return result;
  86. }
  87. public static int GetWS_Port()
  88. {
  89. return WS_Port;
  90. }
  91. public static int GetWSS_Port()
  92. {
  93. return WSS_Port;
  94. }
  95. public static int GetHEARTBEAT_INTERVAL()
  96. {
  97. return DEFAULT_HEARTBEAT_INTERVAL;
  98. }
  99. public static readonly int DB_DefaultConnectionTimeout = 60;
  100. /// <summary>
  101. /// 預設 Null的 DateTime
  102. /// </summary>
  103. public static DateTime DefaultNullTime = new DateTime(1991, 1, 1);
  104. }
  105. }