переделки для того, чтобы библиотека api заводилась
This commit is contained in:
parent
815a081a4c
commit
9037c6b329
@ -34,10 +34,12 @@ add_executable(terminal-web-server
|
|||||||
src/server/server.hpp
|
src/server/server.hpp
|
||||||
src/server/resource.cpp
|
src/server/resource.cpp
|
||||||
src/server/resource.h
|
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(Boost 1.53.0 COMPONENTS system thread filesystem log log_setup REQUIRED)
|
||||||
find_package(OpenSSL 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})
|
target_include_directories(terminal-web-server PRIVATE ${Boost_INCLUDE_DIR} ${OPENSSL_INCLUDE_DIR})
|
||||||
|
|
||||||
|
2
dependencies/control_system/CMakeLists.txt
vendored
2
dependencies/control_system/CMakeLists.txt
vendored
@ -28,6 +28,8 @@ add_library(terminal-client-api SHARED
|
|||||||
client/system_client.cpp
|
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(Boost 1.53.0 COMPONENTS system log log_setup REQUIRED)
|
||||||
find_package(cereal REQUIRED)
|
find_package(cereal REQUIRED)
|
||||||
target_link_libraries(terminal-client-api ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} cereal::cereal)
|
target_link_libraries(terminal-client-api ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} cereal::cereal)
|
||||||
|
2
dependencies/control_system/client/main.cpp
vendored
2
dependencies/control_system/client/main.cpp
vendored
@ -1,5 +1,5 @@
|
|||||||
#include <shared_mutex>
|
#include <shared_mutex>
|
||||||
#include "ControlProtoCInterface.h"
|
#include "terminal_api/ControlProtoCInterface.h"
|
||||||
#include "system_client.h"
|
#include "system_client.h"
|
||||||
|
|
||||||
std::shared_mutex mtx;
|
std::shared_mutex mtx;
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
#ifndef __CONTROL_PROTO_COMMANDS__
|
#ifndef __CONTROL_PROTO_COMMANDS__
|
||||||
#define __CONTROL_PROTO_COMMANDS__
|
#define __CONTROL_PROTO_COMMANDS__
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
#include <cstdint>
|
||||||
#define EXTERNC extern "C"
|
#define EXTERNC extern "C"
|
||||||
#else
|
#else
|
||||||
|
#include <stdint.h>
|
||||||
#define EXTERNC extern
|
#define EXTERNC extern
|
||||||
#endif
|
#endif
|
||||||
|
|
28
src/main.cpp
28
src/main.cpp
@ -15,6 +15,8 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
|
#include "terminal_api_driver.h"
|
||||||
|
|
||||||
|
|
||||||
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
|
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
|
||||||
|
|
||||||
@ -70,9 +72,10 @@ void init_logging() {
|
|||||||
log::add_common_attributes();
|
log::add_common_attributes();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void initResources(http::server::Server& s) {
|
static void initResources(http::server::Server& s, std::shared_ptr<api_driver::ApiDriver>& api) {
|
||||||
s.resources.emplace_back(std::make_unique<http::resource::StaticFileResource>("/", "static/login.html", mime_types::text_html));
|
s.resources.emplace_back(std::make_unique<http::resource::StaticFileResource>("/", "static/login.html", mime_types::text_html));
|
||||||
s.resources.emplace_back(std::make_unique<http::resource::StaticFileResource>("/favicon.ico", "static/favicon.png", mime_types::image_png));
|
s.resources.emplace_back(std::make_unique<http::resource::StaticFileResource>("/favicon.ico", "static/favicon.png", mime_types::image_png));
|
||||||
|
s.resources.emplace_back(std::make_unique<http::resource::StaticFileResource>("/style.css", "static/style.css", mime_types::text_css));
|
||||||
s.resources.emplace_back(std::make_unique<http::resource::StaticFileResource>("/js/vue.js", "static/js/vue.js", mime_types::javascript));
|
s.resources.emplace_back(std::make_unique<http::resource::StaticFileResource>("/js/vue.js", "static/js/vue.js", mime_types::javascript));
|
||||||
|
|
||||||
s.resources.emplace_back(std::make_unique<http::resource::GenericResource>("/api/statistics", [](const auto& req, auto& rep) {
|
s.resources.emplace_back(std::make_unique<http::resource::GenericResource>("/api/statistics", [](const auto& req, auto& rep) {
|
||||||
@ -86,6 +89,20 @@ static void initResources(http::server::Server& s) {
|
|||||||
const char* json = R"({"key":"value"})";
|
const char* json = R"({"key":"value"})";
|
||||||
rep.content.insert(rep.content.end(), json, json + strlen(json));
|
rep.content.insert(rep.content.end(), json, json + strlen(json));
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
s.resources.emplace_back(std::make_unique<http::resource::GenericResource>("/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[]) {
|
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];
|
BOOST_LOG_TRIVIAL(info) << "Starting RELEASE build" << argv[0];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
auto api = std::make_shared<api_driver::ApiDriver>();
|
||||||
|
|
||||||
// Initialise the server.
|
// Initialise the server.
|
||||||
std::unique_ptr<http::server::Server> s;
|
std::unique_ptr<http::server::Server> s;
|
||||||
|
|
||||||
if (strcmp(argv[1], "nossl") == 0) {
|
if (strcmp(argv[1], "nossl") == 0) {
|
||||||
s = std::make_unique<http::server::Server>(argv[2], argv[3]);
|
s = std::make_unique<http::server::Server>(argv[2], argv[3]);
|
||||||
initResources(*s);
|
initResources(*s, api);
|
||||||
s->run();
|
s->run();
|
||||||
|
|
||||||
} else if (strcmp(argv[1], "ssl") == 0) {
|
} 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->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_certificate_chain(boost::asio::buffer(cert));
|
||||||
|
|
||||||
ctx->use_private_key(boost::asio::buffer(key), ssl::context::file_format::pem);
|
ctx->use_private_key(boost::asio::buffer(key), ssl::context::file_format::pem);
|
||||||
|
|
||||||
ctx->use_tmp_dh(boost::asio::buffer(dh));
|
ctx->use_tmp_dh(boost::asio::buffer(dh));
|
||||||
|
|
||||||
s = std::make_unique<http::server::Server>(argv[2], argv[3], ctx);
|
s = std::make_unique<http::server::Server>(argv[2], argv[3], ctx);
|
||||||
initResources(*s);
|
initResources(*s, api);
|
||||||
s->run();
|
s->run();
|
||||||
} else {
|
} else {
|
||||||
std::cerr << "Unsupported ssl mode: " << argv[1] << std::endl;
|
std::cerr << "Unsupported ssl mode: " << argv[1] << std::endl;
|
||||||
|
26
src/terminal_api_driver.cpp
Normal file
26
src/terminal_api_driver.cpp
Normal file
@ -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;
|
37
src/terminal_api_driver.h
Normal file
37
src/terminal_api_driver.h
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#ifndef TERMINAL_API_DRIVER_H
|
||||||
|
#define TERMINAL_API_DRIVER_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <terminal_api/ControlProtoCInterface.h>
|
||||||
|
|
||||||
|
|
||||||
|
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
|
@ -3,13 +3,18 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Login</title>
|
<title>Главная</title>
|
||||||
|
<link rel="stylesheet" type="text/css" href="/style.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<header>
|
<div id="mainState" hidden>
|
||||||
<div id="app">
|
<h1>Общее состояние</h1>
|
||||||
<h1>{{ message }}</h1>
|
<ul>
|
||||||
<p>{{ now }}</p>
|
<li>Прием: {{ rxState }}</li>
|
||||||
|
<li>Передача: {{ txState }}</li>
|
||||||
|
<li>Тест: {{ testState }}</li>
|
||||||
|
<li>Последнее обновление: {{ lastUpdateTime }}</li>
|
||||||
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="status-header"></div>
|
<div id="status-header"></div>
|
||||||
@ -17,27 +22,35 @@
|
|||||||
<!-- Версия для разработки включает в себя возможность вывода в консоль полезных уведомлений -->
|
<!-- Версия для разработки включает в себя возможность вывода в консоль полезных уведомлений -->
|
||||||
<script src="/js/vue.js"></script>
|
<script src="/js/vue.js"></script>
|
||||||
<script>
|
<script>
|
||||||
const app = new Vue({
|
const mainState = new Vue({
|
||||||
el: '#app',
|
el: '#mainState',
|
||||||
data: {
|
data: {
|
||||||
message: 'Hello Vue!',
|
rxState: '?',
|
||||||
now: new Date()
|
txState: '?',
|
||||||
|
testState: '?',
|
||||||
|
lastUpdateTime: new Date()
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
updateDate() {
|
updateMainState(vals) {
|
||||||
this.now = new Date();
|
this.lastUpdateTime = new Date();
|
||||||
|
this.rxState = vals["mainState"]["rxState"]
|
||||||
|
this.txState = vals["mainState"]["txState"]
|
||||||
|
this.testState = vals["mainState"]["testState"]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
this.updateDate();
|
fetch("/api/mainStatistics").then(async (val) => {
|
||||||
|
this.updateMainState(await val.json())
|
||||||
|
});
|
||||||
}, 1000);
|
}, 1000);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
document.getElementById("mainState").removeAttribute("hidden")
|
||||||
|
|
||||||
// import MyComponent from './modules/header'
|
// import MyComponent from './modules/header'
|
||||||
// const sh = new Vue(MyComponent)
|
// const sh = new Vue(MyComponent)
|
||||||
</script>
|
</script>
|
||||||
</header>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
105
static/style.css
105
static/style.css
@ -1,75 +1,82 @@
|
|||||||
:root {
|
/* ========== THEME ========== */
|
||||||
--text: #000;
|
body {
|
||||||
--bg: #fff;
|
--text-color: #262626;
|
||||||
|
--text-color2: #3d3d3d;
|
||||||
|
--text-good: green;
|
||||||
|
--text-bad: red;
|
||||||
|
|
||||||
--item_bg: rgb(248, 246, 243);
|
--brand-bg: #EDF3FE;
|
||||||
--second_text: #6e6d6d;
|
--brand-text: #5488F7;
|
||||||
|
|
||||||
|
--bg-color: #FEFEFE;
|
||||||
|
--bg-selected: #F1F1F1;
|
||||||
|
--bg-action: #5181fe;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
@media (prefers-color-scheme: dark) {
|
||||||
:root {
|
/* defaults to dark theme */
|
||||||
--text: #fff;
|
body {
|
||||||
--bg: #222226;
|
--text-color: #eee;
|
||||||
|
--text-color2: #bbb;
|
||||||
|
--text-good: greenyellow;
|
||||||
|
--text-bad: orangered;
|
||||||
|
|
||||||
--item_bg: #333336;
|
--brand-bg: #393E50;
|
||||||
--second_text: #828282;
|
--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 {
|
body {
|
||||||
display: flex;
|
background: var(--bg-color);
|
||||||
flex-direction: column;
|
margin: 0; /* браузеры зачем-то ставят свое значение */
|
||||||
font-family: 'Roboto', sans-serif;
|
|
||||||
font-size: 16px;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
|
|
||||||
background-color: var(--bg);
|
|
||||||
color: var(--text);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
a {
|
#content {
|
||||||
text-decoration: none;
|
margin: 0.5em;
|
||||||
background-color: var(--bg);
|
|
||||||
color: var(--text);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.container {
|
/* ========== MAIN STYLES ========== */
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
background-color: var(--bg);
|
|
||||||
color: var(--text);
|
|
||||||
}
|
|
||||||
|
|
||||||
.title {
|
header > h1 {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
|
background-color: var(--brand-bg);
|
||||||
|
padding: 0.5em;
|
||||||
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.item {
|
header * {
|
||||||
padding: 20px;
|
color: var(--brand-text);
|
||||||
width: 400px;
|
|
||||||
color: var(--text);
|
|
||||||
background-color: var(--item_bg);
|
|
||||||
border-bottom: 1px solid var(--text);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.item_title {
|
header > nav {
|
||||||
font-weight: 600;
|
display: flex;
|
||||||
text-align: center;
|
flex-direction: row;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.item_status {
|
header > nav > a {
|
||||||
text-align: end;
|
margin: 0.5em;
|
||||||
font-weight: 300;
|
|
||||||
color: var(--second_text);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.item_icon {
|
.value-good {
|
||||||
width: 25px;
|
color: var(--text-good);
|
||||||
|
}
|
||||||
|
|
||||||
|
.value-bad {
|
||||||
|
color: var(--text-bad);
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user