ajax_handler.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "ajax_handler.h"
  2. #include <settings/config_parser.h>
  3. #include <settings/system_settings.h>
  4. namespace webconfig {
  5. Json::Value GetJsonDataCenter(const Json::Value &param)
  6. {
  7. settings::ConfigParser configParser;
  8. Json::Value baseConfig;
  9. std::vector<settings::ConfigParser::DataCenter> dataCenters = configParser.GetDataCenter();
  10. for (settings::ConfigParser::DataCenter dc : dataCenters) {
  11. Json::Value jsonDc;
  12. jsonDc["id"] = dc.ID;
  13. jsonDc["host"] = dc.host;
  14. jsonDc["port"] = dc.port;
  15. jsonDc["secretKey"] = dc.key;
  16. jsonDc["tls"] = dc.isTls ? 1 : 0;
  17. baseConfig.append(jsonDc);
  18. }
  19. return baseConfig;
  20. }
  21. Json::Value GetJsonNetworkInfo(const Json::Value &param)
  22. {
  23. settings::SystemSettings sysSettings;
  24. sysSettings.UpdateNetDev();
  25. Json::Value netJson;
  26. for (settings::SystemSettings::NetCard &netCard : sysSettings.netCards) {
  27. Json::Value netJ;
  28. netJ["ip"] = netCard.ipAddr;
  29. netJ["mac"] = netCard.MAC;
  30. netJ["name"] = netCard.ifname;
  31. netJ["gateway"] = netCard.gateway;
  32. netJ["netmask"] = netCard.mask;
  33. netJ["model"] = false;
  34. netJson.append(netJ);
  35. }
  36. return netJson;
  37. }
  38. Json::Value GetJsonDiskInfo(const Json::Value &param)
  39. {
  40. Json::Value diskJson;
  41. settings::SystemSettings sysSettings;
  42. sysSettings.UpdateDiskInfo();
  43. char disk[20], mem[20];
  44. sprintf(disk, "%d M/%d M", sysSettings.freeDiskSpace, sysSettings.totalDiskSpace);
  45. sprintf(mem, "%d M/%d M", sysSettings.freeMemory, sysSettings.totalMemory);
  46. diskJson["free"] = sysSettings.freeDiskSpace;
  47. diskJson["total"] = sysSettings.totalDiskSpace;
  48. return diskJson;
  49. }
  50. Json::Value GetJsonMemInfo(const Json::Value &param)
  51. {
  52. settings::SystemSettings sysSettings;
  53. sysSettings.UpdateMemInfo();
  54. Json::Value memJson;
  55. memJson["free"] = sysSettings.freeMemory;
  56. memJson["total"] = sysSettings.totalMemory;
  57. return memJson;
  58. }
  59. Json::Value GetJsonSysInfo(const Json::Value &param)
  60. {
  61. settings::SystemSettings sysSettings;
  62. sysSettings.UpdateSysInfo();
  63. Json::Value sysJson;
  64. sysJson["version"] = sysSettings.appVer;
  65. sysJson["time"] = sysSettings.sysTime;
  66. sysJson["upgradeTime"] = sysSettings.upgradeTime;
  67. sysJson["updateConfigTime"] = sysSettings.updateConfigTime;
  68. return sysJson;
  69. }
  70. Json::Value GetJsonGatewayInfo(const Json::Value &param)
  71. {
  72. Json::Value result;
  73. result["disk"] = GetJsonDiskInfo();
  74. result["memory"] = GetJsonMemInfo();
  75. result["network"] = GetJsonNetworkInfo();
  76. result["system"] = GetJsonSysInfo();
  77. return result;
  78. }
  79. } // namespace webconfig