load_json_file.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "load_json_file.h"
  2. #include <json/json.h>
  3. #include <json/reader.h>
  4. #include <iostream>
  5. #include <dbms/dbms_channel.h>
  6. #include <dbms/dbms_device.h>
  7. namespace iot_acq {
  8. LoadJsonFile::LoadJsonFile() {}
  9. LoadJsonFile::~LoadJsonFile() {}
  10. bool LoadJsonFile::LoadJson(const std::string &filePath, Json::Value &root)
  11. {
  12. Json::Reader reader;
  13. std::ifstream file(filePath);
  14. if (!file.is_open()) {
  15. HTELINK_LOG_ERR("load file %s filed", filePath);
  16. return false;
  17. }
  18. if (!reader.parse(file, root)) {
  19. HTELINK_LOG_ERR("Parse error: %s", reader.getFormattedErrorMessages());
  20. file.close();
  21. return false;
  22. }
  23. file.close();
  24. return true;
  25. }
  26. bool LoadJsonFile::ParseJson(const Json::Value &root)
  27. {
  28. if (root.isMember("channels")) {
  29. if (!ParseChannels(root["channels"])) {
  30. return false;
  31. }
  32. }
  33. if (root.isMember("devices")) {
  34. if (!ParseDevices(root["devices"])) {
  35. return false;
  36. }
  37. }
  38. return true;
  39. }
  40. bool LoadJsonFile::LoadChannelFromJson(const std::string &filePath)
  41. {
  42. Json::Value root;
  43. if (!LoadJson(filePath, root)) {
  44. HTELINK_LOG_ERR("load json from file %s failed", filePath);
  45. return false;
  46. }
  47. if (root.isMember("channels")) {
  48. if (!ParseChannels(root["channels"])) {
  49. return false;
  50. }
  51. return true;
  52. }
  53. return false;
  54. }
  55. bool LoadJsonFile::LoadDevicesFromJson(const std::string &filePath)
  56. {
  57. Json::Value root;
  58. if (!LoadJson(filePath, root)) {
  59. HTELINK_LOG_ERR("load json from file %s failed", filePath);
  60. return false;
  61. }
  62. if (root.isMember("devices")) {
  63. if (!ParseDevices(root["devices"])) {
  64. return false;
  65. }
  66. return true;
  67. }
  68. return false;
  69. }
  70. bool LoadJsonFile::ParseChannels(const Json::Value &jsonChn)
  71. {
  72. dbms::DbmsChannel dbmsChannel;
  73. dbmsChannel.DeleteAll();
  74. auto storeChannel = [&](const Json::Value &item,
  75. dbms::ChannelType_t type) -> bool {
  76. dbms::DeviceChannel_t deviceChannel;
  77. deviceChannel.set_id(item["id"].asUInt());
  78. deviceChannel.set_timeout(item["waitTime"].asInt());
  79. deviceChannel.set_interval(item["intervalTime"].asInt());
  80. deviceChannel.set_reportperiod(item["reportPeriod"].asInt());
  81. deviceChannel.set_ctype(type);
  82. if (type == dbms::CT_COM) {
  83. dbms::Serial_t *serial = deviceChannel.mutable_com();
  84. serial->set_name(item["name"].asString());
  85. serial->set_baudrate(item["baudRate"].asInt());
  86. serial->set_stopbit(item["stopBits"].asInt());
  87. serial->set_parity(item["parity"].asInt());
  88. serial->set_databit(item["dataBits"].asInt());
  89. } else if (type == dbms::CT_TCP || type == dbms::CT_UDP) {
  90. dbms::Socket_t *socket = deviceChannel.mutable_socket();
  91. socket->set_ip(item["ip"].asString());
  92. socket->set_port(item["port"].asInt());
  93. } else {
  94. HTELINK_LOG_ERR("type %d not support", static_cast<int32_t>(type));
  95. return false;
  96. }
  97. return dbmsChannel.Save(deviceChannel);
  98. };
  99. if (jsonChn.isMember("serialPort")) {
  100. const Json::Value &serialPort = jsonChn["serialPort"];
  101. for (const auto &item : serialPort) {
  102. if (!storeChannel(item, dbms::CT_COM)) {
  103. HTELINK_LOG_ERR("serial channel parse error");
  104. return false;
  105. }
  106. }
  107. }
  108. if (jsonChn.isMember("tcp")) {
  109. const Json::Value &tcp = jsonChn["tcp"];
  110. for (const auto &item : tcp) {
  111. if (!storeChannel(item, dbms::CT_TCP)) {
  112. HTELINK_LOG_ERR("tcp channel parse error");
  113. return false;
  114. }
  115. }
  116. }
  117. if (jsonChn.isMember("udp")) {
  118. const Json::Value &udp = jsonChn["udp"];
  119. for (const auto &item : udp) {
  120. if (!storeChannel(item, dbms::CT_UDP)) {
  121. HTELINK_LOG_ERR("serial channel parse error");
  122. return false;
  123. }
  124. }
  125. }
  126. return true;
  127. }
  128. bool LoadJsonFile::ParseDevices(const Json::Value &jsonDevice)
  129. {
  130. dbms::DbmsDevice dbmsDevice;
  131. dbmsDevice.DeleteAll();
  132. for (const auto &item : jsonDevice) {
  133. dbms::Device_t device;
  134. device.set_id(item["id"].asString());
  135. device.set_addr(item["address"].asString());
  136. device.set_name(item["name"].asString());
  137. device.set_channelid(item["channelId"].asInt());
  138. device.set_protoid(item["protocolId"].asString());
  139. const auto &functionsJson = item["functions"];
  140. for (const auto &funcJson : functionsJson) {
  141. dbms::Funcpoint_t *func = device.add_funclist();
  142. func->set_id(funcJson["id"].asString());
  143. func->set_type(funcJson["funcType"].asInt());
  144. if (funcJson.isMember("ratio")) {
  145. func->set_ratio(funcJson["ratio"].asFloat());
  146. } else {
  147. func->set_ratio(1);
  148. }
  149. }
  150. dbmsDevice.Save(device);
  151. }
  152. return true;
  153. }
  154. } // namespace iot_acq