From 9037c6b3298788536366087cfee74060537f1230 Mon Sep 17 00:00:00 2001 From: Vladislav Ostapov Date: Wed, 30 Oct 2024 16:16:56 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=B4=D0=BB=D1=8F=20=D1=82=D0=BE=D0=B3=D0=BE,?= =?UTF-8?q?=20=D1=87=D1=82=D0=BE=D0=B1=D1=8B=20=D0=B1=D0=B8=D0=B1=D0=BB?= =?UTF-8?q?=D0=B8=D0=BE=D1=82=D0=B5=D0=BA=D0=B0=20api=20=D0=B7=D0=B0=D0=B2?= =?UTF-8?q?=D0=BE=D0=B4=D0=B8=D0=BB=D0=B0=D1=81=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 4 +- dependencies/control_system/CMakeLists.txt | 2 + dependencies/control_system/client/main.cpp | 2 +- .../terminal_api}/ControlProtoCInterface.h | 3 +- src/main.cpp | 28 ++++- src/terminal_api_driver.cpp | 26 +++++ src/terminal_api_driver.h | 37 ++++++ static/login.html | 73 +++++++----- static/style.css | 105 ++++++++++-------- 9 files changed, 192 insertions(+), 88 deletions(-) rename dependencies/control_system/{client => include/terminal_api}/ControlProtoCInterface.h (99%) create mode 100644 src/terminal_api_driver.cpp create mode 100644 src/terminal_api_driver.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9bd48a6..467b7ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,10 +34,12 @@ add_executable(terminal-web-server src/server/server.hpp src/server/resource.cpp src/server/resource.h + src/terminal_api_driver.h + src/terminal_api_driver.cpp ) find_package(Boost 1.53.0 COMPONENTS system thread filesystem log log_setup REQUIRED) find_package(OpenSSL REQUIRED) -target_link_libraries(terminal-web-server ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES}) +target_link_libraries(terminal-web-server ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} terminal-client-api) target_include_directories(terminal-web-server PRIVATE ${Boost_INCLUDE_DIR} ${OPENSSL_INCLUDE_DIR}) diff --git a/dependencies/control_system/CMakeLists.txt b/dependencies/control_system/CMakeLists.txt index 7f14054..fc6d7b4 100644 --- a/dependencies/control_system/CMakeLists.txt +++ b/dependencies/control_system/CMakeLists.txt @@ -28,6 +28,8 @@ add_library(terminal-client-api SHARED client/system_client.cpp ) +target_include_directories(terminal-client-api PUBLIC "include/") + find_package(Boost 1.53.0 COMPONENTS system log log_setup REQUIRED) find_package(cereal REQUIRED) target_link_libraries(terminal-client-api ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} cereal::cereal) diff --git a/dependencies/control_system/client/main.cpp b/dependencies/control_system/client/main.cpp index 97acf30..cdca466 100644 --- a/dependencies/control_system/client/main.cpp +++ b/dependencies/control_system/client/main.cpp @@ -1,5 +1,5 @@ #include -#include "ControlProtoCInterface.h" +#include "terminal_api/ControlProtoCInterface.h" #include "system_client.h" std::shared_mutex mtx; diff --git a/dependencies/control_system/client/ControlProtoCInterface.h b/dependencies/control_system/include/terminal_api/ControlProtoCInterface.h similarity index 99% rename from dependencies/control_system/client/ControlProtoCInterface.h rename to dependencies/control_system/include/terminal_api/ControlProtoCInterface.h index de62811..abb3b8f 100644 --- a/dependencies/control_system/client/ControlProtoCInterface.h +++ b/dependencies/control_system/include/terminal_api/ControlProtoCInterface.h @@ -1,13 +1,14 @@ #ifndef __CONTROL_PROTO_COMMANDS__ #define __CONTROL_PROTO_COMMANDS__ -#include #include #include #ifdef __cplusplus +#include #define EXTERNC extern "C" #else +#include #define EXTERNC extern #endif diff --git a/src/main.cpp b/src/main.cpp index bfd8bb8..46589b6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,8 @@ #include #include +#include "terminal_api_driver.h" + namespace ssl = boost::asio::ssl; // from @@ -70,9 +72,10 @@ void init_logging() { log::add_common_attributes(); } -static void initResources(http::server::Server& s) { +static void initResources(http::server::Server& s, std::shared_ptr& api) { s.resources.emplace_back(std::make_unique("/", "static/login.html", mime_types::text_html)); s.resources.emplace_back(std::make_unique("/favicon.ico", "static/favicon.png", mime_types::image_png)); + s.resources.emplace_back(std::make_unique("/style.css", "static/style.css", mime_types::text_css)); s.resources.emplace_back(std::make_unique("/js/vue.js", "static/js/vue.js", mime_types::javascript)); s.resources.emplace_back(std::make_unique("/api/statistics", [](const auto& req, auto& rep) { @@ -86,6 +89,20 @@ static void initResources(http::server::Server& s) { const char* json = R"({"key":"value"})"; rep.content.insert(rep.content.end(), json, json + strlen(json)); })); + + s.resources.emplace_back(std::make_unique("/api/mainStatistics", [api](const auto& req, auto& rep) { + if (req.method != "GET") { + http::server::stockReply(http::server::bad_request, rep); + } + + rep.status = http::server::ok; + rep.headers.clear(); + rep.headers.push_back({.name = "Content-Type", .value = toString(mime_types::json)}); + std::string result = R"({"mainState":)"; + result += api->loadTerminalState(); + result += "}"; + rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); + })); } int main(int argc, char *argv[]) { @@ -110,12 +127,14 @@ int main(int argc, char *argv[]) { BOOST_LOG_TRIVIAL(info) << "Starting RELEASE build" << argv[0]; #endif + auto api = std::make_shared(); + // Initialise the server. std::unique_ptr s; if (strcmp(argv[1], "nossl") == 0) { s = std::make_unique(argv[2], argv[3]); - initResources(*s); + initResources(*s, api); s->run(); } else if (strcmp(argv[1], "ssl") == 0) { @@ -131,15 +150,12 @@ int main(int argc, char *argv[]) { }); ctx->set_options(ssl::context::default_workarounds | ssl::context::no_sslv2 | ssl::context::single_dh_use); - ctx->use_certificate_chain(boost::asio::buffer(cert)); - ctx->use_private_key(boost::asio::buffer(key), ssl::context::file_format::pem); - ctx->use_tmp_dh(boost::asio::buffer(dh)); s = std::make_unique(argv[2], argv[3], ctx); - initResources(*s); + initResources(*s, api); s->run(); } else { std::cerr << "Unsupported ssl mode: " << argv[1] << std::endl; diff --git a/src/terminal_api_driver.cpp b/src/terminal_api_driver.cpp new file mode 100644 index 0000000..c2dbec5 --- /dev/null +++ b/src/terminal_api_driver.cpp @@ -0,0 +1,26 @@ +#include "terminal_api_driver.h" +#include "terminal_api/ControlProtoCInterface.h" + + +api_driver::ApiDriver::ApiDriver() { + CP_Login("admin", "pass", &sid, &access); +} + +std::string api_driver::ApiDriver::loadTerminalState() { + + return R"({"rxState":0,"txState":0,"testState":0})"; +} + +std::string api_driver::ApiDriver::loadTxStatistics() { + return R"("{"error":"no impl"}")"; +} + +std::string api_driver::ApiDriver::loadRxStatistics() { + return R"("{"error":"no impl"}")"; +} + +std::string api_driver::ApiDriver::loadDeviceStatistics() { + return R"("{"error":"no impl"}")"; +} + +api_driver::ApiDriver::~ApiDriver() = default; diff --git a/src/terminal_api_driver.h b/src/terminal_api_driver.h new file mode 100644 index 0000000..3a5e7d5 --- /dev/null +++ b/src/terminal_api_driver.h @@ -0,0 +1,37 @@ +#ifndef TERMINAL_API_DRIVER_H +#define TERMINAL_API_DRIVER_H + +#include +#include + + +namespace api_driver { + /** + * Это ApiDriver. Все ответы он будет возвращать в виде json. + */ + class ApiDriver { + public: + explicit ApiDriver(); + + /** + * Запросить общее состояние терминала + * @return {"rxState":0,"txState":0,"testState":0} + */ + std::string loadTerminalState(); + + std::string loadTxStatistics(); + + std::string loadRxStatistics(); + + std::string loadDeviceStatistics(); + + ~ApiDriver(); + + private: + TSID sid{0}; + unsigned int access{0}; + + }; +} + +#endif //TERMINAL_API_DRIVER_H diff --git a/static/login.html b/static/login.html index b9bc581..35b8717 100644 --- a/static/login.html +++ b/static/login.html @@ -3,41 +3,54 @@ - Login + Главная + -
-
-

