export_yaml_file.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "export_yaml_file.h"
  2. #include <yaml-cpp/yaml.h>
  3. #include <iostream>
  4. #include <dbms/dbms_channel.h>
  5. #include <dbms/dbms_device.h>
  6. #include <dbms/dbms_fd_handler.h>
  7. namespace iot_acq {
  8. ExportYamlFile::ExportYamlFile() {}
  9. ExportYamlFile::~ExportYamlFile() {}
  10. bool ExportYamlFile::ExportYaml(const std::string &filePath)
  11. {
  12. YAML::Node root;
  13. if (!ExportRoot(root)) {
  14. HTELINK_LOG_ERR("export file %s failed", filePath);
  15. return false;
  16. }
  17. std::ofstream fout(filePath);
  18. fout << root;
  19. fout.close();
  20. return true;
  21. }
  22. bool ExportYamlFile::ExportRoot(YAML::Node &root)
  23. {
  24. root["channels"] = ExportChannels();
  25. root["devices"] = ExportDevices();
  26. return true;
  27. }
  28. YAML::Node ExportYamlFile::ExportChannels()
  29. {
  30. YAML::Node channelNode;
  31. dbms::DbmsChannel dbmsChannel;
  32. dbmsChannel.Get([&](const dbms::DeviceChannel_t &dc) {
  33. YAML::Node deviceChannel;
  34. deviceChannel["id"] = dc.id();
  35. deviceChannel["timeout"] = dc.timeout();
  36. deviceChannel["interval"] = dc.interval();
  37. deviceChannel["reportPeriod"] = dc.reportperiod();
  38. deviceChannel["type"] = static_cast<int32_t>(dc.ctype());
  39. YAML::Node params;
  40. if (dc.ctype() == dbms::CT_COM) {
  41. params["name"] = dc.com().name();
  42. params["baudRate"] = dc.com().baudrate();
  43. params["dataBits"] = dc.com().databit();
  44. params["parity"] = dc.com().parity();
  45. params["stopBits"] = dc.com().stopbit();
  46. } else if (dc.ctype() == dbms::CT_TCP || dc.ctype() == dbms::CT_UDP) {
  47. params["ip"] = dc.socket().ip();
  48. params["port"] = dc.socket().port();
  49. }
  50. deviceChannel["params"] = params;
  51. channelNode.push_back(deviceChannel);
  52. });
  53. return channelNode;
  54. }
  55. YAML::Node ExportYamlFile::ExportDevices()
  56. {
  57. YAML::Node devicesNode;
  58. dbms::DbmsDevice dbmsDevice;
  59. dbmsDevice.Get([&](const dbms::Device_t &device) {
  60. YAML::Node devNode;
  61. devNode["id"] = device.id();
  62. devNode["addr"] = device.addr();
  63. devNode["name"] = device.name();
  64. devNode["channelId"] = device.channelid();
  65. devNode["protocolId"] = device.protoid();
  66. YAML::Node functionsNode;
  67. for (int i = 0; i < device.funclist_size(); ++i) {
  68. YAML::Node funcNode;
  69. funcNode["id"] = device.funclist(i).id();
  70. funcNode["funcType"] = device.funclist(i).type();
  71. funcNode["ratio"] = device.funclist(i).ratio();
  72. functionsNode.push_back(funcNode);
  73. }
  74. devNode["functions"] = functionsNode;
  75. devicesNode.push_back(devNode);
  76. });
  77. return devicesNode;
  78. }
  79. YAML::Node ExportYamlFile::ExportFdHandler()
  80. {
  81. YAML::Node fdNode;
  82. dbms::DbmsFdHandler fdHandler;
  83. fdHandler.Get([&](const std::string &id) {
  84. YAML::Node idNode;
  85. idNode["ID"] = id;
  86. fdNode.push_back(idNode);
  87. });
  88. return fdNode;
  89. }
  90. } // namespace iot_acq