From 05f0938a65c4f8c330791097680e1e094260bb60 Mon Sep 17 00:00:00 2001 From: Nikita Kostovsky Date: Fri, 6 Mar 2026 20:40:29 +0100 Subject: refactoring --- src/protocols/httpserver.cpp | 47 +++++++++++++++++++++++++------------ src/protocols/httpserver.h | 5 ++-- src/protocols/iprotocol.cpp | 4 ++-- src/protocols/iprotocol.h | 7 +++--- src/protocols/pixelsudpstreamer.cpp | 4 ++-- src/protocols/pixelsudpstreamer.h | 2 +- 6 files changed, 44 insertions(+), 25 deletions(-) (limited to 'src/protocols') 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 camera, +HttpServer::HttpServer(std::shared_ptr scanner, const QHostAddress &address, const uint16_t port) - : IProtocol{camera} + : IProtocol{scanner} , INIT_FIELD(address) , INIT_FIELD(port) // , m_server{std::make_shared()} @@ -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 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; { // 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; diff --git a/src/protocols/httpserver.h b/src/protocols/httpserver.h index e3ca66f..320ce00 100644 --- a/src/protocols/httpserver.h +++ b/src/protocols/httpserver.h @@ -9,7 +9,7 @@ // orpheus #include "iprotocol.h" -class ICamera; +class IScanner; class QHttpServer; class HttpServer : public IProtocol @@ -25,7 +25,7 @@ public: static constexpr uint16_t DefaultPort{8080}; public: - explicit HttpServer(std::shared_ptr camera, + explicit HttpServer(std::shared_ptr scanner, const QHostAddress &address = DefaultAddress, const uint16_t port = DefaultPort); ~HttpServer() override = default; @@ -45,6 +45,7 @@ public: */ QFuture GET_image_async(); QHttpServerResponse GET_pixels(); + QHttpServerResponse GET_profile(); QHttpServerResponse POST_params(const QHttpServerRequest &request); QHttpServerResponse GET_params(); diff --git a/src/protocols/iprotocol.cpp b/src/protocols/iprotocol.cpp index 5422a9d..02270b4 100644 --- a/src/protocols/iprotocol.cpp +++ b/src/protocols/iprotocol.cpp @@ -1,5 +1,5 @@ #include "iprotocol.h" -IProtocol::IProtocol(std::shared_ptr camera) - : m_camera{camera} +IProtocol::IProtocol(std::shared_ptr camera) + : m_scanner{camera} {} diff --git a/src/protocols/iprotocol.h b/src/protocols/iprotocol.h index 75259bc..3f49eb6 100644 --- a/src/protocols/iprotocol.h +++ b/src/protocols/iprotocol.h @@ -2,12 +2,13 @@ #include -class ICamera; +class IScanner; class IProtocol { public: - explicit IProtocol(std::shared_ptr camera); + // TODO: get rid of constructor or rename class + explicit IProtocol(std::shared_ptr camera); virtual ~IProtocol() = default; public: @@ -15,5 +16,5 @@ public: virtual void stop() = 0; protected: - std::shared_ptr m_camera; + std::shared_ptr m_scanner; }; diff --git a/src/protocols/pixelsudpstreamer.cpp b/src/protocols/pixelsudpstreamer.cpp index e7498d7..2855a35 100644 --- a/src/protocols/pixelsudpstreamer.cpp +++ b/src/protocols/pixelsudpstreamer.cpp @@ -2,9 +2,9 @@ #include -PixelsUdpStreamer::PixelsUdpStreamer(std::shared_ptr camera, QObject *parent) +PixelsUdpStreamer::PixelsUdpStreamer(std::shared_ptr scanner, QObject *parent) : QObject{parent} - , IProtocol{camera} + , IProtocol{scanner} {} bool PixelsUdpStreamer::start() diff --git a/src/protocols/pixelsudpstreamer.h b/src/protocols/pixelsudpstreamer.h index a7f69b7..dacc9cb 100644 --- a/src/protocols/pixelsudpstreamer.h +++ b/src/protocols/pixelsudpstreamer.h @@ -15,7 +15,7 @@ class PixelsUdpStreamer : public QObject, public IProtocol Q_OBJECT public: - explicit PixelsUdpStreamer(std::shared_ptr camera, QObject *parent = nullptr); + explicit PixelsUdpStreamer(std::shared_ptr scanner, QObject *parent = nullptr); ~PixelsUdpStreamer() override = default; public slots: -- cgit v1.3