123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- #include "pairing_service/verify_controller.h"
- #include <esp_log.h>
- #include <kbf.h>
- #include <kbf/http/client.h>
- #include "dk.h"
- #include "tasks/display_manager.h"
- using namespace kbf;
- using namespace dk;
- VerifyController::VerifyController() : Controller("/verify") {}
- static bool validate(const nlohmann::json &json) {
- if (json == nullptr) {
- ESP_LOGW(VerifyController::TAG, "json is null");
- return false;
- } else if (json.find("pairingId") == json.end()) {
- ESP_LOGW(VerifyController::TAG, "invalid json: missing pairingId");
- return false;
- }
- return true;
- }
- http::Response VerifyController::post(const http::Request &request) {
- ESP_LOGI(TAG, "verification request: \"%s\"", request.body.c_str());
- auto json = request.json();
- if (!validate(json)) return http::Response("", 500);
- auto pairingId = json.find("pairingId")->get<string>();
- DisplayManager::setMessage("verifying");
- auto client = http::Client(CONFIG_DK_HTTP_CLIENT_TIMEOUT);
- nlohmann::json data = {
- { "device_uuid", DK_DEVICE_ID },
- { "pairing_id", pairingId }
- };
- auto response = client.post(CONFIG_DK_SERVER_API_URL "/register/verify", data);
- if (response->status != 200) {
- ESP_LOGW(TAG, "failed: %d", response->status);
- DisplayManager::setMessage("failed", DisplayManager::defaultTimeout);
- // TODO set pairing mode
- return http::Response(nlohmann::json({{"success", false}}));
- }
- ESP_LOGI(TAG, "verification successful");
- DisplayManager::setMessage("success", DisplayManager::defaultTimeout);
- dk::setPairing(pairingId);
- return http::Response(nlohmann::json({{"success", true}}));
- }
|