update.
This commit is contained in:
23
Tools/Controller/CMakeLists.txt
Normal file
23
Tools/Controller/CMakeLists.txt
Normal file
@ -0,0 +1,23 @@
|
||||
find_package(Boost COMPONENTS json REQUIRED)
|
||||
|
||||
add_executable(Controller
|
||||
main.cpp
|
||||
NngClient.h NngClient.cpp
|
||||
)
|
||||
|
||||
include(FetchContent)
|
||||
|
||||
FetchContent_Declare(FTXUI
|
||||
GIT_REPOSITORY https://github.com/ArthurSonzogni/FTXUI
|
||||
GIT_TAG v6.1.8
|
||||
)
|
||||
FetchContent_MakeAvailable(FTXUI)
|
||||
|
||||
target_link_libraries(Controller
|
||||
PRIVATE Kylin::Core
|
||||
PRIVATE Kylin::Nng
|
||||
PRIVATE ftxui::screen
|
||||
PRIVATE ftxui::dom
|
||||
PRIVATE ftxui::component
|
||||
PRIVATE Boost::json
|
||||
)
|
54
Tools/Controller/NngClient.cpp
Normal file
54
Tools/Controller/NngClient.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
#include "NngClient.h"
|
||||
#include "Core/Logger.h"
|
||||
#include "Nng/SocketAisoWrapper.h"
|
||||
#include <boost/json/object.hpp>
|
||||
#include <boost/json/serialize.hpp>
|
||||
#include <sstream>
|
||||
|
||||
NngClient::NngClient(boost::asio::io_context &ioContex) {
|
||||
m_socket = std::make_shared<Nng::Asio::Socket>(ioContex, Nng::Request);
|
||||
}
|
||||
|
||||
void NngClient::asyncRead() {
|
||||
m_socket->asyncReceive([ptr{weak_from_this()}](const boost::system::error_code &error, const Nng::Buffer &buffer) {
|
||||
if (error) {
|
||||
LOG(error) << error.message();
|
||||
return;
|
||||
}
|
||||
if (ptr.expired()) return;
|
||||
auto self = ptr.lock();
|
||||
// LOG(info) << "nng received: " << buffer.data<char>();
|
||||
self->asyncRead();
|
||||
});
|
||||
}
|
||||
|
||||
void NngClient::start(const std::string &server, uint16_t port) {
|
||||
std::ostringstream oss;
|
||||
oss << "tcp://" << server << ":" << port;
|
||||
std::error_code error;
|
||||
m_socket->dial(oss.str(), error);
|
||||
asyncRead();
|
||||
}
|
||||
|
||||
void NngClient::requestZeroCheck() {
|
||||
boost::json::object request;
|
||||
request["command"] = "ZeroCheck";
|
||||
auto json = boost::json::serialize(request);
|
||||
m_socket->send(json.data(), json.size() + 1);
|
||||
}
|
||||
|
||||
void NngClient::requestZoom(bool in) {
|
||||
boost::json::object request;
|
||||
request["command"] = "Zoom";
|
||||
request["direction"] = in ? "In" : "Out";
|
||||
auto json = boost::json::serialize(request);
|
||||
m_socket->send(json.data(), json.size() + 1);
|
||||
}
|
||||
|
||||
void NngClient::requestFocus(bool far) {
|
||||
boost::json::object request;
|
||||
request["command"] = "Focus";
|
||||
request["direction"] = far ? "Far" : "Near";
|
||||
auto json = boost::json::serialize(request);
|
||||
m_socket->send(json.data(), json.size() + 1);
|
||||
}
|
38
Tools/Controller/NngClient.h
Normal file
38
Tools/Controller/NngClient.h
Normal file
@ -0,0 +1,38 @@
|
||||
#ifndef __NNGCLIENT_H__
|
||||
#define __NNGCLIENT_H__
|
||||
|
||||
#include "Core/Singleton.h"
|
||||
#include <string>
|
||||
|
||||
namespace boost {
|
||||
namespace asio {
|
||||
class io_context;
|
||||
}
|
||||
} // namespace boost
|
||||
|
||||
namespace Nng {
|
||||
|
||||
namespace Asio {
|
||||
|
||||
class Socket;
|
||||
}
|
||||
} // namespace Nng
|
||||
|
||||
class NngClient : public std::enable_shared_from_this<NngClient> {
|
||||
friend class Core::Singleton<NngClient>;
|
||||
|
||||
public:
|
||||
void start(const std::string &server, uint16_t port);
|
||||
void requestZeroCheck();
|
||||
void requestZoom(bool in);
|
||||
void requestFocus(bool far);
|
||||
|
||||
protected:
|
||||
NngClient(boost::asio::io_context &ioContex);
|
||||
void asyncRead();
|
||||
|
||||
private:
|
||||
std::shared_ptr<Nng::Asio::Socket> m_socket;
|
||||
};
|
||||
|
||||
#endif // __NNGCLIENT_H__
|
60
Tools/Controller/main.cpp
Normal file
60
Tools/Controller/main.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
#include "Core/IoContext.h"
|
||||
#include "Core/Singleton.h"
|
||||
#include "NngClient.h"
|
||||
#include <ftxui/component/component.hpp>
|
||||
#include <ftxui/component/screen_interactive.hpp>
|
||||
|
||||
int main() {
|
||||
using namespace Core;
|
||||
auto ioContext = Singleton<IoContext>::construct(std::thread::hardware_concurrency());
|
||||
auto nng = Singleton<NngClient>::construct(*ioContext->ioContext());
|
||||
nng->start("127.0.0.1", 8000);
|
||||
|
||||
auto zeroCheckButton =
|
||||
ftxui::Button("零点校正", [&]() { nng->requestZeroCheck(); }, ftxui::ButtonOption::Animated());
|
||||
|
||||
auto zoomButtons = ftxui::Container::Horizontal({
|
||||
ftxui::Button(
|
||||
"+", [&]() { nng->requestZoom(false); }, ftxui::ButtonOption::Animated()),
|
||||
ftxui::Button(
|
||||
"-", [&]() { nng->requestZoom(true); }, ftxui::ButtonOption::Animated()),
|
||||
});
|
||||
auto zoomItem = ftxui::Renderer(zoomButtons, [&]() {
|
||||
return ftxui::hbox({
|
||||
ftxui::text("zoom:") | ftxui::vcenter,
|
||||
zoomButtons->Render(),
|
||||
});
|
||||
});
|
||||
|
||||
auto focusButtons = ftxui::Container::Horizontal({
|
||||
ftxui::Button(
|
||||
"+", [&]() { nng->requestFocus(true); }, ftxui::ButtonOption::Animated()),
|
||||
ftxui::Button(
|
||||
"-", [&]() { nng->requestFocus(false); }, ftxui::ButtonOption::Animated()),
|
||||
});
|
||||
auto focusItem = ftxui::Renderer(focusButtons, [&]() {
|
||||
return ftxui::hbox({
|
||||
ftxui::text("focus:") | ftxui::vcenter,
|
||||
focusButtons->Render(),
|
||||
});
|
||||
});
|
||||
|
||||
auto controls = ftxui::Container::Vertical({
|
||||
zeroCheckButton,
|
||||
zoomItem,
|
||||
focusItem,
|
||||
});
|
||||
|
||||
// Modify the way to render them on screen:
|
||||
auto component = ftxui::Renderer(controls, [&] {
|
||||
return ftxui::vbox({
|
||||
ftxui::text("Nng 控制端"),
|
||||
controls->Render(),
|
||||
});
|
||||
});
|
||||
|
||||
ioContext->run(false);
|
||||
auto screen = ftxui::ScreenInteractive::FitComponent();
|
||||
screen.Loop(component);
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user