config_parser.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "config_parser.h"
  2. #include "vendor_global.h"
  3. #include <map>
  4. #include <stdlib.h>
  5. #include <utils/macro.h>
  6. #define CHECK_MAP_KEY(kv, key) \
  7. do { \
  8. if (kv.find(#key) == kv.end()) { \
  9. HTELINK_LOG_ERR("dataCenter config not found" #key); \
  10. return; \
  11. } \
  12. } while (0)
  13. namespace settings {
  14. ConfigParser::ConfigParser() : xmlConfig_(new XmlConfig())
  15. {
  16. if (!xmlConfig_->Load(VENDOR_CONFIG_PATH + "/basic_config.xml")) {
  17. HTELINK_LOG_ERR("load xml failed");
  18. }
  19. }
  20. ConfigParser::~ConfigParser()
  21. {
  22. delete xmlConfig_;
  23. }
  24. std::vector<ConfigParser::DataCenter> ConfigParser::GetDataCenter() const
  25. {
  26. std::vector<DataCenter> dataCenters;
  27. xmlConfig_->GetElemValue("dataCenter", [&dataCenters](std::map<std::string, std::string> &kv) -> void {
  28. DataCenter center;
  29. CHECK_MAP_KEY(kv, name);
  30. CHECK_MAP_KEY(kv, id);
  31. CHECK_MAP_KEY(kv, host);
  32. CHECK_MAP_KEY(kv, port);
  33. CHECK_MAP_KEY(kv, key);
  34. CHECK_MAP_KEY(kv, tls);
  35. center.name = kv["name"];
  36. center.ID = kv["id"];
  37. center.host = kv["host"];
  38. center.port = atoi(kv["port"].c_str());
  39. center.protoType = 1;
  40. center.key = kv["key"];
  41. center.isTls = atoi(kv["tls"].c_str()) != 0;
  42. center.yunID = 0;
  43. dataCenters.push_back(center);
  44. });
  45. // 兼容老版本格式
  46. if (dataCenters.empty()) {
  47. DataCenter center;
  48. center.ID = xmlConfig_->GetElemValueString("", "id");
  49. center.host = xmlConfig_->GetElemValueString("", "host");
  50. center.port = xmlConfig_->GetElemValueInt("", "port");
  51. center.key = xmlConfig_->GetElemValueString("", "key");
  52. center.isTls = xmlConfig_->GetElemValueBool("", "tls");
  53. center.protoType = 1;
  54. center.yunID = 0;
  55. dataCenters.push_back(center);
  56. }
  57. return dataCenters;
  58. }
  59. std::vector<ConfigParser::LoginUser> ConfigParser::GetLoginUsers() const
  60. {
  61. std::vector<LoginUser> loginUsers;
  62. xmlConfig_->GetElemValue("logUser", [&loginUsers](std::map<std::string, std::string> &kv) -> void {
  63. LoginUser user;
  64. CHECK_MAP_KEY(kv, username);
  65. CHECK_MAP_KEY(kv, host);
  66. user.user = kv["username"];
  67. user.passwd = kv["password"];
  68. loginUsers.push_back(user);
  69. });
  70. // 兼容老版本格式
  71. if (loginUsers.empty()) {
  72. LoginUser user;
  73. user.user = xmlConfig_->GetElemValueString("", "username");
  74. user.passwd = xmlConfig_->GetElemValueString("", "password");
  75. loginUsers.push_back(user);
  76. }
  77. return loginUsers;
  78. }
  79. int16_t ConfigParser::GetWWWPort() const
  80. {
  81. return xmlConfig_->GetElemValueInt("", "wwwPort");
  82. }
  83. int32_t ConfigParser::GetRetryTimes()
  84. {
  85. return xmlConfig_->GetElemValueInt("", "retry");
  86. }
  87. std::unordered_map<std::string, ConfigParser::Channel> ConfigParser::GetChannels() const
  88. {
  89. std::unordered_map<std::string, Channel> channels;
  90. std::vector<DataCenter> dataCenters;
  91. xmlConfig_->GetElemValue("Channel", [&channels](std::map<std::string, std::string> &kv) -> void {
  92. for (std::pair<std::string, std::string> chn : kv) {
  93. Channel chnn;
  94. chnn.devPath = chn.second;
  95. if (utils::StartWith(chn.first, "COM")) {
  96. chnn.type = CHN_COM;
  97. } else if (utils::StartWith(chn.first, "TCP")) {
  98. chnn.type = CHN_TCP;
  99. } else if (utils::StartWith(chn.first, "UDP")) {
  100. chnn.type = CHN_UDP;
  101. } else if (utils::StartWith(chn.first, "LORA")) {
  102. chnn.type = CHN_LORA;
  103. } else {
  104. chnn.type = CHN_COM;
  105. }
  106. channels[chn.first] = chnn;
  107. }
  108. });
  109. // 兼容老版本格式
  110. if (channels.empty()) {
  111. std::string chanStr = xmlConfig_->GetElemValueString("", "portNames");
  112. std::vector<std::string> chanList = utils::Split(chanStr, ",");
  113. int32_t index = 1;
  114. for (const std::string &chn : chanList) {
  115. if (chn.empty()) {
  116. continue;
  117. }
  118. Channel chnn;
  119. chnn.devPath = chn;
  120. chnn.type = CHN_COM;
  121. channels["COM" + index++] = chnn;
  122. }
  123. }
  124. return channels;
  125. }
  126. } // namespace settings