verify_controller.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "pairing_service/verify_controller.h"
  2. #include <esp_log.h>
  3. #include <kbf.h>
  4. #include <kbf/http/client.h>
  5. #include "dk.h"
  6. #include "tasks/display_manager.h"
  7. using namespace kbf;
  8. using namespace dk;
  9. VerifyController::VerifyController() : Controller("/verify") {}
  10. static bool validate(const nlohmann::json &json) {
  11. if (json == nullptr) {
  12. ESP_LOGW(VerifyController::TAG, "json is null");
  13. return false;
  14. } else if (json.find("pairingId") == json.end()) {
  15. ESP_LOGW(VerifyController::TAG, "invalid json: missing pairingId");
  16. return false;
  17. }
  18. return true;
  19. }
  20. http::Response VerifyController::post(const http::Request &request) {
  21. ESP_LOGI(TAG, "verification request: \"%s\"", request.body.c_str());
  22. auto json = request.json();
  23. if (!validate(json)) return http::Response("", 500);
  24. auto pairingId = json.find("pairingId")->get<string>();
  25. DisplayManager::setMessage("verifying");
  26. auto client = http::Client(CONFIG_DK_HTTP_CLIENT_TIMEOUT);
  27. nlohmann::json data = {
  28. { "device_uuid", DK_DEVICE_ID },
  29. { "pairing_id", pairingId }
  30. };
  31. auto response = client.post(CONFIG_DK_SERVER_API_URL "/register/verify", data);
  32. if (response->status != 200) {
  33. ESP_LOGW(TAG, "failed: %d", response->status);
  34. DisplayManager::setMessage("failed", DisplayManager::defaultTimeout);
  35. // TODO set pairing mode
  36. return http::Response(nlohmann::json({{"success", false}}));
  37. }
  38. ESP_LOGI(TAG, "verification successful");
  39. DisplayManager::setMessage("success", DisplayManager::defaultTimeout);
  40. dk::setPairing(pairingId);
  41. return http::Response(nlohmann::json({{"success", true}}));
  42. }