diff options
Diffstat (limited to 'src/protocols/httpserver.cpp')
| -rw-r--r-- | src/protocols/httpserver.cpp | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/src/protocols/httpserver.cpp b/src/protocols/httpserver.cpp index dc3b59a..3ca1f5c 100644 --- a/src/protocols/httpserver.cpp +++ b/src/protocols/httpserver.cpp @@ -12,7 +12,9 @@ #include "constants.h" #include "image.h" #include "imagealgos.h" +#include "iscanner.h" #include "macro.h" +#include "profile.h" #include "utils/elapsed_logger.h" // rapidjson @@ -24,10 +26,10 @@ extern uint8_t pgm_image[64 + img_width * img_height * sizeof(uint8_t)]; extern size_t pgm_image_size; extern std::mutex pgm_image_mtx; -HttpServer::HttpServer(std::shared_ptr<ICamera> camera, +HttpServer::HttpServer(std::shared_ptr<IScanner> scanner, const QHostAddress &address, const uint16_t port) - : IProtocol{camera} + : IProtocol{scanner} , INIT_FIELD(address) , INIT_FIELD(port) // , m_server{std::make_shared<QHttpServer>()} @@ -40,11 +42,14 @@ bool HttpServer::start() // TODO: move these vars outside const auto apiPrefix = QStringLiteral("/v1"); + // TODO: route automatically const auto pixelsPath = apiPrefix + "/pixels"; + const auto profilesPath = apiPrefix + "/profile"; qDebug().noquote() << Q_FUNC_INFO << ": pixelsPath: " << pixelsPath; // TODO: get rid of lamdas, there should be a better way m_server->route(pixelsPath, [this]() { return GET_pixels(); }); + m_server->route(profilesPath, [this]() { return GET_profile(); }); m_server->route(apiPrefix + QStringLiteral("/sensor/params"), QHttpServerRequest::Method::Get, [this]() { return GET_params(); }); @@ -86,7 +91,7 @@ QHttpServerResponse HttpServer::GET_image() { // Image img; - const auto image = m_camera->getImage(); + const auto image = m_scanner->camera()->getImage(); // if (!m_camera->getImage(&img)) { if (!image) { @@ -110,7 +115,7 @@ QFuture<QHttpServerResponse> HttpServer::GET_image_async() QHttpServerResponse HttpServer::GET_pixels() { - if (!m_camera) { + if (!m_scanner) { qWarning() << "NO CAM"; return QHttpServerResponse::StatusCode::ServiceUnavailable; } @@ -118,7 +123,7 @@ QHttpServerResponse HttpServer::GET_pixels() std::shared_ptr<Image> image; { // const elapsed_logger logger{__func__}; - /*const auto */ image = m_camera->getImage(); + /*const auto */ image = m_scanner->camera()->getImage(); } if (!image) { @@ -176,6 +181,17 @@ QHttpServerResponse HttpServer::GET_pixels() return QHttpServerResponse{res}; } +QHttpServerResponse HttpServer::GET_profile() +{ + const auto image = m_scanner->camera()->getImage(); + const auto pixels = image->sharedPixels(); + const auto profile = Profile{*pixels, ::calibrationTableZ, ::calibrationTableX}; + + const QJsonObject json{profile}; + + return QHttpServerResponse{QJsonDocument{json}.toJson()}; +} + QHttpServerResponse HttpServer::POST_params(const QHttpServerRequest &request) { const auto json = QJsonDocument::fromJson(request.body()).object(); @@ -201,9 +217,11 @@ QHttpServerResponse HttpServer::POST_params(const QHttpServerRequest &request) const auto gain = json["gain"]; const auto triggerExposureDelayUs = json["triggerExposureDelayUs"]; + // TODO: unify args processing to avoid code duplicates if (!autoExposure.isNull()) { if (autoExposure.isBool()) { - m_camera->set_autoExposure(autoExposure.toBool()); + // TODO: enable -Werror and some pedantic checks + m_scanner->camera()->set_autoExposure(autoExposure.toBool()); } else { return {invalidValue("autoExposure"), QHttpServerResponse::StatusCode::BadRequest}; @@ -212,8 +230,7 @@ QHttpServerResponse HttpServer::POST_params(const QHttpServerRequest &request) if (!exposureTime.isNull()) { if (exposureTime.isDouble()) { - m_camera->set_exposureTime( - std::chrono::microseconds{exposureTime.toInt()}); + m_scanner->camera()->set_exposureTime(std::chrono::microseconds{exposureTime.toInt()}); } else { return {invalidValue("exposureTime"), QHttpServerResponse::StatusCode::BadRequest}; @@ -222,7 +239,7 @@ QHttpServerResponse HttpServer::POST_params(const QHttpServerRequest &request) if (!autoGain.isNull()) { if (autoGain.isBool()) { - m_camera->set_autoGain(autoGain.toBool()); + m_scanner->camera()->set_autoGain(autoGain.toBool()); } else { return {invalidValue("autoGain"), QHttpServerResponse::StatusCode::BadRequest}; @@ -231,7 +248,7 @@ QHttpServerResponse HttpServer::POST_params(const QHttpServerRequest &request) if (!gain.isNull()) { if (gain.isDouble()) { - m_camera->set_gain(gain.toDouble()); + m_scanner->camera()->set_gain(gain.toDouble()); } else { return {invalidValue("gain"), QHttpServerResponse::StatusCode::BadRequest}; @@ -240,7 +257,7 @@ QHttpServerResponse HttpServer::POST_params(const QHttpServerRequest &request) if (!triggerExposureDelayUs.isNull()) { if (triggerExposureDelayUs.isDouble()) { - const auto ok = m_camera->set_triggerExposureDelay( + const auto ok = m_scanner->camera()->set_triggerExposureDelay( std::chrono::microseconds{triggerExposureDelayUs.toInt()}); if (!ok) { @@ -270,10 +287,10 @@ QHttpServerResponse HttpServer::GET_params() }; const auto json = QJsonObject{ - {"autoExposure", val_or_null(m_camera->get_autoExposure())}, - {"exposureTime", chrono_val_or_null(m_camera->get_exposureTime())}, - {"autoGain", val_or_null(m_camera->get_autoGain())}, - {"gain", val_or_null(m_camera->get_gain())}, + {"autoExposure", val_or_null(m_scanner->camera()->get_autoExposure())}, + {"exposureTime", chrono_val_or_null(m_scanner->camera()->get_exposureTime())}, + {"autoGain", val_or_null(m_scanner->camera()->get_autoGain())}, + {"gain", val_or_null(m_scanner->camera()->get_gain())}, }; qDebug().noquote() << __func__ << ": " << json; |
