ModuleHTTP.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "ModuleHTTP.h"
  2. #include <httplib.h>
  3. #include <iostream>
  4. #include <thread>
  5. using namespace httplib;
  6. namespace mdd{
  7. bool ModuleHTTP::connect(){
  8. Client cli(_id, _port);
  9. std::string body;
  10. auto res = cli.Get("/status",
  11. [&](const char *data, size_t data_length) {
  12. body.append(data, data_length);
  13. return true;
  14. });
  15. //assert(res->body.empty());
  16. if(body.empty()){
  17. return false;
  18. }
  19. body = std::string(R"()") + body;
  20. json status = json::parse(body.c_str());
  21. if (status["status"].get<std::string>() == "ready"){
  22. return true;
  23. }
  24. else{
  25. return false;
  26. }
  27. }
  28. std::string ModuleHTTP::str_to_json(const std::string& input){
  29. if(input.empty()){
  30. return input;
  31. }
  32. std::string str = input;
  33. size_t start_pos = 0;
  34. while((start_pos = str.find('[', start_pos)) != std::string::npos) {
  35. str.replace(start_pos, 1, "{");
  36. start_pos += 1;
  37. }
  38. start_pos = 0;
  39. while((start_pos = str.find(']', start_pos)) != std::string::npos) {
  40. str.replace(start_pos, 1, "}");
  41. start_pos += 1;
  42. }
  43. return str;
  44. }
  45. void ModuleHTTP::updateLayout(){
  46. Client cli(_id, _port);
  47. std::string body;
  48. auto res = cli.Get("/inputs",
  49. [&](const char *data, size_t data_length) {
  50. body.append(data, data_length);
  51. return true;
  52. });
  53. assert(res->body.empty());
  54. body = std::string(R"()") + body;
  55. json inputs = json::parse(body.c_str());
  56. for(int j = 0; j < (int)getInputs().size()-(int)inputs.size(); ++j){
  57. pop_backInput();
  58. }
  59. for(size_t i=0; i < getInputs().size(); i++){
  60. getInput(i)->setType(inputs[i]["type"].get<std::string>());
  61. getInput(i)->setAppendix(i);
  62. getInput(i)->setDefaultValue() = inputs[i];
  63. }
  64. for(int index = getInputs().size(); index < inputs.size(); ++index){
  65. json val;
  66. val["value"] = inputs[index]["value"];
  67. addInput(inputs[index]["type"].get<std::string>(),val);
  68. }
  69. body.clear();
  70. res = cli.Get("/outputs",
  71. [&](const char *data, size_t data_length) {
  72. body.append(data, data_length);
  73. return true;
  74. });
  75. assert(res->body.empty());
  76. body = std::string(R"()") + body;
  77. json outputs = json::parse(body);
  78. for(int j = 0; j < (int)getOutputs().size()-(int)outputs.size(); ++j){
  79. pop_backOutput();
  80. }
  81. for(size_t i=0; i < getOutputs().size(); i++){
  82. getOutput(i)->setType(outputs[i]["type"].get<std::string>());
  83. getOutput(i)->setAppendix(i);
  84. getOutput(i)->getValueInternal() = outputs[i];
  85. }
  86. for(int index = getOutputs().size(); index < outputs.size(); ++index){
  87. json val;
  88. val["value"] = outputs[index]["value"];
  89. addOutput(outputs[index]["type"].get<std::string>(),val);
  90. }
  91. }
  92. ModuleHTTP::ModuleHTTP(std::string fname, std::string id, int port):
  93. _fname(fname),
  94. _id(id),
  95. _port(port)
  96. {
  97. if(!fname.empty()){
  98. std::string command = "python3 " + fname+"&";
  99. system(command.c_str());
  100. }
  101. while(!connect()){
  102. usleep(1000);
  103. }
  104. updateLayout();
  105. }
  106. bool ModuleHTTP::update() {
  107. updateLayout();
  108. /*Client cli(_id, _port);
  109. std::string outputs;
  110. auto res = cli.Get("/update",
  111. [&](const char *data, size_t data_length) {
  112. outputs.append(data, data_length);
  113. return true;
  114. });
  115. */
  116. }
  117. }