diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cpp | 73 |
1 files changed, 68 insertions, 5 deletions
diff --git a/src/main.cpp b/src/main.cpp index 46f8d6d..aab134e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -40,6 +40,10 @@ #include <QTimer> #include <QtConcurrent/QtConcurrent> +#include "rapidjson/document.h" +#include "rapidjson/stringbuffer.h" +#include "rapidjson/writer.h" + #define try_apply_config() \ if (!applyConfig(config)) \ { \ @@ -484,12 +488,15 @@ int main(int argc, char* argv[]) std::lock_guard<std::mutex> lg(pgm_image_mtx); + // qt json does not allow to limit double precision + QJsonArray pixels; - for (size_t i = 0; i < img_width; ++i) - { + for (size_t i = 0; i < img_width; ++i) { // pixels << img_height - img.pixels[i]; pixels << ::pixels.pixels[i]; + // rjPixels.PushBack(::pixels.pixels[i], jd.GetAllocator()); + // rjPixels.PushBack(rapidjson::Value(::pixels.pixels[i]), al); } QJsonObject json; @@ -507,13 +514,69 @@ int main(int argc, char* argv[]) for (const auto& l : lines) { - jsonLines << QJsonArray{QJsonArray{l.p1().x(), l.p1().y()}, - QJsonArray{l.p2().x(), l.p2().y()}}; + const auto p1 = (l.p1() * 1000).toPoint().toPointF() / 1000.; + const auto p2 = (l.p2() * 1000).toPoint().toPointF() / 1000.; + // jsonLines << QJsonArray{QJsonArray{l.p1().x(), l.p1().y()}, + // QJsonArray{l.p2().x(), l.p2().y()}}; + jsonLines << QJsonArray{QJsonArray{p1.x(), p1.y()}, QJsonArray{p2.x(), p2.y()}}; } json["lines"] = jsonLines; - return QHttpServerResponse(QJsonDocument(json).toJson()); + const auto result = QJsonDocument(json).toJson(); + // qDebug() << "pixels answer size is" << result.size(); + static bool done{false}; + if (!done) { + qDebug().noquote() << result; + done = true; + } + + { + rapidjson::Document jd; + jd.SetObject(); + auto &al = jd.GetAllocator(); + rapidjson::Value rjPixels{rapidjson::kArrayType}; + for (size_t i = 0; i < img_width; ++i) { + if (qIsNaN(::pixels.pixels[i])) { + rjPixels.PushBack(double(0), al); + } else { + rjPixels.PushBack(double(::pixels.pixels[i]), al); + } + } + jd.AddMember("measurementCounter", img.counters.measurementCounter, al); + jd.AddMember("timestampUs", img.counters.timestampUs, al); + jd.AddMember("pixels", rjPixels.Move(), al); + rapidjson::StringBuffer buffer; + rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); + writer.SetMaxDecimalPlaces(2); + jd.Accept(writer); + QString res{(const char *) buffer.GetString()}; + qDebug() << "size:" << res.size(); + // qDebug().noquote() << res; + return QHttpServerResponse{res}; + } + + // { + // std::srand(static_cast<unsigned>(std::time(nullptr))); + // rapidjson::Document doc; + // doc.SetObject(); + // rapidjson::Document::AllocatorType &allocator = doc.GetAllocator(); + // rapidjson::Value valuesArray(rapidjson::kArrayType); + // // for (int i = 0; i < 10; ++i) { + // // valuesArray.PushBack(rapidjson::Value(rand() % 100), allocator); + // // } + // for (size_t i = 0; i < img_width; ++i) { + // valuesArray.PushBack(rapidjson::Value(::pixels.pixels[i]), allocator); + // } + // doc.AddMember("values", valuesArray, allocator); + // rapidjson::StringBuffer buffer; + // rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); + // doc.Accept(writer); + // std::cout << buffer.GetString() << "\n"; + // } + + qDebug() << "size:" << result.size(); + return QHttpServerResponse(result); }); qHttpServer.route("/v1/profile", [&]() -> QHttpServerResponse { |