{{ message }}

-

{{ now }}

-
+ -
+
- - - + -
+ document.getElementById("mainState").removeAttribute("hidden") + + // import MyComponent from './modules/header' + // const sh = new Vue(MyComponent) + \ No newline at end of file diff --git a/static/style.css b/static/style.css index 6f81a66..a352425 100644 --- a/static/style.css +++ b/static/style.css @@ -1,75 +1,82 @@ -:root { - --text: #000; - --bg: #fff; +/* ========== THEME ========== */ +body { + --text-color: #262626; + --text-color2: #3d3d3d; + --text-good: green; + --text-bad: red; - --item_bg: rgb(248, 246, 243); - --second_text: #6e6d6d; + --brand-bg: #EDF3FE; + --brand-text: #5488F7; + + --bg-color: #FEFEFE; + --bg-selected: #F1F1F1; + --bg-action: #5181fe; } @media (prefers-color-scheme: dark) { - :root { - --text: #fff; - --bg: #222226; + /* defaults to dark theme */ + body { + --text-color: #eee; + --text-color2: #bbb; + --text-good: greenyellow; + --text-bad: orangered; - --item_bg: #333336; - --second_text: #828282; + --brand-bg: #393E50; + --brand-text: #5F93F3; + + --bg-color: #2d2c33; + --bg-selected: #424248; + --bg-action: #4a70d5; } } -html { - padding: 10px; +* { + background: transparent; + color: var(--text-color); +} + +*, *::before, *::after { + box-sizing: border-box; } body { - display: flex; - flex-direction: column; - font-family: 'Roboto', sans-serif; - font-size: 16px; - align-items: center; - justify-content: center; - - background-color: var(--bg); - color: var(--text); + background: var(--bg-color); + margin: 0; /* браузеры зачем-то ставят свое значение */ } -a { - text-decoration: none; - background-color: var(--bg); - color: var(--text); +#content { + margin: 0.5em; } -.container { - display: flex; - flex-direction: column; - align-items: center; - justify-content: center; - background-color: var(--bg); - color: var(--text); -} +/* ========== MAIN STYLES ========== */ -.title { +header > h1 { text-align: center; + background-color: var(--brand-bg); + padding: 0.5em; + margin: 0; } -.item { - padding: 20px; - width: 400px; - color: var(--text); - background-color: var(--item_bg); - border-bottom: 1px solid var(--text); +header * { + color: var(--brand-text); } -.item_title { - font-weight: 600; - text-align: center; +header > nav { + display: flex; + flex-direction: row; + flex-wrap: wrap; + justify-content: center; + align-items: center; } -.item_status { - text-align: end; - font-weight: 300; - color: var(--second_text); +header > nav > a { + margin: 0.5em; } -.item_icon { - width: 25px; +.value-good { + color: var(--text-good); } + +.value-bad { + color: var(--text-bad); +} \ No newline at end of file