You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.7 KiB

2 years ago
  1. #include "extapi_service.hpp"
  2. #include "configs/project_setting.hpp"
  3. #include "iflytop/components/zcanreceiver/zcanreceiverhost.hpp"
  4. #include "iflytop/core/components/stringutils.hpp"
  5. #include "iflytop/core/core.hpp"
  6. #include "version.hpp"
  7. using namespace iflytop;
  8. using namespace core;
  9. using namespace std;
  10. using namespace nlohmann;
  11. #define BIND
  12. namespace iflytop {};
  13. /*******************************************************************************
  14. * TOOLS *
  15. *******************************************************************************/
  16. static void getJsonValFromJson(json j, int& val) {
  17. if (j.is_string()) {
  18. string valstr = j;
  19. val = atoi(valstr.c_str());
  20. } else if (j.is_number()) {
  21. val = j;
  22. } else {
  23. throw std::runtime_error("getJsonValFromJson(int) error");
  24. }
  25. }
  26. template <typename T>
  27. static T jsonGet(json j) {
  28. T val;
  29. getJsonValFromJson(j, val);
  30. return val;
  31. }
  32. void ExtAPIService::dosystem(string order) {
  33. logger->info("do:{}", order);
  34. system(order.c_str());
  35. }
  36. void ExtAPIService::initialize() {
  37. GET_TO_SERVICE(m_zconfig);
  38. m_restfulServer.reset(new RestfulServer());
  39. m_restfulServer->regAPI("/hello_world", RESTFUL_SERVER_BIND(ExtAPIService::hello_world));
  40. m_restfulServer->start(20000, 20001, "0.0.0.0");
  41. m_iflytopwsService.reset(new IflytopFrontEndService());
  42. m_iflytopwsService->initialize("0.0.0.0");
  43. m_iflytopwsService->startListen();
  44. };
  45. HttpResponsePtr ExtAPIService::hello_world( //
  46. HttpRequestPtr request, shared_ptr<RestfulServer::Context> context, std::shared_ptr<ConnectionState>) {
  47. return std::make_shared<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "hello_world");
  48. }